-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
jwasinger
wants to merge
29
commits into
ethereum:master
Choose a base branch
from
jwasinger:bal-execution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,258
−229
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
cd191ed
trie: introduce UpdateBatch
rjl493456442 0dcd0e2
integrate trie batch update into statedb intermediate root calculation
jwasinger d902ace
all: add block access list construction via flag --experimentalbal. …
jwasinger 038f038
more cleanups
jwasinger 10dbc70
fix lint
jwasinger 5ad76d3
update metrics for BAL
jwasinger 206b22b
try fix
jwasinger e615211
fix
jwasinger 7b40a5b
remove accidental line deletion
jwasinger c223cec
add debug_getEncodedBlockAccesslist
jwasinger 1c60b1a
remove BAL prefetcher: with account/storage reads planning to be rem…
jwasinger 2322ff9
fix method name
jwasinger 6d26f07
rework of state diff calculation to be much faster. remove diff accu…
jwasinger 43d6f2a
add test data
jwasinger 58cad97
experimental: test gating the tx execution concurrency based on numb…
jwasinger fb98718
instantiate live state object set from reader upfront. Apply BAL cha…
jwasinger 39abcf1
fix tests
jwasinger faad758
cleanups and documentation
jwasinger e14a463
add timer for block preprocess state loading
jwasinger fb50477
Merge remote-tracking branch 'jwasinger/batch-update-intermediate-roo…
jwasinger fdb7123
fix build
jwasinger 83ad18c
optimize tx execution prestate instantiation
jwasinger 9bb4956
moar
jwasinger 3f1e343
fix state diff storage instantation
jwasinger fe78a5e
fix remaining tests
jwasinger d735907
less statedb copies up-front before beginning tx execution
jwasinger c0b1fb9
add metric for BAL->diff creation
jwasinger 88bcc7e
wip: don't build state-diffs from BAL up-front
jwasinger 45d8208
fixes
jwasinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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. | ||
|
@@ -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 | ||
} | ||
|
@@ -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() | ||
|
@@ -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 | ||
} | ||
|
@@ -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() | ||
|
@@ -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() { | ||
|
@@ -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. | ||
|
@@ -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 { | ||
|
@@ -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) | ||
if err != nil { | ||
// TODO: okay to pass nil here as execution result? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 ( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thisif
are quite similar, especially if you then roll the checks fromValidateProcessResult
intoValidateState
.