Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fungible conformance tests: Inspect and Mutate #110

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ log = { version = "0.4.17", default-features = false }
mockall = "0.11"
parity-db = "0.4.8"
parking_lot = "0.12.1"
paste = "1.0"
rlp = { version = "0.5", default-features = false }
scale-codec = { package = "parity-scale-codec", version = "3.2.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.3.1", default-features = false, features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions frame/evm-balances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fp-evm = { workspace = true }
pallet-evm = { workspace = true }
pallet-evm-system = { workspace = true }
pallet-timestamp = { workspace = true }
paste = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }

Expand Down
41 changes: 31 additions & 10 deletions frame/evm-balances/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use frame_support::{
traits::{ConstU32, ConstU64, FindAuthor},
weights::Weight,
};
use pallet_evm::{EnsureAddressNever, FixedGasWeightMapping, IdentityAddressMapping};
use pallet_evm::{AddressMapping, EnsureAddressNever, FixedGasWeightMapping};
use sp_core::{H160, H256, U256};
use sp_runtime::{
generic,
Expand All @@ -37,12 +37,33 @@ use crate::{self as pallet_evm_balances, *};

pub(crate) const INIT_BALANCE: u64 = 10_000_000_000_000_000;

pub(crate) fn alice() -> H160 {
H160::from_str("1000000000000000000000000000000000000000").unwrap()
/// Alice account.
pub(crate) fn alice() -> u64 {
5234
}

pub(crate) fn bob() -> H160 {
H160::from_str("2000000000000000000000000000000000000000").unwrap()
/// Alice H160 account.
pub(crate) fn alice_h160() -> H160 {
H160::from_low_u64_be(alice())
}

/// Bob account.
pub(crate) fn bob() -> u64 {
4325
}

/// Bob H160 account.
pub(crate) fn bob_h160() -> H160 {
H160::from_low_u64_be(bob())
}

/// H160 into u64 address mapper.
pub struct H160IntoU64;

impl AddressMapping<u64> for H160IntoU64 {
fn into_account_id(address: H160) -> u64 {
address.to_low_u64_be()
}
}

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
Expand Down Expand Up @@ -91,7 +112,7 @@ impl frame_system::Config for Test {

impl pallet_evm_system::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AccountId = H160;
type AccountId = u64;
type Index = u64;
type AccountData = AccountData<u64>;
type OnNewAccount = ();
Expand All @@ -100,7 +121,7 @@ impl pallet_evm_system::Config for Test {

impl pallet_evm_balances::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AccountId = H160;
type AccountId = u64;
type Balance = u64;
type ExistentialDeposit = ConstU64<1>;
type AccountStore = EvmSystem;
Expand Down Expand Up @@ -156,7 +177,7 @@ impl pallet_evm::Config for Test {
EnsureAddressNever<<Self::AccountProvider as pallet_evm::AccountProvider>::AccountId>;
type WithdrawOrigin =
EnsureAddressNever<<Self::AccountProvider as pallet_evm::AccountProvider>::AccountId>;
type AddressMapping = IdentityAddressMapping;
type AddressMapping = H160IntoU64;
type Currency = EvmBalances;
type RuntimeEvent = RuntimeEvent;
type PrecompilesType = ();
Expand Down Expand Up @@ -186,8 +207,8 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
nonce: Default::default(),
storage: Default::default(),
};
map.insert(alice(), init_genesis_account.clone());
map.insert(bob(), init_genesis_account);
map.insert(alice_h160(), init_genesis_account.clone());
map.insert(bob_h160(), init_genesis_account);
map
},
},
Expand Down
12 changes: 5 additions & 7 deletions frame/evm-balances/src/tests/currency.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Tests regarding the functionality of the `Currency` trait set implementations.

use frame_support::{assert_noop, assert_ok, traits::Currency};
use sp_core::H160;
use sp_runtime::TokenError;
use sp_std::str::FromStr;

use crate::{mock::*, *};

Expand Down Expand Up @@ -339,7 +337,7 @@ fn deposit_into_existing_fails_overflow() {
#[test]
fn deposit_into_existing_fails_dead_account() {
new_test_ext().execute_with_ext(|_| {
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
let charlie = 3;

// Check test preconditions.
assert_eq!(EvmBalances::total_balance(&charlie), 0);
Expand All @@ -358,7 +356,7 @@ fn deposit_into_existing_fails_dead_account() {
fn deposit_creating_works() {
new_test_ext().execute_with_ext(|_| {
// Prepare test preconditions.
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
let charlie = 3;
let deposited_amount = 10;
assert!(!EvmSystem::account_exists(&charlie));

Expand Down Expand Up @@ -493,7 +491,7 @@ fn withdraw_fails_expendability() {
fn make_free_balance_be_works() {
new_test_ext().execute_with_ext(|_| {
// Prepare test preconditions.
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
let charlie = 3;
let made_free_balance = 100;

// Check test preconditions.
Expand Down Expand Up @@ -537,8 +535,8 @@ fn evm_system_account_should_be_reaped() {
fn transferring_too_high_value_should_not_panic() {
new_test_ext().execute_with_ext(|_| {
// Prepare test preconditions.
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
let eve = H160::from_str("1000000000000000000000000000000000000004").unwrap();
let charlie = 3;
let eve = 4;
EvmBalances::make_free_balance_be(&charlie, u64::MAX);
EvmBalances::make_free_balance_be(&eve, 1);

Expand Down
8 changes: 3 additions & 5 deletions frame/evm-balances/src/tests/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use frame_support::{
tokens::Precision,
},
};
use sp_core::H160;
use sp_runtime::TokenError;
use sp_std::str::FromStr;

use crate::{mock::*, *};

Expand Down Expand Up @@ -779,8 +777,8 @@ fn transfer_fails_not_expendable() {
fn transfer_fails_underflow() {
new_test_ext().execute_with_ext(|_| {
// Prepare test preconditions.
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
let eve = H160::from_str("1000000000000000000000000000000000000004").unwrap();
let charlie = 3;
let eve = 4;
EvmBalances::set_balance(&charlie, u64::MAX);
EvmBalances::set_balance(&eve, 1);

Expand Down Expand Up @@ -880,7 +878,7 @@ fn deposit_flow_works() {
#[test]
fn deposit_works_new_account() {
new_test_ext().execute_with_ext(|_| {
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
let charlie = 3;

// Check test preconditions.
assert_eq!(EvmBalances::total_balance(&charlie), 0);
Expand Down
66 changes: 66 additions & 0 deletions frame/evm-balances/src/tests/fungible_conformance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate};
use paste::paste;

use crate::{mock::*, *};

macro_rules! run_tests {
($path:path, $($name:ident),*) => {
$(
paste! {
#[test]
fn [< $name _dust_trap_on >]() {
let trap_account = 11;
new_test_ext().execute_with_ext(|_| {
EvmBalances::set_balance(&trap_account, EvmBalances::minimum_balance());
$path::$name::<
EvmBalances,
<Test as Config>::AccountId,
>(Some(trap_account));
});
}

#[test]
fn [< $name _dust_trap_off >]() {
new_test_ext().execute_with_ext(|_| {
$path::$name::<
EvmBalances,
<Test as Config>::AccountId,
>(None);
});
}
}
)*
};
($path:path) => {
run_tests!(
$path,
mint_into_success,
mint_into_overflow,
mint_into_below_minimum,
burn_from_exact_success,
burn_from_best_effort_success,
burn_from_exact_insufficient_funds,
restore_success,
restore_overflow,
restore_below_minimum,
shelve_success,
shelve_insufficient_funds,
transfer_success,
transfer_expendable_all,
transfer_expendable_dust,
transfer_protect_preserve,
set_balance_mint_success,
set_balance_burn_success,
can_deposit_success,
can_deposit_below_minimum,
can_deposit_overflow,
can_withdraw_success,
can_withdraw_reduced_to_zero,
can_withdraw_balance_low,
reducible_balance_expendable,
reducible_balance_protect_preserve
);
};
}

run_tests!(conformance_tests::inspect_mutate);
36 changes: 18 additions & 18 deletions frame/evm-balances/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use frame_support::{assert_ok, traits::Currency, weights::Weight};
use pallet_evm::{FeeCalculator, FixedGasWeightMapping, GasWeightMapping, Runner};
use sp_core::{H160, U256};
use sp_runtime::traits::UniqueSaturatedInto;
use sp_std::str::FromStr;

use crate::{mock::*, *};

mod currency;
mod fungible;
mod fungible_conformance;

#[test]
fn basic_setup_works() {
Expand All @@ -29,7 +29,7 @@ fn basic_setup_works() {
#[test]
fn evm_fee_deduction() {
new_test_ext().execute_with_ext(|_| {
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
let charlie = 3;

// Seed account
let _ = <Test as pallet_evm::Config>::Currency::deposit_creating(&charlie, 100);
Expand All @@ -38,14 +38,14 @@ fn evm_fee_deduction() {
// Deduct fees as 10 units
let imbalance =
<<Test as pallet_evm::Config>::OnChargeTransaction as pallet_evm::OnChargeEVMTransaction<Test>>::withdraw_fee(
&charlie,
&H160::from_low_u64_be(charlie),
U256::from(10),
)
.unwrap();
assert_eq!(EvmBalances::free_balance(&charlie), 90);

// Refund fees as 5 units
<<Test as pallet_evm::Config>::OnChargeTransaction as pallet_evm::OnChargeEVMTransaction<Test>>::correct_and_deposit_fee(&charlie, U256::from(5), U256::from(5), imbalance);
<<Test as pallet_evm::Config>::OnChargeTransaction as pallet_evm::OnChargeEVMTransaction<Test>>::correct_and_deposit_fee(&H160::from_low_u64_be(charlie), U256::from(5), U256::from(5), imbalance);
assert_eq!(EvmBalances::free_balance(&charlie), 95);
});
}
Expand All @@ -59,8 +59,8 @@ fn evm_issuance_after_tip() {
let weight_limit = FixedGasWeightMapping::<Test>::gas_to_weight(gas_limit, true);

assert_ok!(<Test as pallet_evm::Config>::Runner::call(
alice(),
bob(),
alice_h160(),
bob_h160(),
Vec::new(),
U256::from(1),
gas_limit,
Expand Down Expand Up @@ -89,7 +89,7 @@ fn evm_issuance_after_tip() {
#[test]
fn evm_refunds_should_work() {
new_test_ext().execute_with_ext(|_| {
let before_call = EVM::account_basic(&alice()).0.balance;
let before_call = EVM::account_basic(&alice_h160()).0.balance;
// Gas price is not part of the actual fee calculations anymore, only the base fee.
//
// Because we first deduct max_fee_per_gas * gas_limit (2_000_000_000 * 1000000) we need
Expand All @@ -99,8 +99,8 @@ fn evm_refunds_should_work() {
let weight_limit = FixedGasWeightMapping::<Test>::gas_to_weight(gas_limit, true);

let _ = <Test as pallet_evm::Config>::Runner::call(
alice(),
bob(),
alice_h160(),
bob_h160(),
Vec::new(),
U256::from(1),
gas_limit,
Expand All @@ -117,15 +117,15 @@ fn evm_refunds_should_work() {

let (base_fee, _) = <Test as pallet_evm::Config>::FeeCalculator::min_gas_price();
let total_cost = (U256::from(21_000) * base_fee) + U256::from(1);
let after_call = EVM::account_basic(&alice()).0.balance;
let after_call = EVM::account_basic(&alice_h160()).0.balance;
assert_eq!(after_call, before_call - total_cost);
});
}

#[test]
fn evm_refunds_and_priority_should_work() {
new_test_ext().execute_with_ext(|_| {
let before_call = EVM::account_basic(&alice()).0.balance;
let before_call = EVM::account_basic(&alice_h160()).0.balance;
// We deliberately set a base fee + max tip > max fee.
// The effective priority tip will be 1GWEI instead 1.5GWEI:
// (max_fee_per_gas - base_fee).min(max_priority_fee)
Expand All @@ -138,8 +138,8 @@ fn evm_refunds_and_priority_should_work() {
let weight_limit = FixedGasWeightMapping::<Test>::gas_to_weight(gas_limit, true);

let _ = <Test as pallet_evm::Config>::Runner::call(
alice(),
bob(),
alice_h160(),
bob_h160(),
Vec::new(),
U256::from(1),
gas_limit,
Expand All @@ -157,7 +157,7 @@ fn evm_refunds_and_priority_should_work() {
let (base_fee, _) = <Test as pallet_evm::Config>::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 after_call = EVM::account_basic(&alice()).0.balance;
let after_call = EVM::account_basic(&alice_h160()).0.balance;
// The tip is deducted but never refunded to the caller.
assert_eq!(after_call, before_call - total_cost);
});
Expand All @@ -173,8 +173,8 @@ fn evm_call_should_fail_with_priority_greater_than_max_fee() {
let weight_limit = FixedGasWeightMapping::<Test>::gas_to_weight(gas_limit, true);

let result = <Test as pallet_evm::Config>::Runner::call(
alice(),
bob(),
alice_h160(),
bob_h160(),
Vec::new(),
U256::from(1),
gas_limit,
Expand Down Expand Up @@ -205,8 +205,8 @@ fn evm_call_should_succeed_with_priority_equal_to_max_fee() {
// Mimics the input for pre-eip-1559 transaction types where `gas_price`
// is used for both `max_fee_per_gas` and `max_priority_fee_per_gas`.
let result = <Test as pallet_evm::Config>::Runner::call(
alice(),
bob(),
alice_h160(),
bob_h160(),
Vec::new(),
U256::from(1),
gas_limit,
Expand Down