Skip to content

Commit

Permalink
check contracts integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrq1911 committed Sep 20, 2024
1 parent 737dc17 commit eb51786
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ polkadot-primitives = { workspace = true }
polkadot-service = { workspace = true, features = ["full-node"] }
polkadot-runtime-parachains = { workspace = true }
rococo-runtime = { workspace = true }
module-evm-utility-macro = { workspace = true }
num_enum = { workspace = true }

ethabi = { workspace = true }
serde_json = { workspace = true }
Expand Down
100 changes: 100 additions & 0 deletions integration-tests/src/contracts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use crate::evm::native_asset_ethereum_address;
use crate::polkadot_test_net::Hydra;
use crate::polkadot_test_net::TestNet;
use crate::polkadot_test_net::ALICE;
use crate::utils::contracts::deploy_contract;
use hex_literal::hex;
use hydradx_runtime::evm::Executor;
use hydradx_runtime::AccountId;
use hydradx_runtime::EVMAccounts;
use hydradx_runtime::Runtime;
use hydradx_traits::evm::CallContext;
use hydradx_traits::evm::EvmAddress;
use hydradx_traits::evm::InspectEvmAccounts;
use hydradx_traits::evm::EVM;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use pallet_evm::ExitReason::Succeed;
use sp_core::H256;
use sp_core::{RuntimeDebug, U256};
use xcm_emulator::Network;
use xcm_emulator::TestExt;

pub fn deployer() -> EvmAddress {
EVMAccounts::evm_address(&Into::<AccountId>::into(ALICE))
}

#[module_evm_utility_macro::generate_function_selector]
#[derive(RuntimeDebug, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Function {
IsContract = "isContract(address)",
Check = "check(address)",
}

fn is_contract(checker: EvmAddress, address: EvmAddress) -> bool {
let mut data = Into::<u32>::into(Function::Check).to_be_bytes().to_vec();
data.extend_from_slice(H256::from(address).as_bytes());
let context = CallContext {
contract: checker,
sender: Default::default(),
origin: Default::default(),
};
let (res, _) = Executor::<Runtime>::call(context, data, U256::zero(), 100_000);
match res {
Succeed(_) => true,
_ => false,
}
}

#[test]
fn contract_check_succeeds_on_deployed_contract() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());

assert_eq!(is_contract(checker, checker), true);
});
}

#[test]
fn contract_check_fails_on_eoa() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());

assert_eq!(is_contract(checker, deployer()), false);
});
}

#[test]
fn contract_check_fails_on_precompile() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());

assert_eq!(is_contract(checker, native_asset_ethereum_address()), false);
});
}

#[test]
fn contract_check_succeeds_on_precompile_with_code() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());
pallet_evm::AccountCodes::<Runtime>::insert(
native_asset_ethereum_address(),
&hex!["365f5f375f5f365f73bebebebebebebebebebebebebebebebebebebebe5af43d5f5f3e5f3d91602a57fd5bf3"][..],
);
assert_eq!(is_contract(checker, native_asset_ethereum_address()), true);
});
}

#[test]
fn contract_check_succeeds_on_precompile_with_invalid_code() {
TestNet::reset();
Hydra::execute_with(|| {
let checker = deploy_contract("ContractCheck", deployer());
pallet_evm::AccountCodes::<Runtime>::insert(native_asset_ethereum_address(), &hex!["00"][..]);
assert_eq!(is_contract(checker, native_asset_ethereum_address()), true);
});
}
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod asset_registry;
mod bonds;
mod call_filter;
mod circuit_breaker;
mod contracts;
mod cross_chain_transfer;
mod dca;
mod dust;
Expand Down
2 changes: 1 addition & 1 deletion runtime/hydradx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ codec = { workspace = true }
scale-info = { version = "2.3.1", default-features = false, features = ["derive"] }
smallvec = "1.9.0"
log = { workspace = true }
num_enum = { version = "0.5.1", default-features = false }
num_enum = { workspace = true }
evm = { workspace = true, features = ["with-codec"] }

# local dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "Address",
"sourceName": "contracts/Address.sol",
"abi": [],
"bytecode": "0x60566050600b82828239805160001a6073146043577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220a3bd1167f4b64c2712fda719899ee18c248d5045fce5d06bf79bc467f1f56b1064736f6c63430008180033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220a3bd1167f4b64c2712fda719899ee18c248d5045fce5d06bf79bc467f1f56b1064736f6c63430008180033",
"linkReferences": {},
"deployedLinkReferences": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "ContractCheck",
"sourceName": "contracts/ContractCheck.sol",
"abi": [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "addr",
"type": "address"
}
],
"name": "Checked",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "_addr",
"type": "address"
}
],
"name": "check",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_addr",
"type": "address"
}
],
"name": "isContract",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
}
],
"bytecode": "0x608060405234801561001057600080fd5b506102b3806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063162790551461003b578063c23697a81461006b575b600080fd5b6100556004803603810190610050919061019d565b610087565b60405161006291906101e5565b60405180910390f35b6100856004803603810190610080919061019d565b610099565b005b600061009282610127565b9050919050565b6100a281610087565b6100e1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016100d89061025d565b60405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff167fe20314ef67750397f75c06a974fa978eaf8f26660fa2e4c14078f87ed2f9e8d760405160405180910390a250565b600080823b905060008111915050919050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061016a8261013f565b9050919050565b61017a8161015f565b811461018557600080fd5b50565b60008135905061019781610171565b92915050565b6000602082840312156101b3576101b261013a565b5b60006101c184828501610188565b91505092915050565b60008115159050919050565b6101df816101ca565b82525050565b60006020820190506101fa60008301846101d6565b92915050565b600082825260208201905092915050565b7f61646472206973206e6f74206120636f6e747261637400000000000000000000600082015250565b6000610247601683610200565b915061025282610211565b602082019050919050565b600060208201905081810360008301526102768161023a565b905091905056fea2646970667358221220883f143f3e31616be16bf2a0118b6259b1a91e54ed3a7dbd91abad45e20fffd664736f6c63430008180033",
"deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100365760003560e01c8063162790551461003b578063c23697a81461006b575b600080fd5b6100556004803603810190610050919061019d565b610087565b60405161006291906101e5565b60405180910390f35b6100856004803603810190610080919061019d565b610099565b005b600061009282610127565b9050919050565b6100a281610087565b6100e1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016100d89061025d565b60405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff167fe20314ef67750397f75c06a974fa978eaf8f26660fa2e4c14078f87ed2f9e8d760405160405180910390a250565b600080823b905060008111915050919050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061016a8261013f565b9050919050565b61017a8161015f565b811461018557600080fd5b50565b60008135905061019781610171565b92915050565b6000602082840312156101b3576101b261013a565b5b60006101c184828501610188565b91505092915050565b60008115159050919050565b6101df816101ca565b82525050565b60006020820190506101fa60008301846101d6565b92915050565b600082825260208201905092915050565b7f61646472206973206e6f74206120636f6e747261637400000000000000000000600082015250565b6000610247601683610200565b915061025282610211565b602082019050919050565b600060208201905081810360008301526102768161023a565b905091905056fea2646970667358221220883f143f3e31616be16bf2a0118b6259b1a91e54ed3a7dbd91abad45e20fffd664736f6c63430008180033",
"linkReferences": {},
"deployedLinkReferences": {}
}
Loading

0 comments on commit eb51786

Please sign in to comment.