Skip to content

Commit 754efa0

Browse files
Fungible conformance tests: Inspect and Mutate (#110)
* Use u64 for evm system accounts at mock * Apply required changes to current tests * Include fungible conformance tests
1 parent ecb115a commit 754efa0

8 files changed

Lines changed: 126 additions & 40 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ log = { version = "0.4.17", default-features = false }
5959
mockall = "0.11"
6060
parity-db = "0.4.8"
6161
parking_lot = "0.12.1"
62+
paste = "1.0"
6263
rlp = { version = "0.5", default-features = false }
6364
scale-codec = { package = "parity-scale-codec", version = "3.2.1", default-features = false, features = ["derive"] }
6465
scale-info = { version = "2.3.1", default-features = false, features = ["derive"] }

frame/evm-balances/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fp-evm = { workspace = true }
2424
pallet-evm = { workspace = true }
2525
pallet-evm-system = { workspace = true }
2626
pallet-timestamp = { workspace = true }
27+
paste = { workspace = true }
2728
sp-core = { workspace = true }
2829
sp-io = { workspace = true }
2930

frame/evm-balances/src/mock.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use frame_support::{
2424
traits::{ConstU32, ConstU64, FindAuthor},
2525
weights::Weight,
2626
};
27-
use pallet_evm::{EnsureAddressNever, FixedGasWeightMapping, IdentityAddressMapping};
27+
use pallet_evm::{AddressMapping, EnsureAddressNever, FixedGasWeightMapping};
2828
use sp_core::{H160, H256, U256};
2929
use sp_runtime::{
3030
generic,
@@ -37,12 +37,33 @@ use crate::{self as pallet_evm_balances, *};
3737

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

40-
pub(crate) fn alice() -> H160 {
41-
H160::from_str("1000000000000000000000000000000000000000").unwrap()
40+
/// Alice account.
41+
pub(crate) fn alice() -> u64 {
42+
5234
4243
}
4344

44-
pub(crate) fn bob() -> H160 {
45-
H160::from_str("2000000000000000000000000000000000000000").unwrap()
45+
/// Alice H160 account.
46+
pub(crate) fn alice_h160() -> H160 {
47+
H160::from_low_u64_be(alice())
48+
}
49+
50+
/// Bob account.
51+
pub(crate) fn bob() -> u64 {
52+
4325
53+
}
54+
55+
/// Bob H160 account.
56+
pub(crate) fn bob_h160() -> H160 {
57+
H160::from_low_u64_be(bob())
58+
}
59+
60+
/// H160 into u64 address mapper.
61+
pub struct H160IntoU64;
62+
63+
impl AddressMapping<u64> for H160IntoU64 {
64+
fn into_account_id(address: H160) -> u64 {
65+
address.to_low_u64_be()
66+
}
4667
}
4768

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

92113
impl pallet_evm_system::Config for Test {
93114
type RuntimeEvent = RuntimeEvent;
94-
type AccountId = H160;
115+
type AccountId = u64;
95116
type Index = u64;
96117
type AccountData = AccountData<u64>;
97118
type OnNewAccount = ();
@@ -100,7 +121,7 @@ impl pallet_evm_system::Config for Test {
100121

101122
impl pallet_evm_balances::Config for Test {
102123
type RuntimeEvent = RuntimeEvent;
103-
type AccountId = H160;
124+
type AccountId = u64;
104125
type Balance = u64;
105126
type ExistentialDeposit = ConstU64<1>;
106127
type AccountStore = EvmSystem;
@@ -156,7 +177,7 @@ impl pallet_evm::Config for Test {
156177
EnsureAddressNever<<Self::AccountProvider as pallet_evm::AccountProvider>::AccountId>;
157178
type WithdrawOrigin =
158179
EnsureAddressNever<<Self::AccountProvider as pallet_evm::AccountProvider>::AccountId>;
159-
type AddressMapping = IdentityAddressMapping;
180+
type AddressMapping = H160IntoU64;
160181
type Currency = EvmBalances;
161182
type RuntimeEvent = RuntimeEvent;
162183
type PrecompilesType = ();
@@ -186,8 +207,8 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
186207
nonce: Default::default(),
187208
storage: Default::default(),
188209
};
189-
map.insert(alice(), init_genesis_account.clone());
190-
map.insert(bob(), init_genesis_account);
210+
map.insert(alice_h160(), init_genesis_account.clone());
211+
map.insert(bob_h160(), init_genesis_account);
191212
map
192213
},
193214
},

frame/evm-balances/src/tests/currency.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Tests regarding the functionality of the `Currency` trait set implementations.
22
33
use frame_support::{assert_noop, assert_ok, traits::Currency};
4-
use sp_core::H160;
54
use sp_runtime::TokenError;
6-
use sp_std::str::FromStr;
75

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

@@ -339,7 +337,7 @@ fn deposit_into_existing_fails_overflow() {
339337
#[test]
340338
fn deposit_into_existing_fails_dead_account() {
341339
new_test_ext().execute_with_ext(|_| {
342-
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
340+
let charlie = 3;
343341

344342
// Check test preconditions.
345343
assert_eq!(EvmBalances::total_balance(&charlie), 0);
@@ -358,7 +356,7 @@ fn deposit_into_existing_fails_dead_account() {
358356
fn deposit_creating_works() {
359357
new_test_ext().execute_with_ext(|_| {
360358
// Prepare test preconditions.
361-
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
359+
let charlie = 3;
362360
let deposited_amount = 10;
363361
assert!(!EvmSystem::account_exists(&charlie));
364362

@@ -493,7 +491,7 @@ fn withdraw_fails_expendability() {
493491
fn make_free_balance_be_works() {
494492
new_test_ext().execute_with_ext(|_| {
495493
// Prepare test preconditions.
496-
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
494+
let charlie = 3;
497495
let made_free_balance = 100;
498496

499497
// Check test preconditions.
@@ -537,8 +535,8 @@ fn evm_system_account_should_be_reaped() {
537535
fn transferring_too_high_value_should_not_panic() {
538536
new_test_ext().execute_with_ext(|_| {
539537
// Prepare test preconditions.
540-
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
541-
let eve = H160::from_str("1000000000000000000000000000000000000004").unwrap();
538+
let charlie = 3;
539+
let eve = 4;
542540
EvmBalances::make_free_balance_be(&charlie, u64::MAX);
543541
EvmBalances::make_free_balance_be(&eve, 1);
544542

frame/evm-balances/src/tests/fungible.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use frame_support::{
77
tokens::Precision,
88
},
99
};
10-
use sp_core::H160;
1110
use sp_runtime::TokenError;
12-
use sp_std::str::FromStr;
1311

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

@@ -779,8 +777,8 @@ fn transfer_fails_not_expendable() {
779777
fn transfer_fails_underflow() {
780778
new_test_ext().execute_with_ext(|_| {
781779
// Prepare test preconditions.
782-
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
783-
let eve = H160::from_str("1000000000000000000000000000000000000004").unwrap();
780+
let charlie = 3;
781+
let eve = 4;
784782
EvmBalances::set_balance(&charlie, u64::MAX);
785783
EvmBalances::set_balance(&eve, 1);
786784

@@ -880,7 +878,7 @@ fn deposit_flow_works() {
880878
#[test]
881879
fn deposit_works_new_account() {
882880
new_test_ext().execute_with_ext(|_| {
883-
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
881+
let charlie = 3;
884882

885883
// Check test preconditions.
886884
assert_eq!(EvmBalances::total_balance(&charlie), 0);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use frame_support::traits::fungible::{conformance_tests, Inspect, Mutate};
2+
use paste::paste;
3+
4+
use crate::{mock::*, *};
5+
6+
macro_rules! run_tests {
7+
($path:path, $($name:ident),*) => {
8+
$(
9+
paste! {
10+
#[test]
11+
fn [< $name _dust_trap_on >]() {
12+
let trap_account = 11;
13+
new_test_ext().execute_with_ext(|_| {
14+
EvmBalances::set_balance(&trap_account, EvmBalances::minimum_balance());
15+
$path::$name::<
16+
EvmBalances,
17+
<Test as Config>::AccountId,
18+
>(Some(trap_account));
19+
});
20+
}
21+
22+
#[test]
23+
fn [< $name _dust_trap_off >]() {
24+
new_test_ext().execute_with_ext(|_| {
25+
$path::$name::<
26+
EvmBalances,
27+
<Test as Config>::AccountId,
28+
>(None);
29+
});
30+
}
31+
}
32+
)*
33+
};
34+
($path:path) => {
35+
run_tests!(
36+
$path,
37+
mint_into_success,
38+
mint_into_overflow,
39+
mint_into_below_minimum,
40+
burn_from_exact_success,
41+
burn_from_best_effort_success,
42+
burn_from_exact_insufficient_funds,
43+
restore_success,
44+
restore_overflow,
45+
restore_below_minimum,
46+
shelve_success,
47+
shelve_insufficient_funds,
48+
transfer_success,
49+
transfer_expendable_all,
50+
transfer_expendable_dust,
51+
transfer_protect_preserve,
52+
set_balance_mint_success,
53+
set_balance_burn_success,
54+
can_deposit_success,
55+
can_deposit_below_minimum,
56+
can_deposit_overflow,
57+
can_withdraw_success,
58+
can_withdraw_reduced_to_zero,
59+
can_withdraw_balance_low,
60+
reducible_balance_expendable,
61+
reducible_balance_protect_preserve
62+
);
63+
};
64+
}
65+
66+
run_tests!(conformance_tests::inspect_mutate);

frame/evm-balances/src/tests/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use frame_support::{assert_ok, traits::Currency, weights::Weight};
44
use pallet_evm::{FeeCalculator, FixedGasWeightMapping, GasWeightMapping, Runner};
55
use sp_core::{H160, U256};
66
use sp_runtime::traits::UniqueSaturatedInto;
7-
use sp_std::str::FromStr;
87

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

1110
mod currency;
1211
mod fungible;
12+
mod fungible_conformance;
1313

1414
#[test]
1515
fn basic_setup_works() {
@@ -29,7 +29,7 @@ fn basic_setup_works() {
2929
#[test]
3030
fn evm_fee_deduction() {
3131
new_test_ext().execute_with_ext(|_| {
32-
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
32+
let charlie = 3;
3333

3434
// Seed account
3535
let _ = <Test as pallet_evm::Config>::Currency::deposit_creating(&charlie, 100);
@@ -38,14 +38,14 @@ fn evm_fee_deduction() {
3838
// Deduct fees as 10 units
3939
let imbalance =
4040
<<Test as pallet_evm::Config>::OnChargeTransaction as pallet_evm::OnChargeEVMTransaction<Test>>::withdraw_fee(
41-
&charlie,
41+
&H160::from_low_u64_be(charlie),
4242
U256::from(10),
4343
)
4444
.unwrap();
4545
assert_eq!(EvmBalances::free_balance(&charlie), 90);
4646

4747
// Refund fees as 5 units
48-
<<Test as pallet_evm::Config>::OnChargeTransaction as pallet_evm::OnChargeEVMTransaction<Test>>::correct_and_deposit_fee(&charlie, U256::from(5), U256::from(5), imbalance);
48+
<<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);
4949
assert_eq!(EvmBalances::free_balance(&charlie), 95);
5050
});
5151
}
@@ -59,8 +59,8 @@ fn evm_issuance_after_tip() {
5959
let weight_limit = FixedGasWeightMapping::<Test>::gas_to_weight(gas_limit, true);
6060

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

101101
let _ = <Test as pallet_evm::Config>::Runner::call(
102-
alice(),
103-
bob(),
102+
alice_h160(),
103+
bob_h160(),
104104
Vec::new(),
105105
U256::from(1),
106106
gas_limit,
@@ -117,15 +117,15 @@ fn evm_refunds_should_work() {
117117

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

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

140140
let _ = <Test as pallet_evm::Config>::Runner::call(
141-
alice(),
142-
bob(),
141+
alice_h160(),
142+
bob_h160(),
143143
Vec::new(),
144144
U256::from(1),
145145
gas_limit,
@@ -157,7 +157,7 @@ fn evm_refunds_and_priority_should_work() {
157157
let (base_fee, _) = <Test as pallet_evm::Config>::FeeCalculator::min_gas_price();
158158
let actual_tip = (max_fee_per_gas - base_fee).min(tip) * used_gas;
159159
let total_cost = (used_gas * base_fee) + U256::from(actual_tip) + U256::from(1);
160-
let after_call = EVM::account_basic(&alice()).0.balance;
160+
let after_call = EVM::account_basic(&alice_h160()).0.balance;
161161
// The tip is deducted but never refunded to the caller.
162162
assert_eq!(after_call, before_call - total_cost);
163163
});
@@ -173,8 +173,8 @@ fn evm_call_should_fail_with_priority_greater_than_max_fee() {
173173
let weight_limit = FixedGasWeightMapping::<Test>::gas_to_weight(gas_limit, true);
174174

175175
let result = <Test as pallet_evm::Config>::Runner::call(
176-
alice(),
177-
bob(),
176+
alice_h160(),
177+
bob_h160(),
178178
Vec::new(),
179179
U256::from(1),
180180
gas_limit,
@@ -205,8 +205,8 @@ fn evm_call_should_succeed_with_priority_equal_to_max_fee() {
205205
// Mimics the input for pre-eip-1559 transaction types where `gas_price`
206206
// is used for both `max_fee_per_gas` and `max_priority_fee_per_gas`.
207207
let result = <Test as pallet_evm::Config>::Runner::call(
208-
alice(),
209-
bob(),
208+
alice_h160(),
209+
bob_h160(),
210210
Vec::new(),
211211
U256::from(1),
212212
gas_limit,

0 commit comments

Comments
 (0)