Skip to content

Commit

Permalink
Cancun support (moonbeam-foundation#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnjscksdn98 committed Dec 3, 2024
1 parent 9b31ca6 commit ac9598d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 41 deletions.
14 changes: 5 additions & 9 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ derive_more = "0.99"
environmental = { version = "1.1.4", default-features = false }
ethereum = { version = "0.15.0", default-features = false }
ethereum-types = { version = "0.14.1", default-features = false }
evm = { version = "0.41.1", default-features = false }
evm-gasometer = { version = "0.41.0", default-features = false }
evm-runtime = { version = "0.41.0", default-features = false }
evm = { git = "https://github.com/bifrost-platform/evm", branch = "bifrost-polkadot-stable2407", default-features = false }
evm-gasometer = { git = "https://github.com/bifrost-platform/evm", branch = "bifrost-polkadot-stable2407", default-features = false }
evm-runtime = { git = "https://github.com/bifrost-platform/evm", branch = "bifrost-polkadot-stable2407", default-features = false }
futures = "0.3.30"
hash-db = { version = "0.16.0", default-features = false }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
Expand Down
29 changes: 5 additions & 24 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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 @@ -184,7 +184,7 @@ pub mod pallet {

/// EVM config used in the module.
fn config() -> &'static EvmConfig {
&SHANGHAI_CONFIG
&CANCUN_CONFIG
}
}

Expand Down Expand Up @@ -792,7 +792,7 @@ impl<T: Config> GasWeightMapping for FixedGasWeightMapping<T> {
}
}

static SHANGHAI_CONFIG: EvmConfig = EvmConfig::shanghai();
static CANCUN_CONFIG: EvmConfig = EvmConfig::cancun();

impl<T: Config> Pallet<T> {
/// Check whether an account is empty.
Expand Down Expand Up @@ -821,32 +821,13 @@ 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(_) => (),
}
}
let _ = <AccountStorages<T>>::clear_prefix(address, u32::max_value(), None);
}

/// Create an account.
Expand Down
40 changes: 39 additions & 1 deletion frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ where
struct SubstrateStackSubstate<'config> {
metadata: StackSubstateMetadata<'config>,
deletes: BTreeSet<H160>,
creates: BTreeSet<H160>,
logs: Vec<Log>,
parent: Option<Box<SubstrateStackSubstate<'config>>>,
}
Expand All @@ -589,6 +590,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 @@ -605,7 +607,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 @@ -640,10 +642,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 @@ -676,6 +692,7 @@ pub struct SubstrateStackState<'vicinity, 'config, T> {
vicinity: &'vicinity Vicinity,
substate: SubstrateStackSubstate<'config>,
original_storage: BTreeMap<(H160, H256), H256>,
transient_storage: BTreeMap<(H160, H256), H256>,
recorded: Recorded,
weight_info: Option<WeightInfo>,
_marker: PhantomData<T>,
Expand All @@ -693,11 +710,13 @@ impl<'vicinity, 'config, T: Config> SubstrateStackState<'vicinity, 'config, T> {
substate: SubstrateStackSubstate {
metadata,
deletes: BTreeSet::new(),
creates: BTreeSet::new(),
logs: Vec::new(),
parent: None,
},
_marker: PhantomData,
original_storage: BTreeMap::new(),
transient_storage: BTreeMap::new(),
recorded: Default::default(),
weight_info,
}
Expand Down Expand Up @@ -791,6 +810,13 @@ where
<AccountStorages<T>>::get(address, index)
}

fn transient_storage(&self, address: H160, index: H256) -> H256 {
self.transient_storage
.get(&(address, index))
.copied()
.unwrap_or_default()
}

fn original_storage(&self, address: H160, index: H256) -> Option<H256> {
Some(
self.original_storage
Expand Down Expand Up @@ -838,6 +864,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 @@ -877,6 +907,10 @@ where
}
}

fn set_transient_storage(&mut self, address: H160, key: H256, value: H256) {
self.transient_storage.insert((address, key), value);
}

fn reset_storage(&mut self, address: H160) {
#[allow(deprecated)]
let _ = <AccountStorages<T>>::remove_prefix(address, None);
Expand All @@ -890,6 +924,10 @@ 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 @@ -1037,7 +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);
assert_eq!(account_2.sufficients, 1);
assert_eq!(account_2.sufficients, 0);
});
}

Expand Down
2 changes: 1 addition & 1 deletion precompiles/src/testing/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl PrecompileHandle for MockHandle {
if self
.record_cost(crate::evm::costs::call_cost(
context.apparent_value,
&evm::Config::london(),
&evm::Config::cancun(),
))
.is_err()
{
Expand Down
4 changes: 2 additions & 2 deletions primitives/evm/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ mod tests {
UnknownError,
}

static SHANGHAI_CONFIG: evm::Config = evm::Config::shanghai();
static CANCUN_CONFIG: evm::Config = evm::Config::cancun();

impl From<TransactionValidationError> for TestError {
fn from(e: TransactionValidationError) -> Self {
Expand Down Expand Up @@ -337,7 +337,7 @@ mod tests {
} = input;
CheckEvmTransaction::<TestError>::new(
CheckEvmTransactionConfig {
evm_config: &SHANGHAI_CONFIG,
evm_config: &CANCUN_CONFIG,
block_gas_limit: blockchain_gas_limit,
base_fee: blockchain_base_fee,
chain_id: blockchain_chain_id,
Expand Down

0 comments on commit ac9598d

Please sign in to comment.