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

Commit de771e5

Browse files
committed
Debug test and script setup logic
1 parent ca5c6fd commit de771e5

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

test/ChainwebInit.t.sol

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// ...existing code...
2+
// SPDX-License-Identifier: UNLICENSED
3+
pragma solidity ^0.8.13;
4+
5+
import {Test, console} from "forge-std/Test.sol";
6+
import {ChainwebConfig, Chainweb, ChainwebConfigReader} from "../src/Chainweb.sol";
7+
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)
12+
contract ChainwebInitTest is Test {
13+
Chainweb chainweb;
14+
ChainwebConfigReader private configReader;
15+
16+
uint256 constant ALICE_KEY = 0xb332ddc4e0801582e154d10cad8b672665656cbf0097f2b47483c0cfe3261299;
17+
address constant ALICE = address(0xFB8Fb7f9bdc8951040a6D195764905138F7462Ed);
18+
address constant BOB = address(0x28f2d8ef4e0fe6B2E945cF5C33a0118a30a62354);
19+
20+
bool _initializeInConstructor;
21+
string _environment;
22+
uint256 _deployerKey;
23+
address _deployer;
24+
25+
constructor () {
26+
_initializeInConstructor = vm.envOr("INIT_IN_CONSTRUCTOR", false);
27+
vm.label(ALICE, "Alice");
28+
vm.label(BOB, "Bob");
29+
30+
_environment = vm.envExists("CHAINWEB") ? vm.envString("CHAINWEB") : "anvil";
31+
configReader = new ChainwebConfigReader();
32+
ChainwebConfig memory config = configReader.readChainwebConfig(_environment);
33+
chainweb = new Chainweb(
34+
uint24(config.numberOfChains), block.chainid, uint24(config.chainwebChainIdOffset), config.externalHostUrl
35+
);
36+
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+
48+
if (_initializeInConstructor) {
49+
chainweb.setupChainsForTest();
50+
}
51+
}
52+
53+
function setUp() public {
54+
if (!_initializeInConstructor) {
55+
chainweb.setupChainsForTest();
56+
}
57+
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;
64+
}
65+
}
66+
67+
function test_SwitchChainNonce() public {
68+
console.log("==========================================================");
69+
console.log("network:", _environment);
70+
console.log("Initialized in constructor:", _initializeInConstructor);
71+
console.log("deployer:", vm.getLabel(_deployer));
72+
console.log("deployer is msg.sender:", msg.sender == _deployer);
73+
uint256[] memory chainIds = chainweb.getChainIds();
74+
for (uint256 i = 0; i < chainIds.length; i++) {
75+
chainweb.switchChain(chainIds[i]);
76+
deal(_deployer, 100 gwei);
77+
console.log("deployer nonce: %d", vm.getNonce(_deployer));
78+
vm.startBroadcast(_deployer);
79+
payable(BOB).transfer(1 gwei);
80+
vm.stopBroadcast();
81+
}
82+
83+
}
84+
}

tests_init.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
echo "# ################################################################### #"
4+
echo "# Test "
5+
6+
CHAINWEB=anvil INIT_IN_CONSTRUCTOR=1 forge test --match-test test_SwitchChainNonce -vv --sender $ALICE | gsed -n '/=====/,/^$/p'
7+
CHAINWEB=anvil INIT_IN_CONSTRUCTOR=0 forge test --match-test test_SwitchChainNonce -vv --sender $ALICE | gsed -n '/=====/,/^$/p'
8+
# CHAINWEB=sandbox INIT_IN_CONSTRUCTOR=1 forge test --match-test test_SwitchChainNonce -vv --sender $ALICE | gsed -n '/=====/,/^$/p'
9+
# CHAINWEB=sandbox INIT_IN_CONSTRUCTOR=0 forge test --match-test test_SwitchChainNonce -vv --sender $ALICE | gsed -n '/=====/,/^$/p'
10+
11+
DEPLOYER_KEY=$CHARLIE_SK CHAINWEB=anvil INIT_IN_CONSTRUCTOR=1 forge test --match-test test_SwitchChainNonce -vv --sender $ALICE | gsed -n '/=====/,/^$/p'
12+
DEPLOYER_KEY=$CHARLIE_SK CHAINWEB=anvil INIT_IN_CONSTRUCTOR=0 forge test --match-test test_SwitchChainNonce -vv --sender $ALICE | gsed -n '/=====/,/^$/p'
13+
# DEPLOYER_KEY=$ALICE_SK CHAINWEB=sandbox INIT_IN_CONSTRUCTOR=1 forge test --match-test test_SwitchChainNonce -vv --sender $ALICE | gsed -n '/=====/,/^$/p'
14+
# DEPLOYER_KEY=$ALICE_SK CHAINWEB=sandbox INIT_IN_CONSTRUCTOR=0 forge test --match-test test_SwitchChainNonce -vv --sender $ALICE | gsed -n '/=====/,/^$/p'
15+
16+
echo "# ################################################################### #"
17+
echo "# Script "
18+
19+
CHAINWEB=anvil INIT_IN_CONSTRUCTOR=1 forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation| gsed -n '/=====/,/^$/p'
20+
CHAINWEB=anvil INIT_IN_CONSTRUCTOR=0 forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation| gsed -n '/=====/,/^$/p'
21+
# CHAINWEB=sandbox INIT_IN_CONSTRUCTOR=1 forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation| gsed -n '/=====/,/^$/p'
22+
# CHAINWEB=sandbox INIT_IN_CONSTRUCTOR=0 forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation| gsed -n '/=====/,/^$/p'
23+
24+
DEPLOYER_KEY=$CHARLIE_SK CHAINWEB=anvil INIT_IN_CONSTRUCTOR=1 forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation| gsed -n '/=====/,/^$/p'
25+
DEPLOYER_KEY=$CHARLIE_SK CHAINWEB=anvil INIT_IN_CONSTRUCTOR=0 forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation| gsed -n '/=====/,/^$/p'
26+
# DEPLOYER_KEY=$ALICE_SK CHAINWEB=sandbox INIT_IN_CONSTRUCTOR=1 forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation| gsed -n '/=====/,/^$/p'
27+
# DEPLOYER_KEY=$ALICE_SK CHAINWEB=sandbox INIT_IN_CONSTRUCTOR=0 forge script ./test/ChainwebInit.t.sol --sig "test_SwitchChainNonce()" -vv --private-key $ALICE_SK --legacy --skip-simulation| gsed -n '/=====/,/^$/p'

0 commit comments

Comments
 (0)