Skip to content

Commit 4c90efd

Browse files
holimankaralabe
authored andcommitted
consensus,core,miner: avoid overhead of creating a new block (#19301)
* consensus,core,miner: avoid overhead of creating a new block * consensus: nitpick dot * consensus: fix some comment formatting nits
1 parent befca7e commit 4c90efd

6 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎consensus/clique/clique.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,16 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
548548
}
549549

550550
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
551-
// rewards given, and returns the final block.
552-
func (c *Clique) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
551+
// rewards given.
552+
func (c *Clique) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
553+
// No block rewards in PoA, so the state remains as is and uncles are dropped
554+
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
555+
header.UncleHash = types.CalcUncleHash(nil)
556+
}
557+
558+
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
559+
// nor block rewards given, and returns the final block.
560+
func (c *Clique) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
553561
// No block rewards in PoA, so the state remains as is and uncles are dropped
554562
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
555563
header.UncleHash = types.CalcUncleHash(nil)

‎consensus/consensus.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,19 @@ type Engine interface {
8080
Prepare(chain ChainReader, header *types.Header) error
8181

8282
// Finalize runs any post-transaction state modifications (e.g. block rewards)
83-
// and assembles the final block.
83+
// but does not assemble the block.
84+
//
8485
// Note: The block header and state database might be updated to reflect any
8586
// consensus rules that happen at finalization (e.g. block rewards).
8687
Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
88+
uncles []*types.Header)
89+
90+
// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
91+
// rewards) and assembles the final block.
92+
//
93+
// Note: The block header and state database might be updated to reflect any
94+
// consensus rules that happen at finalization (e.g. block rewards).
95+
FinalizeAndAssemble(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
8796
uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)
8897

8998
// Seal generates a new sealing request for the given input block and pushes

‎consensus/ethash/consensus.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,16 @@ func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header)
561561
}
562562

563563
// Finalize implements consensus.Engine, accumulating the block and uncle rewards,
564-
// setting the final state and assembling the block.
565-
func (ethash *Ethash) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
564+
// setting the final state on the header
565+
func (ethash *Ethash) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
566+
// Accumulate any block and uncle rewards and commit the final state root
567+
accumulateRewards(chain.Config(), state, header, uncles)
568+
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
569+
}
570+
571+
// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
572+
// uncle rewards, setting the final state and assembling the block.
573+
func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
566574
// Accumulate any block and uncle rewards and commit the final state root
567575
accumulateRewards(chain.Config(), state, header, uncles)
568576
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))

‎core/chain_makers.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
206206
}
207207
if b.engine != nil {
208208
// Finalize and seal the block
209-
block, _ := b.engine.Finalize(chainreader, b.header, statedb, b.txs, b.uncles, b.receipts)
209+
block, _ := b.engine.FinalizeAndAssemble(chainreader, b.header, statedb, b.txs, b.uncles, b.receipts)
210210

211211
// Write state changes to db
212212
root, err := statedb.Commit(config.IsEIP158(b.header.Number))

‎core/state_processor.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
7676
allLogs = append(allLogs, receipt.Logs...)
7777
}
7878
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
79-
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), receipts)
79+
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles())
8080

8181
return receipts, allLogs, *usedGas, nil
8282
}

‎miner/worker.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ func (w *worker) commit(uncles []*types.Header, interval func(), update bool, st
956956
*receipts[i] = *l
957957
}
958958
s := w.current.state.Copy()
959-
block, err := w.engine.Finalize(w.chain, w.current.header, s, w.current.txs, uncles, w.current.receipts)
959+
block, err := w.engine.FinalizeAndAssemble(w.chain, w.current.header, s, w.current.txs, uncles, w.current.receipts)
960960
if err != nil {
961961
return err
962962
}

0 commit comments

Comments
 (0)