Skip to content

Commit

Permalink
fix(forge): correct nonce before vm.getNonce on script (foundry-rs#…
Browse files Browse the repository at this point in the history
…2144)

* correct nonce before vm.getNonce

* update contract name on test
  • Loading branch information
joshieDo authored Jun 28, 2022
1 parent 9dcc095 commit b1bdb4d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cli/tests/it/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ forgetest_async!(
tester
.load_private_keys(vec![0])
.await
.add_sig("BroadcastTestNoLinking", "deployMix()")
.add_sig("BroadcastMix", "deployMix()")
.simulate(ScriptOutcome::OkSimulation)
.broadcast(ScriptOutcome::OkBroadcast)
.assert_nonce_increment(vec![(0, 15)])
Expand Down
4 changes: 3 additions & 1 deletion evm/src/executor/inspector/cheatcodes/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ pub fn apply<DB: Database>(
}
}
HEVMCalls::GetNonce(inner) => {
correct_sender_nonce(&data.env.tx.caller, &mut data.subroutine, state);

// TODO: this is probably not a good long-term solution since it might mess up the gas
// calculations
data.subroutine.load_account(inner.0, data.db);
Expand Down Expand Up @@ -250,7 +252,7 @@ pub fn apply<DB: Database>(
}

/// When using `forge script`, the script method is called using the address from `--sender`.
/// That leads to the its nonce being incremented by `call_raw`. In a `broadcast` scenario this is
/// That leads to its nonce being incremented by `call_raw`. In a `broadcast` scenario this is
/// undesirable. Therefore, we make sure to fix the sender's nonce **once**.
fn correct_sender_nonce(
sender: &Address,
Expand Down
34 changes: 26 additions & 8 deletions testdata/cheats/Broadcast.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ contract BroadcastTestNoLinking is DSTest {
}

function deployMany() public {

assert(cheats.getNonce(msg.sender) == 0);

cheats.startBroadcast();

for(uint i; i< 100; i++) {
Expand All @@ -177,6 +180,25 @@ contract BroadcastTestNoLinking is DSTest {
cheats.stopBroadcast();

}
function errorStaticCall() public {
cheats.broadcast();
NoLink test11 = new NoLink();

cheats.broadcast();
test11.view_me();
}
}

contract BroadcastMix is DSTest {

Cheats constant cheats = Cheats(HEVM_ADDRESS);

// ganache-cli -d 1st
address public ACCOUNT_A = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;

// ganache-cli -d 2nd
address public ACCOUNT_B = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8;

function more() internal {
cheats.broadcast();
NoLink test11 = new NoLink();
Expand Down Expand Up @@ -224,21 +246,17 @@ contract BroadcastTestNoLinking is DSTest {

more();
}

function errorStaticCall() public {
cheats.broadcast();
NoLink test11 = new NoLink();

cheats.broadcast();
test11.view_me();
}
}


contract BroadcastTestSetup is DSTest {
Cheats constant cheats = Cheats(HEVM_ADDRESS);

function setUp() public {

// It predeployed a library first
assert(cheats.getNonce(msg.sender) == 1);

cheats.broadcast();
Test t = new Test();

Expand Down

0 comments on commit b1bdb4d

Please sign in to comment.