|
| 1 | +use alloy::primitives::{Address, U256}; |
| 2 | +use revm::{ |
| 3 | + bytecode::Bytecode, |
| 4 | + database::{Database, DatabaseCommit, TryDatabaseCommit}, |
| 5 | + primitives::HashMap, |
| 6 | + state::{Account, AccountInfo, AccountStatus, EvmStorageSlot}, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Test utilities for databases. |
| 10 | +#[doc(hidden)] |
| 11 | +pub trait DbTestExt: Database + DatabaseCommit { |
| 12 | + /// Modify an account via closure, creating it if it doesn't exist. |
| 13 | + fn test_modify_account<F>(&mut self, address: Address, f: F) |
| 14 | + where |
| 15 | + F: FnOnce(&mut AccountInfo), |
| 16 | + { |
| 17 | + let mut info = self.basic(address).ok().flatten().unwrap_or_default(); |
| 18 | + f(&mut info); |
| 19 | + let account = Account { |
| 20 | + info, |
| 21 | + storage: Default::default(), |
| 22 | + status: AccountStatus::Touched, |
| 23 | + transaction_id: 0, |
| 24 | + }; |
| 25 | + self.commit(HashMap::from_iter([(address, account)])); |
| 26 | + } |
| 27 | + |
| 28 | + /// Set account balance. |
| 29 | + fn test_set_balance(&mut self, address: Address, balance: U256) { |
| 30 | + self.test_modify_account(address, |info| info.balance = balance); |
| 31 | + } |
| 32 | + |
| 33 | + /// Increase account balance (saturating). |
| 34 | + fn test_increase_balance(&mut self, address: Address, amount: U256) { |
| 35 | + self.test_modify_account(address, |info| { |
| 36 | + info.balance = info.balance.saturating_add(amount) |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + /// Decrease account balance (saturating). |
| 41 | + fn test_decrease_balance(&mut self, address: Address, amount: U256) { |
| 42 | + self.test_modify_account(address, |info| { |
| 43 | + info.balance = info.balance.saturating_sub(amount) |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + /// Set account nonce. |
| 48 | + fn test_set_nonce(&mut self, address: Address, nonce: u64) { |
| 49 | + self.test_modify_account(address, |info| info.nonce = nonce); |
| 50 | + } |
| 51 | + |
| 52 | + /// Set a storage slot. |
| 53 | + fn test_set_storage(&mut self, address: Address, slot: U256, value: U256) { |
| 54 | + let info = self.basic(address).ok().flatten().unwrap_or_default(); |
| 55 | + let storage = |
| 56 | + HashMap::from_iter([(slot, EvmStorageSlot::new_changed(U256::ZERO, value, 0))]); |
| 57 | + let account = Account { info, storage, status: AccountStatus::Touched, transaction_id: 0 }; |
| 58 | + self.commit(HashMap::from_iter([(address, account)])); |
| 59 | + } |
| 60 | + |
| 61 | + /// Set account bytecode. |
| 62 | + fn test_set_bytecode(&mut self, address: Address, bytecode: Bytecode) { |
| 63 | + self.test_modify_account(address, |info| { |
| 64 | + info.code_hash = bytecode.hash_slow(); |
| 65 | + info.code = Some(bytecode); |
| 66 | + }); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<Db: Database + DatabaseCommit> DbTestExt for Db {} |
| 71 | + |
| 72 | +/// Test utilities for databases that support fallible commits. |
| 73 | +#[doc(hidden)] |
| 74 | +pub trait TryDbTestExt: Database + TryDatabaseCommit { |
| 75 | + /// Modify an account via closure, creating it if it doesn't exist. |
| 76 | + fn test_try_modify_account<F>( |
| 77 | + &mut self, |
| 78 | + address: Address, |
| 79 | + f: F, |
| 80 | + ) -> Result<(), <Self as TryDatabaseCommit>::Error> |
| 81 | + where |
| 82 | + F: FnOnce(&mut AccountInfo), |
| 83 | + { |
| 84 | + let mut info = self.basic(address).ok().flatten().unwrap_or_default(); |
| 85 | + f(&mut info); |
| 86 | + let account = Account { |
| 87 | + info, |
| 88 | + storage: Default::default(), |
| 89 | + status: AccountStatus::Touched, |
| 90 | + transaction_id: 0, |
| 91 | + }; |
| 92 | + self.try_commit(HashMap::from_iter([(address, account)])) |
| 93 | + } |
| 94 | + |
| 95 | + /// Set account balance. |
| 96 | + fn test_try_set_balance( |
| 97 | + &mut self, |
| 98 | + address: Address, |
| 99 | + balance: U256, |
| 100 | + ) -> Result<(), <Self as TryDatabaseCommit>::Error> { |
| 101 | + self.test_try_modify_account(address, |info| info.balance = balance) |
| 102 | + } |
| 103 | + |
| 104 | + /// Increase account balance (saturating). |
| 105 | + fn test_try_increase_balance( |
| 106 | + &mut self, |
| 107 | + address: Address, |
| 108 | + amount: U256, |
| 109 | + ) -> Result<(), <Self as TryDatabaseCommit>::Error> { |
| 110 | + self.test_try_modify_account(address, |info| { |
| 111 | + info.balance = info.balance.saturating_add(amount) |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + /// Decrease account balance (saturating). |
| 116 | + fn test_try_decrease_balance( |
| 117 | + &mut self, |
| 118 | + address: Address, |
| 119 | + amount: U256, |
| 120 | + ) -> Result<(), <Self as TryDatabaseCommit>::Error> { |
| 121 | + self.test_try_modify_account(address, |info| { |
| 122 | + info.balance = info.balance.saturating_sub(amount) |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + /// Set account nonce. |
| 127 | + fn test_try_set_nonce( |
| 128 | + &mut self, |
| 129 | + address: Address, |
| 130 | + nonce: u64, |
| 131 | + ) -> Result<(), <Self as TryDatabaseCommit>::Error> { |
| 132 | + self.test_try_modify_account(address, |info| info.nonce = nonce) |
| 133 | + } |
| 134 | + |
| 135 | + /// Set a storage slot. |
| 136 | + fn test_try_set_storage( |
| 137 | + &mut self, |
| 138 | + address: Address, |
| 139 | + slot: U256, |
| 140 | + value: U256, |
| 141 | + ) -> Result<(), <Self as TryDatabaseCommit>::Error> { |
| 142 | + let info = self.basic(address).ok().flatten().unwrap_or_default(); |
| 143 | + let storage = |
| 144 | + HashMap::from_iter([(slot, EvmStorageSlot::new_changed(U256::ZERO, value, 0))]); |
| 145 | + let account = Account { info, storage, status: AccountStatus::Touched, transaction_id: 0 }; |
| 146 | + self.try_commit(HashMap::from_iter([(address, account)])) |
| 147 | + } |
| 148 | + |
| 149 | + /// Set account bytecode. |
| 150 | + fn test_try_set_bytecode( |
| 151 | + &mut self, |
| 152 | + address: Address, |
| 153 | + bytecode: Bytecode, |
| 154 | + ) -> Result<(), <Self as TryDatabaseCommit>::Error> { |
| 155 | + self.test_try_modify_account(address, |info| { |
| 156 | + info.code_hash = bytecode.hash_slow(); |
| 157 | + info.code = Some(bytecode); |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl<Db: Database + TryDatabaseCommit> TryDbTestExt for Db {} |
0 commit comments