@@ -301,6 +301,7 @@ type BlockChain struct {
301
301
diffLayerFreezerBlockLimit uint64
302
302
303
303
wg sync.WaitGroup
304
+ dbWg sync.WaitGroup
304
305
quit chan struct {} // shutdown signal, closed in Stop.
305
306
stopping atomic.Bool // false if chain is running, true when stopped
306
307
procInterrupt atomic.Bool // interrupt signaler for block processing
@@ -669,7 +670,7 @@ func (bc *BlockChain) cacheBlock(hash common.Hash, block *types.Block) {
669
670
// into node seamlessly.
670
671
func (bc * BlockChain ) empty () bool {
671
672
genesis := bc .genesisBlock .Hash ()
672
- for _ , hash := range []common.Hash {rawdb .ReadHeadBlockHash (bc .db .BlockStore ()), rawdb .ReadHeadHeaderHash (bc .db .BlockStore ()), rawdb .ReadHeadFastBlockHash (bc .db )} {
673
+ for _ , hash := range []common.Hash {rawdb .ReadHeadBlockHash (bc .db .BlockStore ()), rawdb .ReadHeadHeaderHash (bc .db .BlockStore ()), rawdb .ReadHeadFastBlockHash (bc .db . BlockStore () )} {
673
674
if hash != genesis {
674
675
return false
675
676
}
@@ -738,7 +739,7 @@ func (bc *BlockChain) loadLastState() error {
738
739
bc .currentSnapBlock .Store (headBlock .Header ())
739
740
headFastBlockGauge .Update (int64 (headBlock .NumberU64 ()))
740
741
741
- if head := rawdb .ReadHeadFastBlockHash (bc .db ); head != (common.Hash {}) {
742
+ if head := rawdb .ReadHeadFastBlockHash (bc .db . BlockStore () ); head != (common.Hash {}) {
742
743
if block := bc .GetBlockByHash (head ); block != nil {
743
744
bc .currentSnapBlock .Store (block .Header ())
744
745
headFastBlockGauge .Update (int64 (block .NumberU64 ()))
@@ -1137,7 +1138,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
1137
1138
// If SetHead was only called as a chain reparation method, try to skip
1138
1139
// touching the header chain altogether, unless the freezer is broken
1139
1140
if repair {
1140
- if target , force := updateFn (bc .db , bc .CurrentBlock ()); force {
1141
+ if target , force := updateFn (bc .db . BlockStore () , bc .CurrentBlock ()); force {
1141
1142
bc .hc .SetHead (target .Number .Uint64 (), updateFn , delFn )
1142
1143
}
1143
1144
} else {
@@ -1298,19 +1299,33 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
1298
1299
//
1299
1300
// Note, this function assumes that the `mu` mutex is held!
1300
1301
func (bc * BlockChain ) writeHeadBlock (block * types.Block ) {
1301
- // Add the block to the canonical chain number scheme and mark as the head
1302
- rawdb .WriteCanonicalHash (bc .db .BlockStore (), block .Hash (), block .NumberU64 ())
1303
- rawdb .WriteHeadHeaderHash (bc .db .BlockStore (), block .Hash ())
1304
- rawdb .WriteHeadBlockHash (bc .db .BlockStore (), block .Hash ())
1302
+ bc .dbWg .Add (2 )
1303
+ defer bc .dbWg .Wait ()
1304
+ go func () {
1305
+ defer bc .dbWg .Done ()
1306
+ // Add the block to the canonical chain number scheme and mark as the head
1307
+ blockBatch := bc .db .BlockStore ().NewBatch ()
1308
+ rawdb .WriteCanonicalHash (blockBatch , block .Hash (), block .NumberU64 ())
1309
+ rawdb .WriteHeadHeaderHash (blockBatch , block .Hash ())
1310
+ rawdb .WriteHeadBlockHash (blockBatch , block .Hash ())
1311
+ rawdb .WriteHeadFastBlockHash (blockBatch , block .Hash ())
1312
+ // Flush the whole batch into the disk, exit the node if failed
1313
+ if err := blockBatch .Write (); err != nil {
1314
+ log .Crit ("Failed to update chain indexes and markers in block db" , "err" , err )
1315
+ }
1316
+ }()
1317
+ go func () {
1318
+ defer bc .dbWg .Done ()
1305
1319
1306
- batch := bc .db .NewBatch ()
1307
- rawdb .WriteHeadFastBlockHash (batch , block .Hash ())
1308
- rawdb .WriteTxLookupEntriesByBlock (batch , block )
1320
+ batch := bc .db .NewBatch ()
1321
+ rawdb .WriteTxLookupEntriesByBlock (batch , block )
1322
+
1323
+ // Flush the whole batch into the disk, exit the node if failed
1324
+ if err := batch .Write (); err != nil {
1325
+ log .Crit ("Failed to update chain indexes in chain db" , "err" , err )
1326
+ }
1327
+ }()
1309
1328
1310
- // Flush the whole batch into the disk, exit the node if failed
1311
- if err := batch .Write (); err != nil {
1312
- log .Crit ("Failed to update chain indexes and markers" , "err" , err )
1313
- }
1314
1329
// Update all in-memory chain markers in the last step
1315
1330
bc .hc .SetCurrentHeader (block .Header ())
1316
1331
@@ -1531,7 +1546,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
1531
1546
} else if ! reorg {
1532
1547
return false
1533
1548
}
1534
- rawdb .WriteHeadFastBlockHash (bc .db , head .Hash ())
1549
+ rawdb .WriteHeadFastBlockHash (bc .db . BlockStore () , head .Hash ())
1535
1550
bc .currentSnapBlock .Store (head .Header ())
1536
1551
headFastBlockGauge .Update (int64 (head .NumberU64 ()))
1537
1552
return true
@@ -1774,7 +1789,6 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1774
1789
wg := sync.WaitGroup {}
1775
1790
wg .Add (1 )
1776
1791
go func () {
1777
- rawdb .WritePreimages (bc .db , state .Preimages ())
1778
1792
blockBatch := bc .db .BlockStore ().NewBatch ()
1779
1793
rawdb .WriteTd (blockBatch , block .Hash (), block .NumberU64 (), externTd )
1780
1794
rawdb .WriteBlock (blockBatch , block )
@@ -1783,7 +1797,11 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1783
1797
if bc .chainConfig .IsCancun (block .Number (), block .Time ()) {
1784
1798
rawdb .WriteBlobSidecars (blockBatch , block .Hash (), block .NumberU64 (), block .Sidecars ())
1785
1799
}
1786
- rawdb .WritePreimages (blockBatch , state .Preimages ())
1800
+ if bc .db .StateStore () != nil {
1801
+ rawdb .WritePreimages (bc .db .StateStore (), state .Preimages ())
1802
+ } else {
1803
+ rawdb .WritePreimages (blockBatch , state .Preimages ())
1804
+ }
1787
1805
if err := blockBatch .Write (); err != nil {
1788
1806
log .Crit ("Failed to write block into disk" , "err" , err )
1789
1807
}
0 commit comments