Skip to content

Commit

Permalink
fix(forge): set test contract address during constructor call (#3654)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 10, 2022
1 parent e320eb3 commit d35b405
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
21 changes: 16 additions & 5 deletions evm/src/executor/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ use crate::{
use ethers::{
prelude::{H160, H256, U256},
types::{Address, BlockNumber, Transaction, U64},
utils::keccak256,
};
use hashbrown::HashMap as Map;
pub use in_memory_db::MemDb;
use revm::{
db::{CacheDB, DatabaseRef},
precompiles::Precompiles,
Account, AccountInfo, Bytecode, Database, DatabaseCommit, Env, ExecutionResult, Inspector,
JournaledState, Log, SpecId, TransactTo, EVM, KECCAK_EMPTY,
Account, AccountInfo, Bytecode, CreateScheme, Database, DatabaseCommit, Env, ExecutionResult,
Inspector, JournaledState, Log, SpecId, TransactTo, EVM, KECCAK_EMPTY,
};
use std::collections::{HashMap, HashSet};
use tracing::{trace, warn};
Expand Down Expand Up @@ -666,9 +667,19 @@ impl Backend {
{
self.set_caller(env.tx.caller);
self.set_spec_id(env.cfg.spec_id);
if let TransactTo::Call(to) = env.tx.transact_to {
self.set_test_contract(to);
}

let test_contract = match env.tx.transact_to {
TransactTo::Call(to) => to,
TransactTo::Create(CreateScheme::Create) => {
revm::create_address(env.tx.caller, env.tx.nonce.unwrap_or_default())
}
TransactTo::Create(CreateScheme::Create2 { salt }) => {
let code_hash = H256::from_slice(keccak256(&env.tx.data).as_slice());
revm::create2_address(env.tx.caller, code_hash, salt)
}
};
self.set_test_contract(test_contract);

revm::evm_inner::<Self, true>(env, self, &mut inspector).transact()
}

Expand Down
6 changes: 6 additions & 0 deletions forge/tests/it/repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,9 @@ fn test_issue_3347() {
fn test_issue_3616() {
test_repro!("Issue3616");
}

// <https://github.com/foundry-rs/foundry/issues/3653>
#[test]
fn test_issue_3653() {
test_repro!("Issue3653");
}
24 changes: 24 additions & 0 deletions testdata/repros/Issue3653.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;

import "ds-test/test.sol";
import "../cheats/Cheats.sol";

// https://github.com/foundry-rs/foundry/issues/3653
contract Issue3653Test is DSTest {
Cheats constant vm = Cheats(HEVM_ADDRESS);
uint256 fork;
Token token;

constructor() {
fork = vm.createSelectFork("rpcAlias", 10);
token = new Token();
vm.makePersistent(address(token));
}

function testDummy() public {
assertEq(block.number, 10);
}
}

contract Token {}

0 comments on commit d35b405

Please sign in to comment.