@@ -979,32 +979,31 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
979
979
// range. In this case, all tx indices of newly imported blocks should be
980
980
// generated.
981
981
var batch = bc .db .NewBatch ()
982
- for _ , block := range blockChain {
982
+ for i , block := range blockChain {
983
983
if bc .txLookupLimit == 0 || ancientLimit <= bc .txLookupLimit || block .NumberU64 () >= ancientLimit - bc .txLookupLimit {
984
984
rawdb .WriteTxLookupEntriesByBlock (batch , block )
985
985
} else if rawdb .ReadTxIndexTail (bc .db ) != nil {
986
986
rawdb .WriteTxLookupEntriesByBlock (batch , block )
987
987
}
988
988
stats .processed ++
989
- }
990
989
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 ()
999
1000
}
1000
- return 0 , err
1001
1001
}
1002
1002
1003
1003
// Sync the ancient store explicitly to ensure all data has been flushed to disk.
1004
1004
if err := bc .db .Sync (); err != nil {
1005
1005
return 0 , err
1006
1006
}
1007
-
1008
1007
// Update the current fast block because all block data is now present in DB.
1009
1008
previousFastBlock := bc .CurrentFastBlock ().NumberU64 ()
1010
1009
if ! updateHead (blockChain [len (blockChain )- 1 ]) {
0 commit comments