-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Nonce mismatches what network expects (#726)
* Nonce mismatch on setup with script * Add toggle to update nonce only on first operation to avoid nonce mismatch with the network * Remove prints and format test * Update crates/cheatcodes/src/inspector.rs Co-authored-by: Federico Rodríguez <[email protected]> * Update crates/cheatcodes/src/inspector.rs Co-authored-by: Federico Rodríguez <[email protected]> * Update crates/forge/tests/fixtures/zk/ScriptSetup.s.sol Co-authored-by: Federico Rodríguez <[email protected]> * Update crates/cheatcodes/src/inspector.rs Co-authored-by: Federico Rodríguez <[email protected]> * change variable and test name * Change test name of noncemismatch * Add description, filter before iteration for loop and take() in cheatcode context creation * Remove taking in inspector of zk should update nonce * chore: explicit behavior for nonce update persistence (#729) * explicit behavior for nonce update persistence * prevent short-circuit bug * enable persistance only once per test contract deployment * Format and change test * Forge fmt * improve comments * Add comment to test --------- Co-authored-by: Federico Rodríguez <[email protected]> Co-authored-by: Nisheeth Barthwal <[email protected]>
- Loading branch information
1 parent
420660c
commit f83f08e
Showing
12 changed files
with
270 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import {Script} from "forge-std/Script.sol"; | ||
import {Greeter} from "../src/Greeter.sol"; | ||
|
||
contract ScriptSetupNonce is Script { | ||
function setUp() public { | ||
uint256 initial_nonce = checkNonce(address(tx.origin)); | ||
// Perform transactions and deploy contracts in setup to increment nonce and verify broadcast nonce matches onchain | ||
new Greeter(); | ||
new Greeter(); | ||
new Greeter(); | ||
new Greeter(); | ||
assert(checkNonce(address(tx.origin)) == initial_nonce); | ||
} | ||
|
||
function run() public { | ||
// Get initial nonce | ||
uint256 initial_nonce = checkNonce(address(tx.origin)); | ||
assert(initial_nonce == vm.getNonce(address(tx.origin))); | ||
|
||
// Create and interact with non-broadcasted contract to verify nonce is not incremented | ||
Greeter notBroadcastGreeter = new Greeter(); | ||
notBroadcastGreeter.greeting("john"); | ||
assert(checkNonce(address(tx.origin)) == initial_nonce); | ||
|
||
// Start broadcasting transactions | ||
vm.startBroadcast(); | ||
// Deploy and interact with broadcasted contracts | ||
Greeter greeter = new Greeter(); | ||
greeter.greeting("john"); | ||
|
||
// Deploy checker and verify nonce | ||
NonceChecker checker = new NonceChecker(); | ||
// We expect the nonce to be incremented by 1 because the check is done in an external | ||
// call | ||
checker.assertNonce(vm.getNonce(address(tx.origin)) + 1); | ||
vm.stopBroadcast(); | ||
} | ||
|
||
function checkNonce(address addr) public returns (uint256) { | ||
// We prank here to avoid accidentally "polluting" the nonce of `addr` during the call | ||
// for example when `addr` is `tx.origin` | ||
vm.prank(address(this), address(this)); | ||
return NonceLib.getNonce(addr); | ||
} | ||
} | ||
|
||
contract NonceChecker { | ||
function checkNonce() public returns (uint256) { | ||
return NonceLib.getNonce(address(tx.origin)); | ||
} | ||
|
||
function assertNonce(uint256 expected) public { | ||
uint256 real_nonce = checkNonce(); | ||
require(real_nonce == expected, "Nonce mismatch"); | ||
} | ||
} | ||
|
||
library NonceLib { | ||
address constant NONCE_HOLDER = address(0x8003); | ||
|
||
/// Retrieve tx nonce for `addr` from the NONCE_HOLDER system contract | ||
function getNonce(address addr) internal returns (uint256) { | ||
(bool success, bytes memory data) = NONCE_HOLDER.call(abi.encodeWithSignature("getMinNonce(address)", addr)); | ||
require(success, "Failed to get nonce"); | ||
return abi.decode(data, (uint256)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ mod invariant; | |
mod linking; | ||
mod logs; | ||
mod nft; | ||
mod nonce; | ||
mod ownership; | ||
mod paymaster; | ||
mod proxy; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::{ | ||
config::TestConfig, | ||
test_helpers::{run_zk_script_test, TEST_DATA_DEFAULT}, | ||
}; | ||
use forge::revm::primitives::SpecId; | ||
use foundry_test_utils::{forgetest_async, util, Filter, TestProject}; | ||
|
||
forgetest_async!(setup_block_on_script_test, |prj, cmd| { | ||
setup_deploy_prj(&mut prj); | ||
run_zk_script_test( | ||
prj.root(), | ||
&mut cmd, | ||
"./script/ScriptSetup.s.sol", | ||
"ScriptSetupNonce", | ||
None, | ||
4, | ||
Some(&["-vvvvv"]), | ||
); | ||
}); | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_zk_contract_nonce_mismatch() { | ||
let runner = TEST_DATA_DEFAULT.runner_zksync(); | ||
let filter = Filter::new("testTxOriginNonceDoesNotUpdate", "NonceMismatchTest", ".*"); | ||
|
||
TestConfig::with_filter(runner, filter).evm_spec(SpecId::SHANGHAI).run().await; | ||
} | ||
|
||
fn setup_deploy_prj(prj: &mut TestProject) { | ||
util::initialize(prj.root()); | ||
prj.add_script("ScriptSetup.s.sol", include_str!("../../fixtures/zk/ScriptSetup.s.sol")) | ||
.unwrap(); | ||
prj.add_source("Greeter.sol", include_str!("../../../../../testdata/zk/Greeter.sol")).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.