@@ -5,10 +5,51 @@ pragma solidity ^0.8.13;
55import {Test, console} from "forge-std/Test.sol " ;
66import {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+ ///
1253contract 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}
0 commit comments