Skip to content

Commit

Permalink
Added pre-EIP-2 contract creation behaviors
Browse files Browse the repository at this point in the history
Added a prehomestead field on CacheDB to prevent deletion of empty
contracts that were created before HOMESTEAD as well as out-of-gas to
empty contract creation behavior.
  • Loading branch information
Parker Thompson committed Aug 1, 2022
1 parent b3d6bf2 commit b1768d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
13 changes: 11 additions & 2 deletions crates/revm/src/db/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub type InMemoryDB = CacheDB<EmptyDB>;

impl InMemoryDB {
pub fn default() -> Self {
CacheDB::new(EmptyDB {})
CacheDB::new(EmptyDB {}, false)
}
}

Expand All @@ -32,6 +32,7 @@ pub struct CacheDB<ExtDB: DatabaseRef> {
pub logs: Vec<Log>,
pub block_hashes: Map<U256, H256>,
pub db: ExtDB,
pub prehomestead: bool,
}

#[derive(Debug, Clone, Default)]
Expand All @@ -55,7 +56,7 @@ pub enum AccountState {
}

impl<ExtDB: DatabaseRef> CacheDB<ExtDB> {
pub fn new(db: ExtDB) -> Self {
pub fn new(db: ExtDB, prehomestead: bool) -> Self {
let mut contracts = Map::new();
contracts.insert(KECCAK_EMPTY, Bytes::new());
contracts.insert(H256::zero(), Bytes::new());
Expand All @@ -65,6 +66,7 @@ impl<ExtDB: DatabaseRef> CacheDB<ExtDB> {
logs: Vec::default(),
block_hashes: Map::new(),
db,
prehomestead,
}
}

Expand Down Expand Up @@ -102,6 +104,13 @@ impl<ExtDB: DatabaseRef> DatabaseCommit for CacheDB<ExtDB> {
fn commit(&mut self, changes: Map<H160, Account>) {
for (add, acc) in changes {
if acc.is_empty() || matches!(acc.filth, Filth::Destroyed) {
// pre EIP-2
if acc.info.code.is_some()
&& acc.info.code_hash == KECCAK_EMPTY
&& self.prehomestead
{
continue;
}
// clear account data, and increate its incarnation.
let acc = self.accounts.entry(add).or_default();
acc.account_state = AccountState::EVMStorageCleared;
Expand Down
18 changes: 16 additions & 2 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,22 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
let gas_for_code = code.len() as u64 * crate::gas::CODEDEPOSIT;
// record code deposit gas cost and check if we are out of gas.
if !interp.gas.record_cost(gas_for_code) {
self.data.subroutine.checkpoint_revert(checkpoint);
return (Return::OutOfGas, ret, interp.gas, b);
// EIP-2
if SPEC::enabled(HOMESTEAD) {
self.data.subroutine.checkpoint_revert(checkpoint);
return (Return::OutOfGas, ret, interp.gas, b);
} else {
self.data.subroutine.checkpoint_commit();
let empty_contract = Bytes::new();
let code_hash =
H256::from_slice(Keccak256::digest(&empty_contract).as_slice());
self.data.subroutine.set_code(
created_address,
empty_contract,
code_hash,
);
return (Return::Continue, ret, interp.gas, b);
}
}
}
// if we have enought gas
Expand Down

0 comments on commit b1768d7

Please sign in to comment.