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

db: add setters to BundleBuilder with &mut self #1527

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Changes from 1 commit
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
126 changes: 126 additions & 0 deletions crates/revm/src/db/states/bundle_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,61 @@ impl BundleBuilder {
self
}

/// Set address info of BundleState state.
pub fn set_state_address(&mut self, address: Address) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is a builder type, we can return &mut Self on these functions so they can be chained

the existing self functions should then reuse the &mut self ones so we don't duplicate the actual impl

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, let me know if this is ok for you

self.states.insert(address);
}

/// Set original account info of BundleState state.
pub fn set_state_original_account_info(&mut self, address: Address, original: AccountInfo) {
self.states.insert(address);
self.state_original.insert(address, original);
}

/// Set present account info of BundleState state.
pub fn set_state_present_account_info(&mut self, address: Address, present: AccountInfo) {
self.states.insert(address);
self.state_present.insert(address, present);
}

/// Set storage info of BundleState state.
pub fn set_state_storage(&mut self, address: Address, storage: HashMap<U256, (U256, U256)>) {
self.states.insert(address);
self.state_storage.insert(address, storage);
}

/// Set address info of BundleState reverts.
pub fn set_revert_address(&mut self, block_number: u64, address: Address) {
self.reverts.insert((block_number, address));
}

/// Set account info of BundleState reverts.
pub fn set_revert_account_info(
&mut self,
block_number: u64,
address: Address,
account: Option<Option<AccountInfo>>,
) {
self.reverts.insert((block_number, address));
self.revert_account.insert((block_number, address), account);
}

/// Set storage info of BundleState reverts.
pub fn set_revert_storage(
&mut self,
block_number: u64,
address: Address,
storage: Vec<(U256, U256)>,
) {
self.reverts.insert((block_number, address));
self.revert_storage.insert((block_number, address), storage);
}

/// Set contracts info.
pub fn set_contract(&mut self, address: B256, bytecode: Bytecode) {
self.contracts.insert(address, bytecode);
}

/// Create `BundleState` instance based on collected information
pub fn build(mut self) -> BundleState {
let mut state_size = 0;
Expand Down Expand Up @@ -898,6 +953,39 @@ mod tests {
.build()
}

/// Test bundle three with setters
fn test_bundle3_with_setters() -> BundleState {
let mut bundle_builder = BundleState::builder(0..=0);
bundle_builder.set_state_present_account_info(
account1(),
AccountInfo {
nonce: 1,
balance: U256::from(10),
code_hash: KECCAK_EMPTY,
code: None,
},
);
bundle_builder.set_state_storage(
account1(),
HashMap::from([(slot1(), (U256::from(0), U256::from(10)))]),
);
bundle_builder.set_state_address(account2());
bundle_builder.set_state_present_account_info(
account2(),
AccountInfo {
nonce: 1,
balance: U256::from(10),
code_hash: KECCAK_EMPTY,
code: None,
},
);
bundle_builder.set_revert_address(0, account1());
bundle_builder.set_revert_account_info(0, account1(), Some(None));
bundle_builder.set_revert_storage(0, account1(), vec![(slot1(), U256::from(0))]);
bundle_builder.set_revert_account_info(0, account2(), Some(None));
bundle_builder.build()
}

/// Test bundle four
fn test_bundle4() -> BundleState {
BundleState::builder(0..=0)
Expand Down Expand Up @@ -929,6 +1017,37 @@ mod tests {
.build()
}

/// Test bundle four with setters
fn test_bundle4_with_setters() -> BundleState {
let mut bundle_builder = BundleState::builder(0..=0);
bundle_builder.set_state_present_account_info(
account1(),
AccountInfo {
nonce: 3,
balance: U256::from(20),
code_hash: KECCAK_EMPTY,
code: None,
},
);
bundle_builder.set_state_storage(
account1(),
HashMap::from([(slot1(), (U256::from(0), U256::from(15)))]),
);
bundle_builder.set_revert_address(0, account1());
bundle_builder.set_revert_account_info(
0,
account1(),
Some(Some(AccountInfo {
nonce: 1,
balance: U256::from(10),
code_hash: KECCAK_EMPTY,
code: None,
})),
);
bundle_builder.set_revert_storage(0, account1(), vec![(slot1(), U256::from(10))]);
bundle_builder.build()
}

fn sanity_path(bundle1: BundleState, bundle2: BundleState) {
let mut extended = bundle1.clone();
extended.extend(bundle2.clone());
Expand Down Expand Up @@ -1023,6 +1142,13 @@ mod tests {
sanity_path(test_bundle3(), test_bundle4());
}

#[test]
fn test_sanity_path_with_setters() {
sanity_path(test_bundle1(), test_bundle2());
sanity_path(test_bundle3_with_setters(), test_bundle4());
sanity_path(test_bundle3(), test_bundle4_with_setters());
}

#[test]
fn test_multi_reverts_with_delete() {
let mut state = BundleBuilder::new(0..=3)
Expand Down
Loading