Skip to content

Commit

Permalink
move ethereum transaction test to generic
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Apr 15, 2024
1 parent 94da4b5 commit 13af112
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 169 deletions.
153 changes: 0 additions & 153 deletions runtime/integration-tests/src/evm/ethereum_transaction.rs

This file was deleted.

13 changes: 0 additions & 13 deletions runtime/integration-tests/src/evm/mod.rs

This file was deleted.

104 changes: 104 additions & 0 deletions runtime/integration-tests/src/generic/cases/ethereum_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use cfg_primitives::CFG;
use cfg_traits::ethereum::EthereumTransactor;
use ethereum::{LegacyTransaction, TransactionAction, TransactionV2};
use frame_support::{assert_err, assert_ok};
use pallet_evm::{ExitReason, ExitSucceed};
use sp_core::{H160, U256};

use crate::generic::{
config::Runtime,
env::Env,
envs::runtime_env::RuntimeEnv,
utils::{self},
};

// From: https://github.com/moonbeam-foundation/frontier/blob/moonbeam-polkadot-v1.1.0/frame/ethereum/src/tests/mod.rs#L44
//
// pragma solidity ^0.6.6;
// contract Test {
// function foo() external pure returns (bool) {
// return true;
// }
// function bar() external pure {
// require(false, "error_msg");
// }
// }
pub const TEST_CONTRACT_CODE: &str = "608060405234801561001057600080fd5b50610113806100206000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c8063c2985578146037578063febb0f7e146057575b600080fd5b603d605f565b604051808215151515815260200191505060405180910390f35b605d6068565b005b60006001905090565b600060db576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260098152602001807f6572726f725f6d7367000000000000000000000000000000000000000000000081525060200191505060405180910390fd5b56fea2646970667358221220fde68a3968e0e99b16fabf9b2997a78218b32214031f8e07e2c502daf603a69e64736f6c63430006060033";

fn call<T: Runtime>() {
RuntimeEnv::<T>::default().parachain_state_mut(|| {
// Addresses must be high enough to not map to the precompile space.
let creator_address = H160::from_low_u64_be(1_000_001);
let sender_address = H160::from_low_u64_be(1_000_002);

// From
// https://github.com/moonbeam-foundation/frontier/blob/moonbeam-polkadot-v1.1.0/frame/ethereum/src/tests/legacy.rs#L295
let foo = hex::decode("c2985578").unwrap();
let bar = hex::decode("febb0f7e").unwrap();

utils::evm::mint_balance_into_derived_account::<T>(creator_address, 1 * CFG);
utils::evm::mint_balance_into_derived_account::<T>(sender_address, 1 * CFG);

let contract_address = utils::evm::deploy_contract::<T>(
creator_address,
hex::decode(TEST_CONTRACT_CODE).unwrap(),
);

// Executing Bar should error out since the function returns an error.
assert_err!(
pallet_ethereum_transaction::Pallet::<T>::call(
sender_address,
contract_address,
bar.as_slice(),
U256::zero(),
U256::from(1),
U256::from(0x100000),
),
pallet_ethereum_transaction::Error::<T>::EvmExecutionFailed,
);

let t_hash = TransactionV2::Legacy(LegacyTransaction {
nonce: pallet_ethereum_transaction::Pallet::<T>::nonce(),
gas_price: U256::from(1),
gas_limit: U256::from(0x100000),
action: TransactionAction::Call(contract_address),
value: U256::zero(),
input: foo.as_slice().into(),
signature: pallet_ethereum_transaction::Pallet::<T>::get_transaction_signature()
.unwrap(),
})
.hash();

// Executing Foo should be OK and emit an event with the value returned by the
// function.
assert_ok!(pallet_ethereum_transaction::Pallet::<T>::call(
sender_address,
contract_address,
foo.as_slice(),
U256::zero(),
U256::from(1),
U256::from(0x100000),
));

utils::find_event::<T, _, _>(|e| {
let pallet_ethereum::Event::Executed {
from,
to,
transaction_hash,
exit_reason,
..
} = e;

assert_eq!(transaction_hash, t_hash);

(from == sender_address
&& to == contract_address
&& transaction_hash == t_hash
&& exit_reason == ExitReason::Succeed(ExitSucceed::Returned))
.then_some(())
})
.unwrap();
});
}

crate::test_for_runtimes!([development], call);
4 changes: 3 additions & 1 deletion runtime/integration-tests/src/generic/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ pub trait Runtime:
+ TryInto<pallet_pool_system::Event<Self>>
+ TryInto<pallet_liquidity_pools_gateway::Event<Self>>
+ TryInto<pallet_proxy::Event<Self>>
+ TryInto<pallet_ethereum::Event>
+ From<frame_system::Event<Self>>
+ From<pallet_balances::Event<Self>>
+ From<pallet_investments::Event<Self>>
Expand All @@ -227,7 +228,8 @@ pub trait Runtime:
+ From<pallet_preimage::Event<Self>>
+ From<pallet_collective::Event<Self, CouncilCollective>>
+ From<pallet_proxy::Event<Self>>
+ From<pallet_democracy::Event<Self>>;
+ From<pallet_democracy::Event<Self>>
+ From<pallet_ethereum::Event>;

type RuntimeOriginExt: Into<Result<RawOrigin<Self::AccountId>, <Self as frame_system::Config>::RuntimeOrigin>>
+ From<RawOrigin<Self::AccountId>>
Expand Down
1 change: 1 addition & 0 deletions runtime/integration-tests/src/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod utils;
// Test cases
mod cases {
mod account_derivation;
mod ethereum_transaction;
mod example;
mod investments;
mod liquidity_pools;
Expand Down
30 changes: 29 additions & 1 deletion runtime/integration-tests/src/generic/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ use cfg_types::{
};
use frame_support::{traits::fungible::Mutate, BoundedVec};
use frame_system::RawOrigin;
use pallet_evm::FeeCalculator;
use pallet_oracle_collection::types::CollectionInfo;
use pallet_pool_system::tranches::{TrancheInput, TrancheType};
use runtime_common::{account_conversion::convert_evm_address, oracle::Feeder};
use sp_core::H160;
use sp_core::{H160, U256};
use sp_runtime::{
traits::{Get, One, StaticLookup},
Perquintill,
Expand Down Expand Up @@ -266,4 +267,31 @@ pub mod evm {

pallet_balances::Pallet::<T>::mint_into(&derived_account.into(), balance).unwrap()
}

pub fn deploy_contract<T: Runtime>(address: H160, code: Vec<u8>) -> H160 {
let chain_id = pallet_evm_chain_id::Pallet::<T>::get();
let derived_address = convert_evm_address(chain_id, address.to_fixed_bytes());

let transaction_create_cost = T::config().gas_transaction_create;
let (base_fee, _) = T::FeeCalculator::min_gas_price();

pallet_evm::Pallet::<T>::create(
RawOrigin::from(Some(derived_address)).into(),
address,
code,
U256::from(0),
transaction_create_cost * 10,
U256::from(base_fee + 10),
None,
None,
Vec::new(),
)
.unwrap();

// returns the contract address
pallet_evm::AccountCodes::<T>::iter()
.find(|(_address, code)| code.len() > 0)
.unwrap()
.0
}
}
1 change: 0 additions & 1 deletion runtime/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#![cfg(test)]
#![allow(unused)]

mod evm;
mod generic;
mod rewards;
mod utils;
Expand Down

0 comments on commit 13af112

Please sign in to comment.