From 33b7cd6618bf63ddbfee7933d7ac8976e0e7eb38 Mon Sep 17 00:00:00 2001 From: bear Date: Mon, 12 Aug 2024 16:49:48 +0800 Subject: [PATCH] Code clean --- Cargo.lock | 1 - Cargo.toml | 2 +- frame/ethereum/Cargo.toml | 6 +-- frame/ethereum/src/lib.rs | 11 +++-- frame/ethereum/src/tests/legacy.rs | 1 - frame/evm/Cargo.toml | 3 +- frame/evm/src/lib.rs | 6 +-- frame/evm/src/runner/stack.rs | 2 +- frame/evm/src/tests.rs | 9 ++-- primitives/evm/Cargo.toml | 6 ++- primitives/evm/src/lib.rs | 23 ++-------- primitives/evm/src/validation.rs | 4 +- template/node/Cargo.toml | 5 +-- template/node/src/chain_spec.rs | 1 - template/runtime/Cargo.toml | 3 +- template/runtime/src/lib.rs | 69 ++++++++++++++++-------------- 16 files changed, 70 insertions(+), 82 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 98f918f8e6..7818dd80bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6793,7 +6793,6 @@ dependencies = [ "frame-system", "hex", "libsecp256k1", - "log", "pallet-balances", "pallet-evm", "pallet-timestamp", diff --git a/Cargo.toml b/Cargo.toml index ce6cb52f73..50446ee82b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -127,9 +127,9 @@ sp-session = { git = "https://github.com/paritytech/polkadot-sdk", branch = "sta sp-state-machine = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } sp-std = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } sp-storage = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } -sp-trie = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } sp-timestamp = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } sp-transaction-pool = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } +sp-trie = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } sp-version = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } sp-weights = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2407", default-features = false } # Substrate FRAME diff --git a/frame/ethereum/Cargo.toml b/frame/ethereum/Cargo.toml index 1303d30576..0691af3000 100644 --- a/frame/ethereum/Cargo.toml +++ b/frame/ethereum/Cargo.toml @@ -21,6 +21,8 @@ frame-support = { workspace = true } frame-system = { workspace = true } sp-io = { workspace = true } sp-runtime = { workspace = true } +# Cumulus +cumulus-primitives-storage-weight-reclaim = { workspace = true } # Frontier fp-consensus = { workspace = true } fp-ethereum = { workspace = true } @@ -28,8 +30,6 @@ fp-evm = { workspace = true } fp-rpc = { workspace = true } fp-storage = { workspace = true } pallet-evm = { workspace = true } -cumulus-primitives-storage-weight-reclaim = { workspace = true } -log = { workspace = true } [dev-dependencies] hex = { workspace = true } @@ -51,9 +51,9 @@ std = [ "evm/std", "ethereum-types/std", "rlp/std", - "log/std", "scale-codec/std", "scale-info/std", + # Cumulus "cumulus-primitives-storage-weight-reclaim/std", # Substrate "frame-support/std", diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 83a1a73501..39bee9b588 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -367,12 +367,11 @@ impl Pallet { transaction_data.gas_limit.unique_saturated_into(), true, ); - let proof_size_pre_execution = cumulus_primitives_storage_weight_reclaim::get_proof_size(); - Some(TransactionPov::new( - weight_limit, - extrinsics_len, - proof_size_pre_execution, - )) + cumulus_primitives_storage_weight_reclaim::get_proof_size().map( + |proof_size_pre_execution| { + TransactionPov::new(weight_limit, extrinsics_len, proof_size_pre_execution) + }, + ) } fn recover_signer(transaction: &Transaction) -> Option { diff --git a/frame/ethereum/src/tests/legacy.rs b/frame/ethereum/src/tests/legacy.rs index 9fc504d10e..40ff35b882 100644 --- a/frame/ethereum/src/tests/legacy.rs +++ b/frame/ethereum/src/tests/legacy.rs @@ -478,7 +478,6 @@ fn validated_transaction_apply_zero_gas_price_works() { }); } - #[test] fn proof_size_base_cost_should_keep_the_same_in_execution_and_estimate() { let (pairs, mut ext) = new_test_ext(1); diff --git a/frame/evm/Cargo.toml b/frame/evm/Cargo.toml index 348f6ed5ec..bea8ec6d37 100644 --- a/frame/evm/Cargo.toml +++ b/frame/evm/Cargo.toml @@ -37,9 +37,10 @@ hex = { workspace = true } pallet-balances = { workspace = true, features = ["default", "insecure_zero_ed"] } pallet-evm-precompile-simple = { workspace = true, features = ["default"] } pallet-timestamp = { workspace = true, features = ["default"] } +sp-state-machine = { workspace = true } sp-trie = { workspace = true, features = ["default"] } +# Cumulus cumulus-primitives-storage-weight-reclaim = { workspace = true } -sp-state-machine = { workspace = true } [features] default = ["std"] diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 4fb140abb3..f0ff4f52d9 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -477,8 +477,6 @@ pub mod pallet { #[pallet::error] pub enum Error { - /// Proof size limit is too low - ProofLimitTooLow, /// Not enough balance to perform action BalanceLow, /// Calculating total fee overflowed @@ -503,6 +501,8 @@ pub mod pallet { Reentrancy, /// EIP-3607, TransactionMustComeFromEOA, + /// Proof size limit is too low + ProofLimitTooLow, /// Undefined error. Undefined, } @@ -510,7 +510,6 @@ pub mod pallet { impl From for Error { fn from(validation_error: TransactionValidationError) -> Self { match validation_error { - TransactionValidationError::ProofLimitTooLow => Error::::ProofLimitTooLow, TransactionValidationError::GasLimitTooLow => Error::::GasLimitTooLow, TransactionValidationError::GasLimitTooHigh => Error::::GasLimitTooHigh, TransactionValidationError::BalanceTooLow => Error::::BalanceLow, @@ -521,6 +520,7 @@ pub mod pallet { TransactionValidationError::InvalidFeeInput => Error::::GasPriceTooLow, TransactionValidationError::InvalidChainId => Error::::InvalidChainId, TransactionValidationError::InvalidSignature => Error::::InvalidSignature, + TransactionValidationError::ProofLimitTooLow => Error::::ProofLimitTooLow, TransactionValidationError::UnknownError => Error::::Undefined, } } diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index c17f9f373a..4677a1c78a 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -309,7 +309,7 @@ where standard: opcode_used_gas.into(), effective: used_gas, }, - weight_info: transaction_pov.map(|pov| WeightInfo::from_transaction_pov(pov)), + weight_info: transaction_pov.map(WeightInfo::from_transaction_pov), logs: state.substate.logs, }) } diff --git a/frame/evm/src/tests.rs b/frame/evm/src/tests.rs index 65ee6810eb..144a7f92df 100644 --- a/frame/evm/src/tests.rs +++ b/frame/evm/src/tests.rs @@ -832,7 +832,7 @@ fn proof_size_reach_limit() { None, None, Vec::new(), - true, // transactional + true, // transactional false, // must be validated Some(transaction_pov), &::config().clone(), @@ -853,7 +853,7 @@ fn proof_size_reach_limit() { None, None, Vec::new(), - true, // transactional + true, // transactional false, // must be validated Some(transaction_pov), &::config().clone(), @@ -889,6 +889,9 @@ fn proof_size_reach_limit_nonce_increase() { ) .expect("create contract failed"); assert_eq!(res.exit_reason, ExitReason::Error(ExitError::OutOfGas)); - assert_eq!(EVM::account_basic(&H160::default()).0.nonce, original_nonce + 1); + assert_eq!( + EVM::account_basic(&H160::default()).0.nonce, + original_nonce + 1 + ); }); } diff --git a/primitives/evm/Cargo.toml b/primitives/evm/Cargo.toml index 43285d6dbf..9ddbdf08b1 100644 --- a/primitives/evm/Cargo.toml +++ b/primitives/evm/Cargo.toml @@ -16,11 +16,12 @@ num_enum = { workspace = true, default-features = false } scale-codec = { package = "parity-scale-codec", workspace = true } scale-info = { workspace = true } serde = { workspace = true, optional = true } -cumulus-primitives-storage-weight-reclaim = { workspace = true } # Substrate frame-support = { workspace = true } sp-core = { workspace = true } sp-runtime = { workspace = true } +# Cumulus +cumulus-primitives-storage-weight-reclaim = { workspace = true } [features] default = ["std"] @@ -33,8 +34,9 @@ std = [ # Substrate "frame-support/std", "sp-core/std", - "cumulus-primitives-storage-weight-reclaim/std", "sp-runtime/std", + # Cumulus + "cumulus-primitives-storage-weight-reclaim/std", ] serde = [ "dep:serde", diff --git a/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index a4155c272e..05d0e91d4a 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -61,29 +61,20 @@ pub struct Vicinity { /// `System::Account` 16(hash) + 20 (key) + 60 (AccountInfo::max_encoded_len) pub const ACCOUNT_BASIC_PROOF_SIZE: u64 = 96; -/// `AccountCodesMetadata` read, temptatively 16 (hash) + 20 (key) + 40 (CodeMetadata). -pub const ACCOUNT_CODES_METADATA_PROOF_SIZE: u64 = 76; /// 16 (hash1) + 20 (key1) + 16 (hash2) + 32 (key2) + 32 (value) pub const ACCOUNT_STORAGE_PROOF_SIZE: u64 = 116; -/// Fixed trie 32 byte hash. -pub const WRITE_PROOF_SIZE: u64 = 32; -/// Account basic proof size + 5 bytes max of `decode_len` call. -pub const IS_EMPTY_CHECK_PROOF_SIZE: u64 = 93; + #[derive(Clone, Copy, Eq, PartialEq, Debug, Encode, Decode, TypeInfo)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TransactionPov { pub weight_limit: Weight, pub extrinsics_len: u64, - pub proof_size_pre_execution: Option, + pub proof_size_pre_execution: u64, } impl TransactionPov { - pub fn new( - weight_limit: Weight, - extrinsics_len: u64, - proof_size_pre_execution: Option, - ) -> Self { + pub fn new(weight_limit: Weight, extrinsics_len: u64, proof_size_pre_execution: u64) -> Self { Self { weight_limit, extrinsics_len, @@ -92,12 +83,6 @@ impl TransactionPov { } pub fn proof_size_used(&self) -> u64 { - // if we don't have proof_size_pre_execution, that means that we don't care about the tx proof size - let Some(proof_size_pre_execution) = self.proof_size_pre_execution else { - return 0; - }; - - // If proof_size_pre_execution is enabled, then proof_size_post_execution should also be enabled; otherwise, something is break. let Some(proof_size_post_execution) = cumulus_primitives_storage_weight_reclaim::get_proof_size() else { @@ -105,7 +90,7 @@ impl TransactionPov { }; proof_size_post_execution - .saturating_sub(proof_size_pre_execution) + .saturating_sub(self.proof_size_pre_execution) .saturating_add(self.extrinsics_len) } } diff --git a/primitives/evm/src/validation.rs b/primitives/evm/src/validation.rs index 335d538c3f..e12ec083ff 100644 --- a/primitives/evm/src/validation.rs +++ b/primitives/evm/src/validation.rs @@ -248,7 +248,6 @@ mod tests { #[derive(Debug, PartialEq)] pub enum TestError { - ProofLimitTooLow, GasLimitTooLow, GasLimitTooHigh, GasPriceTooLow, @@ -259,6 +258,7 @@ mod tests { InvalidFeeInput, InvalidChainId, InvalidSignature, + ProofLimitTooLow, UnknownError, } @@ -267,7 +267,6 @@ mod tests { impl From for TestError { fn from(e: TransactionValidationError) -> Self { match e { - TransactionValidationError::ProofLimitTooLow => TestError::ProofLimitTooLow, TransactionValidationError::GasLimitTooLow => TestError::GasLimitTooLow, TransactionValidationError::GasLimitTooHigh => TestError::GasLimitTooHigh, TransactionValidationError::GasPriceTooLow => TestError::GasPriceTooLow, @@ -278,6 +277,7 @@ mod tests { TransactionValidationError::InvalidFeeInput => TestError::InvalidFeeInput, TransactionValidationError::InvalidChainId => TestError::InvalidChainId, TransactionValidationError::InvalidSignature => TestError::InvalidSignature, + TransactionValidationError::ProofLimitTooLow => TestError::ProofLimitTooLow, TransactionValidationError::UnknownError => TestError::UnknownError, } } diff --git a/template/node/Cargo.toml b/template/node/Cargo.toml index 20225389e5..4ba8c42ef3 100644 --- a/template/node/Cargo.toml +++ b/template/node/Cargo.toml @@ -21,9 +21,6 @@ jsonrpsee = { workspace = true, features = ["server", "macros"] } log = { workspace = true } scale-codec = { package = "parity-scale-codec", workspace = true } serde_json = { workspace = true, features = ["arbitrary_precision"] } - -cumulus-client-service = { workspace = true } - # Substrate prometheus-endpoint = { package = "substrate-prometheus-endpoint", workspace = true } sc-basic-authorship = { workspace = true } @@ -57,6 +54,8 @@ sp-runtime = { workspace = true, features = ["default"] } sp-session = { workspace = true, features = ["default"] } sp-timestamp = { workspace = true, features = ["default"] } sp-transaction-pool = { workspace = true, features = ["default"] } +# Cumulus +cumulus-client-service = { workspace = true } # These dependencies are used for RPC frame-system-rpc-runtime-api = { workspace = true } pallet-transaction-payment-rpc = { workspace = true } diff --git a/template/node/src/chain_spec.rs b/template/node/src/chain_spec.rs index e9b290bd77..614161fa35 100644 --- a/template/node/src/chain_spec.rs +++ b/template/node/src/chain_spec.rs @@ -69,7 +69,6 @@ pub fn development_config(enable_manual_seal: bool) -> ChainSpec { AccountId::from(hex!("773539d4Ac0e786233D90A233654ccEE26a613D9")), // Dorothy AccountId::from(hex!("Ff64d3F6efE2317EE2807d223a0Bdc4c0c49dfDB")), // Ethan AccountId::from(hex!("C0F0f4ab324C46e55D02D0033343B4Be8A55532d")), // Faith - AccountId::from(hex!("6Bc9543094D17f52CF6b419FB692797E48d275d0")), // BEAR ], // Initial PoA authorities vec![authority_keys_from_seed("Alice")], diff --git a/template/runtime/Cargo.toml b/template/runtime/Cargo.toml index f8f89d30c8..e9042a0bd4 100644 --- a/template/runtime/Cargo.toml +++ b/template/runtime/Cargo.toml @@ -42,9 +42,8 @@ pallet-sudo = { workspace = true } pallet-timestamp = { workspace = true } pallet-transaction-payment = { workspace = true } pallet-transaction-payment-rpc-runtime-api = { workspace = true } - +# Cumulus cumulus-primitives-storage-weight-reclaim = { workspace = true } - # Frontier fp-account = { workspace = true, features = ["serde"] } fp-evm = { workspace = true, features = ["serde"] } diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 1cd35cb1fc..2e6b473d97 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -806,37 +806,37 @@ impl_runtime_apis! { None }; - // Estimated encoded transaction size must be based on the heaviest transaction - // type (EIP1559Transaction) to be compatible with all transaction types. - let mut estimated_transaction_len = data.len() + - // pallet ethereum index: 1 - // transact call index: 1 - // Transaction enum variant: 1 - // chain_id 8 bytes - // nonce: 32 - // max_priority_fee_per_gas: 32 - // max_fee_per_gas: 32 - // gas_limit: 32 - // action: 21 (enum varianrt + call address) - // value: 32 - // access_list: 1 (empty vec size) - // 65 bytes signature - 258; - - if access_list.is_some() { - estimated_transaction_len += access_list.encoded_size(); - } - - - let gas_limit = if gas_limit > U256::from(u64::MAX) { - u64::MAX - } else { - gas_limit.low_u64() - }; + // Estimated encoded transaction size must be based on the heaviest transaction + // type (EIP1559Transaction) to be compatible with all transaction types. + let mut estimated_transaction_len = data.len() + + // pallet ethereum index: 1 + // transact call index: 1 + // Transaction enum variant: 1 + // chain_id 8 bytes + // nonce: 32 + // max_priority_fee_per_gas: 32 + // max_fee_per_gas: 32 + // gas_limit: 32 + // action: 21 (enum variant + call address) + // value: 32 + // access_list: 1 (empty vec size) + // 65 bytes signature + 258; + if access_list.is_some() { + estimated_transaction_len += access_list.encoded_size(); + } + let gas_limit = if gas_limit > U256::from(u64::MAX) { + u64::MAX + } else { + gas_limit.low_u64() + }; let weight_limit = ::GasWeightMapping::gas_to_weight(gas_limit, true); - let proof_size_pre_execution = cumulus_primitives_storage_weight_reclaim::get_proof_size(); - let transaction_pov = TransactionPov::new(weight_limit, estimated_transaction_len as u64, proof_size_pre_execution); + let transaction_pov = cumulus_primitives_storage_weight_reclaim::get_proof_size().map( + |proof_size_pre_execution| { + TransactionPov::new(weight_limit, estimated_transaction_len as u64, proof_size_pre_execution) + }, + ); ::Runner::call( from, to, @@ -849,7 +849,7 @@ impl_runtime_apis! { access_list.unwrap_or_default(), false, true, - Some(transaction_pov), + transaction_pov, config.as_ref().unwrap_or(::config()), ).map_err(|err| err.error.into()) } @@ -903,8 +903,11 @@ impl_runtime_apis! { gas_limit.low_u64() }; let weight_limit = ::GasWeightMapping::gas_to_weight(gas_limit, true); - let proof_size_pre_execution = cumulus_primitives_storage_weight_reclaim::get_proof_size(); - let transaction_pov = TransactionPov::new(weight_limit, estimated_transaction_len as u64, proof_size_pre_execution); + let transaction_pov = cumulus_primitives_storage_weight_reclaim::get_proof_size().map( + |proof_size_pre_execution| { + TransactionPov::new(weight_limit, estimated_transaction_len as u64, proof_size_pre_execution) + }, + ); ::Runner::create( from, data, @@ -916,7 +919,7 @@ impl_runtime_apis! { access_list.unwrap_or_default(), false, true, - Some(transaction_pov), + transaction_pov, config.as_ref().unwrap_or(::config()), ).map_err(|err| err.error.into()) }