Skip to content

Commit

Permalink
Add function to generate pallet-evm transaction calls
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jan 22, 2025
1 parent 56d0e56 commit 179c2ba
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 45 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions crates/sp-domains-fraud-proof/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ domain-block-builder = { version = "0.1.0", path = "../../domains/client/block-b
domain-block-preprocessor = { version = "0.1.0", path = "../../domains/client/block-preprocessor" }
domain-test-service = { version = "0.1.0", path = "../../domains/test/service" }
ethereum = "0.15.0"
fp-rpc = { version = "3.0.0-dev", git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968", features = ['default'] }
fp-self-contained = { version = "1.0.0-dev", git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968", features = ['default'] }
fp-rpc = { version = "3.0.0-dev", git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968" }
fp-self-contained = { version = "1.0.0-dev", git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968" }
frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
futures = "0.3.31"
libsecp256k1 = { version = "0.7.1", features = ["static-context", "hmac"] }
pallet-balances = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
pallet-ethereum = { git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968", features = ['default'] }
pallet-ethereum = { git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968" }
parking_lot = "0.12.2"
rand = { version = "0.8.5", features = ["min_const_gen"] }
rlp = "0.5.2"
Expand Down
31 changes: 20 additions & 11 deletions crates/sp-domains-fraud-proof/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use codec::Encode;
use domain_runtime_primitives::{Balance, CheckExtrinsicsValidityError};
use domain_test_service::evm_domain_test_runtime::{
Runtime as TestRuntime, RuntimeCall, Signature, UncheckedExtrinsic as RuntimeUncheckedExtrinsic,
Runtime as TestRuntime, RuntimeCall, Signature, UncheckedExtrinsic as EvmUncheckedExtrinsic,
};
use domain_test_service::EcdsaKeyring::{Alice, Charlie};
use domain_test_service::Sr25519Keyring::Ferdie;
use domain_test_service::{construct_extrinsic_raw_payload, EvmDomainNode};
use ethereum::TransactionV2 as Transaction;
use ethereum::TransactionV2 as EthereumTransaction;
use fp_rpc::EthereumRuntimeRPCApi;
use pallet_ethereum::Call;
use rand::distributions::{Distribution, Uniform};
use sc_client_api::{HeaderBackend, StorageProof};
use sc_service::{BasePath, Role};
Expand All @@ -25,11 +24,13 @@ use sp_runtime::OpaqueExtrinsic;
use subspace_test_service::{produce_block_with, produce_blocks, MockConsensusNode};
use tempfile::TempDir;

/// Generate a self-contained EVM domain extrinsic.
// This function depends on the macro-constructed `TestRuntime::RuntimeCall` enum, so it can't be
// shared via `sp_domains::test_ethereum`.
pub fn generate_evm_domain_extrinsic(tx: Transaction) -> RuntimeUncheckedExtrinsic {
let call = Call::<TestRuntime>::transact { transaction: tx };

/// Generate a self-contained EVM domain extrinsic, which can be passed to
/// `runtime_api().check_extrinsics_and_do_pre_dispatch()`.
pub fn generate_eth_domain_sc_extrinsic(tx: EthereumTransaction) -> EvmUncheckedExtrinsic {
let call = pallet_ethereum::Call::<TestRuntime>::transact { transaction: tx };
fp_self_contained::UncheckedExtrinsic::new(RuntimeCall::Ethereum(call), None).unwrap()
}

Expand Down Expand Up @@ -97,7 +98,7 @@ async fn benchmark_bundle_with_evm_tx(
vec![1u8; 100],
gas_price,
);
generate_evm_domain_extrinsic(evm_tx)
generate_eth_domain_sc_extrinsic(evm_tx)
}
1 => {
let evm_tx = generate_eip2930_tx::<TestRuntime>(
Expand All @@ -107,7 +108,7 @@ async fn benchmark_bundle_with_evm_tx(
vec![1u8; 100],
gas_price,
);
generate_evm_domain_extrinsic(evm_tx)
generate_eth_domain_sc_extrinsic(evm_tx)
}
2 => {
let evm_tx = generate_legacy_tx::<TestRuntime>(
Expand All @@ -117,7 +118,7 @@ async fn benchmark_bundle_with_evm_tx(
vec![1u8; 100],
gas_price,
);
generate_evm_domain_extrinsic(evm_tx)
generate_eth_domain_sc_extrinsic(evm_tx)
}
3 => {
let ecdsa_key = Pair::from_seed_slice(&account_info.private_key.0).unwrap();
Expand Down Expand Up @@ -624,7 +625,15 @@ async fn test_evm_domain_block_fee() {
// Construct and send evm transaction
#[allow(clippy::type_complexity)]
let tx_generators: Vec<
Box<dyn Fn(AccountInfo, U256, ethereum::TransactionAction, Vec<u8>, U256) -> Transaction>,
Box<
dyn Fn(
AccountInfo,
U256,
ethereum::TransactionAction,
Vec<u8>,
U256,
) -> EthereumTransaction,
>,
> = vec![
Box::new(generate_eip2930_tx::<TestRuntime>),
Box::new(generate_eip1559_tx::<TestRuntime>),
Expand All @@ -635,7 +644,7 @@ async fn test_evm_domain_block_fee() {
.zip(tx_generators.into_iter())
.enumerate()
{
let tx = generate_evm_domain_extrinsic(tx_generator(
let tx = generate_eth_domain_sc_extrinsic(tx_generator(
acc.clone(),
U256::zero(),
ethereum::TransactionAction::Create,
Expand Down
42 changes: 15 additions & 27 deletions crates/sp-domains/src/test_ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,29 @@ pub fn address_build(seed_number: u128) -> AccountInfo {
}
}

pub fn max_extrinsic_gas<TestRuntime: frame_system::Config + pallet_evm::Config>(
multiplier: u64,
) -> u64 {
let limits: frame_system::limits::BlockWeights =
<TestRuntime as frame_system::Config>::BlockWeights::get();
// `limits.get(DispatchClass::Normal).max_extrinsic` is too large to use as `gas_limit`
// thus use `base_extrinsic`
let max_extrinsic = limits.get(DispatchClass::Normal).base_extrinsic * multiplier;

<TestRuntime as pallet_evm::Config>::GasWeightMapping::weight_to_gas(max_extrinsic)
}

pub fn generate_legacy_tx<TestRuntime: frame_system::Config + pallet_evm::Config>(
account_info: AccountInfo,
nonce: U256,
action: ethereum::TransactionAction,
input: Vec<u8>,
gas_price: U256,
) -> Transaction {
let limits: frame_system::limits::BlockWeights =
<TestRuntime as frame_system::Config>::BlockWeights::get();
// `limits.get(DispatchClass::Normal).max_extrinsic` is too large to use as `gas_limit`
// thus use `base_extrinsic`
let max_extrinsic = limits.get(DispatchClass::Normal).base_extrinsic * 1000;
let max_extrinsic_gas =
<TestRuntime as pallet_evm::Config>::GasWeightMapping::weight_to_gas(max_extrinsic);

LegacyUnsignedTransaction {
nonce,
gas_price,
gas_limit: U256::from(max_extrinsic_gas),
gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(1000)),
action,
value: U256::zero(),
input,
Expand All @@ -64,18 +68,10 @@ pub fn generate_eip2930_tx<TestRuntime: frame_system::Config + pallet_evm::Confi
input: Vec<u8>,
gas_price: U256,
) -> Transaction {
let limits: frame_system::limits::BlockWeights =
<TestRuntime as frame_system::Config>::BlockWeights::get();
// `limits.get(DispatchClass::Normal).max_extrinsic` is too large to use as `gas_limit`
// thus use `base_extrinsic`
let max_extrinsic = limits.get(DispatchClass::Normal).base_extrinsic * 100;
let max_extrinsic_gas =
<TestRuntime as pallet_evm::Config>::GasWeightMapping::weight_to_gas(max_extrinsic);

EIP2930UnsignedTransaction {
nonce,
gas_price,
gas_limit: U256::from(max_extrinsic_gas),
gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(100)),
action,
value: U256::one(),
input,
Expand All @@ -90,19 +86,11 @@ pub fn generate_eip1559_tx<TestRuntime: frame_system::Config + pallet_evm::Confi
input: Vec<u8>,
gas_price: U256,
) -> Transaction {
let limits: frame_system::limits::BlockWeights =
<TestRuntime as frame_system::Config>::BlockWeights::get();
// `limits.get(DispatchClass::Normal).max_extrinsic` is too large to use as `gas_limit`
// thus use `base_extrinsic`
let max_extrinsic = limits.get(DispatchClass::Normal).base_extrinsic * 1000;
let max_extrinsic_gas =
<TestRuntime as pallet_evm::Config>::GasWeightMapping::weight_to_gas(max_extrinsic);

EIP1559UnsignedTransaction {
nonce,
max_priority_fee_per_gas: U256::from(1),
max_fee_per_gas: gas_price,
gas_limit: U256::from(max_extrinsic_gas),
gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(1000)),
action,
value: U256::zero(),
input,
Expand Down
4 changes: 4 additions & 0 deletions domains/client/domain-operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ auto-id-domain-test-runtime = { version = "0.1.0", path = "../../test/runtime/au
cross-domain-message-gossip = { path = "../../client/cross-domain-message-gossip" }
domain-test-service = { version = "0.1.0", path = "../../test/service" }
domain-test-primitives = { version = "0.1.0", path = "../../test/primitives" }
ethereum = "0.15.0"
evm-domain-test-runtime = { version = "0.1.0", path = "../../test/runtime/evm" }
fp-self-contained = { version = "1.0.0-dev", git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968" }
frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
hex-literal = "0.4.1"
pallet-balances = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
pallet-domains = { version = "0.1.0", path = "../../../crates/pallet-domains" }
pallet-domain-sudo = { version = "0.1.0", path = "../../pallets/domain-sudo" }
pallet-ethereum = { git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968" }
pallet-evm = { version = "6.0.0-dev", git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968" }
pallet-evm-tracker = { version = "0.1.0", path = "../../pallets/evm-tracker" }
pallet-messenger = { version = "0.1.0", path = "../../../domains/pallets/messenger" }
pallet-sudo = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
Expand Down
58 changes: 54 additions & 4 deletions domains/client/domain-operator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ use codec::{Decode, Encode};
use cross_domain_message_gossip::get_channel_state;
use domain_runtime_primitives::{AccountId20Converter, AccountIdConverter, Hash};
use domain_test_primitives::{OnchainStateApi, TimestampApi};
use domain_test_service::evm_domain_test_runtime::{Header, UncheckedExtrinsic};
use domain_test_service::evm_domain_test_runtime::{
Header, Runtime as TestRuntime, RuntimeCall, UncheckedExtrinsic as EvmUncheckedExtrinsic,
};
use domain_test_service::EcdsaKeyring::{Alice, Bob, Charlie, Eve};
use domain_test_service::Sr25519Keyring::{self, Alice as Sr25519Alice, Ferdie};
use domain_test_service::{construct_extrinsic_generic, AUTO_ID_DOMAIN_ID, EVM_DOMAIN_ID};
use ethereum::TransactionV2 as EthereumTransaction;
use futures::StreamExt;
use hex_literal::hex;
use pallet_domains::OperatorConfig;
Expand All @@ -27,10 +30,11 @@ use sp_api::{ProvideRuntimeApi, StorageProof};
use sp_consensus::SyncOracle;
use sp_core::storage::StateVersion;
use sp_core::traits::{FetchRuntimeCode, SpawnEssentialNamed};
use sp_core::{Pair, H256};
use sp_core::{Pair, H256, U256};
use sp_domain_digests::AsPredigest;
use sp_domains::core_api::DomainCoreApi;
use sp_domains::merkle_tree::MerkleTree;
use sp_domains::test_ethereum::{max_extrinsic_gas, AccountInfo};
use sp_domains::{
Bundle, BundleValidity, ChainId, ChannelId, DomainsApi, HeaderHashingFor, InboxedBundle,
InvalidBundleType, PermissionedActionAllowedBy, Transfers,
Expand All @@ -46,7 +50,7 @@ use sp_messenger::MessengerApi;
use sp_mmr_primitives::{EncodableOpaqueLeaf, LeafProof as MmrProof};
use sp_runtime::generic::{BlockId, DigestItem};
use sp_runtime::traits::{
BlakeTwo256, Block as BlockT, Convert, Hash as HashT, Header as HeaderT, Zero,
BlakeTwo256, Block as BlockT, Convert, Extrinsic, Hash as HashT, Header as HeaderT, Zero,
};
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidityError,
Expand Down Expand Up @@ -78,6 +82,52 @@ fn number_of(consensus_node: &MockConsensusNode, block_hash: Hash) -> u32 {
.unwrap_or_else(|| panic!("header {block_hash} not in the chain"))
}

// These functions depend on the macro-constructed `TestRuntime::RuntimeCall` enum, so they can't
// be shared via `sp_domains::test_ethereum`.

/// Generate a self-contained EVM domain extrinsic, which can be passed to
/// `runtime_api().check_extrinsics_and_do_pre_dispatch()`.
pub fn generate_eth_domain_sc_extrinsic(tx: EthereumTransaction) -> EvmUncheckedExtrinsic {
let call = pallet_ethereum::Call::<TestRuntime>::transact { transaction: tx };
fp_self_contained::UncheckedExtrinsic::new(RuntimeCall::Ethereum(call), None).unwrap()
}

/// Generate a pallet-evm call, which can be passed to `construct_and_send_extrinsic_with()`.
pub fn generate_evm_domain_call_extrinsic(
account_info: AccountInfo,
nonce: Option<U256>,
init: Vec<u8>,
max_fee_per_gas: U256,
use_create: bool,
) -> <TestRuntime as frame_system::Config>::RuntimeCall {
let call = if use_create {
pallet_evm::Call::<TestRuntime>::create {
source: account_info.address,
init,
value: U256::zero(),
gas_limit: max_extrinsic_gas::<TestRuntime>(1000),
max_fee_per_gas,
access_list: vec![],
max_priority_fee_per_gas: Some(U256::from(1)),
nonce,
}
} else {
pallet_evm::Call::<TestRuntime>::create2 {
source: account_info.address,
init,
salt: H256::zero(),
value: U256::zero(),
gas_limit: max_extrinsic_gas::<TestRuntime>(1000),
max_fee_per_gas,
access_list: vec![],
max_priority_fee_per_gas: Some(U256::from(1)),
nonce,
}
};

RuntimeCall::EVM(call)
}

#[tokio::test(flavor = "multi_thread")]
async fn test_domain_instance_bootstrapper() {
let directory = TempDir::new().expect("Must be able to create temporary directory");
Expand Down Expand Up @@ -3394,7 +3444,7 @@ async fn set_new_code_should_work() {
.unwrap()
.into_iter()
.map(|encoded_extrinsic| {
UncheckedExtrinsic::decode(&mut encoded_extrinsic.encode().as_slice()).unwrap()
EvmUncheckedExtrinsic::decode(&mut encoded_extrinsic.encode().as_slice()).unwrap()
})
.collect::<Vec<_>>();
panic!("`set_code` not executed, extrinsics in the block: {extrinsics:?}")
Expand Down

0 comments on commit 179c2ba

Please sign in to comment.