Skip to content

Commit

Permalink
feat: support cancun selfdestruct changes (EIP-6780)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadkaouk committed May 22, 2024
1 parent 7dfd999 commit 982dab0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 4 additions & 22 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use scale_info::TypeInfo;
// Substrate
use frame_support::{
dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo},
storage::{child::KillStorageResult, KeyPrefixIterator},
storage::KeyPrefixIterator,
traits::{
fungible::{Balanced, Credit, Debt},
tokens::{
Expand Down Expand Up @@ -821,32 +821,14 @@ impl<T: Config> Pallet<T> {
/// Remove an account.
pub fn remove_account(address: &H160) {
if <AccountCodes<T>>::contains_key(address) {
// Remember to call `dec_sufficients` when clearing Suicided.
<Suicided<T>>::insert(address, ());

// In theory, we can always have pre-EIP161 contracts, so we
// make sure the account nonce is at least one.
let account_id = T::AddressMapping::into_account_id(*address);
frame_system::Pallet::<T>::inc_account_nonce(&account_id);
let _ = frame_system::Pallet::<T>::dec_sufficients(&account_id);
}

<AccountCodes<T>>::remove(address);
<AccountCodesMetadata<T>>::remove(address);

if T::SuicideQuickClearLimit::get() > 0 {
#[allow(deprecated)]
let res = <AccountStorages<T>>::remove_prefix(address, Some(T::SuicideQuickClearLimit::get()));

match res {
KillStorageResult::AllRemoved(_) => {
<Suicided<T>>::remove(address);

let account_id = T::AddressMapping::into_account_id(*address);
let _ = frame_system::Pallet::<T>::dec_sufficients(&account_id);
}
KillStorageResult::SomeRemaining(_) => (),
}
}
#[allow(deprecated)]
let _ = <AccountStorages<T>>::remove_prefix(address, None);
}

/// Create an account.
Expand Down
25 changes: 24 additions & 1 deletion frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ where
struct SubstrateStackSubstate<'config> {
metadata: StackSubstateMetadata<'config>,
deletes: BTreeSet<H160>,
creates: BTreeSet<H160>,
logs: Vec<Log>,
parent: Option<Box<SubstrateStackSubstate<'config>>>,
}
Expand All @@ -638,6 +639,7 @@ impl<'config> SubstrateStackSubstate<'config> {
metadata: self.metadata.spit_child(gas_limit, is_static),
parent: None,
deletes: BTreeSet::new(),
creates: BTreeSet::new(),
logs: Vec::new(),
};
mem::swap(&mut entering, self);
Expand All @@ -654,7 +656,7 @@ impl<'config> SubstrateStackSubstate<'config> {
self.metadata.swallow_commit(exited.metadata)?;
self.logs.append(&mut exited.logs);
self.deletes.append(&mut exited.deletes);

self.creates.append(&mut exited.creates);
sp_io::storage::commit_transaction();
Ok(())
}
Expand Down Expand Up @@ -689,10 +691,24 @@ impl<'config> SubstrateStackSubstate<'config> {
false
}

pub fn created(&self, address: H160) -> bool {
if self.creates.contains(&address) {
return true;
}

if let Some(parent) = self.parent.as_ref() {
return parent.created(address);
}

false
}

pub fn set_deleted(&mut self, address: H160) {
self.deletes.insert(address);
}

pub fn set_created(&mut self, address: H160) { self.creates.insert(address); }

pub fn log(&mut self, address: H160, topics: Vec<H256>, data: Vec<u8>) {
self.logs.push(Log {
address,
Expand Down Expand Up @@ -746,6 +762,7 @@ impl<'vicinity, 'config, T: Config> SubstrateStackState<'vicinity, 'config, T> {
substate: SubstrateStackSubstate {
metadata,
deletes: BTreeSet::new(),
creates: BTreeSet::new(),
logs: Vec::new(),
parent: None,
},
Expand Down Expand Up @@ -900,6 +917,10 @@ where
self.substate.deleted(address)
}

fn created(&self, address: H160) -> bool {
self.substate.created(address)
}

fn inc_nonce(&mut self, address: H160) -> Result<(), ExitError> {
let account_id = T::AddressMapping::into_account_id(address);
frame_system::Pallet::<T>::inc_account_nonce(&account_id);
Expand Down Expand Up @@ -956,6 +977,8 @@ where
self.substate.set_deleted(address)
}

fn set_created(&mut self, address: H160) { self.substate.set_created(address); }

fn set_code(&mut self, address: H160, code: Vec<u8>) {
log::debug!(
target: "evm",
Expand Down
2 changes: 1 addition & 1 deletion frame/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ fn handle_sufficient_reference() {
assert_eq!(account_2.sufficients, 1);
EVM::remove_account(&addr_2);
let account_2 = frame_system::Account::<Test>::get(substrate_addr_2);
assert_eq!(account_2.sufficients, 1);
assert_eq!(account_2.sufficients, 0);
});
}

Expand Down

0 comments on commit 982dab0

Please sign in to comment.