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

chore(engine): refactor code to transform EvmState into HashedPostState #13207

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Changes from all 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
89 changes: 35 additions & 54 deletions crates/engine/tree/src/tree/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,38 @@ impl Drop for StateHookSender {
}
}

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
}

/// Standalone task that receives a transaction state stream and updates relevant
/// data structures to calculate state root.
///
Expand Down Expand Up @@ -258,33 +290,7 @@ where
proof_sequence_number: u64,
state_root_message_sender: Sender<StateRootMessage>,
) {
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 = 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 {
Expand Down Expand Up @@ -696,34 +702,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(evm_state_to_hashed_post_state(update.clone()));

for (address, account) in update {
let storage: HashMap<B256, U256> = account
.storage
.iter()
Expand Down
Loading