Skip to content

Block access list changes - BAL construction, execution and validation #32263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cd191ed
trie: introduce UpdateBatch
rjl493456442 Aug 15, 2025
0dcd0e2
integrate trie batch update into statedb intermediate root calculation
jwasinger Aug 18, 2025
d902ace
all: add block access list construction via flag --experimentalbal. …
jwasinger Jul 2, 2025
038f038
more cleanups
jwasinger Aug 4, 2025
10dbc70
fix lint
jwasinger Aug 4, 2025
5ad76d3
update metrics for BAL
jwasinger Aug 4, 2025
206b22b
try fix
jwasinger Aug 4, 2025
e615211
fix
jwasinger Aug 4, 2025
7b40a5b
remove accidental line deletion
jwasinger Aug 5, 2025
c223cec
add debug_getEncodedBlockAccesslist
jwasinger Aug 6, 2025
1c60b1a
remove BAL prefetcher: with account/storage reads planning to be rem…
jwasinger Aug 6, 2025
2322ff9
fix method name
jwasinger Aug 6, 2025
6d26f07
rework of state diff calculation to be much faster. remove diff accu…
jwasinger Aug 7, 2025
43d6f2a
add test data
jwasinger Aug 7, 2025
58cad97
experimental: test gating the tx execution concurrency based on numb…
jwasinger Aug 7, 2025
fb98718
instantiate live state object set from reader upfront. Apply BAL cha…
jwasinger Aug 11, 2025
39abcf1
fix tests
jwasinger Aug 12, 2025
faad758
cleanups and documentation
jwasinger Aug 13, 2025
e14a463
add timer for block preprocess state loading
jwasinger Aug 16, 2025
fb50477
Merge remote-tracking branch 'jwasinger/batch-update-intermediate-roo…
jwasinger Aug 19, 2025
fdb7123
fix build
jwasinger Aug 19, 2025
83ad18c
optimize tx execution prestate instantiation
jwasinger Aug 19, 2025
9bb4956
moar
jwasinger Aug 20, 2025
3f1e343
fix state diff storage instantation
jwasinger Aug 20, 2025
fe78a5e
fix remaining tests
jwasinger Aug 20, 2025
d735907
less statedb copies up-front before beginning tx execution
jwasinger Aug 20, 2025
c0b1fb9
add metric for BAL->diff creation
jwasinger Aug 20, 2025
88bcc7e
wip: don't build state-diffs from BAL up-front
jwasinger Aug 21, 2025
45d8208
fixes
jwasinger Aug 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/evm/blockrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func runBlockTest(ctx *cli.Context, fname string) ([]testResult, error) {
continue
}
result := &testResult{Name: name, Pass: true}
if err := tests[name].Run(false, rawdb.PathScheme, ctx.Bool(WitnessCrossCheckFlag.Name), tracer, func(res error, chain *core.BlockChain) {
if err := tests[name].Run(false, rawdb.PathScheme, ctx.Bool(WitnessCrossCheckFlag.Name), false, tracer, func(res error, chain *core.BlockChain) {
if ctx.Bool(DumpFlag.Name) {
if s, _ := chain.State(); s != nil {
result.State = dump(s)
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ var (
utils.BeaconGenesisTimeFlag,
utils.BeaconCheckpointFlag,
utils.BeaconCheckpointFileFlag,
utils.ExperimentalBALFlag,
}, utils.NetworkFlags, utils.DatabaseFlags)

rpcFlags = []cli.Flag{
Expand Down
11 changes: 11 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,14 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
Value: metrics.DefaultConfig.InfluxDBOrganization,
Category: flags.MetricsCategory,
}

// Block Access List flags

ExperimentalBALFlag = &cli.BoolFlag{
Name: "experimentalbal",
Usage: "Enable block-access-list building when importing post-Cancun blocks, and validation that access lists contained in post-Cancun blocks correctly correspond to the state changes in those blocks. This is used for development purposes only. Do not enable it otherwise.",
Category: flags.MiscCategory,
}
)

var (
Expand Down Expand Up @@ -1852,6 +1860,8 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.VMTraceJsonConfig = ctx.String(VMTraceJsonConfigFlag.Name)
}
}

cfg.ExperimentalBAL = ctx.Bool(ExperimentalBALFlag.Name)
}

// MakeBeaconLightConfig constructs a beacon light client config based on the
Expand Down Expand Up @@ -2244,6 +2254,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
}
options.VmConfig = vmcfg

options.EnableBAL = ctx.Bool(ExperimentalBALFlag.Name)
chain, err := core.NewBlockChain(chainDb, gspec, engine, options)
if err != nil {
Fatalf("Can't create BlockChain: %v", err)
Expand Down
48 changes: 48 additions & 0 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,53 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
return nil
}

// ValidateProcessResult validates block fields against the result of execution.
// It is used for the purpose of validating access-list-containing blocks and
// does not check the integrity of the block's state root.
func (v *BlockValidator) ValidateProcessResult(block *types.Block, resCh chan *ProcessResultWithMetrics, stateless bool) (*ProcessResultWithMetrics, error) {
header := block.Header()

res := <-resCh
if res.ProcessResult.Error != nil {
return nil, res.ProcessResult.Error
}

if block.GasUsed() != res.ProcessResult.GasUsed {
return res, fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), res.ProcessResult.GasUsed)
}
// Validate the received block's bloom with the one derived from the generated receipts.
// For valid blocks this should always validate to true.
//
// Receipts must go through MakeReceipt to calculate the receipt's bloom
// already. Merge the receipt's bloom together instead of recalculating
// everything.
rbloom := types.MergeBloom(res.ProcessResult.Receipts)
if rbloom != header.Bloom {
return res, fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom)
}
// In stateless mode, return early because the receipt and state root are not
// provided through the witness, rather the cross validator needs to return it.
if stateless {
return res, nil
}
// The receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]]))
receiptSha := types.DeriveSha(res.ProcessResult.Receipts, trie.NewStackTrie(nil))
if receiptSha != header.ReceiptHash {
return res, fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
}
// Validate the parsed requests match the expected header value.
if header.RequestsHash != nil {
reqhash := types.CalcRequestsHash(res.ProcessResult.Requests)
if reqhash != *header.RequestsHash {
return res, fmt.Errorf("invalid requests hash (remote: %x local: %x)", *header.RequestsHash, reqhash)
}
} else if res.ProcessResult.Requests != nil {
return res, errors.New("block has requests before prague fork")
}

return res, nil
}

// ValidateState validates the various changes that happen after a state transition,
// such as amount of used gas, the receipt roots and the state root itself.
func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, res *ProcessResult, stateless bool) error {
Expand Down Expand Up @@ -160,6 +207,7 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
} else if res.Requests != nil {
return errors.New("block has requests before prague fork")
}

// Validate the state root against the received state root and throw
// an error if they don't match.
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
Expand Down
169 changes: 134 additions & 35 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ var (
blockExecutionTimer = metrics.NewRegisteredResettingTimer("chain/execution", nil)
blockWriteTimer = metrics.NewRegisteredResettingTimer("chain/write", nil)

// BAL-specific timers
blockPreprocessingTimer = metrics.NewRegisteredResettingTimer("chain/preprocess", nil)
blockPreprocessingLoadTimer = metrics.NewRegisteredResettingTimer("chain/preprocessload", nil)
blockPreprocessingDiffCreationTimer = metrics.NewRegisteredResettingTimer("chain/preprocessdiff", nil)
txExecutionTimer = metrics.NewRegisteredResettingTimer("chain/txexecution", nil)
stateRootCalctimer = metrics.NewRegisteredResettingTimer("chain/rootcalculation", nil)
blockPostprocessingTimer = metrics.NewRegisteredResettingTimer("chain/postprocess", nil)

blockReorgMeter = metrics.NewRegisteredMeter("chain/reorg/executes", nil)
blockReorgAddMeter = metrics.NewRegisteredMeter("chain/reorg/add", nil)
blockReorgDropMeter = metrics.NewRegisteredMeter("chain/reorg/drop", nil)
Expand Down Expand Up @@ -193,6 +201,9 @@ type BlockChainConfig struct {
// If the value is zero, all transactions of the entire chain will be indexed.
// If the value is -1, indexing is disabled.
TxLookupLimit int64

// EnableBAL enables block access list creation and verification for post-Cancun blocks which contain access lists.
EnableBAL bool
}

// DefaultConfig returns the default config.
Expand Down Expand Up @@ -1881,7 +1892,16 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
}
// The traced section of block import.
start := time.Now()
res, err := bc.processBlock(parent.Root, block, setHead, makeWitness && len(chain) == 1)

blockHasAccessList := block.Body().AccessList != nil
// BAL generation/verification is not enabled pre-selfdestruct removal.
// TODO: there is a bug where state diff recording will be activated if
// cancun is enabled, this should be fixed before merging!
forkSupportsBAL := bc.chainConfig.IsCancun(block.Number(), block.Time())
makeBAL := forkSupportsBAL && !blockHasAccessList
validateBAL := forkSupportsBAL && blockHasAccessList

res, err := bc.processBlock(parent.Root, block, setHead, makeWitness && len(chain) == 1, makeBAL, validateBAL)
if err != nil {
return nil, it.index, err
}
Expand Down Expand Up @@ -1949,7 +1969,7 @@ type blockProcessingResult struct {

// processBlock executes and validates the given block. If there was no error
// it writes the block and associated state to database.
func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, setHead bool, makeWitness bool) (_ *blockProcessingResult, blockEndErr error) {
func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, setHead bool, makeWitness bool, constructBAL bool, validateBAL bool) (bpr *blockProcessingResult, blockEndErr error) {
var (
err error
startTime = time.Now()
Expand All @@ -1960,6 +1980,9 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s

if bc.cfg.NoPrefetch {
statedb, err = state.New(parentRoot, bc.statedb)
if constructBAL || validateBAL {
statedb.EnableStateDiffRecording()
}
if err != nil {
return nil, err
}
Expand All @@ -1981,6 +2004,9 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s
if err != nil {
return nil, err
}
if constructBAL || validateBAL {
statedb.EnableStateDiffRecording()
}
// Upload the statistics of reader at the end
defer func() {
stats := prefetch.GetStats()
Expand All @@ -1999,7 +2025,10 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s
// Disable tracing for prefetcher executions.
vmCfg := bc.cfg.VmConfig
vmCfg.Tracer = nil
bc.prefetcher.Prefetch(block, throwaway, vmCfg, &interrupt)
if block.Body().AccessList == nil {
// only use the state prefetcher for non-BAL blocks.
bc.prefetcher.Prefetch(block, throwaway, vmCfg, &interrupt)
}

blockPrefetchExecuteTimer.Update(time.Since(start))
if interrupt.Load() {
Expand All @@ -2008,6 +2037,10 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s
}(time.Now(), throwaway, block)
}

if constructBAL {
statedb.EnableBALConstruction()
}

// If we are past Byzantium, enable prefetching to pull in trie node paths
// while processing transactions. Before Byzantium the prefetcher is mostly
// useless due to the intermediate root hashing after each transaction.
Expand All @@ -2022,8 +2055,15 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s
return nil, err
}
}
statedb.StartPrefetcher("chain", witness)
defer statedb.StopPrefetcher()

// access-list containing blocks don't use the prefetcher because
// state root computation proceeds concurrently with transaction
// execution, meaning the prefetcher doesn't have any time to run
// before the trie nodes are needed for state root computation.
if block.Body().AccessList == nil {
statedb.StartPrefetcher("chain", witness)
defer statedb.StopPrefetcher()
}
}

if bc.logger != nil && bc.logger.OnBlockStart != nil {
Expand All @@ -2039,21 +2079,67 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s
}()
}

// Process block using the parent state as reference point
pstart := time.Now()
res, err := bc.processor.Process(block, statedb, bc.cfg.VmConfig)
if err != nil {
bc.reportBlock(block, res, err)
return nil, err
var res *ProcessResult
var resWithMetrics *ProcessResultWithMetrics
var ptime, vtime time.Duration
if block.Body().AccessList != nil {
if block.NumberU64() == 0 {
return nil, fmt.Errorf("genesis block cannot have a block access list")
}
if !validateBAL && !bc.chainConfig.IsGlamsterdam(block.Number(), block.Time()) {
bc.reportBlock(block, res, fmt.Errorf("received block containing access list before glamsterdam activated"))
return nil, err
}
// Process block using the parent state as reference point
pstart := time.Now()
var resCh chan *ProcessResultWithMetrics
resCh, err = bc.processor.ProcessWithAccessList(block, statedb, bc.cfg.VmConfig)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to return a channel? Seems like it could be a blocking operation. Also I would probably do the branching for access list vs. not in the Process function instead of here since it is really more related to the processing of the block than the broader chain. It should also simplify the changes a bit since both branches of this if are quite similar, especially if you then roll the checks from ValidateProcessResult into ValidateState.

if err != nil {
// TODO: okay to pass nil here as execution result?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine, we just use it to get out the receipts (if it is non nil).

bc.reportBlock(block, nil, err)
return nil, err
}
ptime = time.Since(pstart)

vstart := time.Now()
var err error
resWithMetrics, err = bc.validator.ValidateProcessResult(block, resCh, false)
if err != nil {
// TODO: okay to pass nil here as execution result?
bc.reportBlock(block, nil, err)
return nil, err
}
res = resWithMetrics.ProcessResult
vtime = time.Since(vstart)
} else {
// Process block using the parent state as reference point
pstart := time.Now()
res, err = bc.processor.Process(block, statedb, bc.cfg.VmConfig)
if err != nil {
bc.reportBlock(block, res, err)
return nil, err
}
ptime = time.Since(pstart)

vstart := time.Now()
if err := bc.validator.ValidateState(block, statedb, res, false); err != nil {
bc.reportBlock(block, res, err)
return nil, err
}
vtime = time.Since(vstart)
}
ptime := time.Since(pstart)

vstart := time.Now()
if err := bc.validator.ValidateState(block, statedb, res, false); err != nil {
bc.reportBlock(block, res, err)
return nil, err
blockHadBAL := block.Body().AccessList != nil

if constructBAL {
// very ugly... deep-copy the block body before setting the block access
// list on it to prevent mutating the block instance passed by the caller.
existingBody := block.Body()
block = block.WithBody(*existingBody)
existingBody = block.Body()
existingBody.AccessList = statedb.ConstructionBlockAccessList().ToEncodingObj()
block = block.WithBody(*existingBody)
}
vtime := time.Since(vstart)

// If witnesses was generated and stateless self-validation requested, do
// that now. Self validation should *never* run in production, it's more of
Expand Down Expand Up @@ -2083,26 +2169,39 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s
return nil, fmt.Errorf("stateless self-validation receipt root mismatch (cross: %x local: %x)", crossReceiptRoot, block.ReceiptHash())
}
}
xvtime := time.Since(xvstart)
proctime := time.Since(startTime) // processing + validation + cross validation

// Update the metrics touched during block processing and validation
accountReadTimer.Update(statedb.AccountReads) // Account reads are complete(in processing)
storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete(in processing)
if statedb.AccountLoaded != 0 {
accountReadSingleTimer.Update(statedb.AccountReads / time.Duration(statedb.AccountLoaded))
}
if statedb.StorageLoaded != 0 {
storageReadSingleTimer.Update(statedb.StorageReads / time.Duration(statedb.StorageLoaded))
var proctime time.Duration
if blockHadBAL {
blockPreprocessingTimer.Update(resWithMetrics.PreProcessTime)
blockPreprocessingLoadTimer.Update(resWithMetrics.PreProcessLoadTime)
blockPreprocessingDiffCreationTimer.Update(resWithMetrics.StateDiffCalcTime)
txExecutionTimer.Update(resWithMetrics.ExecTime)
stateRootCalctimer.Update(resWithMetrics.RootCalcTime)
blockPostprocessingTimer.Update(resWithMetrics.PostProcessTime)

accountHashTimer.Update(statedb.AccountHashes)
} else {
xvtime := time.Since(xvstart)
proctime = time.Since(startTime) // processing + validation + cross validation

// Update the metrics touched during block processing and validation
accountReadTimer.Update(statedb.AccountReads) // Account reads are complete(in processing)
storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete(in processing)
if statedb.AccountLoaded != 0 {
accountReadSingleTimer.Update(statedb.AccountReads / time.Duration(statedb.AccountLoaded))
}
if statedb.StorageLoaded != 0 {
storageReadSingleTimer.Update(statedb.StorageReads / time.Duration(statedb.StorageLoaded))
}
accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete(in validation)
storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete(in validation)
accountHashTimer.Update(statedb.AccountHashes) // Account hashes are complete(in validation)
triehash := statedb.AccountHashes // The time spent on tries hashing
trieUpdate := statedb.AccountUpdates + statedb.StorageUpdates // The time spent on tries update
blockExecutionTimer.Update(ptime - (statedb.AccountReads + statedb.StorageReads)) // The time spent on EVM processing
blockValidationTimer.Update(vtime - (triehash + trieUpdate)) // The time spent on block validation
blockCrossValidationTimer.Update(xvtime) // The time spent on stateless cross validation
}
accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete(in validation)
storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete(in validation)
accountHashTimer.Update(statedb.AccountHashes) // Account hashes are complete(in validation)
triehash := statedb.AccountHashes // The time spent on tries hashing
trieUpdate := statedb.AccountUpdates + statedb.StorageUpdates // The time spent on tries update
blockExecutionTimer.Update(ptime - (statedb.AccountReads + statedb.StorageReads)) // The time spent on EVM processing
blockValidationTimer.Update(vtime - (triehash + trieUpdate)) // The time spent on block validation
blockCrossValidationTimer.Update(xvtime) // The time spent on stateless cross validation

// Write the block to the chain and get the status.
var (
Expand Down
4 changes: 2 additions & 2 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ func (b *BlockGen) collectRequests(readonly bool) (requests [][]byte) {
blockContext := NewEVMBlockContext(b.header, b.cm, &b.header.Coinbase)
evm := vm.NewEVM(blockContext, statedb, b.cm.config, vm.Config{})
// EIP-7002
if err := ProcessWithdrawalQueue(&requests, evm); err != nil {
if _, err := ProcessWithdrawalQueue(&requests, evm); err != nil {
panic(fmt.Sprintf("could not process withdrawal requests: %v", err))
}
// EIP-7251
if err := ProcessConsolidationQueue(&requests, evm); err != nil {
if _, err := ProcessConsolidationQueue(&requests, evm); err != nil {
panic(fmt.Sprintf("could not process consolidation requests: %v", err))
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
},
}
if faucet != nil {
genesis.Alloc[*faucet] = types.Account{Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))}
genesis.Alloc[*faucet] = types.Account{Balance: new(big.Int).Lsh(big.NewInt(1), 95)}
}
return genesis
}
Expand Down
Loading