Skip to content

Commit

Permalink
Handle system refcounts for pallet-evm managed contracts (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Feb 23, 2021
1 parent b4ce6ec commit bbc3f00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 91 deletions.
90 changes: 0 additions & 90 deletions frame/evm/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,93 +141,3 @@ impl<'vicinity, T: Trait> BackendT for Backend<'vicinity, T> {
AccountStorages::get(address, index)
}
}

impl<'vicinity, T: Trait> ApplyBackend for Backend<'vicinity, T> {
fn apply<A, I, L>(
&mut self,
values: A,
logs: L,
delete_empty: bool,
) where
A: IntoIterator<Item=Apply<I>>,
I: IntoIterator<Item=(H256, H256)>,
L: IntoIterator<Item=evm::backend::Log>,
{
for apply in values {
match apply {
Apply::Modify {
address, basic, code, storage, reset_storage,
} => {
Module::<T>::mutate_account_basic(&address, Account {
nonce: basic.nonce,
balance: basic.balance,
});

if let Some(code) = code {
debug::debug!(
target: "evm",
"Inserting code ({} bytes) at {:?}",
code.len(),
address
);
AccountCodes::insert(address, code);
}

if reset_storage {
AccountStorages::remove_prefix(address);
}

for (index, value) in storage {
if value == H256::default() {
debug::debug!(
target: "evm",
"Removing storage for {:?} [index: {:?}]",
address,
index
);
AccountStorages::remove(address, index);
} else {
debug::debug!(
target: "evm",
"Updating storage for {:?} [index: {:?}, value: {:?}]",
address,
index,
value
);
AccountStorages::insert(address, index, value);
}
}

if delete_empty {
Module::<T>::remove_account_if_empty(&address);
}
},
Apply::Delete { address } => {
debug::debug!(
target: "evm",
"Deleting account at {:?}",
address
);
Module::<T>::remove_account(&address)
},
}
}

for log in logs {
debug::trace!(
target: "evm",
"Inserting log for {:?}, topics ({}) {:?}, data ({}): {:?}]",
log.address,
log.topics.len(),
log.topics,
log.data.len(),
log.data
);
Module::<T>::deposit_event(Event::<T>::Log(Log {
address: log.address,
topics: log.topics,
data: log.data,
}));
}
}
}
19 changes: 19 additions & 0 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,29 @@ impl<T: Config> Module<T> {

/// Remove an account.
pub fn remove_account(address: &H160) {
if AccountCodes::contains_key(address) {
let account_id = T::AddressMapping::into_account_id(*address);
frame_system::Module::<T>::dec_ref(&account_id);
}

AccountCodes::remove(address);
AccountStorages::remove_prefix(address);
}

/// Create an account.
pub fn create_account(address: H160, code: Vec<u8>) {
if code.is_empty() {
return
}

if !AccountCodes::contains_key(&address) {
let account_id = T::AddressMapping::into_account_id(address);
frame_system::Module::<T>::inc_ref(&account_id);
}

AccountCodes::insert(address, code);
}

/// Get the account basic in EVM format.
pub fn account_basic(address: &H160) -> Account {
let account_id = T::AddressMapping::into_account_id(*address);
Expand Down
2 changes: 1 addition & 1 deletion frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ impl<'vicinity, 'config, T: Config> StackStateT<'config> for SubstrateStackState
code.len(),
address
);
AccountCodes::insert(address, code);
Module::<T>::create_account(address, code);
}

fn transfer(&mut self, transfer: Transfer) -> Result<(), ExitError> {
Expand Down

0 comments on commit bbc3f00

Please sign in to comment.