Skip to content

Commit

Permalink
refactor: replace BatchBlockExecutionOutput by `BundleStateWithRece…
Browse files Browse the repository at this point in the history
…ipts` (paradigmxyz#8709)
  • Loading branch information
tcoratger authored Jun 10, 2024
1 parent 3a8baa0 commit ad0be4c
Show file tree
Hide file tree
Showing 27 changed files with 138 additions and 120 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,15 @@ impl Command {
let db = StateProviderDatabase::new(blockchain_db.latest()?);
let executor = block_executor!(provider_factory.chain_spec()).executor(db);

let BlockExecutionOutput { state, receipts, .. } =
let BlockExecutionOutput { state, receipts, requests, .. } =
executor.execute((&block_with_senders.clone().unseal(), U256::MAX).into())?;
let state = BundleStateWithReceipts::new(state, receipts.into(), block.number);
let state = BundleStateWithReceipts::new(
state,
receipts.into(),
block.number,
vec![requests.into()],
);

debug!(target: "reth::cli", ?state, "Executed block");

let hashed_state = state.hash_state_slow();
Expand Down
9 changes: 7 additions & 2 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Command {

let merkle_block_td =
provider.header_td_by_number(merkle_block_number)?.unwrap_or_default();
let BlockExecutionOutput { state, receipts, .. } = executor.execute(
let BlockExecutionOutput { state, receipts, requests, .. } = executor.execute(
(
&block
.clone()
Expand All @@ -146,7 +146,12 @@ impl Command {
)
.into(),
)?;
let block_state = BundleStateWithReceipts::new(state, receipts.into(), block.number);
let block_state = BundleStateWithReceipts::new(
state,
receipts.into(),
block.number,
vec![requests.into()],
);

// Unpacked `BundleState::state_root_slow` function
let (in_memory_state_root, in_memory_updates) =
Expand Down
10 changes: 4 additions & 6 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use reth_config::Config;
use reth_consensus::Consensus;
use reth_db::{tables, DatabaseEnv};
use reth_db_api::{cursor::DbCursorRO, transaction::DbTx};
use reth_evm::execute::{BatchBlockExecutionOutput, BatchExecutor, BlockExecutorProvider};
use reth_evm::execute::{BatchExecutor, BlockExecutorProvider};
use reth_network::NetworkHandle;
use reth_network_api::NetworkInfo;
use reth_network_p2p::full_block::FullBlockClient;
use reth_primitives::{stage::StageCheckpoint, BlockHashOrNumber};
use reth_provider::{
BlockNumReader, BlockWriter, BundleStateWithReceipts, ChainSpecProvider, HeaderProvider,
LatestStateProviderRef, OriginalValuesKnown, ProviderError, ProviderFactory, StateWriter,
BlockNumReader, BlockWriter, ChainSpecProvider, HeaderProvider, LatestStateProviderRef,
OriginalValuesKnown, ProviderError, ProviderFactory, StateWriter,
};
use reth_prune_types::PruneModes;
use reth_revm::database::StateProviderDatabase;
Expand Down Expand Up @@ -161,9 +161,7 @@ impl Command {
PruneModes::none(),
);
executor.execute_and_verify_one((&sealed_block.clone().unseal(), td).into())?;
let BatchBlockExecutionOutput { bundle, receipts, requests: _, first_block } =
executor.finalize();
BundleStateWithReceipts::new(bundle, receipts, first_block).write_to_storage(
executor.finalize().write_to_storage(
provider_rw.tx_ref(),
None,
OriginalValuesKnown::Yes,
Expand Down
7 changes: 6 additions & 1 deletion bin/reth/src/commands/import_receipts_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ where
// We're reusing receipt writing code internal to
// `BundleStateWithReceipts::write_to_storage`, so we just use a default empty
// `BundleState`.
let bundled_state = BundleStateWithReceipts::new(Default::default(), receipts, first_block);
let bundled_state = BundleStateWithReceipts::new(
Default::default(),
receipts,
first_block,
Default::default(),
);

let static_file_producer =
static_file_provider.get_writer(first_block, StaticFileSegment::Receipts)?;
Expand Down
7 changes: 6 additions & 1 deletion crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ impl AppendableChain {
.consensus
.validate_block_post_execution(&block, PostExecutionInput::new(&receipts, &requests))?;

let bundle_state = BundleStateWithReceipts::new(state, receipts.into(), block.number);
let bundle_state = BundleStateWithReceipts::new(
state,
receipts.into(),
block.number,
vec![requests.into()],
);

// check state root if the block extends the canonical chain __and__ if state root
// validation was requested.
Expand Down
9 changes: 7 additions & 2 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,14 @@ impl StorageInner {
);

// execute the block
let BlockExecutionOutput { state, receipts, .. } =
let BlockExecutionOutput { state, receipts, requests: block_execution_requests, .. } =
executor.executor(&mut db).execute((&block, U256::ZERO).into())?;
let bundle_state = BundleStateWithReceipts::new(state, receipts.into(), block.number);
let bundle_state = BundleStateWithReceipts::new(
state,
receipts.into(),
block.number,
vec![block_execution_requests.into()],
);

// todo(onbjerg): we should not pass requests around as this is building a block, which
// means we need to extract the requests from the execution output and compute the requests
Expand Down
1 change: 1 addition & 0 deletions crates/ethereum/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ reth-primitives.workspace = true
reth-revm.workspace = true
reth-ethereum-consensus.workspace = true
reth-prune-types.workspace = true
reth-execution-types.workspace = true

# Ethereum
revm-primitives.workspace = true
Expand Down
11 changes: 6 additions & 5 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use crate::{
use reth_ethereum_consensus::validate_block_post_execution;
use reth_evm::{
execute::{
BatchBlockExecutionOutput, BatchExecutor, BlockExecutionError, BlockExecutionInput,
BlockExecutionOutput, BlockExecutorProvider, BlockValidationError, Executor, ProviderError,
BatchExecutor, BlockExecutionError, BlockExecutionInput, BlockExecutionOutput,
BlockExecutorProvider, BlockValidationError, Executor, ProviderError,
},
ConfigureEvm,
};
use reth_execution_types::BundleStateWithReceipts;
use reth_primitives::{
BlockNumber, BlockWithSenders, ChainSpec, Hardfork, Header, Receipt, Request, Withdrawals,
MAINNET, U256,
Expand Down Expand Up @@ -405,7 +406,7 @@ where
DB: Database<Error = ProviderError>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Output = BatchBlockExecutionOutput;
type Output = BundleStateWithReceipts;
type Error = BlockExecutionError;

fn execute_and_verify_one(&mut self, input: Self::Input<'_>) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -435,11 +436,11 @@ where
fn finalize(mut self) -> Self::Output {
self.stats.log_debug();

BatchBlockExecutionOutput::new(
BundleStateWithReceipts::new(
self.executor.state.take_bundle(),
self.batch_record.take_receipts(),
self.batch_record.take_requests(),
self.batch_record.first_block().unwrap_or_default(),
self.batch_record.take_requests(),
)
}

Expand Down
8 changes: 6 additions & 2 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,12 @@ where
// and 4788 contract call
db.merge_transitions(BundleRetention::PlainState);

let bundle =
BundleStateWithReceipts::new(db.take_bundle(), vec![receipts].into(), block_number);
let bundle = BundleStateWithReceipts::new(
db.take_bundle(),
vec![receipts].into(),
block_number,
vec![requests.clone().unwrap_or_default()],
);
let receipts_root = bundle.receipts_root_slow(block_number).expect("Number is in range");
let logs_bloom = bundle.block_logs_bloom(block_number).expect("Number is in range");

Expand Down
1 change: 1 addition & 0 deletions crates/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ reth-primitives.workspace = true
revm-primitives.workspace = true
reth-prune-types.workspace = true
reth-storage-errors.workspace = true
reth-execution-types.workspace = true

revm.workspace = true

Expand Down
1 change: 0 additions & 1 deletion crates/evm/execution-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ workspace = true
reth-primitives.workspace = true
reth-execution-errors.workspace = true
reth-trie.workspace = true
reth-evm.workspace = true

revm.workspace = true

Expand Down
41 changes: 18 additions & 23 deletions crates/evm/execution-types/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use reth_evm::execute::BatchBlockExecutionOutput;
use reth_primitives::{
logs_bloom,
revm::compat::{into_reth_acc, into_revm_acc},
Account, Address, BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, StorageEntry, B256,
U256,
Account, Address, BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, Requests, StorageEntry,
B256, U256,
};
use reth_trie::HashedPostState;
use revm::{
Expand All @@ -27,23 +26,13 @@ pub struct BundleStateWithReceipts {
pub receipts: Receipts,
/// First block of bundle state.
pub first_block: BlockNumber,
}

// TODO(mattsse): unify the types, currently there's a cyclic dependency between
impl From<BatchBlockExecutionOutput> for BundleStateWithReceipts {
fn from(value: BatchBlockExecutionOutput) -> Self {
let BatchBlockExecutionOutput { bundle, receipts, requests: _, first_block } = value;
Self { bundle, receipts, first_block }
}
}

// TODO(mattsse): unify the types, currently there's a cyclic dependency between
impl From<BundleStateWithReceipts> for BatchBlockExecutionOutput {
fn from(value: BundleStateWithReceipts) -> Self {
let BundleStateWithReceipts { bundle, receipts, first_block } = value;
// TODO(alexey): add requests
Self { bundle, receipts, requests: Vec::default(), first_block }
}
/// The collection of EIP-7685 requests.
/// Outer vector stores requests for each block sequentially.
/// The inner vector stores requests ordered by transaction number.
///
/// A transaction may have zero or more requests, so the length of the inner vector is not
/// guaranteed to be the same as the number of transactions.
pub requests: Vec<Requests>,
}

/// Type used to initialize revms bundle state.
Expand All @@ -58,8 +47,13 @@ pub type RevertsInit = HashMap<BlockNumber, HashMap<Address, AccountRevertInit>>

impl BundleStateWithReceipts {
/// Create Bundle State.
pub const fn new(bundle: BundleState, receipts: Receipts, first_block: BlockNumber) -> Self {
Self { bundle, receipts, first_block }
pub const fn new(
bundle: BundleState,
receipts: Receipts,
first_block: BlockNumber,
requests: Vec<Requests>,
) -> Self {
Self { bundle, receipts, first_block, requests }
}

/// Create new bundle state with receipts.
Expand All @@ -69,6 +63,7 @@ impl BundleStateWithReceipts {
contracts_init: Vec<(B256, Bytecode)>,
receipts: Receipts,
first_block: BlockNumber,
requests: Vec<Requests>,
) -> Self {
// sort reverts by block number
let mut reverts = revert_init.into_iter().collect::<Vec<_>>();
Expand Down Expand Up @@ -97,7 +92,7 @@ impl BundleStateWithReceipts {
contracts_init.into_iter().map(|(code_hash, bytecode)| (code_hash, bytecode.0)),
);

Self { bundle, receipts, first_block }
Self { bundle, receipts, first_block, requests }
}

/// Return revm bundle state.
Expand Down
2 changes: 2 additions & 0 deletions crates/evm/execution-types/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ mod tests {
),
vec![vec![]].into(),
1,
vec![],
);

let block_state2 = BundleStateWithReceipts::new(
Expand All @@ -541,6 +542,7 @@ mod tests {
),
vec![vec![]].into(),
2,
vec![],
);

let mut block1 = SealedBlockWithSenders::default();
Expand Down
10 changes: 5 additions & 5 deletions crates/evm/src/either.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Helper type that represents one of two possible executor types
use crate::execute::{
BatchBlockExecutionOutput, BatchExecutor, BlockExecutionInput, BlockExecutionOutput,
BlockExecutorProvider, Executor,
BatchExecutor, BlockExecutionInput, BlockExecutionOutput, BlockExecutorProvider, Executor,
};
use reth_execution_errors::BlockExecutionError;
use reth_execution_types::BundleStateWithReceipts;
use reth_primitives::{BlockNumber, BlockWithSenders, Receipt};
use reth_prune_types::PruneModes;
use reth_storage_errors::provider::ProviderError;
Expand Down Expand Up @@ -76,19 +76,19 @@ where
A: for<'a> BatchExecutor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Output = BatchBlockExecutionOutput,
Output = BundleStateWithReceipts,
Error = BlockExecutionError,
>,
B: for<'a> BatchExecutor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Output = BatchBlockExecutionOutput,
Output = BundleStateWithReceipts,
Error = BlockExecutionError,
>,
DB: Database<Error = ProviderError>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Output = BatchBlockExecutionOutput;
type Output = BundleStateWithReceipts;
type Error = BlockExecutionError;

fn execute_and_verify_one(&mut self, input: Self::Input<'_>) -> Result<(), Self::Error> {
Expand Down
Loading

0 comments on commit ad0be4c

Please sign in to comment.