Skip to content

Commit

Permalink
chore: swap blockenv values instead of cloning (#10)
Browse files Browse the repository at this point in the history
* chore: remove redundant blockenv clone be swaping the individual values instead

* fix: switch to core from std

* chore: repeat for op
  • Loading branch information
Evalir authored Feb 4, 2025
1 parent 0384bf0 commit 316250b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions crates/evm/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,20 @@ impl<EXT, DB: Database> Evm for EthEvm<'_, EXT, DB> {

*self.tx_mut() = tx_env;

let prev_block_env = self.block().clone();
let mut gas_limit = U256::from(self.tx().gas_limit);
let mut basefee = U256::ZERO;

// ensure the block gas limit is >= the tx
self.block_mut().gas_limit = U256::from(self.tx().gas_limit);

core::mem::swap(&mut self.block_mut().gas_limit, &mut gas_limit);
// disable the base fee check for this call by setting the base fee to zero
self.block_mut().basefee = U256::ZERO;
core::mem::swap(&mut self.block_mut().basefee, &mut basefee);

let res = self.0.transact();

// re-set the block env
*self.block_mut() = prev_block_env;
// swap back to the previous gas limit
core::mem::swap(&mut self.block_mut().gas_limit, &mut gas_limit);
// swap back to the previous base fee
core::mem::swap(&mut self.block_mut().basefee, &mut basefee);

res
}
Expand Down
14 changes: 8 additions & 6 deletions crates/op-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,20 @@ impl<EXT, DB: Database> Evm for OpEvm<'_, EXT, DB> {

*self.tx_mut() = tx_env;

let prev_block_env = self.block().clone();
let mut gas_limit = U256::from(self.tx().gas_limit);
let mut basefee = U256::ZERO;

// ensure the block gas limit is >= the tx
self.block_mut().gas_limit = U256::from(self.tx().gas_limit);

core::mem::swap(&mut self.block_mut().gas_limit, &mut gas_limit);
// disable the base fee check for this call by setting the base fee to zero
self.block_mut().basefee = U256::ZERO;
core::mem::swap(&mut self.block_mut().basefee, &mut basefee);

let res = self.0.transact();

// re-set the block env
*self.block_mut() = prev_block_env;
// swap back to the previous gas limit
core::mem::swap(&mut self.block_mut().gas_limit, &mut gas_limit);
// swap back to the previous base fee
core::mem::swap(&mut self.block_mut().basefee, &mut basefee);

res
}
Expand Down

0 comments on commit 316250b

Please sign in to comment.