Skip to content
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

refactor: use BundleBuilder as a state builder #8861

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
57 changes: 23 additions & 34 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/reth/src/commands/stage/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Command {
Default::default(),
)?;
let alloc = &self.env.chain.genesis().alloc;
insert_genesis_state::<DatabaseEnv>(tx, alloc.len(), alloc.iter())?;
insert_genesis_state::<DatabaseEnv>(tx, alloc.iter())?;
}
StageEnum::AccountHashing => {
tx.clear::<tables::HashedAccounts>()?;
Expand Down
144 changes: 11 additions & 133 deletions crates/evm/execution-types/src/execution_outcome.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use reth_primitives::{
logs_bloom,
revm::compat::{into_reth_acc, into_revm_acc},
Account, Address, BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, Requests, StorageEntry,
B256, U256,
logs_bloom, revm::compat::into_reth_acc, Account, Address, BlockNumber, Bloom, Bytecode, Log,
Receipt, Receipts, Requests, StorageEntry, B256, U256,
};
use reth_trie::HashedPostState;
use revm::{
db::{states::BundleState, BundleAccount},
db::{
states::{BundleBuilder, BundleState},
BundleAccount,
},
primitives::AccountInfo,
};
use std::collections::HashMap;
Expand Down Expand Up @@ -64,42 +65,13 @@ impl ExecutionOutcome {
///
/// This constructor initializes a new `ExecutionOutcome` instance using detailed
/// initialization parameters.
pub fn new_init(
state_init: BundleStateInit,
revert_init: RevertsInit,
contracts_init: Vec<(B256, Bytecode)>,
pub fn from_state_builder(
state_builder: BundleBuilder,
receipts: Receipts,
first_block: BlockNumber,
requests: Vec<Requests>,
) -> Self {
// sort reverts by block number
let mut reverts = revert_init.into_iter().collect::<Vec<_>>();
reverts.sort_unstable_by_key(|a| a.0);

// initialize revm bundle
let bundle = BundleState::new(
state_init.into_iter().map(|(address, (original, present, storage))| {
(
address,
original.map(into_revm_acc),
present.map(into_revm_acc),
storage.into_iter().map(|(k, s)| (k.into(), s)).collect(),
)
}),
reverts.into_iter().map(|(_, reverts)| {
// does not needs to be sorted, it is done when taking reverts.
reverts.into_iter().map(|(address, (original, storage))| {
(
address,
original.map(|i| i.map(into_revm_acc)),
storage.into_iter().map(|entry| (entry.key.into(), entry.value)),
)
})
}),
contracts_init.into_iter().map(|(code_hash, bytecode)| (code_hash, bytecode.0)),
);

Self { bundle, receipts, first_block, requests }
Self { bundle: state_builder.build(), receipts, first_block, requests }
}

/// Return revm bundle state.
Expand Down Expand Up @@ -330,103 +302,9 @@ impl ExecutionOutcome {
#[cfg(test)]
mod tests {
use super::*;
use alloy_eips::{eip6110::DepositRequest, eip7002::WithdrawalRequest};
use alloy_eips::eip6110::DepositRequest;
use alloy_primitives::{FixedBytes, LogData};
use reth_primitives::{Address, Receipts, Request, Requests, TxType, B256};
use std::collections::HashMap;

#[test]
fn test_initialisation() {
// Create a new BundleState object with initial data
let bundle = BundleState::new(
vec![(Address::new([2; 20]), None, Some(AccountInfo::default()), HashMap::default())],
vec![vec![(Address::new([2; 20]), None, vec![])]],
vec![],
);

// Create a Receipts object with a vector of receipt vectors
let receipts = Receipts {
receipt_vec: vec![vec![Some(Receipt {
tx_type: TxType::Legacy,
cumulative_gas_used: 46913,
logs: vec![],
success: true,
#[cfg(feature = "optimism")]
deposit_nonce: Some(18),
#[cfg(feature = "optimism")]
deposit_receipt_version: Some(34),
})]],
};

// Create a Requests object with a vector of requests, including DepositRequest and
// WithdrawalRequest
let requests = vec![Requests(vec![
Request::DepositRequest(DepositRequest {
pubkey: FixedBytes::<48>::from([1; 48]),
withdrawal_credentials: B256::from([0; 32]),
amount: 1111,
signature: FixedBytes::<96>::from([2; 96]),
index: 222,
}),
Request::DepositRequest(DepositRequest {
pubkey: FixedBytes::<48>::from([23; 48]),
withdrawal_credentials: B256::from([0; 32]),
amount: 34343,
signature: FixedBytes::<96>::from([43; 96]),
index: 1212,
}),
Request::WithdrawalRequest(WithdrawalRequest {
source_address: Address::from([1; 20]),
validator_public_key: FixedBytes::<48>::from([10; 48]),
amount: 72,
}),
])];

// Define the first block number
let first_block = 123;

// Create a ExecutionOutcome object with the created bundle, receipts, requests, and
// first_block
let exec_res = ExecutionOutcome {
bundle: bundle.clone(),
receipts: receipts.clone(),
requests: requests.clone(),
first_block,
};

// Assert that creating a new ExecutionOutcome using the constructor matches exec_res
assert_eq!(
ExecutionOutcome::new(bundle, receipts.clone(), first_block, requests.clone()),
exec_res
);

// Create a BundleStateInit object and insert initial data
let mut state_init: BundleStateInit = HashMap::new();
state_init
.insert(Address::new([2; 20]), (None, Some(Account::default()), HashMap::default()));

// Create a HashMap for account reverts and insert initial data
let mut revert_inner: HashMap<Address, AccountRevertInit> = HashMap::new();
revert_inner.insert(Address::new([2; 20]), (None, vec![]));

// Create a RevertsInit object and insert the revert_inner data
let mut revert_init: RevertsInit = HashMap::new();
revert_init.insert(123, revert_inner);

// Assert that creating a new ExecutionOutcome using the new_init method matches
// exec_res
assert_eq!(
ExecutionOutcome::new_init(
state_init,
revert_init,
vec![],
receipts,
first_block,
requests,
),
exec_res
);
}
use reth_primitives::{Receipts, Request, Requests, TxType, B256};

#[test]
fn test_block_number_to_index() {
Expand Down
Loading
Loading