|
| 1 | +use alloy_primitives::{address, Address, U256}; |
| 2 | +use revm::{database::State, Database}; |
| 3 | + |
| 4 | +const L2_MESSAGE_QUEUE_ADDRESS: Address = address!("0x5300000000000000000000000000000000000000"); |
| 5 | +const WITHDRAW_TRIE_ROOT_SLOT: U256 = U256::ZERO; |
| 6 | + |
| 7 | +/// Instance that implements the trait can load the `L2MessageQueue` withdraw root in state. |
| 8 | +pub trait LoadWithdrawRoot<DB: Database> { |
| 9 | + /// Load the withdrawal root. |
| 10 | + fn load_withdraw_root(&mut self) -> Result<(), DB::Error>; |
| 11 | +} |
| 12 | + |
| 13 | +impl<DB: Database> LoadWithdrawRoot<DB> for State<DB> { |
| 14 | + fn load_withdraw_root(&mut self) -> Result<(), DB::Error> { |
| 15 | + // we load the account in cache and query the storage slot. The storage slot will only be |
| 16 | + // loaded from database if it is not already know. |
| 17 | + self.load_cache_account(L2_MESSAGE_QUEUE_ADDRESS)?; |
| 18 | + let _ = revm::Database::storage(self, L2_MESSAGE_QUEUE_ADDRESS, WITHDRAW_TRIE_ROOT_SLOT); |
| 19 | + |
| 20 | + Ok(()) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#[cfg(test)] |
| 25 | +mod tests { |
| 26 | + use super::*; |
| 27 | + use std::{collections::HashMap, convert::Infallible}; |
| 28 | + |
| 29 | + use alloy_primitives::B256; |
| 30 | + use revm::{bytecode::Bytecode, state::AccountInfo}; |
| 31 | + use revm_primitives::{StorageKey, StorageValue}; |
| 32 | + |
| 33 | + #[derive(Default)] |
| 34 | + struct InMemoryDb { |
| 35 | + pub accounts: HashMap<Address, AccountInfo>, |
| 36 | + pub storage: HashMap<(Address, U256), U256>, |
| 37 | + } |
| 38 | + |
| 39 | + impl Database for InMemoryDb { |
| 40 | + type Error = Infallible; |
| 41 | + |
| 42 | + fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> { |
| 43 | + Ok(self.accounts.get(&address).cloned()) |
| 44 | + } |
| 45 | + |
| 46 | + fn code_by_hash(&mut self, _code_hash: B256) -> Result<Bytecode, Self::Error> { |
| 47 | + Ok(Default::default()) |
| 48 | + } |
| 49 | + |
| 50 | + fn storage( |
| 51 | + &mut self, |
| 52 | + address: Address, |
| 53 | + index: StorageKey, |
| 54 | + ) -> Result<StorageValue, Self::Error> { |
| 55 | + Ok(self.storage.get(&(address, index)).copied().unwrap_or_default()) |
| 56 | + } |
| 57 | + |
| 58 | + fn block_hash(&mut self, _number: u64) -> Result<B256, Self::Error> { |
| 59 | + Ok(Default::default()) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn test_should_load_withdraw_root() -> eyre::Result<()> { |
| 65 | + // init db |
| 66 | + let mut db = InMemoryDb::default(); |
| 67 | + |
| 68 | + // load L2 message queue contract |
| 69 | + let withdraw_root = U256::random(); |
| 70 | + db.accounts.insert(L2_MESSAGE_QUEUE_ADDRESS, Default::default()); |
| 71 | + db.storage.insert((L2_MESSAGE_QUEUE_ADDRESS, U256::ZERO), withdraw_root); |
| 72 | + |
| 73 | + let mut state = |
| 74 | + State::builder().with_database(db).with_bundle_update().without_state_clear().build(); |
| 75 | + |
| 76 | + assert!(state |
| 77 | + .cache |
| 78 | + .accounts |
| 79 | + .get(&L2_MESSAGE_QUEUE_ADDRESS) |
| 80 | + .map(|acc| acc.storage_slot(WITHDRAW_TRIE_ROOT_SLOT)) |
| 81 | + .is_none()); |
| 82 | + |
| 83 | + // load root |
| 84 | + state.load_withdraw_root()?; |
| 85 | + |
| 86 | + assert_eq!( |
| 87 | + state |
| 88 | + .cache |
| 89 | + .accounts |
| 90 | + .get(&L2_MESSAGE_QUEUE_ADDRESS) |
| 91 | + .unwrap() |
| 92 | + .storage_slot(WITHDRAW_TRIE_ROOT_SLOT) |
| 93 | + .unwrap(), |
| 94 | + withdraw_root |
| 95 | + ); |
| 96 | + |
| 97 | + Ok(()) |
| 98 | + } |
| 99 | +} |
0 commit comments