diff --git a/crates/revm/src/db/states/bundle_state.rs b/crates/revm/src/db/states/bundle_state.rs index 22797c8761..ace2b431f9 100644 --- a/crates/revm/src/db/states/bundle_state.rs +++ b/crates/revm/src/db/states/bundle_state.rs @@ -18,8 +18,8 @@ use std::{ #[derive(Debug)] pub struct BundleBuilder { states: HashSet
, - state_original: HashMap, - state_present: HashMap, + state_original: HashMap>, + state_present: HashMap>, state_storage: HashMap>, reverts: BTreeSet<(u64, Address)>, @@ -103,13 +103,21 @@ impl BundleBuilder { } /// Collect account info of BundleState state - pub fn state_original_account_info(mut self, address: Address, original: AccountInfo) -> Self { + pub fn state_original_account_info( + mut self, + address: Address, + original: Option, + ) -> Self { self.set_state_original_account_info(address, original); self } /// Collect account info of BundleState state - pub fn state_present_account_info(mut self, address: Address, present: AccountInfo) -> Self { + pub fn state_present_account_info( + mut self, + address: Address, + present: Option, + ) -> Self { self.set_state_present_account_info(address, present); self } @@ -173,7 +181,7 @@ impl BundleBuilder { pub fn set_state_original_account_info( &mut self, address: Address, - original: AccountInfo, + original: Option, ) -> &mut Self { self.states.insert(address); self.state_original.insert(address, original); @@ -184,7 +192,7 @@ impl BundleBuilder { pub fn set_state_present_account_info( &mut self, address: Address, - present: AccountInfo, + present: Option, ) -> &mut Self { self.states.insert(address); self.state_present.insert(address, present); @@ -255,8 +263,8 @@ impl BundleBuilder { }) .unwrap_or_default(); let bundle_account = BundleAccount::new( - self.state_original.remove(&address), - self.state_present.remove(&address), + self.state_original.remove(&address).unwrap_or_default(), + self.state_present.remove(&address).unwrap_or_default(), storage, AccountStatus::Changed, ); @@ -327,12 +335,12 @@ impl BundleBuilder { } /// Mutable getter for `state_original` field - pub fn get_state_original_mut(&mut self) -> &mut HashMap { + pub fn get_state_original_mut(&mut self) -> &mut HashMap> { &mut self.state_original } /// Mutable getter for `state_present` field - pub fn get_state_present_mut(&mut self) -> &mut HashMap { + pub fn get_state_present_mut(&mut self) -> &mut HashMap> { &mut self.state_present } @@ -958,12 +966,12 @@ mod tests { BundleState::builder(0..=0) .state_present_account_info( account1(), - AccountInfo { + Some(AccountInfo { nonce: 1, balance: U256::from(10), code_hash: KECCAK_EMPTY, code: None, - }, + }), ) .state_storage( account1(), @@ -972,12 +980,12 @@ mod tests { .state_address(account2()) .state_present_account_info( account2(), - AccountInfo { + Some(AccountInfo { nonce: 1, balance: U256::from(10), code_hash: KECCAK_EMPTY, code: None, - }, + }), ) .revert_address(0, account1()) .revert_account_info(0, account1(), Some(None)) @@ -991,12 +999,12 @@ mod tests { BundleState::builder(0..=0) .state_present_account_info( account1(), - AccountInfo { + Some(AccountInfo { nonce: 3, balance: U256::from(20), code_hash: KECCAK_EMPTY, code: None, - }, + }), ) .state_storage( account1(), @@ -1213,12 +1221,12 @@ mod tests { }; let present_state = BundleState::builder(2..=2) - .state_present_account_info(address1, account1_changed.clone()) + .state_present_account_info(address1, Some(account1_changed.clone())) .build(); assert_eq!(present_state.reverts.len(), 1); let previous_state = BundleState::builder(1..=1) - .state_present_account_info(address1, account1) - .state_present_account_info(address2, account2.clone()) + .state_present_account_info(address1, Some(account1)) + .state_present_account_info(address2, Some(account2.clone())) .build(); assert_eq!(previous_state.reverts.len(), 1); @@ -1251,14 +1259,14 @@ mod tests { assert!(builder.get_state_original_mut().is_empty()); builder .get_state_original_mut() - .insert(account1(), AccountInfo::default()); + .insert(account1(), Some(AccountInfo::default())); assert!(builder.get_state_original_mut().contains_key(&account1())); // Test get_state_present_mut assert!(builder.get_state_present_mut().is_empty()); builder .get_state_present_mut() - .insert(account1(), AccountInfo::default()); + .insert(account1(), Some(AccountInfo::default())); assert!(builder.get_state_present_mut().contains_key(&account1())); // Test get_state_storage_mut @@ -1301,4 +1309,27 @@ mod tests { .insert(B256::default(), Bytecode::default()); assert!(builder.get_contracts_mut().contains_key(&B256::default())); } + + #[test] + fn destroyed_account() { + let address1 = account1(); + let address2 = account2(); + + // Test to insert None as present state account info + let present_state = BundleState::builder(2..=2) + .state_present_account_info(address1, None) + .build(); + assert!(present_state.state.get(&address1).unwrap().info.is_none()); + + // Test to insert None as original state account info + let original_state = BundleState::builder(2..=2) + .state_original_account_info(address2, None) + .build(); + assert!(original_state + .state + .get(&address2) + .unwrap() + .original_info + .is_none()); + } }