From 222e10810e226e3191b3d823d3aaabf8ce9b34ea Mon Sep 17 00:00:00 2001 From: NathanBSC Date: Mon, 22 Jul 2024 10:16:01 +0800 Subject: [PATCH 1/2] core: cache block after wroten into db --- core/blockchain.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index a91b149179..f377d04acb 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1798,6 +1798,12 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. if err := blockBatch.Write(); err != nil { log.Crit("Failed to write block into disk", "err", err) } + bc.hc.tdCache.Add(block.Hash(), externTd) + bc.blockCache.Add(block.Hash(), block) + bc.receiptsCache.Add(block.Hash(), receipts) + if bc.chainConfig.IsCancun(block.Number(), block.Time()) { + bc.sidecarsCache.Add(block.Hash(), block.Sidecars()) + } wg.Done() }() From c6cb43b7ca3be8a2160f133cfa70f1036c13d5e6 Mon Sep 17 00:00:00 2001 From: NathanBSC Date: Mon, 22 Jul 2024 18:23:10 +0800 Subject: [PATCH 2/2] Revert "miner/worker: broadcast block immediately once sealed (#2576)" This reverts commit 6d5b4ad64d31907e0d3def45ac283b540cbe9ec9. --- core/blockchain.go | 18 +++++++----------- core/events.go | 5 +---- eth/handler.go | 7 +++---- miner/worker.go | 2 +- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index f377d04acb..51387a13f0 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1914,30 +1914,26 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. // WriteBlockAndSetHead writes the given block and all associated state to the database, // and applies the block as the new chain head. -func (bc *BlockChain) WriteBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool, mux *event.TypeMux) (status WriteStatus, err error) { +func (bc *BlockChain) WriteBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { if !bc.chainmu.TryLock() { return NonStatTy, errChainStopped } defer bc.chainmu.Unlock() - return bc.writeBlockAndSetHead(block, receipts, logs, state, emitHeadEvent, mux) + return bc.writeBlockAndSetHead(block, receipts, logs, state, emitHeadEvent) } // writeBlockAndSetHead is the internal implementation of WriteBlockAndSetHead. // This function expects the chain mutex to be held. -func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool, mux *event.TypeMux) (status WriteStatus, err error) { +func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { + if err := bc.writeBlockWithState(block, receipts, state); err != nil { + return NonStatTy, err + } currentBlock := bc.CurrentBlock() reorg, err := bc.forker.ReorgNeededWithFastFinality(currentBlock, block.Header()) if err != nil { return NonStatTy, err } - if reorg && mux != nil { - mux.Post(NewSealedBlockEvent{Block: block}) - } - - if err := bc.writeBlockWithState(block, receipts, state); err != nil { - return NonStatTy, err - } if reorg { // Reorganise the chain if the parent is not the head block if block.ParentHash() != currentBlock.Hash() { @@ -2310,7 +2306,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) // Don't set the head, only insert the block err = bc.writeBlockWithState(block, receipts, statedb) } else { - status, err = bc.writeBlockAndSetHead(block, receipts, logs, statedb, false, nil) + status, err = bc.writeBlockAndSetHead(block, receipts, logs, statedb, false) } if err != nil { return it.index, err diff --git a/core/events.go b/core/events.go index c479835662..ce8bcca744 100644 --- a/core/events.go +++ b/core/events.go @@ -27,10 +27,7 @@ type NewTxsEvent struct{ Txs []*types.Transaction } // ReannoTxsEvent is posted when a batch of local pending transactions exceed a specified duration. type ReannoTxsEvent struct{ Txs []*types.Transaction } -// NewSealedBlockEvent is posted when a block has been sealed. -type NewSealedBlockEvent struct{ Block *types.Block } - -// NewMinedBlockEvent is posted when a block has been mined. +// NewMinedBlockEvent is posted when a block has been imported. type NewMinedBlockEvent struct{ Block *types.Block } // RemovedLogsEvent is posted when a reorg happens diff --git a/eth/handler.go b/eth/handler.go index 5a6dbda1fc..23dba9e14d 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -729,7 +729,7 @@ func (h *handler) Start(maxPeers int, maxPeersPerIP int) { // broadcast mined blocks h.wg.Add(1) - h.minedBlockSub = h.eventMux.Subscribe(core.NewMinedBlockEvent{}, core.NewSealedBlockEvent{}) + h.minedBlockSub = h.eventMux.Subscribe(core.NewMinedBlockEvent{}) go h.minedBroadcastLoop() // start sync handlers @@ -946,9 +946,8 @@ func (h *handler) minedBroadcastLoop() { if obj == nil { continue } - if ev, ok := obj.Data.(core.NewSealedBlockEvent); ok { - h.BroadcastBlock(ev.Block, true) // Propagate block to peers - } else if ev, ok := obj.Data.(core.NewMinedBlockEvent); ok { + if ev, ok := obj.Data.(core.NewMinedBlockEvent); ok { + h.BroadcastBlock(ev.Block, true) // First propagate block to peers h.BroadcastBlock(ev.Block, false) // Only then announce to the rest } case <-h.stopCh: diff --git a/miner/worker.go b/miner/worker.go index 0a48d90352..1dc05554f8 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -665,7 +665,7 @@ func (w *worker) resultLoop() { // Commit block and state to database. task.state.SetExpectedStateRoot(block.Root()) start := time.Now() - status, err := w.chain.WriteBlockAndSetHead(block, receipts, logs, task.state, true, w.mux) + status, err := w.chain.WriteBlockAndSetHead(block, receipts, logs, task.state, true) if status != core.CanonStatTy { if err != nil { log.Error("Failed writing block to chain", "err", err, "status", status)