Skip to content

Commit 4bd2d0e

Browse files
committed
core: periodically flush the transaction indexes
1 parent 66a908c commit 4bd2d0e

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

core/blockchain.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -979,32 +979,31 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
979979
// range. In this case, all tx indices of newly imported blocks should be
980980
// generated.
981981
var batch = bc.db.NewBatch()
982-
for _, block := range blockChain {
982+
for i, block := range blockChain {
983983
if bc.txLookupLimit == 0 || ancientLimit <= bc.txLookupLimit || block.NumberU64() >= ancientLimit-bc.txLookupLimit {
984984
rawdb.WriteTxLookupEntriesByBlock(batch, block)
985985
} else if rawdb.ReadTxIndexTail(bc.db) != nil {
986986
rawdb.WriteTxLookupEntriesByBlock(batch, block)
987987
}
988988
stats.processed++
989-
}
990989

991-
// Flush all tx-lookup index data.
992-
size += int64(batch.ValueSize())
993-
if err := batch.Write(); err != nil {
994-
// The tx index data could not be written.
995-
// Roll back the ancient store update.
996-
fastBlock := bc.CurrentFastBlock().NumberU64()
997-
if err := bc.db.TruncateAncients(fastBlock + 1); err != nil {
998-
log.Error("Can't truncate ancient store after failed insert", "err", err)
990+
if batch.ValueSize() > ethdb.IdealBatchSize || i == len(blockChain)-1 {
991+
size += int64(batch.ValueSize())
992+
if err = batch.Write(); err != nil {
993+
fastBlock := bc.CurrentFastBlock().NumberU64()
994+
if err := bc.db.TruncateAncients(fastBlock + 1); err != nil {
995+
log.Error("Can't truncate ancient store after failed insert", "err", err)
996+
}
997+
return 0, err
998+
}
999+
batch.Reset()
9991000
}
1000-
return 0, err
10011001
}
10021002

10031003
// Sync the ancient store explicitly to ensure all data has been flushed to disk.
10041004
if err := bc.db.Sync(); err != nil {
10051005
return 0, err
10061006
}
1007-
10081007
// Update the current fast block because all block data is now present in DB.
10091008
previousFastBlock := bc.CurrentFastBlock().NumberU64()
10101009
if !updateHead(blockChain[len(blockChain)-1]) {

0 commit comments

Comments
 (0)