Skip to content

Commit

Permalink
Fix evm tests, add more test case, fix typo
Browse files Browse the repository at this point in the history
Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed Feb 24, 2025
1 parent f38656f commit 4110a03
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 30 deletions.
6 changes: 6 additions & 0 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,12 @@ fn test_type_with_default_nonce_encode() {
}
}

let nonce_1_default = 0;
let nonce_2_default = TypeWithDefault::<Nonce, DefaultNonceProvider>::min_value();
let encode_1 = nonce_1_default.encode();
let encode_2 = nonce_2_default.encode();
assert_eq!(encode_1, encode_2);

let nonce_1_default = 13452234;
let nonce_2_default = TypeWithDefault::<Nonce, DefaultNonceProvider>::default();
let encode_1 = nonce_1_default.encode();
Expand Down
22 changes: 17 additions & 5 deletions crates/sp-domains-fraud-proof/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ async fn benchmark_bundle_with_evm_tx(
tx_type_to_use <= TX_TYPES,
"random tx_type is out of bounds"
);
let nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_info.address)
.unwrap()
.nonce;
let extrinsic = match tx_type_to_use {
0 => {
let evm_tx = generate_eip1559_tx::<TestRuntime>(
account_info.clone(),
U256::zero(),
nonce,
ethereum::TransactionAction::Create,
vec![1u8; 100],
gas_price,
Expand All @@ -103,7 +109,7 @@ async fn benchmark_bundle_with_evm_tx(
1 => {
let evm_tx = generate_eip2930_tx::<TestRuntime>(
account_info.clone(),
U256::zero(),
nonce,
ethereum::TransactionAction::Create,
vec![1u8; 100],
gas_price,
Expand All @@ -113,7 +119,7 @@ async fn benchmark_bundle_with_evm_tx(
2 => {
let evm_tx = generate_legacy_tx::<TestRuntime>(
account_info.clone(),
U256::zero(),
nonce,
ethereum::TransactionAction::Create,
vec![1u8; 100],
gas_price,
Expand All @@ -138,7 +144,7 @@ async fn benchmark_bundle_with_evm_tx(
genesis_block_hash,
function.clone(),
false,
0,
nonce.try_into().unwrap(),
1,
);
let signature = raw_payload.using_encoded(|e| {
Expand Down Expand Up @@ -656,9 +662,15 @@ async fn test_evm_domain_block_fee() {
.zip(tx_generators.into_iter())
.enumerate()
{
let nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, acc.address)
.unwrap()
.nonce;
let tx = generate_eth_domain_sc_extrinsic(tx_generator(
acc.clone(),
U256::zero(),
nonce,
ethereum::TransactionAction::Create,
vec![(i + 1) as u8; 100],
gas_price,
Expand Down
16 changes: 2 additions & 14 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ use subspace_core_primitives::solutions::{
pieces_to_solution_range, solution_range_to_pieces, SolutionRange,
};
use subspace_core_primitives::{PublicKey, Randomness, SlotNumber, U256};
use subspace_runtime_primitives::utility::MaybeIntoUtilityCall;
use subspace_runtime_primitives::utility::{DefaultNonceProvider, MaybeIntoUtilityCall};
use subspace_runtime_primitives::{
maximum_normal_block_length, AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash,
HoldIdentifier, Moment, Nonce, Signature, SlowAdjustingFeeUpdate, BLOCK_WEIGHT_FOR_2_SEC,
Expand Down Expand Up @@ -192,18 +192,6 @@ parameter_types! {

pub type SS58Prefix = ConstU16<6094>;

// `DefaultNonceProvider` uses the current block number as the nonce of the new account,
// this is used to prevent the replay attack see https://wiki.polkadot.network/docs/transaction-attacks#replay-attack
// for more detail.
#[derive(Debug, TypeInfo)]
pub struct DefaultNonceProvider;

impl Get<Nonce> for DefaultNonceProvider {
fn get() -> Nonce {
System::block_number()
}
}

// Configure FRAME pallets to include in runtime.

impl frame_system::Config for Runtime {
Expand All @@ -225,7 +213,7 @@ impl frame_system::Config for Runtime {
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = AccountIdLookup<AccountId, ()>;
/// The type for storing how many extrinsics an account has signed.
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider>;
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider<System, Nonce>>;
/// The type for hashing blocks and tries.
type Hash = Hash;
/// The hashing algorithm used.
Expand Down
70 changes: 60 additions & 10 deletions domains/client/domain-operator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ async fn test_private_evm_domain_create_contracts_with_allow_list_default() {
.unwrap();

// Any account should be able to create contracts by default
let mut eth_nonce = U256::zero();
let mut eth_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[0].address)
.unwrap()
.nonce;
let eth_tx = generate_eth_domain_extrinsic(
account_infos[0].clone(),
ethereum::TransactionAction::Create,
Expand All @@ -330,7 +335,12 @@ async fn test_private_evm_domain_create_contracts_with_allow_list_default() {
"Unexpectedly failed to send self-contained extrinsic"
);

let mut evm_nonce = U256::zero();
let mut evm_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[1].address)
.unwrap()
.nonce;
let mut evm_tx = generate_evm_domain_call(
account_infos[1].clone(),
ethereum::TransactionAction::Create,
Expand Down Expand Up @@ -431,7 +441,12 @@ async fn test_public_evm_domain_create_contracts() {
.unwrap();

// Any account should be able to create contracts in a public EVM domain
let mut eth_nonce = U256::zero();
let mut eth_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[0].address)
.unwrap()
.nonce;
let eth_tx = generate_eth_domain_extrinsic(
account_infos[0].clone(),
ethereum::TransactionAction::Create,
Expand All @@ -447,7 +462,12 @@ async fn test_public_evm_domain_create_contracts() {
"Unexpectedly failed to send self-contained extrinsic"
);

let mut evm_nonce = U256::zero();
let mut evm_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[1].address)
.unwrap()
.nonce;
let mut evm_tx = generate_evm_domain_call(
account_infos[1].clone(),
ethereum::TransactionAction::Create,
Expand Down Expand Up @@ -548,7 +568,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_reject_all() {
.unwrap();

// Create contracts used for testing contract calls
let mut eth_nonce = U256::zero();
let mut eth_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[0].address)
.unwrap()
.nonce;
let mut eth_tx = generate_eth_domain_extrinsic(
account_infos[0].clone(),
ethereum::TransactionAction::Create,
Expand All @@ -560,7 +585,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_reject_all() {

alice.send_extrinsic(eth_tx).await.unwrap();

let mut evm_nonce = U256::zero();
let mut evm_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[1].address)
.unwrap()
.nonce;
let mut evm_tx = generate_evm_domain_call(
account_infos[1].clone(),
ethereum::TransactionAction::Create,
Expand Down Expand Up @@ -771,7 +801,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_single() {
.unwrap();

// Create contracts used for testing contract calls
let mut eth_nonce = U256::zero();
let mut eth_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[0].address)
.unwrap()
.nonce;
let mut eth_tx = generate_eth_domain_extrinsic(
account_infos[0].clone(),
ethereum::TransactionAction::Create,
Expand All @@ -782,7 +817,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_single() {

alice.send_extrinsic(eth_tx).await.unwrap();

let mut evm_nonce = U256::zero();
let mut evm_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[1].address)
.unwrap()
.nonce;
let mut evm_tx = generate_evm_domain_call(
account_infos[1].clone(),
ethereum::TransactionAction::Create,
Expand Down Expand Up @@ -1015,7 +1055,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_multiple() {
.unwrap();

// Accounts 0-2 should be able to create contracts
let mut eth_nonce = U256::zero();
let mut eth_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[1].address)
.unwrap()
.nonce;
let mut eth_tx = generate_eth_domain_extrinsic(
account_infos[1].clone(),
ethereum::TransactionAction::Create,
Expand All @@ -1031,7 +1076,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_multiple() {
"Unexpectedly failed to send self-contained extrinsic"
);

let mut evm_nonce = U256::zero();
let mut evm_nonce = alice
.client
.runtime_api()
.account_basic(alice.client.info().best_hash, account_infos[2].address)
.unwrap()
.nonce;
let mut evm_tx = generate_evm_domain_call(
account_infos[2].clone(),
ethereum::TransactionAction::Create,
Expand Down
2 changes: 1 addition & 1 deletion test/subspace-test-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ impl MockConsensusNode {
.expect("Fail to get account nonce")
}

/// Get the nonce of the node account
/// Get the nonce of the given account
pub fn account_nonce_of(&self, account_id: AccountId) -> u32 {
self.client
.runtime_api()
Expand Down

0 comments on commit 4110a03

Please sign in to comment.