Skip to content

Commit

Permalink
Implement EIP-6780: SELFDESTRUCT only in same transaction (#1471)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp authored Nov 19, 2024
1 parent 712d0b9 commit 8ab83d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 6 additions & 2 deletions crates/evm/src/evm/db_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ impl<'a, C: sov_modules_api::Context> DatabaseCommit for EvmDb<'a, C> {
});
let db_account = DbAccount::new(address);

// https://github.com/Sovereign-Labs/sovereign-sdk/issues/425
if account.is_selfdestructed() {
// TODO find mroe efficient way to clear storage
if self.current_spec.is_enabled_in(SpecId::CANCUN) {
// SELFDESTRUCT does not delete any data (including storage keys, code, or the account itself).
continue;
}

// TODO find more efficient way to clear storage
// https://github.com/chainwayxyz/rollup-modules/issues/4
// clear storage

Expand Down
26 changes: 24 additions & 2 deletions crates/evm/src/evm/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,29 @@ fn calc_diff_size<EXT, SPEC: Spec, DB: Database>(
// So we need to only force the nonce change.
account.account_info_changed = true;
}
JournalEntry::AccountDestroyed { address, .. } => {
JournalEntry::AccountDestroyed {
address,
target,
was_destroyed,
had_balance,
} => {
// This event is produced only if acc.is_created() || !is_cancun_enabled
// State is not changed:
// * if we are after Cancun upgrade and
// * Selfdestruct account that is created in the same transaction and
// * Specify the target is same as selfdestructed account. The balance stays unchanged.

if *was_destroyed {
// It was already destroyed before in the log, no need to do anything.
continue;
}

if address != target && !had_balance.is_zero() {
// mark changes to the target account
let target = account_changes.entry(target).or_default();
target.account_info_changed = true;
}

let account = account_changes.entry(address).or_default();
if account.created {
// That's a temporary account.
Expand Down Expand Up @@ -514,7 +536,7 @@ fn calc_diff_size<EXT, SPEC: Spec, DB: Database>(
}

for (addr, account) in account_changes {
if account.destroyed {
if account.destroyed && !SPEC::enabled(SpecId::CANCUN) {
// Each 'delete' key produces a write of 'key' + 1 byte
// account_info:
diff_size += DB_ACCOUNT_KEY_SIZE + 1;
Expand Down

0 comments on commit 8ab83d0

Please sign in to comment.