Skip to content

Commit

Permalink
Update frontier (evm + weight v2) (#2316)
Browse files Browse the repository at this point in the history
* Update evm to 0.39

* Adapted tests

Fixed parameter in execute is_precompile_or_fail check

format

* Removed unused imports

* Wip fallible discriminant

* fmt

* wip fix test assets costs

* fix test assets costs

* TODO temp ignore precompile macro tests

* update frontier pin

* update tests

* just hardcode `GasLimitPovSizeRatio` as a constant

* fix xcm ts tests

* oops

* configure runtime api weigh_limit

* suggestions

* Updates EVM PoV to support  PoV Gasometer

* fix test evm-over-pov

* Update evm pin

* update ts tests

* Update evm pin 2

* editorconfig

* update frontier pin

* prettier

* fix

* compute properly eth tx limits in ts tests

* compute safely EXTRINSIC_GAS_LIMIT

* typo

* runtime api TODO fix

* Adds test for Gasometer PoV limit/estimate check

* runtime api eth_call estimate tx len

* fix ts tests estimate gas

* prettier

* suggestion

* suggestion

* optimize estimated_transaction_len

* prettier gas_limit check

* fix xcm to evm encoded tx len

* Revert "fix xcm to evm encoded tx len"

This reverts commit 93bee9e.

* account for proof size in precompiles try_dispatch

* fmt

* precompile handler: add helper function record_db_read

* migrate precompile db reads: xcm-utils

* migrate precompile db reads: xcm-transactor

* migrate precompile db reads: assets-erc20::eip2612

* migrate precompile db reads: precompile utils

* Revert "migrate precompile db reads: assets-erc20::eip2612"

This reverts commit 6801477.

* migrate precompile db reads: assets-erc20::eip2612

* fix rust tests

* Update precompiles/xcm-transactor/src/functions.rs

Co-authored-by: Agustín Rodriguez <agus@purestake.com>

* migrate precompile db reads: pallet-democracy

* migrate precompile db reads: fix pallet-democracy

* more precise comment

* account AssetId

* migrate precompile db reads: assets-erc20

* migrate precompile db reads: assets-erc20 (fix)

* migrate precompile db reads: author-mapping

* migrate precompile db reads: balances-erc20::eip2612

* migrate precompile db reads: balances-erc20

* migrate precompile db reads: balances-erc20::eip2612 (fix)

* migrate precompile db reads: call-permit

* migrate precompile db reads: collective

* migrate precompile db reads: conviction-voting

* migrate precompile db reads: crowdloan-rewards

* wip migrate precompile db reads: parachain-staking

* migrate precompile db reads: parachain-staking

* migrate precompile db reads: precompile-registry

* migrate precompile db reads: proxy

* precompile-registry add some comments

* migrate precompile db reads: referenda

* conviction-voting fix

* call-permit update

* REMOVE precompile db reads: relay-encoder

* migrate precompile db reads: gmp

* record arbitrary cost on relay encoder precompile to prevent free spam

* migrate precompile db reads: precompile-registry rework

* refactor randomness pallet & precompile to comply with weight v2

* add test for precompile batch PoV check

* fix rust tests compilation

* adapt some rust tests

* suggestion

* Add back missing db read precompile_set

* editorconfig + prettier

* update cumulus pin

* update frontier pin

* fix ts precompile xcm utils tests

* add more tests for precompile PoV and fmt

* update cumulus pin

* comply prettier & editorconfig

* fix more ts tests (update frontier pin)

* suggestion

---------

Co-authored-by: Francisco Gamundi <francisco@purestake.com>
Co-authored-by: crystalin <alan.sapede@gmail.com>
Co-authored-by: librelois <c@elo.tf>
Co-authored-by: Agustín Rodriguez <agus@purestake.com>
Co-authored-by: agus <agusrodriguez2456@gmail.com>
  • Loading branch information
6 people authored Jun 6, 2023
1 parent 2f07d6c commit 16b1898
Show file tree
Hide file tree
Showing 86 changed files with 1,857 additions and 662 deletions.
186 changes: 84 additions & 102 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -226,28 +226,38 @@ ethereum = { version = "0.14.0", default-features = false, features = [
"with-codec",
] }
ethereum-types = { version = "0.14", default-features = false }
evm = { version = "0.39.0", default-features = false, features = [
"with-codec",
] }
evm-gasometer = { version = "0.39.0", default-features = false }
evm-runtime = { version = "0.39.0", default-features = false }
evm = { git = "https://github.com/purestake/evm", branch = "tgm-record-external-cost", default-features = false }
evm-gasometer = { git = "https://github.com/purestake/evm", branch = "tgm-record-external-cost", default-features = false }
evm-runtime = { git = "https://github.com/purestake/evm", branch = "tgm-record-external-cost", default-features = false }
fp-ethereum = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
fp-evm = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
fp-rpc = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
fp-self-contained = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
pallet-base-fee = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
pallet-ethereum = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false, features = [
"forbid-evm-reentrancy",
"evm-with-weight-limit",
] }
pallet-evm = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false, features = [
"forbid-evm-reentrancy",
"evm-with-weight-limit",
] }
pallet-evm-precompile-blake2 = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false, features = [
"evm-with-weight-limit",
] }
pallet-evm-precompile-bn128 = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false, features = [
"evm-with-weight-limit",
] }
pallet-evm-precompile-dispatch = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false, features = [
"evm-with-weight-limit",
] }
pallet-evm-precompile-modexp = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false, features = [
"evm-with-weight-limit",
] }
pallet-evm-precompile-blake2 = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
pallet-evm-precompile-bn128 = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
pallet-evm-precompile-dispatch = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
pallet-evm-precompile-modexp = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
pallet-evm-precompile-sha3fips = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40", default-features = false, features = [
"evm-with-weight-limit",
] }

# Frontier (client)
fc-consensus = { git = "https://github.com/purestake/frontier", branch = "moonbeam-polkadot-v0.9.40" }
Expand Down Expand Up @@ -364,12 +374,6 @@ tracing-core = "0.1.29"
trie-root = "0.15.2"
url = "2.2.2"

[patch.crates-io]
evm = { git = "https://github.com/rust-blockchain/evm", rev = "842e03d068ddb6a3195a2dedc4a9b63caadb3355" }
evm-core = { git = "https://github.com/rust-blockchain/evm", rev = "842e03d068ddb6a3195a2dedc4a9b63caadb3355" }
evm-gasometer = { git = "https://github.com/rust-blockchain/evm", rev = "842e03d068ddb6a3195a2dedc4a9b63caadb3355" }
evm-runtime = { git = "https://github.com/rust-blockchain/evm", rev = "842e03d068ddb6a3195a2dedc4a9b63caadb3355" }

# The list of dependencies below (which can be both direct and indirect dependencies) are crates
# that are suspected to be CPU-intensive, and that are unlikely to require debugging (as some of
# their debug info might be missing) or to require to be frequently recompiled. We compile these
Expand Down
93 changes: 71 additions & 22 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,16 +614,31 @@ where
let overrides = crate::rpc::overrides_handle(client.clone());
let fee_history_limit = rpc_config.fee_history_limit;

rpc::spawn_essential_tasks(rpc::SpawnTasksParams {
task_manager: &task_manager,
client: client.clone(),
substrate_backend: backend.clone(),
frontier_backend: frontier_backend.clone(),
filter_pool: filter_pool.clone(),
overrides: overrides.clone(),
fee_history_limit,
fee_history_cache: fee_history_cache.clone(),
});
// Sinks for pubsub notifications.
// Everytime a new subscription is created, a new mpsc channel is added to the sink pool.
// The MappingSyncWorker sends through the channel on block import and the subscription emits a
// notification to the subscriber on receiving a message through this channel.
// This way we avoid race conditions when using native substrate block import notification
// stream.
let pubsub_notification_sinks: fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<Block>,
> = Default::default();
let pubsub_notification_sinks = Arc::new(pubsub_notification_sinks);

rpc::spawn_essential_tasks(
rpc::SpawnTasksParams {
task_manager: &task_manager,
client: client.clone(),
substrate_backend: backend.clone(),
frontier_backend: frontier_backend.clone(),
filter_pool: filter_pool.clone(),
overrides: overrides.clone(),
fee_history_limit,
fee_history_cache: fee_history_cache.clone(),
},
sync_service.clone(),
pubsub_notification_sinks.clone(),
);

let ethapi_cmd = rpc_config.ethapi.clone();
let tracing_requesters =
Expand Down Expand Up @@ -669,6 +684,7 @@ where
let overrides = overrides.clone();
let fee_history_cache = fee_history_cache.clone();
let block_data_cache = block_data_cache.clone();
let pubsub_notification_sinks = pubsub_notification_sinks.clone();

move |deny_unsafe, subscription_task_executor| {
let deps = rpc::FullDeps {
Expand All @@ -690,6 +706,7 @@ where
xcm_senders: None,
block_data_cache: block_data_cache.clone(),
overrides: overrides.clone(),
forced_parent_hashes: None,
};
if ethapi_cmd.contains(&EthApiCmd::Debug) || ethapi_cmd.contains(&EthApiCmd::Trace) {
rpc::create_full(
Expand All @@ -699,10 +716,17 @@ where
tracing_requesters: tracing_requesters.clone(),
trace_filter_max_count: rpc_config.ethapi_trace_max_count,
}),
pubsub_notification_sinks.clone(),
)
.map_err(Into::into)
} else {
rpc::create_full(deps, subscription_task_executor, None).map_err(Into::into)
rpc::create_full(
deps,
subscription_task_executor,
None,
pubsub_notification_sinks.clone(),
)
.map_err(Into::into)
}
}
};
Expand Down Expand Up @@ -1090,16 +1114,32 @@ where
}),
);
}
rpc::spawn_essential_tasks(rpc::SpawnTasksParams {
task_manager: &task_manager,
client: client.clone(),
substrate_backend: backend.clone(),
frontier_backend: frontier_backend.clone(),
filter_pool: filter_pool.clone(),
overrides: overrides.clone(),
fee_history_limit,
fee_history_cache: fee_history_cache.clone(),
});

// Sinks for pubsub notifications.
// Everytime a new subscription is created, a new mpsc channel is added to the sink pool.
// The MappingSyncWorker sends through the channel on block import and the subscription emits a
// notification to the subscriber on receiving a message through this channel.
// This way we avoid race conditions when using native substrate block import notification
// stream.
let pubsub_notification_sinks: fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<Block>,
> = Default::default();
let pubsub_notification_sinks = Arc::new(pubsub_notification_sinks);

rpc::spawn_essential_tasks(
rpc::SpawnTasksParams {
task_manager: &task_manager,
client: client.clone(),
substrate_backend: backend.clone(),
frontier_backend: frontier_backend.clone(),
filter_pool: filter_pool.clone(),
overrides: overrides.clone(),
fee_history_limit,
fee_history_cache: fee_history_cache.clone(),
},
sync_service.clone(),
pubsub_notification_sinks.clone(),
);
let ethapi_cmd = rpc_config.ethapi.clone();
let tracing_requesters =
if ethapi_cmd.contains(&EthApiCmd::Debug) || ethapi_cmd.contains(&EthApiCmd::Trace) {
Expand Down Expand Up @@ -1142,6 +1182,7 @@ where
let overrides = overrides.clone();
let fee_history_cache = fee_history_cache.clone();
let block_data_cache = block_data_cache.clone();
let pubsub_notification_sinks = pubsub_notification_sinks.clone();

move |deny_unsafe, subscription_task_executor| {
let deps = rpc::FullDeps {
Expand All @@ -1163,6 +1204,7 @@ where
xcm_senders: xcm_senders.clone(),
overrides: overrides.clone(),
block_data_cache: block_data_cache.clone(),
forced_parent_hashes: None,
};

if ethapi_cmd.contains(&EthApiCmd::Debug) || ethapi_cmd.contains(&EthApiCmd::Trace) {
Expand All @@ -1173,10 +1215,17 @@ where
tracing_requesters: tracing_requesters.clone(),
trace_filter_max_count: rpc_config.ethapi_trace_max_count,
}),
pubsub_notification_sinks.clone(),
)
.map_err(Into::into)
} else {
rpc::create_full(deps, subscription_task_executor, None).map_err(Into::into)
rpc::create_full(
deps,
subscription_task_executor,
None,
pubsub_notification_sinks.clone(),
)
.map_err(Into::into)
}
}
};
Expand Down
23 changes: 21 additions & 2 deletions node/service/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub struct FullDeps<C, P, A: ChainApi, BE> {
pub overrides: Arc<OverrideHandle<Block>>,
/// Cache for Ethereum block data.
pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,
/// Mandated parent hashes for a given block hash.
pub forced_parent_hashes: Option<BTreeMap<H256, H256>>,
}

pub struct TracingConfig {
Expand Down Expand Up @@ -169,6 +171,11 @@ pub fn create_full<C, P, BE, A>(
deps: FullDeps<C, P, A, BE>,
subscription_task_executor: SubscriptionTaskExecutor,
maybe_tracing_config: Option<TracingConfig>,
pubsub_notification_sinks: Arc<
fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<Block>,
>,
>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
BE: Backend<Block> + 'static,
Expand Down Expand Up @@ -215,6 +222,7 @@ where
xcm_senders,
overrides,
block_data_cache,
forced_parent_hashes,
} = deps;

io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;
Expand Down Expand Up @@ -249,6 +257,7 @@ where
fee_history_cache,
fee_history_limit,
10,
forced_parent_hashes,
)
.replace_config::<MoonbeamEthConfig<C, BE>>()
.into_rpc(),
Expand Down Expand Up @@ -286,6 +295,7 @@ where
sync.clone(),
subscription_task_executor,
overrides,
pubsub_notification_sinks.clone(),
)
.into_rpc(),
)?;
Expand Down Expand Up @@ -345,8 +355,15 @@ pub struct SpawnTasksParams<'a, B: BlockT, C, BE> {
}

/// Spawn the tasks that are required to run Moonbeam.
pub fn spawn_essential_tasks<B, C, BE>(params: SpawnTasksParams<B, C, BE>)
where
pub fn spawn_essential_tasks<B, C, BE>(
params: SpawnTasksParams<B, C, BE>,
sync: Arc<SyncingService<B>>,
pubsub_notification_sinks: Arc<
fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<B>,
>,
>,
) where
C: ProvideRuntimeApi<B> + BlockOf,
C: HeaderBackend<B> + HeaderMetadata<B, Error = BlockChainError> + 'static,
C: BlockchainEvents<B> + StorageProvider<B, BE>,
Expand All @@ -373,6 +390,8 @@ where
3,
0,
SyncStrategy::Parachain,
sync,
pubsub_notification_sinks,
)
.for_each(|()| futures::future::ready(())),
);
Expand Down
13 changes: 7 additions & 6 deletions pallets/erc20-xcm-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub mod pallet {
use ethereum_types::BigEndianHash;
use fp_evm::{ExitReason, ExitSucceed};
use frame_support::pallet_prelude::*;
use pallet_evm::Runner;
use pallet_evm::{GasWeightMapping, Runner};
use sp_core::{H160, H256, U256};
use sp_std::vec::Vec;
use xcm::latest::{
Expand Down Expand Up @@ -66,11 +66,7 @@ pub mod pallet {
Erc20Matcher::<T::Erc20MultilocationPrefix>::is_erc20_asset(asset)
}
pub fn weight_of_erc20_transfer() -> Weight {
let gas_limit = T::Erc20TransferGasLimit::get();
Weight::from_parts(
T::Erc20TransferGasLimit::get().saturating_mul(T::WeightPerGas::get().ref_time()),
gas_limit / 4, // TODO: apply gas/proof_size ratio
)
T::GasWeightMapping::gas_to_weight(T::Erc20TransferGasLimit::get(), true)
}
fn erc20_transfer(
erc20_contract_address: H160,
Expand All @@ -86,6 +82,9 @@ pub mod pallet {
// append amount to be transferred
input.extend_from_slice(H256::from_uint(&amount).as_bytes());

let weight_limit =
T::GasWeightMapping::gas_to_weight(T::Erc20TransferGasLimit::get(), true);

let exec_info = T::EvmRunner::call(
from,
erc20_contract_address,
Expand All @@ -98,6 +97,8 @@ pub mod pallet {
Default::default(),
false,
false,
Some(weight_limit),
Some(0),
&<T as pallet_evm::Config>::config(),
)
.map_err(|_| Erc20TransferError::EvmCallFail)?;
Expand Down
2 changes: 1 addition & 1 deletion pallets/ethereum-xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors = [ "Parity Technologies <admin@parity.io>" ]
description = "Xcm Transact compatibility for pallet-etherum."
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/paritytech/frontier/"
repository = "https://github.com/purestake/moonbeam/"
version = "1.0.0-dev"

[package.metadata.docs.rs]
Expand Down
22 changes: 22 additions & 0 deletions pallets/ethereum-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
fn transaction_len(transaction: &Transaction) -> u64 {
transaction
.encode()
.len()
// pallet + call indexes
.saturating_add(2) as u64
}

fn validate_and_apply(
source: H160,
xcm_transaction: EthereumXcmTransaction,
Expand All @@ -249,6 +257,18 @@ impl<T: Config> Pallet<T> {
if let Some(transaction) = transaction {
let transaction_data: TransactionData = (&transaction).into();

let (weight_limit, proof_size_base_cost) =
match <T as pallet_evm::Config>::GasWeightMapping::gas_to_weight(
transaction_data.gas_limit.unique_saturated_into(),
true,
) {
weight_limit if weight_limit.proof_size() > 0 => (
Some(weight_limit),
Some(Self::transaction_len(&transaction)),
),
_ => (None, None),
};

let _ = CheckEvmTransaction::<T::InvalidEvmTransactionError>::new(
CheckEvmTransactionConfig {
evm_config: T::config(),
Expand All @@ -262,6 +282,8 @@ impl<T: Config> Pallet<T> {
is_transactional: true,
},
transaction_data.into(),
weight_limit,
proof_size_base_cost,
)
// We only validate the gas limit against the evm transaction cost.
// No need to validate fee payment, as it is handled by the xcm executor.
Expand Down
Loading

0 comments on commit 16b1898

Please sign in to comment.