Skip to content

Commit

Permalink
Test verifying the EVM log returns the correct address (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
birchmd committed Nov 10, 2021
1 parent aa071f5 commit 696f028
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
11 changes: 11 additions & 0 deletions engine-tests/src/test_utils/solidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ impl ContractConstructor {
}
}

pub fn deploy_without_constructor(&self, nonce: U256) -> TransactionLegacy {
TransactionLegacy {
nonce,
gas_price: Default::default(),
gas_limit: u64::MAX.into(),
to: None,
value: Default::default(),
data: self.code.clone(),
}
}

pub fn deploy_without_args(&self, nonce: U256) -> TransactionLegacy {
self.deploy_with_args(nonce, &[])
}
Expand Down
18 changes: 18 additions & 0 deletions engine-tests/src/tests/res/caller.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 Caller {
function greet(address to) public {
to.call(abi.encodeWithSelector(Greeter(to).greet.selector));
}
}


contract Greeter { // callee contract
event Logger(address sender);

function greet() public {
emit Logger(msg.sender);
}
}

48 changes: 40 additions & 8 deletions engine-tests/src/tests/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,45 @@ fn test_deploy_largest_contract() {
test_utils::assert_gas_bound(profile.all_gas(), 28);
}

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

let mut deploy_contract = |name: &str, signer: &mut test_utils::Signer| {
let constructor = test_utils::solidity::ContractConstructor::compile_from_source(
"src/tests/res",
"target/solidity_build",
"caller.sol",
name,
);

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

let greet_contract = deploy_contract("Greeter", &mut signer);
let caller_contract = deploy_contract("Caller", &mut signer);

let result = runner
.submit_with_signer(&mut signer, |nonce| {
caller_contract.call_method_with_args(
"greet",
&[ethabi::Token::Address(greet_contract.address)],
nonce,
)
})
.unwrap();

// Address included in the log should come from the contract emitting the log,
// not the contract that invoked the call.
let log_address = result.logs.first().unwrap().address;
assert_eq!(Address(log_address), greet_contract.address);
}

#[test]
fn test_timestamp() {
let (mut runner, mut signer, _) = initialize_transfer();
Expand All @@ -111,14 +150,7 @@ fn test_timestamp() {
let nonce = signer.use_nonce();
let contract = runner.deploy_contract(
&signer.secret_key,
|c| crate::prelude::transaction::legacy::TransactionLegacy {
nonce: nonce.into(),
gas_price: Default::default(),
gas_limit: u64::MAX.into(),
to: None,
value: Default::default(),
data: c.code.clone(),
},
|c| c.deploy_without_constructor(nonce.into()),
constructor,
);

Expand Down

0 comments on commit 696f028

Please sign in to comment.