Skip to content

Commit 6902b05

Browse files
committed
naive storage changes tracking'
1 parent 62643ba commit 6902b05

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

crates/fbal/tests/builder.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
use std::{collections::HashMap, sync::Arc};
44

55
use alloy_consensus::Header;
6-
use alloy_eip7928::{AccountChanges, BalanceChange, CodeChange, NonceChange};
6+
use alloy_eip7928::{
7+
AccountChanges, BalanceChange, CodeChange, EMPTY_BLOCK_ACCESS_LIST_HASH, NonceChange,
8+
SlotChanges, StorageChange,
9+
};
710
use alloy_primitives::{Address, B256, TxKind, U256};
811
use base_fbal::{FlashblockAccessList, TouchedAccountsInspector};
912
use op_revm::{DefaultOp, OpBuilder, OpContext, OpSpecId, OpTransaction};
1013
use reth_evm::{ConfigureEvm, Evm};
1114
use reth_optimism_chainspec::{BASE_MAINNET, OpChainSpec};
1215
use reth_optimism_evm::{OpEvmConfig, OpNextBlockEnvAttributes};
1316
use revm::{
14-
Context, DatabaseCommit, ExecuteCommitEvm, ExecuteEvm, InspectEvm, MainBuilder, MainContext,
17+
Context, DatabaseCommit, DatabaseRef, ExecuteCommitEvm, ExecuteEvm, InspectEvm, MainBuilder,
18+
MainContext,
1519
context::{CfgEnv, ContextTr, TxEnv, result::ResultAndState},
1620
database::InMemoryDB,
1721
inspector::JournalExt,
1822
interpreter::instructions::utility::IntoAddress,
23+
primitives::KECCAK_EMPTY,
1924
state::AccountInfo,
2025
};
2126

@@ -41,7 +46,7 @@ fn execute_txns_build_access_list(
4146
min_tx_index: 0,
4247
max_tx_index: (txs.len() - 1) as u64,
4348
account_changes: vec![],
44-
fal_hash: B256::ZERO,
49+
fal_hash: EMPTY_BLOCK_ACCESS_LIST_HASH,
4550
};
4651

4752
for (idx, tx) in txs.into_iter().enumerate() {
@@ -71,8 +76,7 @@ fn execute_txns_build_access_list(
7176

7277
let initial_balance = initial_account.map(|a| a.balance).unwrap_or_default();
7378
let initial_nonce = initial_account.map(|a| a.nonce).unwrap_or_default();
74-
let initially_no_code =
75-
initial_account.map(|a| a.is_empty_code_hash()).unwrap_or_default();
79+
let initial_code_hash = initial_account.map(|a| a.code_hash()).unwrap_or(KECCAK_EMPTY);
7680

7781
if initial_balance != account.info.balance {
7882
entry.balance_changes.push(BalanceChange::new(idx as u64, account.info.balance));
@@ -82,18 +86,44 @@ fn execute_txns_build_access_list(
8286
entry.nonce_changes.push(NonceChange::new(idx as u64, account.info.nonce));
8387
}
8488

85-
if initially_no_code && !account.info.is_empty_code_hash() {
86-
entry
87-
.code_changes
88-
.push(CodeChange::new(idx as u64, account.info.code.as_ref().unwrap().bytes()));
89+
if initial_code_hash != account.info.code_hash() {
90+
let bytecode = match account.info.code.clone() {
91+
Some(code) => code,
92+
None => evm.db_mut().code_by_hash_ref(account.info.code_hash()).unwrap(),
93+
};
94+
entry.code_changes.push(CodeChange::new(idx as u64, bytecode.bytes()));
8995
}
96+
97+
// TODO: This currently does not check if a storage key already exists within `storage_changes`
98+
// for a given account, and instead adds a new `SlotChanges` struct for the same storage key
99+
account.storage.iter().for_each(|(key, value)| {
100+
let previous_value = evm.db_mut().storage_ref(*address, *key);
101+
match previous_value {
102+
Ok(prev) => {
103+
if prev != value.present_value {
104+
entry.storage_changes.push(SlotChanges::new(
105+
B256::from(*key),
106+
vec![StorageChange::new(
107+
idx as u64,
108+
B256::from(value.present_value()),
109+
)],
110+
));
111+
}
112+
}
113+
Err(_) => {
114+
entry.storage_changes.push(SlotChanges::new(
115+
B256::from(*key),
116+
vec![StorageChange::new(idx as u64, B256::from(value.present_value()))],
117+
));
118+
}
119+
}
120+
});
90121
}
91122

92123
evm.db_mut().commit(state);
93124
db = evm.into_db();
94125

95126
access_list.merge_account_changes(account_changes.values().cloned().collect());
96-
// access_list.account_changes.extend(account_changes.values().cloned());
97127
}
98128

99129
access_list.finalize();

0 commit comments

Comments
 (0)