mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-30 09:38:24 -05:00
chore: get rid of some duplicated execution code (#3447)
This commit is contained in:
@@ -184,37 +184,6 @@ impl AppendableChain {
|
||||
externals: &TreeExternals<DB, C, EF>,
|
||||
block_kind: BlockKind,
|
||||
) -> Result<PostState, Error>
|
||||
where
|
||||
PSDP: PostStateDataProvider,
|
||||
DB: Database,
|
||||
C: Consensus,
|
||||
EF: ExecutorFactory,
|
||||
{
|
||||
if block_kind.extends_canonical_head() {
|
||||
Self::validate_and_execute_canonical_head_descendant(
|
||||
block,
|
||||
parent_block,
|
||||
post_state_data_provider,
|
||||
externals,
|
||||
)
|
||||
} else {
|
||||
Self::validate_and_execute_sidechain(
|
||||
block,
|
||||
parent_block,
|
||||
post_state_data_provider,
|
||||
externals,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Validate and execute the given block that _extends the canonical chain_, validating its
|
||||
/// state root after execution.
|
||||
fn validate_and_execute_canonical_head_descendant<PSDP, DB, C, EF>(
|
||||
block: SealedBlockWithSenders,
|
||||
parent_block: &SealedHeader,
|
||||
post_state_data_provider: PSDP,
|
||||
externals: &TreeExternals<DB, C, EF>,
|
||||
) -> Result<PostState, Error>
|
||||
where
|
||||
PSDP: PostStateDataProvider,
|
||||
DB: Database,
|
||||
@@ -237,19 +206,45 @@ impl AppendableChain {
|
||||
let mut executor = externals.executor_factory.with_sp(&provider);
|
||||
let post_state = executor.execute_and_verify_receipt(&block, U256::MAX, Some(senders))?;
|
||||
|
||||
// check state root
|
||||
let state_root = provider.state_root(post_state.clone())?;
|
||||
if block.state_root != state_root {
|
||||
return Err(ConsensusError::BodyStateRootDiff {
|
||||
got: state_root,
|
||||
expected: block.state_root,
|
||||
// check state root if the block extends the canonical chain.
|
||||
if block_kind.extends_canonical_head() {
|
||||
// check state root
|
||||
let state_root = provider.state_root(post_state.clone())?;
|
||||
if block.state_root != state_root {
|
||||
return Err(ConsensusError::BodyStateRootDiff {
|
||||
got: state_root,
|
||||
expected: block.state_root,
|
||||
}
|
||||
.into())
|
||||
}
|
||||
.into())
|
||||
}
|
||||
|
||||
Ok(post_state)
|
||||
}
|
||||
|
||||
/// Validate and execute the given block that _extends the canonical chain_, validating its
|
||||
/// state root after execution.
|
||||
fn validate_and_execute_canonical_head_descendant<PSDP, DB, C, EF>(
|
||||
block: SealedBlockWithSenders,
|
||||
parent_block: &SealedHeader,
|
||||
post_state_data_provider: PSDP,
|
||||
externals: &TreeExternals<DB, C, EF>,
|
||||
) -> Result<PostState, Error>
|
||||
where
|
||||
PSDP: PostStateDataProvider,
|
||||
DB: Database,
|
||||
C: Consensus,
|
||||
EF: ExecutorFactory,
|
||||
{
|
||||
Self::validate_and_execute(
|
||||
block,
|
||||
parent_block,
|
||||
post_state_data_provider,
|
||||
externals,
|
||||
BlockKind::ExtendsCanonicalHead,
|
||||
)
|
||||
}
|
||||
|
||||
/// Validate and execute the given sidechain block, skipping state root validation.
|
||||
fn validate_and_execute_sidechain<PSDP, DB, C, EF>(
|
||||
block: SealedBlockWithSenders,
|
||||
@@ -263,23 +258,13 @@ impl AppendableChain {
|
||||
C: Consensus,
|
||||
EF: ExecutorFactory,
|
||||
{
|
||||
// ensure the block is a valid descendant of the parent, according to consensus rules
|
||||
externals.consensus.validate_header_against_parent(&block, parent_block)?;
|
||||
|
||||
let (block, senders) = block.into_components();
|
||||
let block = block.unseal();
|
||||
|
||||
// get the state provider.
|
||||
let db = externals.database();
|
||||
let canonical_fork = post_state_data_provider.canonical_fork();
|
||||
let state_provider = db.history_by_block_number(canonical_fork.number)?;
|
||||
|
||||
let provider = PostStateProvider::new(state_provider, post_state_data_provider);
|
||||
|
||||
let mut executor = externals.executor_factory.with_sp(&provider);
|
||||
let post_state = executor.execute_and_verify_receipt(&block, U256::MAX, Some(senders))?;
|
||||
|
||||
Ok(post_state)
|
||||
Self::validate_and_execute(
|
||||
block,
|
||||
parent_block,
|
||||
post_state_data_provider,
|
||||
externals,
|
||||
BlockKind::ForksHistoricalBlock,
|
||||
)
|
||||
}
|
||||
|
||||
/// Validate and execute the given block, and append it to this chain.
|
||||
|
||||
Reference in New Issue
Block a user