From 1c93397d53e0d067cae21e3430b3b543502747f4 Mon Sep 17 00:00:00 2001 From: Federico Gimenez Date: Sat, 7 Dec 2024 12:42:50 +0100 Subject: [PATCH 1/2] chore(engine): refactor code to transform EvmState into HashedPostState --- crates/engine/tree/src/tree/root.rs | 99 +++++++++++++---------------- 1 file changed, 45 insertions(+), 54 deletions(-) diff --git a/crates/engine/tree/src/tree/root.rs b/crates/engine/tree/src/tree/root.rs index 2d00feba50d4..f072419fec71 100644 --- a/crates/engine/tree/src/tree/root.rs +++ b/crates/engine/tree/src/tree/root.rs @@ -181,6 +181,48 @@ impl Drop for StateHookSender { } } +/// Extension trait for `HashedPostState` to provide conversion functionality +trait HashedPostStateExt { + /// Convert an `EvmState` into a `HashedPostState` + fn from_evm_state(update: EvmState) -> HashedPostState; +} + +impl HashedPostStateExt for HashedPostState { + fn from_evm_state(update: EvmState) -> Self { + let mut hashed_state = Self::default(); + + for (address, account) in update { + if account.is_touched() { + let hashed_address = keccak256(address); + trace!(target: "engine::root", ?address, ?hashed_address, "Adding account to state update"); + + let destroyed = account.is_selfdestructed(); + let info = if destroyed { None } else { Some(account.info.into()) }; + hashed_state.accounts.insert(hashed_address, info); + + let mut changed_storage_iter = account + .storage + .into_iter() + .filter_map(|(slot, value)| { + value + .is_changed() + .then(|| (keccak256(B256::from(slot)), value.present_value)) + }) + .peekable(); + + if destroyed || changed_storage_iter.peek().is_some() { + hashed_state.storages.insert( + hashed_address, + HashedStorage::from_iter(destroyed, changed_storage_iter), + ); + } + } + } + + hashed_state + } +} + /// Standalone task that receives a transaction state stream and updates relevant /// data structures to calculate state root. /// @@ -258,33 +300,7 @@ where proof_sequence_number: u64, state_root_message_sender: Sender, ) { - let mut hashed_state_update = HashedPostState::default(); - for (address, account) in update { - if account.is_touched() { - let hashed_address = keccak256(address); - trace!(target: "engine::root", ?address, ?hashed_address, "Adding account to state update"); - - let destroyed = account.is_selfdestructed(); - let info = if destroyed { None } else { Some(account.info.into()) }; - hashed_state_update.accounts.insert(hashed_address, info); - - let mut changed_storage_iter = account - .storage - .into_iter() - .filter_map(|(slot, value)| { - value - .is_changed() - .then(|| (keccak256(B256::from(slot)), value.present_value)) - }) - .peekable(); - if destroyed || changed_storage_iter.peek().is_some() { - hashed_state_update.storages.insert( - hashed_address, - HashedStorage::from_iter(destroyed, changed_storage_iter), - ); - } - } - } + let hashed_state_update = HashedPostState::from_evm_state(update); let proof_targets = get_proof_targets(&hashed_state_update, fetched_proof_targets); for (address, slots) in &proof_targets { @@ -696,34 +712,9 @@ mod tests { } for update in &state_updates { - for (address, account) in update { - let hashed_address = keccak256(*address); - - if account.is_touched() { - let destroyed = account.is_selfdestructed(); - hashed_state.accounts.insert( - hashed_address, - if destroyed || account.is_empty() { - None - } else { - Some(account.info.clone().into()) - }, - ); - - if destroyed || !account.storage.is_empty() { - let storage = account - .storage - .iter() - .filter(|&(_slot, value)| (!destroyed && value.is_changed())) - .map(|(slot, value)| { - (keccak256(B256::from(*slot)), value.present_value) - }); - hashed_state - .storages - .insert(hashed_address, HashedStorage::from_iter(destroyed, storage)); - } - } + hashed_state.extend(HashedPostState::from_evm_state(update.clone())); + for (address, account) in update { let storage: HashMap = account .storage .iter() From d80bd62d332dd0abd79701527fca0aa160154ab2 Mon Sep 17 00:00:00 2001 From: Federico Gimenez Date: Sat, 7 Dec 2024 14:00:49 +0100 Subject: [PATCH 2/2] local fn instead of trait --- crates/engine/tree/src/tree/root.rs | 68 ++++++++++++----------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/crates/engine/tree/src/tree/root.rs b/crates/engine/tree/src/tree/root.rs index f072419fec71..dc0563ade502 100644 --- a/crates/engine/tree/src/tree/root.rs +++ b/crates/engine/tree/src/tree/root.rs @@ -181,46 +181,36 @@ impl Drop for StateHookSender { } } -/// Extension trait for `HashedPostState` to provide conversion functionality -trait HashedPostStateExt { - /// Convert an `EvmState` into a `HashedPostState` - fn from_evm_state(update: EvmState) -> HashedPostState; -} - -impl HashedPostStateExt for HashedPostState { - fn from_evm_state(update: EvmState) -> Self { - let mut hashed_state = Self::default(); - - for (address, account) in update { - if account.is_touched() { - let hashed_address = keccak256(address); - trace!(target: "engine::root", ?address, ?hashed_address, "Adding account to state update"); - - let destroyed = account.is_selfdestructed(); - let info = if destroyed { None } else { Some(account.info.into()) }; - hashed_state.accounts.insert(hashed_address, info); - - let mut changed_storage_iter = account - .storage - .into_iter() - .filter_map(|(slot, value)| { - value - .is_changed() - .then(|| (keccak256(B256::from(slot)), value.present_value)) - }) - .peekable(); - - if destroyed || changed_storage_iter.peek().is_some() { - hashed_state.storages.insert( - hashed_address, - HashedStorage::from_iter(destroyed, changed_storage_iter), - ); - } +fn evm_state_to_hashed_post_state(update: EvmState) -> HashedPostState { + let mut hashed_state = HashedPostState::default(); + + for (address, account) in update { + if account.is_touched() { + let hashed_address = keccak256(address); + trace!(target: "engine::root", ?address, ?hashed_address, "Adding account to state update"); + + let destroyed = account.is_selfdestructed(); + let info = if destroyed { None } else { Some(account.info.into()) }; + hashed_state.accounts.insert(hashed_address, info); + + let mut changed_storage_iter = account + .storage + .into_iter() + .filter_map(|(slot, value)| { + value.is_changed().then(|| (keccak256(B256::from(slot)), value.present_value)) + }) + .peekable(); + + if destroyed || changed_storage_iter.peek().is_some() { + hashed_state.storages.insert( + hashed_address, + HashedStorage::from_iter(destroyed, changed_storage_iter), + ); } } - - hashed_state } + + hashed_state } /// Standalone task that receives a transaction state stream and updates relevant @@ -300,7 +290,7 @@ where proof_sequence_number: u64, state_root_message_sender: Sender, ) { - let hashed_state_update = HashedPostState::from_evm_state(update); + let hashed_state_update = evm_state_to_hashed_post_state(update); let proof_targets = get_proof_targets(&hashed_state_update, fetched_proof_targets); for (address, slots) in &proof_targets { @@ -712,7 +702,7 @@ mod tests { } for update in &state_updates { - hashed_state.extend(HashedPostState::from_evm_state(update.clone())); + hashed_state.extend(evm_state_to_hashed_post_state(update.clone())); for (address, account) in update { let storage: HashMap = account