Skip to content

Commit

Permalink
Initial support for asynchronous suiciding (#1212)
Browse files Browse the repository at this point in the history
* Initial support for asynchronous suiciding

* Use contains_key instead of is_some

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>

* Fix compile

* Remove unnecessary Suicided check

* Don't call dec_sufficients

* Handle pre-EIP161 case

* Add extra safeguard

* Always inc nonce

* Fix tests

---------

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
  • Loading branch information
sorpaas and xlc committed Oct 5, 2023
1 parent 789fa51 commit aea5281
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
20 changes: 17 additions & 3 deletions frame/evm/src/lib.rs
Expand Up @@ -561,6 +561,9 @@ pub mod pallet {
#[pallet::storage]
pub type AccountStorages<T: Config> =
StorageDoubleMap<_, Blake2_128Concat, H160, Blake2_128Concat, H256, H256, ValueQuery>;

#[pallet::storage]
pub type Suicided<T: Config> = StorageMap<_, Blake2_128Concat, H160, (), OptionQuery>;
}

/// Type alias for currency balance.
Expand Down Expand Up @@ -793,18 +796,29 @@ 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);
let _ = frame_system::Pallet::<T>::dec_sufficients(&account_id);
frame_system::Pallet::<T>::inc_account_nonce(&account_id);
}

<AccountCodes<T>>::remove(address);
<AccountCodesMetadata<T>>::remove(address);
#[allow(deprecated)]
let _ = <AccountStorages<T>>::remove_prefix(address, None);
}

/// Create an account.
pub fn create_account(address: H160, code: Vec<u8>) {
if <Suicided<T>>::contains_key(address) {
// This branch should never trigger, because when Suicided
// contains an address, then its nonce will be at least one,
// which causes CreateCollision error in EVM, but we add it
// here for safeguard.
return;
}

if code.is_empty() {
return;
}
Expand Down
3 changes: 1 addition & 2 deletions frame/evm/src/tests.rs
Expand Up @@ -1037,8 +1037,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);
// We decreased the sufficient reference by 1 on removing the account.
assert_eq!(account_2.sufficients, 0);
assert_eq!(account_2.sufficients, 1);
});
}

Expand Down

0 comments on commit aea5281

Please sign in to comment.