diff --git a/Cargo.lock b/Cargo.lock index a8eafe2c5..f800dc79a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6565,14 +6565,18 @@ dependencies = [ [[package]] name = "pallet-evm-balances" -version = "1.0.0-dev" -source = "git+https://github.com/humanode-network/frontier?tag=locked/polkadot-v0.9.42-2025-02-08#174c0227af00dbc26485c1eac68738e1d01980e8" +version = "0.1.0" dependencies = [ + "fp-evm", "frame-support", "frame-system", - "log", + "pallet-evm", + "pallet-evm-system", + "pallet-timestamp", "parity-scale-codec", "scale-info", + "sp-core", + "sp-io", "sp-runtime", "sp-std", ] @@ -6625,15 +6629,16 @@ dependencies = [ [[package]] name = "pallet-evm-system" -version = "1.0.0-dev" -source = "git+https://github.com/humanode-network/frontier?tag=locked/polkadot-v0.9.42-2025-02-08#174c0227af00dbc26485c1eac68738e1d01980e8" +version = "0.1.0" dependencies = [ "fp-evm", "frame-support", "frame-system", - "log", + "mockall", "parity-scale-codec", "scale-info", + "sp-core", + "sp-io", "sp-runtime", "sp-std", ] diff --git a/Cargo.toml b/Cargo.toml index d33fd04a0..148e04d77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -155,13 +155,11 @@ fp-self-contained = { git = "https://github.com/humanode-network/frontier", tag fp-storage = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } pallet-ethereum = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } pallet-evm = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } -pallet-evm-balances = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } pallet-evm-precompile-blake2 = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } pallet-evm-precompile-bn128 = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } pallet-evm-precompile-modexp = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } pallet-evm-precompile-sha3fips = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } pallet-evm-precompile-simple = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } -pallet-evm-system = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } pallet-evm-test-vector-support = { git = "https://github.com/humanode-network/frontier", tag = "locked/polkadot-v0.9.42-2025-02-08", default-features = false } [profile.release] diff --git a/crates/humanode-runtime/Cargo.toml b/crates/humanode-runtime/Cargo.toml index 311c31f55..1e4c8083e 100644 --- a/crates/humanode-runtime/Cargo.toml +++ b/crates/humanode-runtime/Cargo.toml @@ -27,6 +27,8 @@ pallet-dummy-precompiles-code = { path = "../pallet-dummy-precompiles-code", def pallet-erc20-support = { path = "../pallet-erc20-support", default-features = false } pallet-ethereum-chain-id = { path = "../pallet-ethereum-chain-id", default-features = false } pallet-evm-accounts-mapping = { path = "../pallet-evm-accounts-mapping", default-features = false } +pallet-evm-balances = { path = "../pallet-evm-balances", default-features = false } +pallet-evm-system = { path = "../pallet-evm-system", default-features = false } pallet-humanode-offences = { path = "../pallet-humanode-offences", default-features = false } pallet-humanode-session = { path = "../pallet-humanode-session", default-features = false } pallet-pot = { path = "../pallet-pot", default-features = false } @@ -64,13 +66,11 @@ pallet-babe = { workspace = true } pallet-balances = { workspace = true } pallet-ethereum = { workspace = true } pallet-evm = { workspace = true } -pallet-evm-balances = { workspace = true } pallet-evm-precompile-blake2 = { workspace = true } pallet-evm-precompile-bn128 = { workspace = true } pallet-evm-precompile-modexp = { workspace = true } pallet-evm-precompile-sha3fips = { workspace = true } pallet-evm-precompile-simple = { workspace = true } -pallet-evm-system = { workspace = true } pallet-grandpa = { workspace = true } pallet-im-online = { workspace = true } pallet-multisig = { workspace = true } diff --git a/crates/pallet-currency-swap/Cargo.toml b/crates/pallet-currency-swap/Cargo.toml index 41fc9a231..8849f6006 100644 --- a/crates/pallet-currency-swap/Cargo.toml +++ b/crates/pallet-currency-swap/Cargo.toml @@ -16,10 +16,11 @@ sp-runtime = { workspace = true } sp-std = { workspace = true } [dev-dependencies] +pallet-evm-balances = { path = "../pallet-evm-balances", features = ["default"] } +pallet-evm-system = { path = "../pallet-evm-system", features = ["default"] } + mockall = { workspace = true } pallet-balances = { workspace = true, features = ["default"] } -pallet-evm-balances = { workspace = true, features = ["default"] } -pallet-evm-system = { workspace = true, features = ["default"] } sp-core = { workspace = true } [features] diff --git a/crates/pallet-dummy-precompiles-code/Cargo.toml b/crates/pallet-dummy-precompiles-code/Cargo.toml index a5fc5ab3d..bbe2b4462 100644 --- a/crates/pallet-dummy-precompiles-code/Cargo.toml +++ b/crates/pallet-dummy-precompiles-code/Cargo.toml @@ -14,11 +14,12 @@ sp-core = { workspace = true } sp-std = { workspace = true } [dev-dependencies] +pallet-evm-balances = { path = "../pallet-evm-balances", features = ["default"] } +pallet-evm-system = { path = "../pallet-evm-system", features = ["default"] } + fp-evm = { workspace = true } hex-literal = { workspace = true } pallet-balances = { workspace = true, features = ["default"] } -pallet-evm-balances = { workspace = true, features = ["default"] } -pallet-evm-system = { workspace = true, features = ["default"] } pallet-timestamp = { workspace = true, features = ["default"] } [features] diff --git a/crates/pallet-evm-balances/Cargo.toml b/crates/pallet-evm-balances/Cargo.toml index a0b49cb61..2fac8aaa1 100644 --- a/crates/pallet-evm-balances/Cargo.toml +++ b/crates/pallet-evm-balances/Cargo.toml @@ -1,28 +1,22 @@ [package] name = "pallet-evm-balances" -version = "1.0.0-dev" -license = "Apache-2.0" -description = "FRAME EVM BALANCES pallet." -edition = { workspace = true } -repository = { workspace = true } - -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] +version = "0.1.0" +edition = "2021" +publish = false [dependencies] -log = { workspace = true, default-features = false } -scale-codec = { package = "parity-scale-codec", workspace = true } -scale-info = { workspace = true } -# Substrate +codec = { workspace = true } frame-support = { workspace = true } frame-system = { workspace = true } +scale-info = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } [dev-dependencies] +pallet-evm-system = { path = "../pallet-evm-system", features = ["default"] } + fp-evm = { workspace = true } pallet-evm = { workspace = true } -pallet-evm-system = { workspace = true } pallet-timestamp = { workspace = true } sp-core = { workspace = true } sp-io = { workspace = true } @@ -30,21 +24,24 @@ sp-io = { workspace = true } [features] default = ["std"] std = [ - "log/std", - "scale-codec/std", - "scale-info/std", - # Substrate - "frame-support/std", - "frame-system/std", - "pallet-timestamp/std", - "sp-runtime/std", - "sp-std/std", - # Frontier - "fp-evm/std", - "pallet-evm/std", - "pallet-evm-system/std", + "codec/std", + "fp-evm/std", + "frame-support/std", + "frame-system/std", + "pallet-evm-system/std", + "pallet-evm/std", + "pallet-timestamp/std", + "scale-info/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", ] try-runtime = [ - "frame-support/try-runtime", - "frame-system/try-runtime", + "frame-support/try-runtime", + "frame-system/try-runtime", + "pallet-evm-system/try-runtime", + "pallet-evm/try-runtime", + "pallet-timestamp/try-runtime", + "sp-runtime/try-runtime", ] diff --git a/crates/pallet-evm-balances/src/imbalances.rs b/crates/pallet-evm-balances/src/imbalances.rs index 77529d439..8d5014157 100644 --- a/crates/pallet-evm-balances/src/imbalances.rs +++ b/crates/pallet-evm-balances/src/imbalances.rs @@ -60,7 +60,10 @@ impl, I: 'static> Imbalance for PositiveImbalance fn split(self, amount: T::Balance) -> (Self, Self) { let first = self.0.min(amount); - let second = self.0 - first; + let second = self + .0 + .checked_sub(&first) + .expect("valid operation due to the check before; qed."); mem::forget(self); (Self(first), Self(second)) @@ -83,8 +86,12 @@ impl, I: 'static> Imbalance for PositiveImbalance mem::forget((self, other)); match a.cmp(&b) { - Ordering::Greater => SameOrOther::Same(Self(a - b)), - Ordering::Less => SameOrOther::Other(NegativeImbalance::new(b - a)), + Ordering::Greater => { + SameOrOther::Same(Self(a.checked_sub(&b).expect("a is greater than b; qed."))) + } + Ordering::Less => SameOrOther::Other(NegativeImbalance::new( + b.checked_sub(&a).expect("b is greater than a; qed."), + )), Ordering::Equal => SameOrOther::None, } } @@ -123,7 +130,10 @@ impl, I: 'static> Imbalance for NegativeImbalance fn split(self, amount: T::Balance) -> (Self, Self) { let first = self.0.min(amount); - let second = self.0 - first; + let second = self + .0 + .checked_sub(&first) + .expect("valid operation due to the check before; qed."); mem::forget(self); (Self(first), Self(second)) @@ -146,8 +156,12 @@ impl, I: 'static> Imbalance for NegativeImbalance mem::forget((self, other)); match a.cmp(&b) { - Ordering::Greater => SameOrOther::Same(Self(a - b)), - Ordering::Less => SameOrOther::Other(PositiveImbalance::new(b - a)), + Ordering::Greater => { + SameOrOther::Same(Self(a.checked_sub(&b).expect("a is greater than b; qed."))) + } + Ordering::Less => SameOrOther::Other(PositiveImbalance::new( + b.checked_sub(&a).expect("b is greater than a; qed."), + )), Ordering::Equal => SameOrOther::None, } } diff --git a/crates/pallet-evm-balances/src/impl_currency.rs b/crates/pallet-evm-balances/src/impl_currency.rs index 1fd978a94..d1edaf5c4 100644 --- a/crates/pallet-evm-balances/src/impl_currency.rs +++ b/crates/pallet-evm-balances/src/impl_currency.rs @@ -67,7 +67,9 @@ where } >::mutate(|issued| { *issued = issued.checked_add(&amount).unwrap_or_else(|| { - amount = Self::Balance::max_value() - *issued; + amount = Self::Balance::max_value() + .checked_sub(issued) + .expect("valid operation; qed."); Self::Balance::max_value() }) }); @@ -129,7 +131,7 @@ where return (NegativeImbalance::zero(), value); } - let result = match Self::try_mutate_account_handling_dust( + let result = Self::try_mutate_account_handling_dust( who, |account, _is_new| -> Result<(Self::NegativeImbalance, Self::Balance), DispatchError> { // Best value is the most amount we can slash following liveness rules. @@ -138,7 +140,9 @@ where let remaining = value.saturating_sub(actual); Ok((NegativeImbalance::new(actual), remaining)) }, - ) { + ); + + match result { Ok((imbalance, remaining)) => { Self::deposit_event(Event::Slashed { who: who.clone(), @@ -147,8 +151,7 @@ where (imbalance, remaining) } Err(_) => (Self::NegativeImbalance::zero(), value), - }; - result + } } /// Deposit some `value` into the free balance of an existing target account `who`. @@ -283,9 +286,17 @@ where ensure!(value >= ed || !is_new, Error::::ExistentialDeposit); let imbalance = if account.free <= value { - SignedImbalance::Positive(PositiveImbalance::new(value - account.free)) + SignedImbalance::Positive(PositiveImbalance::new( + value + .checked_sub(&account.free) + .expect("valid operation due to the check before; qed.") + )) } else { - SignedImbalance::Negative(NegativeImbalance::new(account.free - value)) + SignedImbalance::Negative(NegativeImbalance::new( + account.free + .checked_sub(&value) + .expect("valid operation due to the check before; qed.") + )) }; account.free = value; Self::deposit_event(Event::BalanceSet { diff --git a/crates/pallet-evm-balances/src/lib.rs b/crates/pallet-evm-balances/src/lib.rs index 58b8f8c02..ca5786ba9 100644 --- a/crates/pallet-evm-balances/src/lib.rs +++ b/crates/pallet-evm-balances/src/lib.rs @@ -1,10 +1,8 @@ -// SPDX-License-Identifier: Apache-2.0 - //! # EVM Balances Pallet. -// Ensure we're `no_std` when compiling for Wasm. #![cfg_attr(not(feature = "std"), no_std)] +use codec::{Codec, Decode, Encode, MaxEncodedLen}; use frame_support::{ ensure, traits::{ @@ -14,7 +12,6 @@ use frame_support::{ StoredMap, WithdrawReasons, }, }; -use scale_codec::{Codec, Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; use sp_runtime::{ traits::{Bounded, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Zero}, @@ -41,6 +38,9 @@ pub use pallet::*; /// The current storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); +// We have to temporarily allow some clippy lints. Later on we'll send patches to substrate to +// fix them at their end. +#[allow(clippy::missing_docs_in_private_items)] #[frame_support::pallet] pub mod pallet { use frame_support::pallet_prelude::*; @@ -111,65 +111,94 @@ pub mod pallet { pub enum Event, I: 'static = ()> { /// An account was created with some free balance. Endowed { + /// The associated account id. account: >::AccountId, + /// The `free_balance` balance value. free_balance: T::Balance, }, /// An account was removed whose balance was non-zero but below ExistentialDeposit, /// resulting in an outright loss. DustLost { + /// The associated account id. account: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// Transfer succeeded. Transfer { + /// The associated account id transferred from. from: >::AccountId, + /// The associated account id transferred to. to: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// A balance was set by root. BalanceSet { + /// The associated account id. who: >::AccountId, + /// The `free` balance value. free: T::Balance, }, /// Some amount was deposited (e.g. for transaction fees). Deposit { + /// The associated account id. who: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// Some amount was withdrawn from the account (e.g. for transaction fees). Withdraw { + /// The associated account id. who: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// Some amount was removed from the account (e.g. for misbehavior). Slashed { + /// The associated account id. who: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// Some amount was minted into an account. Minted { + /// The associated account id. who: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// Some amount was burned from an account. Burned { + /// The associated account id. who: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// Some amount was suspended from an account (it can be restored later). Suspended { + /// The associated account id. who: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// Some amount was restored into an account. Restored { + /// The associated account id. who: >::AccountId, + /// The `amount` balance value. amount: T::Balance, }, /// Total issuance was increased by `amount`, creating a credit to be balanced. - Issued { amount: T::Balance }, + Issued { + /// The `amount` balance value. + amount: T::Balance, + }, /// Total issuance was decreased by `amount`, creating a debt to be balanced. - Rescinded { amount: T::Balance }, + Rescinded { + /// The `amount` balance value. + amount: T::Balance, + }, } #[pallet::error] diff --git a/crates/pallet-evm-balances/src/mock.rs b/crates/pallet-evm-balances/src/mock.rs index fc808139e..bbe264559 100644 --- a/crates/pallet-evm-balances/src/mock.rs +++ b/crates/pallet-evm-balances/src/mock.rs @@ -1,22 +1,3 @@ -// SPDX-License-Identifier: Apache-2.0 -// This file is part of Frontier. -// -// Copyright (c) 2020-2022 Parity Technologies (UK) Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//! Test mock for unit tests. - use std::collections::BTreeMap; use frame_support::{ diff --git a/crates/pallet-evm-balances/src/tests/currency.rs b/crates/pallet-evm-balances/src/tests/currency.rs index a9496d927..810f59428 100644 --- a/crates/pallet-evm-balances/src/tests/currency.rs +++ b/crates/pallet-evm-balances/src/tests/currency.rs @@ -27,7 +27,7 @@ fn total_balance_works() { fn free_balance_works() { new_test_ext().execute_with_ext(|_| { // Check the free balance value. - assert_eq!(EvmBalances::free_balance(&alice()), INIT_BALANCE); + assert_eq!(EvmBalances::free_balance(alice()), INIT_BALANCE); }); } @@ -123,7 +123,7 @@ fn transfer_works() { // Check test preconditions. assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); - let transfered_amount = 100; + let transferred_amount = 100; // Set block number to enable events. System::set_block_number(1); @@ -132,23 +132,23 @@ fn transfer_works() { assert_ok!(EvmBalances::transfer( &alice(), &bob(), - transfered_amount, + transferred_amount, ExistenceRequirement::KeepAlive )); // Assert state changes. assert_eq!( EvmBalances::total_balance(&alice()), - INIT_BALANCE - transfered_amount + INIT_BALANCE - transferred_amount ); assert_eq!( EvmBalances::total_balance(&bob()), - INIT_BALANCE + transfered_amount + INIT_BALANCE + transferred_amount ); System::assert_has_event(RuntimeEvent::EvmBalances(crate::Event::Transfer { from: alice(), to: bob(), - amount: transfered_amount, + amount: transferred_amount, })); assert_total_issuance_invariant(); @@ -161,7 +161,7 @@ fn transfer_works_full_balance() { // Check test preconditions. assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); - let transfered_amount = INIT_BALANCE; + let transferred_amount = INIT_BALANCE; // Set block number to enable events. System::set_block_number(1); @@ -170,23 +170,23 @@ fn transfer_works_full_balance() { assert_ok!(EvmBalances::transfer( &alice(), &bob(), - transfered_amount, + transferred_amount, ExistenceRequirement::AllowDeath )); // Assert state changes. assert_eq!( EvmBalances::total_balance(&alice()), - INIT_BALANCE - transfered_amount + INIT_BALANCE - transferred_amount ); assert_eq!( EvmBalances::total_balance(&bob()), - INIT_BALANCE + transfered_amount + INIT_BALANCE + transferred_amount ); System::assert_has_event(RuntimeEvent::EvmBalances(crate::Event::Transfer { from: alice(), to: bob(), - amount: transfered_amount, + amount: transferred_amount, })); assert!(!EvmSystem::account_exists(&alice())); System::assert_has_event(RuntimeEvent::EvmSystem( @@ -203,7 +203,7 @@ fn transfer_fails_funds_unavailable() { // Check test preconditions. assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); - let transfered_amount = INIT_BALANCE + 1; + let transferred_amount = INIT_BALANCE + 1; // Set block number to enable events. System::set_block_number(1); @@ -213,7 +213,7 @@ fn transfer_fails_funds_unavailable() { EvmBalances::transfer( &alice(), &bob(), - transfered_amount, + transferred_amount, ExistenceRequirement::KeepAlive ), TokenError::FundsUnavailable @@ -227,7 +227,7 @@ fn transfer_fails_not_expendable() { // Check test preconditions. assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); - let transfered_amount = INIT_BALANCE; + let transferred_amount = INIT_BALANCE; // Set block number to enable events. System::set_block_number(1); @@ -237,7 +237,7 @@ fn transfer_fails_not_expendable() { EvmBalances::transfer( &alice(), &bob(), - transfered_amount, + transferred_amount, ExistenceRequirement::KeepAlive ), TokenError::NotExpendable @@ -551,7 +551,7 @@ fn evm_system_account_should_be_reaped() { )); // Assert state changes. - assert_eq!(EvmBalances::free_balance(&bob()), 0); + assert_eq!(EvmBalances::free_balance(bob()), 0); assert!(!EvmSystem::account_exists(&bob())); System::assert_has_event(RuntimeEvent::EvmSystem( pallet_evm_system::Event::KilledAccount { account: bob() }, diff --git a/crates/pallet-evm-balances/src/tests/fungible.rs b/crates/pallet-evm-balances/src/tests/fungible.rs index 562c6c5a0..55fb2642c 100644 --- a/crates/pallet-evm-balances/src/tests/fungible.rs +++ b/crates/pallet-evm-balances/src/tests/fungible.rs @@ -683,7 +683,7 @@ fn transfer_works() { // Check test preconditions. assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); - let transfered_amount = 100; + let transferred_amount = 100; // Set block number to enable events. System::set_block_number(1); @@ -692,23 +692,23 @@ fn transfer_works() { assert_ok!(EvmBalances::transfer( &alice(), &bob(), - transfered_amount, + transferred_amount, Preservation::Preserve )); // Assert state changes. assert_eq!( EvmBalances::total_balance(&alice()), - INIT_BALANCE - transfered_amount + INIT_BALANCE - transferred_amount ); assert_eq!( EvmBalances::total_balance(&bob()), - INIT_BALANCE + transfered_amount + INIT_BALANCE + transferred_amount ); System::assert_has_event(RuntimeEvent::EvmBalances(Event::Transfer { from: alice(), to: bob(), - amount: transfered_amount, + amount: transferred_amount, })); assert_total_issuance_invariant(); @@ -721,7 +721,7 @@ fn transfer_works_full_balance() { // Check test preconditions. assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); - let transfered_amount = INIT_BALANCE; + let transferred_amount = INIT_BALANCE; // Set block number to enable events. System::set_block_number(1); @@ -730,23 +730,23 @@ fn transfer_works_full_balance() { assert_ok!(EvmBalances::transfer( &alice(), &bob(), - transfered_amount, + transferred_amount, Preservation::Expendable )); // Assert state changes. assert_eq!( EvmBalances::total_balance(&alice()), - INIT_BALANCE - transfered_amount + INIT_BALANCE - transferred_amount ); assert_eq!( EvmBalances::total_balance(&bob()), - INIT_BALANCE + transfered_amount + INIT_BALANCE + transferred_amount ); System::assert_has_event(RuntimeEvent::EvmBalances(Event::Transfer { from: alice(), to: bob(), - amount: transfered_amount, + amount: transferred_amount, })); assert!(!EvmSystem::account_exists(&alice())); System::assert_has_event(RuntimeEvent::EvmSystem( @@ -763,14 +763,14 @@ fn transfer_fails_funds_unavailable() { // Check test preconditions. assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); - let transfered_amount = INIT_BALANCE + 1; + let transferred_amount = INIT_BALANCE + 1; // Set block number to enable events. System::set_block_number(1); // Invoke the function under test. assert_noop!( - EvmBalances::transfer(&alice(), &bob(), transfered_amount, Preservation::Preserve), + EvmBalances::transfer(&alice(), &bob(), transferred_amount, Preservation::Preserve), TokenError::FundsUnavailable ); }); @@ -782,14 +782,14 @@ fn transfer_fails_not_expendable() { // Check test preconditions. assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); - let transfered_amount = INIT_BALANCE; + let transferred_amount = INIT_BALANCE; // Set block number to enable events. System::set_block_number(1); // Invoke the function under test. assert_noop!( - EvmBalances::transfer(&alice(), &bob(), transfered_amount, Preservation::Preserve), + EvmBalances::transfer(&alice(), &bob(), transferred_amount, Preservation::Preserve), TokenError::NotExpendable ); }); diff --git a/crates/pallet-evm-balances/src/tests/mod.rs b/crates/pallet-evm-balances/src/tests/mod.rs index f46a562eb..ec798e26e 100644 --- a/crates/pallet-evm-balances/src/tests/mod.rs +++ b/crates/pallet-evm-balances/src/tests/mod.rs @@ -1,4 +1,4 @@ -//! Unit tests. +//! The tests for the pallet. use frame_support::{assert_ok, traits::Currency, weights::Weight}; use pallet_evm::{FeeCalculator, FixedGasWeightMapping, GasWeightMapping, Runner}; @@ -53,13 +53,13 @@ fn evm_system_removing_account_non_zero_balance() { ExistenceRequirement::KeepAlive )); - assert_eq!(EvmBalances::free_balance(&contract), 1000); + assert_eq!(EvmBalances::free_balance(contract), 1000); // Invoke the function under test. EVM::remove_account(&contract); // Assert state changes. - assert_eq!(EvmBalances::free_balance(&contract), 1000); + assert_eq!(EvmBalances::free_balance(contract), 1000); assert!(EvmSystem::account_exists(&contract)); assert_total_issuance_invariant(); @@ -73,7 +73,7 @@ fn evm_fee_deduction() { // Seed account let _ = ::Currency::deposit_creating(&charlie, 100); - assert_eq!(EvmBalances::free_balance(&charlie), 100); + assert_eq!(EvmBalances::free_balance(charlie), 100); // Deduct fees as 10 units let imbalance = @@ -82,11 +82,11 @@ fn evm_fee_deduction() { U256::from(10), ) .unwrap(); - assert_eq!(EvmBalances::free_balance(&charlie), 90); + assert_eq!(EvmBalances::free_balance(charlie), 90); // Refund fees as 5 units <::OnChargeTransaction as pallet_evm::OnChargeEVMTransaction>::correct_and_deposit_fee(&charlie, U256::from(5), U256::from(5), imbalance); - assert_eq!(EvmBalances::free_balance(&charlie), 95); + assert_eq!(EvmBalances::free_balance(charlie), 95); assert_total_issuance_invariant(); }); @@ -202,7 +202,7 @@ fn evm_refunds_and_priority_should_work() { let (base_fee, _) = ::FeeCalculator::min_gas_price(); let actual_tip = (max_fee_per_gas - base_fee).min(tip) * used_gas; - let total_cost = (used_gas * base_fee) + U256::from(actual_tip) + U256::from(1); + let total_cost = (used_gas * base_fee) + actual_tip + U256::from(1); let after_call = EVM::account_basic(&alice()).0.balance; // The tip is deducted but never refunded to the caller. assert_eq!(after_call, before_call - total_cost); diff --git a/crates/pallet-evm-system/Cargo.toml b/crates/pallet-evm-system/Cargo.toml index 1a114304f..2e00f444f 100644 --- a/crates/pallet-evm-system/Cargo.toml +++ b/crates/pallet-evm-system/Cargo.toml @@ -1,25 +1,17 @@ [package] name = "pallet-evm-system" -version = "1.0.0-dev" -license = "Apache-2.0" -description = "FRAME EVM SYSTEM pallet." -edition = { workspace = true } -repository = { workspace = true } - -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] +version = "0.1.0" +edition = "2021" +publish = false [dependencies] -log = { workspace = true, default-features = false } -scale-codec = { package = "parity-scale-codec", workspace = true } -scale-info = { workspace = true } -# Substrate +codec = { workspace = true } +fp-evm = { workspace = true } frame-support = { workspace = true } frame-system = { workspace = true } +scale-info = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } -# Frontier -fp-evm = { workspace = true } [dev-dependencies] mockall = { workspace = true } @@ -29,18 +21,18 @@ sp-io = { workspace = true } [features] default = ["std"] std = [ - "log/std", - "scale-codec/std", - "scale-info/std", - # Substrate - "frame-support/std", - "frame-system/std", - "sp-runtime/std", - "sp-std/std", - # Frontier - "fp-evm/std", + "codec/std", + "fp-evm/std", + "frame-support/std", + "frame-system/std", + "scale-info/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", ] try-runtime = [ - "frame-support/try-runtime", - "frame-system/try-runtime", + "frame-support/try-runtime", + "frame-system/try-runtime", + "sp-runtime/try-runtime", ] diff --git a/crates/pallet-evm-system/src/lib.rs b/crates/pallet-evm-system/src/lib.rs index 7322b7874..172e651c5 100644 --- a/crates/pallet-evm-system/src/lib.rs +++ b/crates/pallet-evm-system/src/lib.rs @@ -1,14 +1,13 @@ //! # EVM System Pallet. -// Ensure we're `no_std` when compiling for Wasm. #![cfg_attr(not(feature = "std"), no_std)] +use codec::{Decode, Encode, FullCodec, MaxEncodedLen}; use frame_support::traits::StoredMap; -use scale_codec::{Decode, Encode, FullCodec, MaxEncodedLen}; use scale_info::TypeInfo; use sp_runtime::{ traits::{One, Zero}, - DispatchError, RuntimeDebug, + DispatchError, RuntimeDebug, Saturating, }; #[cfg(test)] @@ -28,6 +27,9 @@ pub struct AccountInfo { pub data: AccountData, } +// We have to temporarily allow some clippy lints. Later on we'll send patches to substrate to +// fix them at their end. +#[allow(clippy::missing_docs_in_private_items)] #[frame_support::pallet] pub mod pallet { use frame_support::pallet_prelude::*; @@ -93,9 +95,15 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { /// A new account was created. - NewAccount { account: ::AccountId }, + NewAccount { + /// The associated account id. + account: ::AccountId, + }, /// An account was reaped. - KilledAccount { account: ::AccountId }, + KilledAccount { + /// The associated account id. + account: ::AccountId, + }, } } @@ -136,8 +144,9 @@ impl Pallet { let is_new_account = Account::::mutate_exists(who, |maybe_account| { let is_new_account = maybe_account.is_none(); - let account = maybe_account.get_or_insert_with(Default::default); - account.nonce += ::Index::one(); + let account = maybe_account.get_or_insert_with(Default::default); + // `AtLeast32Bit` is big enough for this overflow to be practically impossible. + account.nonce = account.nonce.saturating_add(::Index::one()); is_new_account }); diff --git a/crates/pallet-evm-system/src/mock.rs b/crates/pallet-evm-system/src/mock.rs index b05b5298d..8beb62295 100644 --- a/crates/pallet-evm-system/src/mock.rs +++ b/crates/pallet-evm-system/src/mock.rs @@ -1,22 +1,3 @@ -// SPDX-License-Identifier: Apache-2.0 -// This file is part of Frontier. -// -// Copyright (c) 2020-2022 Parity Technologies (UK) Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//! Test mock for unit tests. - use frame_support::traits::{ConstU32, ConstU64}; use mockall::mock; use sp_core::{H160, H256}; diff --git a/crates/pallet-evm-system/src/tests.rs b/crates/pallet-evm-system/src/tests.rs index bedef9b8b..c8d6c51fd 100644 --- a/crates/pallet-evm-system/src/tests.rs +++ b/crates/pallet-evm-system/src/tests.rs @@ -1,4 +1,4 @@ -//! Unit tests. +//! The tests for the pallet. use frame_support::{assert_noop, assert_storage_noop}; use mockall::predicate; @@ -38,7 +38,7 @@ fn create_account_created() { // Assert state changes. assert!(EvmSystem::account_exists(&account_id)); assert_eq!( - >::get(&account_id), + >::get(account_id), AccountInfo::<_, _>::default() ); System::assert_has_event(RuntimeEvent::EvmSystem(Event::NewAccount { @@ -57,7 +57,7 @@ fn create_account_already_exists() { new_test_ext().execute_with_ext(|_| { // Prepare test data. let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); - >::insert(account_id.clone(), AccountInfo::<_, _>::default()); + >::insert(account_id, AccountInfo::<_, _>::default()); // Invoke the function under test. assert_storage_noop!(assert_eq!( @@ -119,7 +119,7 @@ fn inc_account_nonce_account_exists() { new_test_ext().execute_with_ext(|_| { // Prepare test data. let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); - >::insert(account_id.clone(), AccountInfo::<_, _>::default()); + >::insert(account_id, AccountInfo::<_, _>::default()); // Check test preconditions. assert!(EvmSystem::account_exists(&account_id)); @@ -135,7 +135,7 @@ fn inc_account_nonce_account_exists() { }); } -/// This test verifies that try_mutate_exists works as expected in case data wasn't providing +/// This test verifies that `try_mutate_exists` works as expected in case data wasn't providing /// and returned data is `Some`. As a result, a new account has been created. #[test] fn try_mutate_exists_account_created() { @@ -167,7 +167,7 @@ fn try_mutate_exists_account_created() { // Assert state changes. assert!(EvmSystem::account_exists(&account_id)); assert_eq!( - >::get(&account_id), + >::get(account_id), AccountInfo { data: 1, ..Default::default() @@ -182,7 +182,7 @@ fn try_mutate_exists_account_created() { }); } -/// This test verifies that try_mutate_exists works as expected in case data was providing +/// This test verifies that `try_mutate_exists` works as expected in case data was providing /// and returned data is `Some`. As a result, the account has been updated. #[test] fn try_mutate_exists_account_updated() { @@ -191,7 +191,7 @@ fn try_mutate_exists_account_updated() { let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); let nonce = 1; let data = 1; - >::insert(account_id.clone(), AccountInfo { nonce, data }); + >::insert(account_id, AccountInfo { nonce, data }); // Check test preconditions. assert!(EvmSystem::account_exists(&account_id)); @@ -211,7 +211,7 @@ fn try_mutate_exists_account_updated() { // Assert state changes. assert!(EvmSystem::account_exists(&account_id)); assert_eq!( - >::get(&account_id), + >::get(account_id), AccountInfo { nonce, data: data + 1, @@ -220,14 +220,14 @@ fn try_mutate_exists_account_updated() { }); } -/// This test verifies that try_mutate_exists works as expected in case data was providing +/// This test verifies that `try_mutate_exists` works as expected in case data was providing /// and returned data is `None`, account has zero nonce. As a result, the account has been removed. #[test] fn try_mutate_exists_account_removed() { new_test_ext().execute_with_ext(|_| { // Prepare test data. let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); - >::insert(account_id.clone(), AccountInfo::<_, _>::default()); + >::insert(account_id, AccountInfo::<_, _>::default()); // Check test preconditions. assert!(EvmSystem::account_exists(&account_id)); @@ -261,7 +261,7 @@ fn try_mutate_exists_account_removed() { }); } -/// This test verifies that try_mutate_exists works as expected in case data was providing +/// This test verifies that `try_mutate_exists` works as expected in case data was providing /// and returned data is `None`, account has non zero nonce. As a result, the account has been retained. #[test] fn try_mutate_exists_account_retained() { @@ -272,7 +272,7 @@ fn try_mutate_exists_account_retained() { let data = 100; let account_info = AccountInfo { nonce, data }; - >::insert(account_id.clone(), account_info); + >::insert(account_id, account_info); // Check test preconditions. assert!(EvmSystem::account_exists(&account_id)); @@ -287,7 +287,7 @@ fn try_mutate_exists_account_retained() { // Assert state changes. assert!(EvmSystem::account_exists(&account_id)); assert_eq!( - >::get(&account_id), + >::get(account_id), AccountInfo { nonce, ..Default::default() @@ -296,7 +296,7 @@ fn try_mutate_exists_account_retained() { }); } -/// This test verifies that try_mutate_exists works as expected in case data wasn't providing +/// This test verifies that `try_mutate_exists` works as expected in case data wasn't providing /// and returned data is `None`. As a result, the account hasn't been created. #[test] fn try_mutate_exists_account_not_created() { @@ -322,14 +322,14 @@ fn try_mutate_exists_account_not_created() { }); } -/// This test verifies that try_mutate_exists works as expected in case getting error +/// This test verifies that `try_mutate_exists` works as expected in case getting error /// during data mutation. #[test] fn try_mutate_exists_without_changes() { new_test_ext().execute_with_ext(|_| { // Prepare test data. let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap(); - >::insert(account_id.clone(), AccountInfo::<_, _>::default()); + >::insert(account_id, AccountInfo::<_, _>::default()); // Check test preconditions. assert!(EvmSystem::account_exists(&account_id)); diff --git a/crates/precompile-currency-swap/Cargo.toml b/crates/precompile-currency-swap/Cargo.toml index 45951c673..a81ea63bf 100644 --- a/crates/precompile-currency-swap/Cargo.toml +++ b/crates/precompile-currency-swap/Cargo.toml @@ -17,13 +17,14 @@ scale-info = { workspace = true, features = ["derive"] } sp-core = { workspace = true } [dev-dependencies] +pallet-evm-balances = { path = "../pallet-evm-balances", features = ["default"] } +pallet-evm-system = { path = "../pallet-evm-system", features = ["default"] } + frame-system = { workspace = true } hex-literal = { workspace = true } mockall = { workspace = true } pallet-balances = { workspace = true, features = ["default"] } pallet-evm = { workspace = true } -pallet-evm-balances = { workspace = true, features = ["default"] } -pallet-evm-system = { workspace = true, features = ["default"] } pallet-timestamp = { workspace = true, features = ["default"] } [features] diff --git a/crates/precompile-native-currency/Cargo.toml b/crates/precompile-native-currency/Cargo.toml index 741e55be6..4b55eb1b1 100644 --- a/crates/precompile-native-currency/Cargo.toml +++ b/crates/precompile-native-currency/Cargo.toml @@ -6,6 +6,7 @@ publish = false [dependencies] pallet-erc20-support = { path = "../pallet-erc20-support", default-features = false } +pallet-evm-balances = { path = "../pallet-evm-balances", default-features = false } precompile-utils = { path = "../precompile-utils", default-features = false } codec = { workspace = true, features = ["derive"] } @@ -13,18 +14,17 @@ fp-evm = { workspace = true } frame-support = { workspace = true } num_enum = { workspace = true } pallet-evm = { workspace = true } -pallet-evm-balances = { workspace = true } scale-info = { workspace = true, features = ["derive"] } sp-core = { workspace = true } [dev-dependencies] +pallet-evm-system = { path = "../pallet-evm-system", features = ["default"] } precompile-utils = { path = "../precompile-utils", features = ["testing"] } frame-system = { workspace = true } hex-literal = { workspace = true } pallet-balances = { workspace = true, features = ["default"] } pallet-evm = { workspace = true } -pallet-evm-system = { workspace = true, features = ["default"] } pallet-timestamp = { workspace = true, features = ["default"] } [features] diff --git a/utils/checks/snapshots/features.yaml b/utils/checks/snapshots/features.yaml index 252f44bcb..de6e263a3 100644 --- a/utils/checks/snapshots/features.yaml +++ b/utils/checks/snapshots/features.yaml @@ -2176,7 +2176,7 @@ - default - frame-benchmarking - std -- name: pallet-evm-balances 1.0.0-dev +- name: pallet-evm-balances 0.1.0 features: - default - std @@ -2195,7 +2195,7 @@ - name: pallet-evm-precompile-simple 2.0.0-dev features: - std -- name: pallet-evm-system 1.0.0-dev +- name: pallet-evm-system 0.1.0 features: - default - std