github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/config/network_test.go (about) 1 package config 2 3 import ( 4 "flag" 5 "os" 6 "testing" 7 8 "github.com/0xPolygon/supernets2-node/etherman" 9 "github.com/0xPolygon/supernets2-node/merkletree" 10 "github.com/0xPolygon/supernets2-node/state" 11 "github.com/ethereum/go-ethereum/common" 12 "github.com/stretchr/testify/require" 13 "github.com/urfave/cli/v2" 14 ) 15 16 func TestLoadCustomNetworkConfig(t *testing.T) { 17 tcs := []struct { 18 description string 19 inputConfigStr string 20 expectedConfig NetworkConfig 21 expectedErrorMsg string 22 }{ 23 { 24 description: "happy path", 25 inputConfigStr: `{ 26 "root": "0xBEEF", 27 "genesisBlockNumber": 69, 28 "l1Config" : { 29 "chainId": 420, 30 "supernets2Address": "0xc949254d682d8c9ad5682521675b8f43b102aec4", 31 "maticTokenAddress": "0xc949254d682d8c9ad5682521675b8f43b102aec4", 32 "supernets2GlobalExitRootAddress": "0xc949254d682d8c9ad5682521675b8f43b102aec4", 33 "supernets2DataCommitteeContract": "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6" 34 }, 35 "genesis": [ 36 { 37 "balance": "0", 38 "nonce": "2", 39 "address": "0xc949254d682d8c9ad5682521675b8f43b102aec4" 40 }, 41 { 42 "balance": "0", 43 "nonce": "1", 44 "address": "0xae4bb80be56b819606589de61d5ec3b522eeb032", 45 "bytecode": "0xbeef1", 46 "storage": { 47 "0x0000000000000000000000000000000000000000000000000000000000000002": "0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988" 48 }, 49 "contractName": "PolygonZkEVMGlobalExitRootL2 proxy" 50 }, 51 { 52 "balance": "100000000000000000000000", 53 "nonce": "2", 54 "address": "0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988", 55 "bytecode": "0xbeef2", 56 "storage": { 57 "0x0000000000000000000000000000000000000000000000000000000000000000": "0xc949254d682d8c9ad5682521675b8f43b102aec4" 58 }, 59 "contractName": "PolygonZkEVMBridge proxy" 60 }, 61 { 62 "balance": "0", 63 "nonce": "1", 64 "address": "0x61ba0248b0986c2480181c6e76b6adeeaa962483", 65 "bytecode": "0xbeef3", 66 "storage": { 67 "0x0000000000000000000000000000000000000000000000000000000000000000": "0x01" 68 } 69 } 70 ], 71 "maxCumulativeGasUsed": 300000 72 }`, 73 expectedConfig: NetworkConfig{ 74 L2GlobalExitRootManagerAddr: common.HexToAddress("0xae4bb80be56b819606589de61d5ec3b522eeb032"), 75 L2BridgeAddr: common.HexToAddress("0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988"), 76 L1Config: etherman.L1Config{ 77 L1ChainID: 420, 78 Supernets2Addr: common.HexToAddress("0xc949254d682d8c9ad5682521675b8f43b102aec4"), 79 MaticAddr: common.HexToAddress("0xc949254d682d8c9ad5682521675b8f43b102aec4"), 80 GlobalExitRootManagerAddr: common.HexToAddress("0xc949254d682d8c9ad5682521675b8f43b102aec4"), 81 DataCommitteeAddr: common.HexToAddress("0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"), 82 }, 83 Genesis: state.Genesis{ 84 Root: common.HexToHash("0xBEEF"), 85 GenesisBlockNum: 69, 86 GenesisActions: []*state.GenesisAction{ 87 { 88 Address: "0xc949254d682d8c9ad5682521675b8f43b102aec4", 89 Type: int(merkletree.LeafTypeNonce), 90 Value: "2", 91 }, 92 { 93 Address: "0xae4bb80be56b819606589de61d5ec3b522eeb032", 94 Type: int(merkletree.LeafTypeNonce), 95 Value: "1", 96 }, 97 { 98 Address: "0xae4bb80be56b819606589de61d5ec3b522eeb032", 99 Type: int(merkletree.LeafTypeCode), 100 Bytecode: "0xbeef1", 101 }, 102 { 103 Address: "0xae4bb80be56b819606589de61d5ec3b522eeb032", 104 Type: int(merkletree.LeafTypeStorage), 105 StoragePosition: "0x0000000000000000000000000000000000000000000000000000000000000002", 106 Value: "0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988", 107 }, 108 { 109 Address: "0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988", 110 Type: int(merkletree.LeafTypeBalance), 111 Value: "100000000000000000000000", 112 }, 113 { 114 Address: "0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988", 115 Type: int(merkletree.LeafTypeNonce), 116 Value: "2", 117 }, 118 { 119 Address: "0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988", 120 Type: int(merkletree.LeafTypeCode), 121 Bytecode: "0xbeef2", 122 }, 123 { 124 Address: "0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988", 125 Type: int(merkletree.LeafTypeStorage), 126 StoragePosition: "0x0000000000000000000000000000000000000000000000000000000000000000", 127 Value: "0xc949254d682d8c9ad5682521675b8f43b102aec4", 128 }, 129 { 130 Address: "0x61ba0248b0986c2480181c6e76b6adeeaa962483", 131 Type: int(merkletree.LeafTypeNonce), 132 Value: "1", 133 }, 134 { 135 Address: "0x61ba0248b0986c2480181c6e76b6adeeaa962483", 136 Type: int(merkletree.LeafTypeCode), 137 Bytecode: "0xbeef3", 138 }, 139 { 140 Address: "0x61ba0248b0986c2480181c6e76b6adeeaa962483", 141 Type: int(merkletree.LeafTypeStorage), 142 StoragePosition: "0x0000000000000000000000000000000000000000000000000000000000000000", 143 Value: "0x01", 144 }, 145 }, 146 }, 147 }, 148 }, 149 { 150 description: "imported from network-config.example.json", 151 inputConfigStr: `{ 152 "genesis": [ 153 { 154 "address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", 155 "balance": "1000000000000000000000" 156 }, 157 { 158 "address": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", 159 "balance": "2000000000000000000000" 160 }, 161 { 162 "address": "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", 163 "balance": "3000000000000000000000" 164 } 165 ], 166 "maxCumulativeGasUsed": 123456 167 }`, 168 expectedConfig: NetworkConfig{ 169 Genesis: state.Genesis{ 170 GenesisActions: []*state.GenesisAction{ 171 { 172 Address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", 173 Type: int(merkletree.LeafTypeBalance), 174 Value: "1000000000000000000000", 175 }, 176 { 177 Address: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", 178 Type: int(merkletree.LeafTypeBalance), 179 Value: "2000000000000000000000", 180 }, 181 { 182 Address: "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", 183 Type: int(merkletree.LeafTypeBalance), 184 Value: "3000000000000000000000", 185 }, 186 }, 187 }, 188 }, 189 }, 190 { 191 description: "not valid JSON gives error", 192 inputConfigStr: "not a valid json", 193 expectedErrorMsg: "failed to load genesis configuration from file. Error: invalid character 'o' in literal null (expecting 'u')", 194 }, 195 { 196 description: "empty JSON gives error", 197 expectedErrorMsg: "failed to load genesis configuration from file. Error: unexpected end of JSON input", 198 }, 199 } 200 201 for _, tc := range tcs { 202 tc := tc 203 t.Run(tc.description, func(t *testing.T) { 204 file, err := os.CreateTemp("", "genesisConfig") 205 require.NoError(t, err) 206 defer func() { 207 require.NoError(t, os.Remove(file.Name())) 208 }() 209 require.NoError(t, os.WriteFile(file.Name(), []byte(tc.inputConfigStr), 0600)) 210 211 flagSet := flag.NewFlagSet("test", flag.ExitOnError) 212 flagSet.String(FlagNetwork, string(custom), "") 213 flagSet.String(FlagCustomNetwork, file.Name(), "") 214 ctx := cli.NewContext(nil, flagSet, nil) 215 216 c := &Config{} 217 if tc.expectedErrorMsg != "" { 218 panicFunc := func() { 219 c.loadNetworkConfig(ctx) 220 } 221 require.PanicsWithError(t, tc.expectedErrorMsg, panicFunc) 222 // require.Panics(t, panicFunc) 223 } else { 224 c.loadNetworkConfig(ctx) 225 require.Equal(t, tc.expectedConfig, c.NetworkConfig) 226 } 227 }) 228 } 229 }