Skip to content

Commit

Permalink
force execution outcome to have chain_id
Browse files Browse the repository at this point in the history
  • Loading branch information
CeciliaZ030 committed Sep 27, 2024
1 parent a0091f7 commit 27cfd6b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 46 deletions.
2 changes: 1 addition & 1 deletion crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl AppendableChain {
// TODO(Cecilie): refactor the bundle state provider for cross-chain bundles
let mut execution_outcome =
provider.block_execution_data_provider.execution_outcome().clone();
execution_outcome.chain_id = Some(chain_id);
execution_outcome.chain_id = chain_id;
execution_outcome.extend(initial_execution_outcome.clone());
let hashed_state = execution_outcome.hash_state_slow();
ParallelStateRoot::new(consistent_view, hashed_state)
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/execution-types/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ mod tests {
// Create an ExecutionOutcome object with the created bundle, receipts, an empty requests
// vector, and first_block set to 10
let execution_outcome = ExecutionOutcome {
chain_id: None,
chain_id: ETHEREUM_CHAIN_ID,
bundle: Default::default(),
receipts,
requests: vec![],
Expand All @@ -706,7 +706,7 @@ mod tests {

// Create an ExecutionOutcome object with a single receipt vector containing receipt1
let execution_outcome1 = ExecutionOutcome {
chain_id: None,
chain_id: ETHEREUM_CHAIN_ID,
bundle: Default::default(),
receipts: Receipts { receipt_vec: vec![vec![Some(receipt1)]] },
requests: vec![],
Expand Down
56 changes: 27 additions & 29 deletions crates/evm/execution-types/src/execution_outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl ChangedAccount {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ExecutionOutcome {
/// Chain id of this execution outcome.
pub chain_id: Option<u64>,
pub chain_id: u64,
/// Bundle state with reverts.
pub bundle: BundleState,
// FIX(Cecilia): Add (chain_id, Reciepts)

Check failure on line 42 in crates/evm/execution-types/src/execution_outcome.rs

View workflow job for this annotation

GitHub Actions / codespell

Reciepts ==> Receipts
Expand Down Expand Up @@ -73,13 +73,14 @@ impl ExecutionOutcome {
///
/// This constructor initializes a new `ExecutionOutcome` instance with the provided
/// bundle state, receipts, first block number, and EIP-7685 requests.
pub const fn new(
pub fn new(
chain_id: Option<u64>,
bundle: BundleState,
receipts: Receipts,
first_block: BlockNumber,
requests: Vec<Requests>,
) -> Self {
let chain_id = chain_id.unwrap_or(ETHEREUM_CHAIN_ID);
Self { chain_id, bundle, receipts, first_block, requests }
}

Expand All @@ -96,7 +97,7 @@ impl ExecutionOutcome {
first_block: BlockNumber,
requests: Vec<Requests>,
) -> Self {
let chain_id_inner = chain_id.unwrap_or(ETHEREUM_CHAIN_ID);
let chain_id = chain_id.unwrap_or(ETHEREUM_CHAIN_ID);
// sort reverts by block number
let mut reverts = revert_init.into_iter().collect::<Vec<_>>();
reverts.sort_unstable_by_key(|a| a.0);
Expand All @@ -105,7 +106,7 @@ impl ExecutionOutcome {
let bundle = BundleState::new(
state_init.into_iter().map(|(address, (original, present, storage))| {
(
ChainAddress(chain_id_inner, address),
ChainAddress(chain_id, address),
original.map(Into::into),
present.map(Into::into),
storage.into_iter().map(|(k, s)| (k.into(), s)).collect(),
Expand All @@ -115,15 +116,15 @@ impl ExecutionOutcome {
// does not needs to be sorted, it is done when taking reverts.
reverts.into_iter().map(|(address, (original, storage))| {
(
ChainAddress(chain_id_inner, address),
ChainAddress(chain_id, address),
original.map(|i| i.map(Into::into)),
storage.into_iter().map(|entry| (entry.key.into(), entry.value)),
)
})
}),
contracts_init
.into_iter()
.map(|(code_hash, bytecode)| ((chain_id_inner, code_hash), bytecode.0)),
.map(|(code_hash, bytecode)| ((chain_id, code_hash), bytecode.0)),
);

Self { chain_id, bundle, receipts, first_block, requests }
Expand All @@ -132,7 +133,7 @@ impl ExecutionOutcome {
/// Reture the `ExecutionOutcome` for a speicific chain.

Check failure on line 133 in crates/evm/execution-types/src/execution_outcome.rs

View workflow job for this annotation

GitHub Actions / codespell

Reture ==> Return

Check failure on line 133 in crates/evm/execution-types/src/execution_outcome.rs

View workflow job for this annotation

GitHub Actions / codespell

speicific ==> specific
pub fn filter_chain(&self, chain_id: u64) -> Self {
Self {
chain_id: Some(chain_id),
chain_id: chain_id,
bundle: self.bundle.filter_for_chain(chain_id),
// FIX(Cecilia): with (chain_id, Reciepts) & (chain_id, Requests)

Check failure on line 138 in crates/evm/execution-types/src/execution_outcome.rs

View workflow job for this annotation

GitHub Actions / codespell

Reciepts ==> Receipts
// we can filter out the right ones
Expand Down Expand Up @@ -173,7 +174,7 @@ impl ExecutionOutcome {

/// Reture states for a speicific chain.

Check failure on line 175 in crates/evm/execution-types/src/execution_outcome.rs

View workflow job for this annotation

GitHub Actions / codespell

Reture ==> Return

Check failure on line 175 in crates/evm/execution-types/src/execution_outcome.rs

View workflow job for this annotation

GitHub Actions / codespell

speicific ==> specific
pub fn current_state(&self) -> BundleState {
self.bundle.filter_for_chain(self.chain_id.unwrap_or(ETHEREUM_CHAIN_ID))
self.bundle.filter_for_chain(self.chain_id)
}

/// Set first block.
Expand All @@ -194,26 +195,23 @@ impl ExecutionOutcome {
/// Get account if account is known.
/// Only support the account of current chain, or default to Ethereum.
pub fn account(&self, address: &Address) -> Option<Option<Account>> {
let chain_id = self.chain_id.unwrap_or(ETHEREUM_CHAIN_ID);
self.bundle
.account(&ChainAddress(chain_id, *address))
.account(&ChainAddress(self.chain_id, *address))
.map(|a| a.info.clone().map(Into::into))
}

/// Get storage if value is known.
///
/// This means that depending on status we can potentially return `U256::ZERO`.
pub fn storage(&self, address: &Address, storage_key: U256) -> Option<U256> {
let chain_id = self.chain_id.unwrap_or(ETHEREUM_CHAIN_ID);
self.bundle
.account(&ChainAddress(chain_id, *address))
.account(&ChainAddress(self.chain_id, *address))
.and_then(|a| a.storage_slot(storage_key))
}

/// Return bytecode if known.
pub fn bytecode(&self, code_hash: &B256) -> Option<Bytecode> {
let chain_id = self.chain_id.unwrap_or(ETHEREUM_CHAIN_ID);
self.bundle.bytecode(chain_id, code_hash).map(Bytecode)
self.bundle.bytecode(self.chain_id, code_hash).map(Bytecode)
}

/// Returns [`HashedPostState`] for this execution outcome.
Expand Down Expand Up @@ -414,7 +412,7 @@ impl ExecutionOutcome {
impl From<(BlockExecutionOutput<Receipt>, u64, BlockNumber)> for ExecutionOutcome {
fn from(value: (BlockExecutionOutput<Receipt>, u64, BlockNumber)) -> Self {
Self {
chain_id: Some(value.1),
chain_id: value.1,
bundle: value.0.state,
receipts: Receipts::from(value.0.receipts),
first_block: value.2,
Expand All @@ -426,7 +424,7 @@ impl From<(BlockExecutionOutput<Receipt>, u64, BlockNumber)> for ExecutionOutcom
impl From<(BlockExecutionOutput<Receipt>, BlockNumber)> for ExecutionOutcome {
fn from(value: (BlockExecutionOutput<Receipt>, BlockNumber)) -> Self {
Self {
chain_id: None,
chain_id: ETHEREUM_CHAIN_ID,
bundle: value.0.state,
receipts: Receipts::from(value.0.receipts),
first_block: value.1,
Expand Down Expand Up @@ -503,7 +501,7 @@ mod tests {
// Create a ExecutionOutcome object with the created bundle, receipts, requests, and
// first_block
let exec_res = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: bundle.clone(),
receipts: receipts.clone(),
requests: requests.clone(),
Expand Down Expand Up @@ -573,7 +571,7 @@ mod tests {
// Create a ExecutionOutcome object with the created bundle, receipts, requests, and
// first_block
let exec_res = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(),
receipts,
requests: vec![],
Expand Down Expand Up @@ -612,7 +610,7 @@ mod tests {
// Create a ExecutionOutcome object with the created bundle, receipts, requests, and
// first_block
let exec_res = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(),
receipts,
requests: vec![],
Expand Down Expand Up @@ -648,7 +646,7 @@ mod tests {
// Create a ExecutionOutcome object with the created bundle, receipts, requests, and
// first_block
let exec_res = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(), // Default value for bundle
receipts, // Include the created receipts
requests: vec![], // Empty vector for requests
Expand Down Expand Up @@ -699,7 +697,7 @@ mod tests {
// Create a ExecutionOutcome object with the created bundle, receipts, requests, and
// first_block
let exec_res = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(), // Default value for bundle
receipts, // Include the created receipts
requests: vec![], // Empty vector for requests
Expand All @@ -714,7 +712,7 @@ mod tests {

// Create a ExecutionOutcome object with an empty Receipts object
let exec_res_empty_receipts = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(), // Default value for bundle
receipts: receipts_empty, // Include the empty receipts
requests: vec![], // Empty vector for requests
Expand Down Expand Up @@ -765,7 +763,7 @@ mod tests {
// Create a ExecutionOutcome object with the created bundle, receipts, requests, and
// first_block
let mut exec_res = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(),
receipts,
requests,
Expand Down Expand Up @@ -824,7 +822,7 @@ mod tests {

// Create an ExecutionOutcome object.
let mut exec_res = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(),
receipts,
requests,
Expand All @@ -838,7 +836,7 @@ mod tests {
assert_eq!(
exec_res,
ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(),
receipts: Receipts {
receipt_vec: vec![vec![Some(receipt.clone())], vec![Some(receipt)]]
Expand Down Expand Up @@ -891,7 +889,7 @@ mod tests {
// Create a ExecutionOutcome object with the created bundle, receipts, requests, and
// first_block
let exec_res = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(),
receipts,
requests,
Expand All @@ -903,7 +901,7 @@ mod tests {

// Define the expected lower ExecutionOutcome after splitting
let lower_execution_outcome = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(),
receipts: Receipts { receipt_vec: vec![vec![Some(receipt.clone())]] },
requests: vec![Requests(vec![request])],
Expand All @@ -912,7 +910,7 @@ mod tests {

// Define the expected higher ExecutionOutcome after splitting
let higher_execution_outcome = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: Default::default(),
receipts: Receipts {
receipt_vec: vec![vec![Some(receipt.clone())], vec![Some(receipt)]],
Expand Down Expand Up @@ -975,7 +973,7 @@ mod tests {
);

let execution_outcome = ExecutionOutcome {
chain_id: Some(CHAIN_ID),
chain_id: CHAIN_ID,
bundle: bundle_state,
receipts: Receipts::default(),
first_block: 0,
Expand Down
5 changes: 1 addition & 4 deletions crates/stages/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,7 @@ where

// prepare execution output for writing
let time = Instant::now();
let ExecutionOutcome { chain_id, bundle, receipts, requests, first_block } =
executor.finalize();
let state = ExecutionOutcome::new(chain_id, bundle, receipts, first_block, requests)
.filter_current_chain();
let state = executor.finalize().filter_current_chain();
// TODO(Cecilia): If building for other chains, get the ExecutionOutcome of other chains by
// doing this: let other_states = state.filter_chain(other_chain_id);
let write_preparation_duration = time.elapsed();
Expand Down
12 changes: 2 additions & 10 deletions crates/storage/provider/src/providers/bundle_state_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ impl<SP: StateProvider, EDP: ExecutionDataProvider> BundleStateProvider<SP, EDP>
}

pub fn filter_bundle_state(&self) -> HashMap<ChainAddress, BundleAccount> {
let chain_id = self
.block_execution_data_provider
.execution_outcome()
.chain_id
.unwrap_or(ETHEREUM_CHAIN_ID);
let chain_id = self.block_execution_data_provider.execution_outcome().chain_id;
self.block_execution_data_provider
.execution_outcome()
.current_state()
Expand Down Expand Up @@ -134,11 +130,7 @@ impl<SP: StateProvider, EDP: ExecutionDataProvider> StorageRootProvider
address: Address,
hashed_storage: HashedStorage,
) -> ProviderResult<B256> {
let chain_id = self
.block_execution_data_provider
.execution_outcome()
.chain_id
.unwrap_or(ETHEREUM_CHAIN_ID);
let chain_id = self.block_execution_data_provider.execution_outcome().chain_id;
let bundle_state = self.block_execution_data_provider.execution_outcome().current_state();
let mut storage = bundle_state
.account(&ChainAddress(chain_id, address))
Expand Down

0 comments on commit 27cfd6b

Please sign in to comment.