Skip to content

Commit

Permalink
Test for isContract function
Browse files Browse the repository at this point in the history
  • Loading branch information
birchmd committed Mar 15, 2022
1 parent 7d8c023 commit 15ad8dc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
18 changes: 18 additions & 0 deletions engine-tests/src/tests/res/is_contract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract IsContract {

function isContract(address account) public view returns (bool) {
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
// for accounts without code, i.e. `keccak256('')`
bytes32 codehash;
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
// solhint-disable-next-line no-inline-assembly
assembly { codehash := extcodehash(account) }
return (codehash != accountHash && codehash != 0x0);
}

}
60 changes: 60 additions & 0 deletions engine-tests/src/tests/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,66 @@ fn test_log_address() {
assert_eq!(log_address, greet_contract.address);
}

#[test]
fn test_is_contract() {
let (mut runner, mut signer, _) = initialize_transfer();
let signer_address = test_utils::address_from_secret_key(&signer.secret_key);

let constructor = test_utils::solidity::ContractConstructor::force_compile(
"src/tests/res",
"target/solidity_build",
"is_contract.sol",
"IsContract",
);

let nonce = signer.use_nonce();
let contract = runner.deploy_contract(
&signer.secret_key,
|c| c.deploy_without_constructor(nonce.into()),
constructor,
);

let call_contract = |account: Address,
runner: &mut test_utils::AuroraRunner,
signer: &mut test_utils::Signer|
-> bool {
let result = runner
.submit_with_signer(signer, |nonce| {
contract.call_method_with_args(
"isContract",
&[ethabi::Token::Address(account.raw())],
nonce,
)
})
.unwrap();
let bytes = test_utils::unwrap_success_slice(&result);
ethabi::decode(&[ethabi::ParamType::Bool], bytes)
.unwrap()
.pop()
.unwrap()
.into_bool()
.unwrap()
};

// Should return false for accounts that don't exist
assert_eq!(
call_contract(Address::from_array([1; 20]), &mut runner, &mut signer),
false,
);

// Should return false for accounts that don't have contract code
assert_eq!(
call_contract(signer_address, &mut runner, &mut signer),
false,
);

// Should return true for contracts
assert_eq!(
call_contract(contract.address, &mut runner, &mut signer),
true,
);
}

#[test]
fn test_solidity_pure_bench() {
let (mut runner, mut signer, _) = initialize_transfer();
Expand Down

0 comments on commit 15ad8dc

Please sign in to comment.