core, internal, miner: wrap the block construction as a function

This commit is contained in:
Gary Rong 2026-04-21 21:54:12 +08:00
parent 1db7f94df0
commit e606b6a3d9
4 changed files with 18 additions and 34 deletions

View file

@ -31,7 +31,6 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/triedb"
"github.com/holiman/uint256"
)
@ -425,15 +424,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
body.Withdrawals = make([]*types.Withdrawal, 0)
}
}
// Finalize the state transition by applying operations such as withdrawals,
// uncle rewards, and related processing.
b.engine.Finalize(cm, b.header, statedb, &body)
// Calculate the state root after applying all mutations.
b.header.Root = statedb.IntermediateRoot(cm.Config().IsEIP158(b.header.Number))
// Assemble the block for delivery.
block := types.NewBlock(b.header, &body, b.receipts, trie.NewStackTrie(nil))
block := AssembleBlock(b.engine, cm, b.header, statedb, &body, b.receipts)
// Write state changes to db
root, err := statedb.Commit(b.header.Number.Uint64(), config.IsEIP158(b.header.Number), config.IsCancun(b.header.Number, b.header.Time))

View file

@ -22,6 +22,7 @@ import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
@ -30,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/telemetry"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
)
// StateProcessor is a basic Processor, which takes care of transitioning
@ -372,3 +374,11 @@ func onSystemCallStart(tracer *tracing.Hooks, ctx *tracing.VMContext) {
tracer.OnSystemCallStart()
}
}
// AssembleBlock finalizes the state and assembles the block with provided
// body and receipts.
func AssembleBlock(engine consensus.Engine, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) *types.Block {
engine.Finalize(chain, header, state, body)
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
return types.NewBlock(header, body, receipts, trie.NewStackTrie(nil))
}

View file

@ -37,7 +37,6 @@ import (
"github.com/ethereum/go-ethereum/internal/ethapi/override"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
)
const (
@ -421,15 +420,8 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header,
}
chainHeadReader := &simChainHeadReader{ctx, sim.b}
// Finalize the state transition by applying operations such as withdrawals,
// uncle rewards, and related processing.
sim.b.Engine().Finalize(chainHeadReader, header, sim.state, blockBody)
// Calculate the state root after applying all mutations.
header.Root = sim.state.IntermediateRoot(chainHeadReader.Config().IsEIP158(header.Number))
// Assemble the block for delivery.
b := types.NewBlock(header, blockBody, receipts, trie.NewStackTrie(nil))
// Assemble the block
b := core.AssembleBlock(sim.b.Engine(), chainHeadReader, header, sim.state, blockBody, receipts)
repairLogs(callResults, b.Hash())
return b, callResults, senders, nil

View file

@ -36,7 +36,6 @@ import (
"github.com/ethereum/go-ethereum/internal/telemetry"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/holiman/uint256"
)
@ -226,21 +225,10 @@ func (miner *Miner) generateWork(ctx context.Context, genParam *generateParams,
reqHash := types.CalcRequestsHash(requests)
work.header.RequestsHash = &reqHash
}
// Finalize the state transition by applying operations such as withdrawals,
// uncle rewards, and related processing.
_, _, finalizeSpanEnd := telemetry.StartSpan(ctx, "miner.Finalize")
miner.engine.Finalize(miner.chain, work.header, work.state, &body)
finalizeSpanEnd(nil)
// Calculate the state root after applying all mutations.
_, _, rootSpanEnd := telemetry.StartSpan(ctx, "miner.IntermediateRoot")
work.header.Root = work.state.IntermediateRoot(miner.chain.Config().IsEIP158(work.header.Number))
rootSpanEnd(nil)
// Assemble the block for delivery.
_, _, blockSpanEnd := telemetry.StartSpan(ctx, "miner.NewBlock")
block := types.NewBlock(work.header, &body, work.receipts, trie.NewStackTrie(nil))
blockSpanEnd(nil)
_, _, assembleSpanEnd := telemetry.StartSpan(ctx, "miner.AssembleBlock")
block := core.AssembleBlock(miner.engine, miner.chain, work.header, work.state, &body, work.receipts)
assembleSpanEnd(nil)
return &newPayloadResult{
block: block,
@ -439,6 +427,7 @@ func (miner *Miner) applyTransaction(env *environment, tx *types.Transaction) (*
func (miner *Miner) commitTransactions(ctx context.Context, env *environment, plainTxs, blobTxs *transactionsByPriceAndNonce, interrupt *atomic.Int32) error {
ctx, _, spanEnd := telemetry.StartSpan(ctx, "miner.commitTransactions")
defer spanEnd(nil)
isCancun := miner.chainConfig.IsCancun(env.header.Number, env.header.Time)
for {
// Check interruption signal and abort building if it's fired.
@ -555,6 +544,7 @@ func (miner *Miner) commitTransactions(ctx context.Context, env *environment, pl
func (miner *Miner) fillTransactions(ctx context.Context, interrupt *atomic.Int32, env *environment) (err error) {
ctx, span, spanEnd := telemetry.StartSpan(ctx, "miner.fillTransactions")
defer spanEnd(&err)
miner.confMu.RLock()
tip := miner.config.GasPrice
prio := miner.prio