From 5a57c8b78c0e99f33ef8a5e791ae44af286fe17e Mon Sep 17 00:00:00 2001 From: Grigoriy Simonov Date: Thu, 7 Sep 2023 20:48:57 +0000 Subject: [PATCH 1/3] fix: clippy warnings --- node/cli/src/command.rs | 2 +- node/rpc/src/lib.rs | 2 +- pallets/common/src/benchmarking.rs | 5 ++++- pallets/identity/src/benchmarking.rs | 2 +- pallets/refungible/src/benchmarking.rs | 2 +- pallets/scheduler-v2/src/benchmarking.rs | 3 +-- pallets/structure/src/benchmarking.rs | 2 +- pallets/unique/src/benchmarking.rs | 4 ++-- runtime/common/tests/mod.rs | 2 +- 9 files changed, 13 insertions(+), 11 deletions(-) diff --git a/node/cli/src/command.rs b/node/cli/src/command.rs index c4be6cbc81..da2cddf72b 100644 --- a/node/cli/src/command.rs +++ b/node/cli/src/command.rs @@ -425,7 +425,7 @@ pub fn run() -> Result<()> { .map(|cfg| &cfg.registry); let task_manager = sc_service::TaskManager::new(runner.config().tokio_handle.clone(), *registry) - .map_err(|e| format!("Error: {:?}", e))?; + .map_err(|e| format!("Error: {e:?}"))?; let info_provider = Some(timestamp_with_aura_info(12000)); runner.async_run(|config| -> Result<(Pin>>, _)> { diff --git a/node/rpc/src/lib.rs b/node/rpc/src/lib.rs index 91a20977ae..c544c8f57f 100644 --- a/node/rpc/src/lib.rs +++ b/node/rpc/src/lib.rs @@ -162,7 +162,7 @@ where io.merge(Unique::new(client.clone()).into_rpc())?; - io.merge(AppPromotion::new(client.clone()).into_rpc())?; + io.merge(AppPromotion::new(client).into_rpc())?; #[cfg(feature = "pov-estimate")] io.merge( diff --git a/pallets/common/src/benchmarking.rs b/pallets/common/src/benchmarking.rs index ba2006dcf0..2b01a48b68 100644 --- a/pallets/common/src/benchmarking.rs +++ b/pallets/common/src/benchmarking.rs @@ -46,7 +46,10 @@ pub fn create_u16_data() -> BoundedVec> { .unwrap() } pub fn create_var_data(size: u32) -> BoundedVec> { - assert!(size <= S, "size ({size}) should be less within bound ({S})",); + assert!( + size <= S, + "size ({size}) should be less within bound ({S})" + ); (0..size) .map(|v| (v & 0xff) as u8) .collect::>() diff --git a/pallets/identity/src/benchmarking.rs b/pallets/identity/src/benchmarking.rs index 16d416229e..fa534e1851 100644 --- a/pallets/identity/src/benchmarking.rs +++ b/pallets/identity/src/benchmarking.rs @@ -199,7 +199,7 @@ benchmarks! { set_subs_new { let caller: T::AccountId = whitelisted_caller(); // Create a new subs vec with s sub accounts - let s in 0 .. T::MaxSubAccounts::get() => (); + let s in 0 .. T::MaxSubAccounts::get(); let subs = create_sub_accounts::(&caller, s)?; ensure!(SubsOf::::get(&caller).1.len() == 0, "Caller already has subs"); }: set_subs(RawOrigin::Signed(caller.clone()), subs) diff --git a/pallets/refungible/src/benchmarking.rs b/pallets/refungible/src/benchmarking.rs index 1e35cef12e..67efadaa05 100644 --- a/pallets/refungible/src/benchmarking.rs +++ b/pallets/refungible/src/benchmarking.rs @@ -294,7 +294,7 @@ benchmarks! { owner: sub; collection: collection(owner); sender: cross_from_sub(owner); owner: cross_sub; }; - let item = create_max_item(&collection, &sender, [(owner.clone(), 100)])?; + let item = create_max_item(&collection, &sender, [(owner, 100)])?; }: {>::token_owner(collection.id, item).unwrap()} set_allowance_for_all { diff --git a/pallets/scheduler-v2/src/benchmarking.rs b/pallets/scheduler-v2/src/benchmarking.rs index b70c086c65..4fb58b1eb1 100644 --- a/pallets/scheduler-v2/src/benchmarking.rs +++ b/pallets/scheduler-v2/src/benchmarking.rs @@ -136,8 +136,7 @@ fn make_call(maybe_lookup_len: Option) -> ScheduledCall { let bound = EncodedCall::bound() as u32; let mut len = match maybe_lookup_len { Some(len) => { - len.min(>::MaxSize::get() - 2) - .max(bound) - 3 + len.clamp(bound, >::MaxSize::get() - 2) - 3 } None => bound.saturating_sub(4), }; diff --git a/pallets/structure/src/benchmarking.rs b/pallets/structure/src/benchmarking.rs index 915513d01a..178b7b9d2e 100644 --- a/pallets/structure/src/benchmarking.rs +++ b/pallets/structure/src/benchmarking.rs @@ -43,7 +43,7 @@ benchmarks! { let dispatch = T::CollectionDispatch::dispatch(CollectionId(1))?; let dispatch = dispatch.as_dyn(); - dispatch.create_item(caller_cross.clone(), caller_cross.clone(), CreateItemData::NFT(CreateNftData::default()), &Unlimited)?; + dispatch.create_item(caller_cross.clone(), caller_cross, CreateItemData::NFT(CreateNftData::default()), &Unlimited)?; }: { let parent = >::find_parent(CollectionId(1), TokenId(1))?; assert!(matches!(parent, Parent::User(_))) diff --git a/pallets/unique/src/benchmarking.rs b/pallets/unique/src/benchmarking.rs index 0615d28bfa..1d182a1781 100644 --- a/pallets/unique/src/benchmarking.rs +++ b/pallets/unique/src/benchmarking.rs @@ -70,7 +70,7 @@ benchmarks! { let mode: CollectionMode = CollectionMode::NFT; let caller: T::AccountId = account("caller", 0, SEED); let _ = ::Currency::deposit(&caller, T::CollectionCreationPrice::get(), Precision::Exact).unwrap(); - }: _(RawOrigin::Signed(caller.clone()), col_name.clone(), col_desc.clone(), token_prefix.clone(), mode) + }: _(RawOrigin::Signed(caller.clone()), col_name, col_desc, token_prefix, mode) verify { assert_eq!(>::get(CollectionId(1)).unwrap().owner, caller); } @@ -155,6 +155,6 @@ benchmarks! { force_repair_collection { let caller: T::AccountId = account("caller", 0, SEED); - let collection = create_nft_collection::(caller.clone())?; + let collection = create_nft_collection::(caller)?; }: _(RawOrigin::Root, collection) } diff --git a/runtime/common/tests/mod.rs b/runtime/common/tests/mod.rs index 44eebea8b4..da9cb73104 100644 --- a/runtime/common/tests/mod.rs +++ b/runtime/common/tests/mod.rs @@ -33,7 +33,7 @@ const PARA_ID: u32 = 2095; const PARA_ID: u32 = 2037; fn get_from_seed(seed: &str) -> ::Public { - TPublic::Pair::from_string(&format!("//{}", seed), None) + TPublic::Pair::from_string(&format!("//{seed}"), None) .expect("static values are valid; qed") .public() } From c3d8db80c61d14e28cb8eb08d4d9678daa4318df Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Thu, 7 Sep 2023 20:48:57 +0000 Subject: [PATCH 2/3] feat: Move sponsorship from frontier --- Cargo.lock | 36 ++-- Cargo.toml | 26 +-- pallets/common/src/benchmarking.rs | 5 +- pallets/evm-transaction-payment/Cargo.toml | 1 + pallets/evm-transaction-payment/src/lib.rs | 222 +++++++++++++++++++-- pallets/scheduler-v2/src/benchmarking.rs | 5 +- runtime/common/config/ethereum.rs | 6 +- tests/src/eth/contractSponsoring.test.ts | 2 +- 8 files changed, 242 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e33f307ab..f85495bc93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2835,7 +2835,7 @@ dependencies = [ [[package]] name = "fc-consensus" version = "2.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "async-trait", "fp-consensus", @@ -2851,7 +2851,7 @@ dependencies = [ [[package]] name = "fc-db" version = "2.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "async-trait", "fp-storage", @@ -2871,7 +2871,7 @@ dependencies = [ [[package]] name = "fc-mapping-sync" version = "2.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "fc-db", "fc-storage", @@ -2892,7 +2892,7 @@ dependencies = [ [[package]] name = "fc-rpc" version = "2.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "ethereum", "ethereum-types", @@ -2942,7 +2942,7 @@ dependencies = [ [[package]] name = "fc-rpc-core" version = "1.1.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "ethereum", "ethereum-types", @@ -2955,7 +2955,7 @@ dependencies = [ [[package]] name = "fc-storage" version = "1.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "ethereum", "ethereum-types", @@ -3107,7 +3107,7 @@ dependencies = [ [[package]] name = "fp-account" version = "1.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "hex", "impl-serde", @@ -3126,7 +3126,7 @@ dependencies = [ [[package]] name = "fp-consensus" version = "2.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "ethereum", "parity-scale-codec", @@ -3138,7 +3138,7 @@ dependencies = [ [[package]] name = "fp-ethereum" version = "1.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "ethereum", "ethereum-types", @@ -3152,11 +3152,10 @@ dependencies = [ [[package]] name = "fp-evm" version = "3.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "evm", "frame-support", - "impl-trait-for-tuples", "parity-scale-codec", "scale-info", "serde", @@ -3168,7 +3167,7 @@ dependencies = [ [[package]] name = "fp-rpc" version = "3.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "ethereum", "ethereum-types", @@ -3185,7 +3184,7 @@ dependencies = [ [[package]] name = "fp-self-contained" version = "1.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "frame-support", "parity-scale-codec", @@ -3197,7 +3196,7 @@ dependencies = [ [[package]] name = "fp-storage" version = "2.0.0" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "parity-scale-codec", "serde", @@ -6367,7 +6366,7 @@ dependencies = [ [[package]] name = "pallet-base-fee" version = "1.0.0" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "fp-evm", "frame-support", @@ -6634,7 +6633,7 @@ dependencies = [ [[package]] name = "pallet-ethereum" version = "4.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "ethereum", "ethereum-types", @@ -6657,7 +6656,7 @@ dependencies = [ [[package]] name = "pallet-evm" version = "6.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "environmental", "evm", @@ -6737,7 +6736,7 @@ dependencies = [ [[package]] name = "pallet-evm-precompile-simple" version = "2.0.0-dev" -source = "git+https://github.com/uniquenetwork/unique-frontier?branch=unique-polkadot-v0.9.43#3ae25048cce709349b242e8ad3c54ada2b321564" +source = "git+https://github.com/uniquenetwork/unique-frontier?branch=patches#c87f5486862ec82e6668ad2ca22361086a1444c1" dependencies = [ "fp-evm", "ripemd", @@ -6754,6 +6753,7 @@ dependencies = [ "pallet-evm", "parity-scale-codec", "scale-info", + "sp-arithmetic", "sp-core", "sp-runtime", "sp-std", diff --git a/Cargo.toml b/Cargo.toml index a942d9877f..1fbe6593b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,19 +68,19 @@ quartz-runtime = { path = "runtime/quartz" } unique-runtime = { path = "runtime/unique" } # Frontier (Unique patches over the Parity version) -fc-consensus = { git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -fc-db = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -fc-mapping-sync = { git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -fc-rpc = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -fc-rpc-core = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -fp-rpc = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -fp-self-contained = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -fp-storage = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -pallet-base-fee = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } -pallet-evm-precompile-simple = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "unique-polkadot-v0.9.43" } +fc-consensus = { git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +fc-db = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +fc-mapping-sync = { git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +fc-rpc = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +fc-rpc-core = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +fp-rpc = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +fp-self-contained = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +fp-storage = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +pallet-base-fee = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } +pallet-evm-precompile-simple = { default-features = false, git = "https://github.com/uniquenetwork/unique-frontier", branch = "patches" } # Parity codec = { default-features = false, features = ['derive'], package = 'parity-scale-codec', version = "3.2.2" } diff --git a/pallets/common/src/benchmarking.rs b/pallets/common/src/benchmarking.rs index 2b01a48b68..06d3353bd5 100644 --- a/pallets/common/src/benchmarking.rs +++ b/pallets/common/src/benchmarking.rs @@ -46,10 +46,7 @@ pub fn create_u16_data() -> BoundedVec> { .unwrap() } pub fn create_var_data(size: u32) -> BoundedVec> { - assert!( - size <= S, - "size ({size}) should be less within bound ({S})" - ); + assert!(size <= S, "size ({size}) should be less within bound ({S})"); (0..size) .map(|v| (v & 0xff) as u8) .collect::>() diff --git a/pallets/evm-transaction-payment/Cargo.toml b/pallets/evm-transaction-payment/Cargo.toml index 7a46caf796..a339d7a363 100644 --- a/pallets/evm-transaction-payment/Cargo.toml +++ b/pallets/evm-transaction-payment/Cargo.toml @@ -14,6 +14,7 @@ fp-evm = { workspace = true } frame-support = { workspace = true } frame-system = { workspace = true } pallet-evm = { workspace = true } +sp-arithmetic = { workspace = true } sp-core = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } diff --git a/pallets/evm-transaction-payment/src/lib.rs b/pallets/evm-transaction-payment/src/lib.rs index 184aa25850..739a92330e 100644 --- a/pallets/evm-transaction-payment/src/lib.rs +++ b/pallets/evm-transaction-payment/src/lib.rs @@ -19,13 +19,20 @@ #![deny(missing_docs)] use core::marker::PhantomData; -use fp_evm::WithdrawReason; -use frame_support::traits::IsSubType; +use fp_evm::{CheckEvmTransaction, FeeCalculator, WithdrawReason, InvalidEvmTransactionError}; +use frame_support::{ + storage::with_transaction, + traits::{IsSubType, Currency, Imbalance, OnUnbalanced}, +}; pub use pallet::*; -use pallet_evm::{account::CrossAccountId, EnsureAddressOrigin}; +use pallet_evm::{ + account::CrossAccountId, EnsureAddressOrigin, OnCheckEvmTransaction, OnChargeEVMTransaction, + NegativeImbalanceOf, +}; use sp_core::{H160, U256}; use sp_runtime::{TransactionOutcome, DispatchError}; use up_sponsorship::SponsorshipHandler; +use sp_arithmetic::traits::UniqueSaturatedInto; #[frame_support::pallet] pub mod pallet { @@ -53,29 +60,63 @@ pub mod pallet { pub struct Pallet(_); } -/// Implements [`fp_evm::TransactionValidityHack`], which provides sponsor address to pallet-evm -pub struct TransactionValidityHack(PhantomData<*const T>); -impl fp_evm::TransactionValidityHack for TransactionValidityHack { - fn who_pays_fee( - origin: H160, - max_fee: U256, - reason: &WithdrawReason, - ) -> Option { - match reason { - WithdrawReason::Call { target, input } => { - let origin_sub = T::CrossAccountId::from_eth(origin); - let call_context = CallContext { - contract_address: *target, - input: input.clone(), - max_fee, - }; - T::EvmSponsorshipHandler::get_sponsor(&origin_sub, &call_context) - } - _ => None, +fn who_pays_fee( + origin: H160, + max_fee: U256, + reason: &WithdrawReason, +) -> Option { + match reason { + WithdrawReason::Call { target, input, .. } => { + let origin_sub = T::CrossAccountId::from_eth(origin); + let call_context = CallContext { + contract_address: *target, + input: input.clone(), + max_fee, + }; + T::EvmSponsorshipHandler::get_sponsor(&origin_sub, &call_context) } + _ => None, } } +fn get_sponsor( + source: H160, + max_fee_per_gas: Option, + gas_limit: U256, + reason: &WithdrawReason, + is_transactional: bool, + is_check: bool, +) -> Option { + let accept_gas_fee = |gas_fee| { + let (base_fee, _) = T::FeeCalculator::min_gas_price(); + base_fee <= gas_fee && gas_fee <= base_fee * 21 / 10 + }; + let (max_fee_per_gas, may_sponsor) = match (max_fee_per_gas, is_transactional) { + (Some(max_fee_per_gas), _) => (max_fee_per_gas, accept_gas_fee(max_fee_per_gas)), + // Gas price check is skipped for non-transactional calls that don't + // define a `max_fee_per_gas` input. + (None, false) => (Default::default(), true), + _ => return None, + }; + + let max_fee = max_fee_per_gas.saturating_mul(gas_limit); + + // #[cfg(feature = "debug-logging")] + // log::trace!(target: "sponsoring", "checking who will pay fee for {:?} {:?}", source, reason); + with_transaction(|| { + let result = may_sponsor + .then(|| who_pays_fee::(source, max_fee, reason)) + .flatten(); + if is_check { + TransactionOutcome::Rollback(Ok::<_, DispatchError>(result)) + } else { + TransactionOutcome::Commit(Ok(result)) + } + }) + .ok() + .flatten() +} + /// Implements sponsoring for evm calls performed from pallet-evm (via api.tx.ethereum.transact/api.tx.evm.call) pub struct BridgeSponsorshipHandler(PhantomData); impl SponsorshipHandler for BridgeSponsorshipHandler @@ -120,3 +161,140 @@ where } } } + +/// Bla bla bla Mr. Freeman +pub struct TransactionValidity>( + PhantomData<(T, E)>, +); +impl> OnCheckEvmTransaction + for TransactionValidity +{ + fn on_check_evm_transaction( + v: &mut CheckEvmTransaction, + origin: &T::CrossAccountId, + ) -> Result<(), E> { + let who = &v.who; + let max_fee_per_gas = Some(v.transaction_fee_input()?.0); + let gas_limit = v.transaction.gas_limit; + let reason = if let Some(to) = v.transaction.to { + WithdrawReason::Call { + target: to, + input: v.transaction.input.clone(), + max_fee_per_gas, + gas_limit, + is_transactional: v.config.is_transactional, + is_check: true, + } + } else { + WithdrawReason::Create + }; + let sponsor = get_sponsor::( + *origin.as_eth(), + max_fee_per_gas, + gas_limit, + &reason, + v.config.is_transactional, + true, + ) + .as_ref() + .map(pallet_evm::Pallet::::account_basic_by_id) + .map(|v| v.0); + + let fee = max_fee_per_gas + .unwrap() + .saturating_mul(v.transaction.gas_limit); + if let Some(sponsor) = sponsor.as_ref() { + if who.balance < v.transaction.value || sponsor.balance < fee { + return Err(InvalidEvmTransactionError::BalanceTooLow.into()); + } + } else { + let total_payment = v.transaction.value.saturating_add(fee); + if who.balance < total_payment { + return Err(InvalidEvmTransactionError::BalanceTooLow.into()); + } + } + + let who = sponsor.unwrap_or_else(|| v.who.clone()); + v.who.balance = who.balance; + Ok(()) + } +} + +/// Implements the transaction payment for a pallet implementing the `Currency` +/// trait (eg. the pallet_balances) using an unbalance handler (implementing +/// `OnUnbalanced`). +/// Similar to `CurrencyAdapter` of `pallet_transaction_payment` +pub struct WrappedEVMCurrencyAdapter(sp_std::marker::PhantomData<(C, OU)>); +impl OnChargeEVMTransaction for WrappedEVMCurrencyAdapter +where + T: Config, + C: Currency<::AccountId>, + C::PositiveImbalance: Imbalance< + ::AccountId>>::Balance, + Opposite = C::NegativeImbalance, + >, + C::NegativeImbalance: Imbalance< + ::AccountId>>::Balance, + Opposite = C::PositiveImbalance, + >, + OU: OnUnbalanced>, + U256: UniqueSaturatedInto<::AccountId>>::Balance>, +{ + // Kept type as Option to satisfy bound of Default + type LiquidityInfo = (Option>, Option); + + fn withdraw_fee( + who: &T::CrossAccountId, + reason: WithdrawReason, + fee: U256, + ) -> Result> { + let sponsor = match reason { + WithdrawReason::Call { + max_fee_per_gas, + gas_limit, + is_transactional, + is_check, + .. + } => get_sponsor::( + *who.as_eth(), + max_fee_per_gas, + gas_limit, + &reason, + is_transactional, + is_check, + ), + _ => None, + }; + + let who = sponsor.as_ref().unwrap_or(who); + as OnChargeEVMTransaction>::withdraw_fee( + who, reason, fee, + ) + .map(|li| (li, sponsor)) + } + + fn correct_and_deposit_fee( + who: &T::CrossAccountId, + corrected_fee: U256, + base_fee: U256, + already_withdrawn: Self::LiquidityInfo, + ) -> Self::LiquidityInfo { + let (already_withdrawn, sponsor) = already_withdrawn; + let who = sponsor.as_ref().unwrap_or(who); + ( + as OnChargeEVMTransaction>::correct_and_deposit_fee( + who, + corrected_fee, + base_fee, + already_withdrawn, + ), + None + ) + } + + fn pay_priority_fee(tip: Self::LiquidityInfo) { + as OnChargeEVMTransaction>::pay_priority_fee( + tip.0, + ) + } +} diff --git a/pallets/scheduler-v2/src/benchmarking.rs b/pallets/scheduler-v2/src/benchmarking.rs index 4fb58b1eb1..6ef47aaf7a 100644 --- a/pallets/scheduler-v2/src/benchmarking.rs +++ b/pallets/scheduler-v2/src/benchmarking.rs @@ -136,7 +136,10 @@ fn make_call(maybe_lookup_len: Option) -> ScheduledCall { let bound = EncodedCall::bound() as u32; let mut len = match maybe_lookup_len { Some(len) => { - len.clamp(bound, >::MaxSize::get() - 2) - 3 + len.clamp( + bound, + >::MaxSize::get() - 2, + ) - 3 } None => bound.saturating_sub(4), }; diff --git a/runtime/common/config/ethereum.rs b/runtime/common/config/ethereum.rs index b70e875ccc..85b3593e7d 100644 --- a/runtime/common/config/ethereum.rs +++ b/runtime/common/config/ethereum.rs @@ -87,12 +87,14 @@ impl pallet_evm::Config for Runtime { type OnCreate = pallet_evm_contract_helpers::HelpersOnCreate; type ChainId = ChainId; type Runner = pallet_evm::runner::stack::Runner; - type OnChargeTransaction = pallet_evm::EVMCurrencyAdapter; - type TransactionValidityHack = pallet_evm_transaction_payment::TransactionValidityHack; + type OnChargeTransaction = + pallet_evm_transaction_payment::WrappedEVMCurrencyAdapter; type FindAuthor = EthereumFindAuthor; type Timestamp = crate::Timestamp; type WeightInfo = pallet_evm::weights::SubstrateWeight; type GasLimitPovSizeRatio = ProofSizePerGas; + type OnCheckEvmTransaction> = + pallet_evm_transaction_payment::TransactionValidity; } impl pallet_evm_migration::Config for Runtime { diff --git a/tests/src/eth/contractSponsoring.test.ts b/tests/src/eth/contractSponsoring.test.ts index 55d073c56b..91a7e8dfb0 100644 --- a/tests/src/eth/contractSponsoring.test.ts +++ b/tests/src/eth/contractSponsoring.test.ts @@ -482,7 +482,7 @@ describe('Sponsoring Fee Limit', () => { before(async () => { await usingEthPlaygrounds(async (helper, privateKey) => { donor = await privateKey({url: import.meta.url}); - [alice] = await helper.arrange.createAccounts([100n], donor); + [alice] = await helper.arrange.createAccounts([1000n], donor); }); }); From 78ef2a9b6ad23c72781e7d586751503e810cbb50 Mon Sep 17 00:00:00 2001 From: Grigoriy Simonov Date: Thu, 7 Sep 2023 21:12:32 +0000 Subject: [PATCH 3/3] fix: runtime tests --- Cargo.lock | 1 + pallets/common/src/eth.rs | 2 +- runtime/tests/Cargo.toml | 2 ++ runtime/tests/src/lib.rs | 2 +- tests/src/util/globalSetup.ts | 2 +- tests/src/xcm/xcmQuartz.test.ts | 6 ++---- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f85495bc93..1861552fd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13129,6 +13129,7 @@ name = "tests" version = "0.1.1" dependencies = [ "evm-coder", + "fp-evm", "frame-support", "frame-system", "pallet-balances", diff --git a/pallets/common/src/eth.rs b/pallets/common/src/eth.rs index 2b659a2c15..ba56ac4314 100644 --- a/pallets/common/src/eth.rs +++ b/pallets/common/src/eth.rs @@ -119,7 +119,7 @@ impl CrossAddress { } else if self.sub == Default::default() { Ok(Some(T::CrossAccountId::from_eth(self.eth))) } else { - Err(format!("All fields of cross account is non zeroed {:?}", self).into()) + Err(format!("All fields of cross account is non zeroed {self:?}").into()) } } diff --git a/runtime/tests/Cargo.toml b/runtime/tests/Cargo.toml index 98ed01c751..8b2806b4bb 100644 --- a/runtime/tests/Cargo.toml +++ b/runtime/tests/Cargo.toml @@ -16,6 +16,8 @@ sp-io = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } +fp-evm = { workspace = true } + frame-support = { workspace = true } frame-system = { workspace = true } pallet-xcm = { workspace = true } diff --git a/runtime/tests/src/lib.rs b/runtime/tests/src/lib.rs index 0ec974d0f1..21df62235a 100644 --- a/runtime/tests/src/lib.rs +++ b/runtime/tests/src/lib.rs @@ -242,9 +242,9 @@ impl pallet_evm::Config for Test { type OnChargeTransaction = (); type FindAuthor = (); type BlockHashMapping = SubstrateBlockHashMapping; - type TransactionValidityHack = (); type Timestamp = Timestamp; type GasLimitPovSizeRatio = ConstU64<0>; + type OnCheckEvmTransaction> = (); } impl pallet_evm_coder_substrate::Config for Test {} diff --git a/tests/src/util/globalSetup.ts b/tests/src/util/globalSetup.ts index 81bf61f65b..94584f4d78 100644 --- a/tests/src/util/globalSetup.ts +++ b/tests/src/util/globalSetup.ts @@ -115,5 +115,5 @@ const fundFilenamesWithRetries = (retriesLeft: number): Promise => { globalSetup().catch(e => { console.error('Setup error'); console.error(e); - process.exit(1) + process.exit(1); }); diff --git a/tests/src/xcm/xcmQuartz.test.ts b/tests/src/xcm/xcmQuartz.test.ts index 15bed8cac3..354dbfb6eb 100644 --- a/tests/src/xcm/xcmQuartz.test.ts +++ b/tests/src/xcm/xcmQuartz.test.ts @@ -688,10 +688,8 @@ describeXCM('[XCM] Integration test: Exchanging tokens with Karura', () => { maliciousXcmProgramSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent); }); - await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.Fail, event => { - return event.messageHash == maliciousXcmProgramSent.messageHash - && event.outcome.isFailedToTransactAsset; - }); + await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.Fail, event => event.messageHash == maliciousXcmProgramSent.messageHash + && event.outcome.isFailedToTransactAsset); targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address); expect(targetAccountBalance).to.be.equal(0n);