Skip to content

Commit 9492b3d

Browse files
Add more ethereum checks for evm to native swap tests
1 parent e7e34d7 commit 9492b3d

File tree

1 file changed

+66
-42
lines changed

1 file changed

+66
-42
lines changed
Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use pallet_evm::{Config, Runner};
1+
use fp_ethereum::ValidatedTransaction;
2+
use fp_evm::{ExitReason, ExitSucceed};
3+
use frame_support::{
4+
dispatch::{Pays, PostDispatchInfo},
5+
traits::OnFinalize,
6+
};
7+
use pallet_evm::GasWeightMapping;
28
use precompile_utils::{EvmDataWriter, LogsBuilder};
39
use sp_core::H256;
410

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

7-
/// A test utility that performs gas to fee computation.
8-
/// Might not be explicitly correct, but does the job.
9-
fn gas_to_fee(gas: u64) -> Balance {
10-
u128::from(gas) * u128::try_from(*GAS_PRICE).unwrap()
11-
}
12-
1313
/// This test verifies that the swap precompile call works in the happy path.
1414
#[test]
1515
fn swap_works() {
@@ -20,7 +20,6 @@ fn swap_works() {
2020
let swap_balance = 100;
2121

2222
let expected_gas_usage: u64 = 21216 + 200;
23-
let expected_fee: Balance = gas_to_fee(expected_gas_usage);
2423

2524
// Check test preconditions.
2625
assert_eq!(EvmBalances::total_balance(&alice_evm()), INIT_BALANCE);
@@ -30,48 +29,47 @@ fn swap_works() {
3029
System::set_block_number(1);
3130

3231
// Prepare EVM call.
33-
let input = EvmDataWriter::new_with_selector(precompile::Action::Swap)
34-
.write(H256::from(swap_native_account.as_ref()))
35-
.build();
32+
let transaction = pallet_ethereum::Transaction::EIP1559(ethereum::EIP1559Transaction {
33+
chain_id: <Test as pallet_evm::Config>::ChainId::get(),
34+
nonce: pallet_evm::Pallet::<Test>::account_basic(&alice_evm())
35+
.0
36+
.nonce,
37+
max_priority_fee_per_gas: 0.into(),
38+
max_fee_per_gas: 0.into(),
39+
gas_limit: 50_000.into(), // a reasonable upper bound for tests
40+
action: ethereum::TransactionAction::Call(*PRECOMPILE_ADDRESS),
41+
value: U256::from(swap_balance),
42+
input: EvmDataWriter::new_with_selector(precompile::Action::Swap)
43+
.write(H256::from(swap_native_account.as_ref()))
44+
.build(),
45+
access_list: Default::default(),
46+
odd_y_parity: false,
47+
r: Default::default(),
48+
s: Default::default(),
49+
});
50+
let transaction_hash = transaction.hash();
3651

3752
// Invoke the function under test.
38-
let execinfo = <Test as pallet_evm::Config>::Runner::call(
39-
alice_evm(),
40-
*PRECOMPILE_ADDRESS,
41-
input,
42-
swap_balance.into(),
43-
50_000, // a reasonable upper bound for tests
44-
Some(*GAS_PRICE),
45-
Some(*GAS_PRICE),
46-
None,
47-
Vec::new(),
48-
true,
49-
true,
50-
None,
51-
None,
52-
Test::config(),
53-
)
54-
.unwrap();
55-
assert_eq!(
56-
execinfo.exit_reason,
57-
fp_evm::ExitReason::Succeed(fp_evm::ExitSucceed::Returned)
58-
);
59-
assert_eq!(execinfo.used_gas.standard, expected_gas_usage.into());
60-
assert_eq!(execinfo.value, EvmDataWriter::new().write(true).build());
53+
let post_info =
54+
pallet_ethereum::ValidatedTransaction::<Test>::apply(alice_evm(), transaction).unwrap();
55+
6156
assert_eq!(
62-
execinfo.logs,
63-
vec![LogsBuilder::new(*PRECOMPILE_ADDRESS).log3(
64-
precompile::SELECTOR_LOG_SWAP,
65-
alice_evm(),
66-
H256::from(swap_native_account.as_ref()),
67-
EvmDataWriter::new().write(swap_balance).build(),
68-
)]
57+
post_info,
58+
PostDispatchInfo {
59+
actual_weight: Some(
60+
<Test as pallet_evm::Config>::GasWeightMapping::gas_to_weight(
61+
expected_gas_usage,
62+
true
63+
)
64+
),
65+
pays_fee: Pays::No
66+
},
6967
);
7068

7169
// Assert state changes.
7270
assert_eq!(
7371
EvmBalances::total_balance(&alice_evm()),
74-
INIT_BALANCE - swap_balance - expected_fee
72+
INIT_BALANCE - swap_balance
7573
);
7674
assert_eq!(
7775
EvmBalances::total_balance(&BridgePotEvm::get()),
@@ -82,5 +80,31 @@ fn swap_works() {
8280
Balances::total_balance(&BridgePotNative::get()),
8381
INIT_BALANCE - swap_balance
8482
);
83+
System::assert_has_event(RuntimeEvent::Ethereum(pallet_ethereum::Event::Executed {
84+
from: alice_evm(),
85+
to: *PRECOMPILE_ADDRESS,
86+
transaction_hash,
87+
exit_reason: ExitReason::Succeed(ExitSucceed::Returned),
88+
extra_data: vec![],
89+
}));
90+
91+
// Finalize block to check transaction logs.
92+
Ethereum::on_finalize(1);
93+
94+
let transaction_logs = pallet_ethereum::pallet::CurrentTransactionStatuses::<Test>::get()
95+
.unwrap()
96+
.first()
97+
.cloned()
98+
.unwrap()
99+
.logs;
100+
assert_eq!(
101+
transaction_logs,
102+
vec![LogsBuilder::new(*PRECOMPILE_ADDRESS).log3(
103+
precompile::SELECTOR_LOG_SWAP,
104+
alice_evm(),
105+
H256::from(swap_native_account.as_ref()),
106+
EvmDataWriter::new().write(swap_balance).build(),
107+
)]
108+
);
85109
});
86110
}

0 commit comments

Comments
 (0)