Skip to content
This repository was archived by the owner on Jan 9, 2026. It is now read-only.

Commit c6bd15e

Browse files
committed
add more test paramters
1 parent de771e5 commit c6bd15e

File tree

3 files changed

+159
-49
lines changed

3 files changed

+159
-49
lines changed

test/ChainwebInit.t.sol

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,51 @@ pragma solidity ^0.8.13;
55
import {Test, console} from "forge-std/Test.sol";
66
import {ChainwebConfig, Chainweb, ChainwebConfigReader} from "../src/Chainweb.sol";
77

8-
/// Environment variables:
9-
/// -- CHAINWEB: "anvil" or "sandbox" (default: "anvil")
10-
/// -- INIT_IN_CONSTRUCTOR: Whether setupChainsForTest() is called in the constructor or in setUp() (default: "false")
11-
/// -- DEPLOYER_KEY: Private key to use for broadcasting transactions (default is to use msg.sender)
8+
/// Test initialiization of Chainweb chains in different ways and with different
9+
/// paramaeters.
10+
///
11+
/// # Environment variables:
12+
///
13+
/// - CHAINWEB: "anvil" or "sandbox" (default: "anvil")
14+
/// - INIT_IN_CONSTRUCTOR: Whether setupChainsForTest() is called in the
15+
/// constructor or in setUp() (default: "false")
16+
/// - SET_SENDER_IN_CONSTRUCTOR: Whether the deployer is set in the
17+
/// constructor or in setUp() (default: "false")
18+
/// - DEPLOYER_KEY: Private key to use for broadcasting transactions (default
19+
/// is to use msg.sender)
20+
///
21+
/// # Notes
22+
///
23+
/// - network (anvil or external) has no effect on result
24+
/// - SEPARATE_DEPLOYER=1 implies -> result=true
25+
/// - BUILDIN_SENDER=1 implies -> result=false
26+
///
27+
/// # Conclusions
28+
///
29+
/// - The builtin sender always uses global nonces, which is consistent with
30+
/// the design of foundry.
31+
/// - A separate deployer always uses local nonces, which is consistent with
32+
/// the design of foundry.
33+
/// - The use of an external account as sender for the test or script leads to
34+
/// unconsistent and unpredictable behavior.
35+
///
36+
/// Therefore the use of command lines flags like `--sender` or `--private-key`
37+
/// should be avoided in the context of multiple chains.
38+
///
39+
/// # Details of inconsistent behavior:
40+
///
41+
/// ```
42+
/// TYPE INIT_IN_CONSTRUCTOR SET_SENDER_IN_CONSTRUCTOR LOCAL_NONCE
43+
/// test 1 1 true
44+
/// script 1 1 false
45+
/// test 1 0 false
46+
/// script 1 0 false
47+
/// test 0 1 false
48+
/// script 0 1 false
49+
/// test 0 0 false
50+
/// script 0 0 true
51+
/// ```
52+
///
1253
contract ChainwebInitTest is Test {
1354
Chainweb chainweb;
1455
ChainwebConfigReader private configReader;
@@ -18,12 +59,14 @@ contract ChainwebInitTest is Test {
1859
address constant BOB = address(0x28f2d8ef4e0fe6B2E945cF5C33a0118a30a62354);
1960

2061
bool _initializeInConstructor;
62+
bool _setSenderInConstructor;
2163
string _environment;
2264
uint256 _deployerKey;
2365
address _deployer;
2466

2567
constructor () {
2668
_initializeInConstructor = vm.envOr("INIT_IN_CONSTRUCTOR", false);
69+
_setSenderInConstructor = vm.envOr("SET_SENDER_IN_CONSTRUCTOR", false);
2770
vm.label(ALICE, "Alice");
2871
vm.label(BOB, "Bob");
2972

@@ -34,33 +77,36 @@ contract ChainwebInitTest is Test {
3477
uint24(config.numberOfChains), block.chainid, uint24(config.chainwebChainIdOffset), config.externalHostUrl
3578
);
3679

37-
// initializing the deployer here causes the --sender flag to be
38-
// ignored.
39-
//
40-
// _deployerKey = vm.envOr("DEPLOYER_KEY", uint256(0x0));
41-
// if (_deployerKey != 0) {
42-
// _deployer = vm.rememberKey(_deployerKey);
43-
// vm.label(_deployer, "EnvDeployer");
44-
// } else {
45-
// _deployer = msg.sender;
46-
// }
47-
4880
if (_initializeInConstructor) {
4981
chainweb.setupChainsForTest();
5082
}
83+
84+
// initializing the deployer here causes the --sender flag to be
85+
// ignored.
86+
if (_setSenderInConstructor) {
87+
_deployerKey = vm.envOr("DEPLOYER_KEY", uint256(0x0));
88+
if (_deployerKey != 0) {
89+
_deployer = vm.rememberKey(_deployerKey);
90+
vm.label(_deployer, "EnvDeployer");
91+
} else {
92+
_deployer = msg.sender;
93+
}
94+
}
5195
}
5296

5397
function setUp() public {
5498
if (!_initializeInConstructor) {
5599
chainweb.setupChainsForTest();
56100
}
57101

58-
_deployerKey = vm.envOr("DEPLOYER_KEY", uint256(0x0));
59-
if (_deployerKey != 0) {
60-
_deployer = vm.rememberKey(_deployerKey);
61-
vm.label(_deployer, "EnvDeployer");
62-
} else {
63-
_deployer = msg.sender;
102+
if (!_setSenderInConstructor) {
103+
_deployerKey = vm.envOr("DEPLOYER_KEY", uint256(0x0));
104+
if (_deployerKey != 0) {
105+
_deployer = vm.rememberKey(_deployerKey);
106+
vm.label(_deployer, "EnvDeployer");
107+
} else {
108+
_deployer = msg.sender;
109+
}
64110
}
65111
}
66112

@@ -71,14 +117,23 @@ contract ChainwebInitTest is Test {
71117
console.log("deployer:", vm.getLabel(_deployer));
72118
console.log("deployer is msg.sender:", msg.sender == _deployer);
73119
uint256[] memory chainIds = chainweb.getChainIds();
120+
uint256 curNonce;
121+
bool result = true;
74122
for (uint256 i = 0; i < chainIds.length; i++) {
75123
chainweb.switchChain(chainIds[i]);
124+
if (i == 0) {
125+
curNonce = vm.getNonce(_deployer);
126+
} else {
127+
uint256 newNonce = vm.getNonce(_deployer);
128+
result = result && newNonce == curNonce;
129+
curNonce = newNonce;
130+
}
76131
deal(_deployer, 100 gwei);
77132
console.log("deployer nonce: %d", vm.getNonce(_deployer));
78133
vm.startBroadcast(_deployer);
79134
payable(BOB).transfer(1 gwei);
80135
vm.stopBroadcast();
81136
}
82-
137+
console.log("result:", result);
83138
}
84139
}

test/scripts/run-setup-tests.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env bash
2+
3+
# USAGE:
4+
#
5+
# Run from the root of the repository via:
6+
#
7+
# ```
8+
# bash ./run-setup-tests.sh | column -s, -t
9+
# ```
10+
11+
# NOTES:
12+
#
13+
# * network (anvil or external) has no effect on LOCAL_NONCE
14+
# * SEPARATE_DEPLOYER=1 implies -> LOCAL_NONCE=1
15+
# * BUILDIN_SENDER=1 implies -> LOCAL_NONCE=0
16+
#
17+
# CONCLUSIONS:
18+
#
19+
# * The builtin sender always uses global nonces, which is consistent with the
20+
# design of foundry.
21+
# * A separate deployer always uses local nonces, which is consistent with the
22+
# design of foundry.
23+
# * The use of an external account as sender for the test or script leads to
24+
# unconsistent and unpredictable behavior.
25+
#
26+
# Therefore the use of command lines flags like `--sender` or `--private-key`
27+
# should be avoided in the context of multiple chains.
28+
29+
# Simplified version that focuses on inconsistent behavior:
30+
echo "TYPE, INIT_IN_CONSTRUCTOR, SET_SENDER_IN_CONSTRUCTOR, LOCAL_NONCE"
31+
for initChains in 1 0; do
32+
export INIT_IN_CONSTRUCTOR=$initChains
33+
for setSender in 1 0; do
34+
export SET_SENDER_IN_CONSTRUCTOR=$setSender
35+
36+
echo -n "test, $INIT_IN_CONSTRUCTOR, $SET_SENDER_IN_CONSTRUCTOR, "
37+
forge test --match-test test_SwitchChainNonce -vv --sender $ALICE 2>&1 |
38+
sed -n -E 's/.*result:.*(true|false).*/\1/p'
39+
40+
echo -n "script, $INIT_IN_CONSTRUCTOR, $SET_SENDER_IN_CONSTRUCTOR, "
41+
forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation |
42+
sed -n -E 's/.*result:.*(true|false).*/\1/p'
43+
done
44+
done
45+
46+
# Full version that covers all combinations (except for network):
47+
#
48+
# echo "TYPE, INIT_IN_CONSTRUCTOR, SET_SENDER_IN_CONSTRUCTOR, SEPARATE_DEPLOYER, BUILDIN_SENDER, LOCAL_NONCE"
49+
# for initChains in 1 0; do
50+
# export INIT_IN_CONSTRUCTOR=$initChains
51+
# for setSender in 1 0; do
52+
# export SET_SENDER_IN_CONSTRUCTOR=$setSender
53+
# # for deployer in "" "$CHARLIE_SK"; do
54+
# for deployer in ""; do
55+
# # for deployer in "$CHARLIE_SK"; do
56+
# if [ -n "$deployer" ]; then
57+
# export DEPLOYER_KEY=$deployer
58+
# SEPARATE_DEPLOYER=1
59+
# else
60+
# unset DEPLOYER_KEY
61+
# SEPARATE_DEPLOYER=0
62+
# fi
63+
# echo -n "test, $INIT_IN_CONSTRUCTOR, $SET_SENDER_IN_CONSTRUCTOR, $SEPARATE_DEPLOYER, 0, "
64+
# forge test --match-test test_SwitchChainNonce -vv --sender $ALICE 2>&1 |
65+
# sed -n -E 's/.*result:.*(true|false).*/\1/p'
66+
#
67+
# echo -n "script, $INIT_IN_CONSTRUCTOR, $SET_SENDER_IN_CONSTRUCTOR, $SEPARATE_DEPLOYER, 0, "
68+
# forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation |
69+
# sed -n -E 's/.*result:.*(true|false).*/\1/p'
70+
#
71+
# echo -n "test, $INIT_IN_CONSTRUCTOR, $SET_SENDER_IN_CONSTRUCTOR, $SEPARATE_DEPLOYER, 1, "
72+
# forge test --match-test test_SwitchChainNonce -vv 2>&1 |
73+
# sed -n -E 's/.*result:.*(true|false).*/\1/p'
74+
#
75+
# echo -n "script, $INIT_IN_CONSTRUCTOR, $SET_SENDER_IN_CONSTRUCTOR, $SEPARATE_DEPLOYER, 1, "
76+
# forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --legacy --skip-simulation |
77+
# sed -n -E 's/.*result:.*(true|false).*/\1/p'
78+
#
79+
# done
80+
# done
81+
# done
82+

tests_init.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)