github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/tests/bats/chainconfig.bats (about) 1 #!/usr/bin/env bats 2 3 : ${GETH_CMD:=$GOPATH/bin/geth} 4 5 setup() { 6 DATA_DIR=`mktemp -d` 7 default_mainnet_genesis_hash='"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"' 8 customnet_genesis_hash='"0x76bc07fbdfe084b9aff37425c24453f774d1945b28412a3b4b8c25d8d3c81df2"' 9 testnet_genesis_hash='"0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303"' 10 GENESIS_TESTNET=0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303 11 } 12 13 teardown() { 14 rm -fr $DATA_DIR 15 unset default_mainnet_genesis_hash 16 unset customnet_genesis_hash 17 unset GENESIS_TESTNET 18 } 19 20 ## dump-chain-config JSON 21 22 # Test dumping chain configuration to JSON file. 23 @test "dump-chain-config | exit 0" { 24 run $GETH_CMD --datadir $DATA_DIR --maxpeers 0 dump-chain-config $DATA_DIR/dump.json 25 echo "$output" 26 27 [ "$status" -eq 0 ] 28 [[ "$output" == *"Wrote chain config file"* ]] 29 [ -f $DATA_DIR/dump.json ] 30 31 run grep -R "mainnet" $DATA_DIR/dump.json 32 [ "$status" -eq 0 ] 33 [[ "$output" == *"\"identity\": \"mainnet\","* ]] 34 } 35 36 @test "--testnet dump-chain-config | exit 0" { 37 run $GETH_CMD --datadir $DATA_DIR --testnet dump-chain-config $DATA_DIR/dump.json 38 echo "$output" 39 40 [ "$status" -eq 0 ] 41 [[ "$output" == *"Wrote chain config file"* ]] 42 [ -f $DATA_DIR/dump.json ] 43 44 run grep -R "morden" $DATA_DIR/dump.json 45 echo "$output" 46 [[ "$output" == *"\"identity\": \"morden\"," ]] 47 } 48 49 @test "--chain morden dump-chain-config | exit 0" { 50 run $GETH_CMD --datadir $DATA_DIR --chain morden dump-chain-config $DATA_DIR/dump.json 51 echo "$output" 52 53 [ "$status" -eq 0 ] 54 [[ "$output" == *"Wrote chain config file"* ]] 55 [ -f $DATA_DIR/dump.json ] 56 57 run grep -R "morden" $DATA_DIR/dump.json 58 [[ "$output" == *"\"identity\": \"morden\"," ]] 59 } 60 61 @test "--chain kittyCoin dump-chain-config | exit !=0" { 62 run $GETH_CMD --datadir $DATA_DIR --chain kittyCoin dump-chain-config $DATA_DIR/dump.json 63 echo "$output" 64 [ "$status" -ne 0 ] 65 } 66 67 @test "dump-chain-config | --chain | exit 0" { 68 69 # Same as 'chainconfig customnet dump'... higher complexity::more confidence 70 customnet="$DATA_DIR"/kitty 71 mkdir -p "$customnet" 72 73 run $GETH_CMD --datadir $DATA_DIR dump-chain-config "$customnet"/chain.json 74 echo "$output" 75 [ "$status" -eq 0 ] 76 [[ "$output" == *"Wrote chain config file"* ]] 77 78 [ -f "$customnet"/chain.json ] 79 80 run grep -R "mainnet" "$customnet"/chain.json 81 [ "$status" -eq 0 ] 82 echo "$output" 83 [[ "$output" == *"\"identity\": \"mainnet\"," ]] 84 85 # Ensure JSON file dump is loadable as external config 86 sed -i.bak s/mainnet/kitty/ "$customnet"/chain.json 87 run $GETH_CMD --datadir $DATA_DIR --chain kitty --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).hash' console 88 [ "$status" -eq 0 ] 89 echo "$output" 90 [[ "$output" == *"$default_mainnet_genesis_hash"* ]] 91 } 92 93 @test "dump-chain-config | --chain privatenet.json | exit 0" { 94 95 run $GETH_CMD --data-dir $DATA_DIR --chain morden dump-chain-config $DATA_DIR/privatenet.json 96 echo "$output" 97 [ "$status" -eq 0 ] 98 [[ "$output" == *"Wrote chain config file"* ]] 99 100 [ -f "$DATA_DIR"/privatenet.json ] 101 102 run grep -R "morden" "$DATA_DIR"/privatenet.json 103 [ "$status" -eq 0 ] 104 echo "$output" 105 [[ "$output" == *"\"identity\": \"morden\"," ]] 106 107 # Ensure JSON file dump is loadable as external config 108 sed -i.bak s/morden/kitty/ "$DATA_DIR"/privatenet.json 109 run $GETH_CMD --datadir $DATA_DIR --chain "$DATA_DIR"/privatenet.json --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).hash' console 110 [ "$status" -eq 0 ] 111 echo "$output" 112 [[ "$output" == *"$testnet_genesis_hash"* ]] 113 [[ "$output" == *"kitty"* ]] 114 115 # ensure chain config file is copied to chaindata dir 116 [ -f "$DATA_DIR"/kitty/chain.json ] 117 } 118 119 @test "--chain morden dump-chain-config | --chain == morden | exit 0" { 120 121 # Same as 'chainconfig customnet dump'... higher complexity::more confidence 122 customnet="$DATA_DIR"/kitty 123 mkdir -p "$customnet" 124 125 run $GETH_CMD --datadir $DATA_DIR --chain=morden dump-chain-config "$customnet"/chain.json 126 echo "$output" 127 [ "$status" -eq 0 ] 128 [[ "$output" == *"Wrote chain config file"* ]] 129 130 [ -f "$customnet"/chain.json ] 131 132 run grep -R "morden" "$customnet"/chain.json 133 [ "$status" -eq 0 ] 134 echo "$output" 135 [[ "$output" == *"\"identity\": \"morden\"," ]] 136 137 # Ensure JSON file dump is loadable as external config 138 sed -i.bak s/morden/kitty/ "$customnet"/chain.json 139 run $GETH_CMD --datadir $DATA_DIR --chain kitty --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).hash' console 140 [ "$status" -eq 0 ] 141 echo "$output" 142 [[ "$output" == *"$testnet_genesis_hash"* ]] 143 } 144 145 # Dump morden and make customization and test that customizations are installed. 146 @test "--chain morden dump-chain-config | --chain -> kitty | exit 0" { 147 148 # Same as 'chainconfig customnet dump'... higher complexity::more confidence 149 customnet="$DATA_DIR"/kitty 150 mkdir -p "$customnet" 151 152 run $GETH_CMD --datadir $DATA_DIR --chain=morden dump-chain-config "$customnet"/chain.json 153 echo "$output" 154 [ "$status" -eq 0 ] 155 [[ "$output" == *"Wrote chain config file"* ]] 156 157 [ -f "$customnet"/chain.json ] 158 159 run grep -R "morden" "$customnet"/chain.json 160 [ "$status" -eq 0 ] 161 echo "$output" 162 [[ "$output" == *"\"identity\": \"morden\"," ]] 163 164 # Ensure JSON file dump is loadable as external config 165 sed -i.bak s/morden/kitty/ "$customnet"/chain.json 166 sed -i.bak s/identity/id/ "$customnet"/chain.json # ensure identity is aliases to identity 167 # remove starting nonce from external config 168 # config file should still be valid, but genesis should have different hash than default morden genesis 169 grep -v 'startingNonce' "$customnet"/chain.json > "$DATA_DIR"/stripped.json 170 [ "$status" -eq 0 ] 171 mv "$DATA_DIR"/stripped.json "$customnet"/chain.json 172 173 sed -e '0,/2/ s/2/666/' "$customnet"/chain.json > "$DATA_DIR"/net_id.json 174 mv "$DATA_DIR"/net_id.json "$customnet"/chain.json 175 176 run $GETH_CMD --datadir $DATA_DIR --chain kitty --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).hash' console 177 [ "$status" -eq 0 ] 178 echo "$output" 179 [[ "$output" != *"$testnet_genesis_hash"* ]] # different genesis (stateRoot is diff) 180 181 run $GETH_CMD --datadir $DATA_DIR --chain kitty status 182 [ "$status" -eq 0 ] 183 echo "$output" 184 [[ "$output" != *"Network: 666"* ]] # new network id 185 } 186 187 ## load /data 188 189 # Test loading mainnet chain configuration from data/ JSON file. 190 # Test ensures 191 # - can load default external JSON config 192 # - use datadir/subdir schema (/mainnet) 193 # - configured nonce matches external nonce (soft check since 42 is default, too) 194 @test "--chain=main | exit 0" { 195 run $GETH_CMD --datadir $DATA_DIR --chain=main --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).hash' console 196 echo "$output" 197 [ "$status" -eq 0 ] 198 [[ "$output" == *"$default_mainnet_genesis_hash"* ]] 199 200 # Ensure we're using the --chain named subdirectory under main $DATA_DIR. 201 [ -d $DATA_DIR/mainnet ] 202 [ -d $DATA_DIR/mainnet/chaindata ] 203 [ -f $DATA_DIR/mainnet/chaindata/CURRENT ] 204 [ -f $DATA_DIR/mainnet/chaindata/LOCK ] 205 [ -f $DATA_DIR/mainnet/chaindata/LOG ] 206 [ -d $DATA_DIR/mainnet/keystore ] 207 } 208 209 # Test loading testnet chain configuration from data/ JSON file. 210 # Test ensures 211 # - external chain config can determine chain configuration 212 # - use datadir/subdir schema (/morden) 213 @test "--chain morden | exit 0" { 214 run $GETH_CMD --datadir $DATA_DIR --chain=morden --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).hash' console 215 echo "$output" 216 [ "$status" -eq 0 ] 217 [[ "$output" == *"0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303"* ]] 218 219 # Ensure we're using the --chain named subdirectory under main $DATA_DIR. 220 [ -d $DATA_DIR/morden ] 221 [ -d $DATA_DIR/morden/chaindata ] 222 [ -f $DATA_DIR/morden/chaindata/CURRENT ] 223 [ -f $DATA_DIR/morden/chaindata/LOCK ] 224 [ -f $DATA_DIR/morden/chaindata/LOG ] 225 [ -d $DATA_DIR/morden/keystore ] 226 } 227 228 # prove that trailing slashes in chain=val/ get removed harmlessly 229 @test "--chain='morden/' | exit 0" { 230 231 # *nix path separator == / 232 run $GETH_CMD --data-dir $DATA_DIR --chain=morden/ --exec 'eth.getBlock(0).hash' console 233 echo "$output" 234 [ "$status" -eq 0 ] 235 [[ "$output" == *"0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303"* ]] 236 237 # Ensure we're using the --chain named subdirectory under main $DATA_DIR. 238 [ -d $DATA_DIR/morden ] 239 [ -d $DATA_DIR/morden/chaindata ] 240 } 241 242 ## load /testdata 243 244 # Test loading customnet chain configuration from testdata/ JSON file. 245 # Test ensures 246 # - chain is loaded from custom external file and determines datadir/subdir scheme 247 @test "--chain customnet @ chain_config_dump-ok-custom.json | exit 0" { 248 # Ensure non-default nonce 43 (42 is default). 249 # Ensure chain subdir is determined by config `id` 250 mkdir -p "$DATA_DIR"/customnet 251 cp $BATS_TEST_DIRNAME/../../cmd/geth/testdata/chain_config_dump-ok-custom.json $DATA_DIR/customnet/chain.json 252 sed -i.bak s/mainnet/customnet/ $DATA_DIR/customnet/chain.json 253 run $GETH_CMD --datadir $DATA_DIR --chain=customnet --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).hash' console 254 echo "$output" 255 [ "$status" -eq 0 ] 256 [[ "$output" == *"$customnet_genesis_hash"* ]] 257 258 # Ensure we're using the --chain named subdirectory under main $DATA_DIR. 259 [ -d $DATA_DIR/customnet ] 260 [ -d $DATA_DIR/customnet/chaindata ] 261 [ -f $DATA_DIR/customnet/chaindata/CURRENT ] 262 [ -f $DATA_DIR/customnet/chaindata/LOCK ] 263 [ -f $DATA_DIR/customnet/chaindata/LOG ] 264 [ -d $DATA_DIR/customnet/keystore ] 265 } 266 267 @test "--chain kitty --testnet | exit !=0" { 268 mkdir -p $DATA_DIR/kitty 269 cp $BATS_TEST_DIRNAME/../../core/config/mainnet.json $DATA_DIR/kitty/chain.json 270 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/chain.json 271 run $GETH_CMD --data-dir $DATA_DIR --chain kitty --testnet console 272 echo "$output" 273 [ "$status" -ne 0 ] 274 [[ "$output" == *"invalid flag or context value: used redundant/conflicting flags"* ]] 275 } 276 277 @test "--chain kitty --bootnodes=enode://e809c4a2fec7daed400e5e28564e23693b23b2cc5a019b612505631bbe7b9ccf709c1796d2a3d29ef2b045f210caf51e3c4f5b6d3587d43ad5d6397526fa6179@174.112.32.157:30303 | exit 0" { 278 mkdir -p $DATA_DIR/kitty 279 cp $BATS_TEST_DIRNAME/../../core/config/mainnet.json $DATA_DIR/kitty/chain.json 280 cp $BATS_TEST_DIRNAME/../../core/config/mainnet_genesis.json $DATA_DIR/kitty/kitty_genesis.json 281 cp $BATS_TEST_DIRNAME/../../core/config/mainnet_bootnodes.json $DATA_DIR/kitty/kitty_bootnodes.json 282 cp $BATS_TEST_DIRNAME/../../core/config/mainnet_genesis_alloc.csv $DATA_DIR/kitty/kitty_genesis_alloc.csv 283 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/chain.json 284 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/kitty_genesis.json 285 run $GETH_CMD --data-dir $DATA_DIR --chain kitty --bootnodes=enode://e809c4a2fec7daed400e5e28564e23693b23b2cc5a019b612505631bbe7b9ccf709c1796d2a3d29ef2b045f210caf51e3c4f5b6d3587d43ad5d6397526fa6179@174.112.32.157:30303 --exec 'exit;' console 286 echo "$output" 287 [ "$status" -eq 0 ] 288 [[ "$output" == *"Overwriting external bootnodes"* ]] 289 } 290 291 # Test fails to load invalid chain configuration from testdata/ JSON file. 292 # Test ensures 293 # - external chain configuration should require JSON to parse 294 @test "--chain kitty @ chain_config_dump-invalid-comment.json | exit !=0" { 295 mkdir -p $DATA_DIR/kitty 296 cp $BATS_TEST_DIRNAME/../../cmd/geth/testdata/chain_config_dump-invalid-comment.json $DATA_DIR/kitty/chain.json 297 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/chain.json 298 run $GETH_CMD --datadir $DATA_DIR --chain=kitty --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).nonce' console 299 echo "$output" 300 [ "$status" -ne 0 ] 301 } 302 303 # Test fails to load invalid chain configuration from testdata/ JSON file. 304 # Test ensures 305 # - external chain configuration should require JSON to parse 306 @test "--chain kitty testdata/chain_config_dump-invalid-coinbase.json | exit !=0" { 307 mkdir -p $DATA_DIR/kitty 308 cp $BATS_TEST_DIRNAME/../../cmd/geth/testdata/chain_config_dump-invalid-coinbase.json $DATA_DIR/kitty/chain.json 309 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/chain.json 310 run $GETH_CMD --datadir $DATA_DIR --chain kitty --maxpeers 0 --nodiscover --nat none --ipcdisable --exec 'eth.getBlock(0).nonce' console 311 echo "$output" 312 [ "$status" -ne 0 ] 313 } 314 315 freshconfig() { 316 rm -fr $DATA_DIR 317 DATA_DIR=`mktemp -d` 318 mkdir -p $DATA_DIR/kitty 319 cp "$BATS_TEST_DIRNAME/../../core/config/mainnet.json" "$DATA_DIR/kitty/chain.json" 320 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/chain.json 321 } 322 323 @test "--chain kitty @ sed -i s/key/badkey/ kitty/chain.json | exit !=0" { 324 declare -a OK_VARS=(id genesis chainConfig bootstrap) # 'name' can be blank... it's only for human consumption 325 declare -a NOTOK_VARS=(did genes chainconfig bootsrap) 326 327 mkdir -p $DATA_DIR/kitty 328 cp $BATS_TEST_DIRNAME/../../core/config/mainnet.json $DATA_DIR/kitty/chain.json 329 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/chain.json 330 331 counter=0 332 for var in "${OK_VARS[@]}" 333 do 334 sed -i.bu "s/${var}/${NOTOK_VARS[counter]}/" "$DATA_DIR/kitty/chain.json" 335 336 run $GETH_CMD --datadir $DATA_DIR --chain kitty console 337 echo "$output" 338 [ "$status" -ne 0 ] 339 if [ ! "$status" -ne 0 ]; then 340 echo "allowed invalid attribute: ${var}" 341 fi 342 343 freshconfig() 344 ((counter=counter+1)) 345 done 346 } 347 348 @test "--chain kitty @ sed -i s/subkey/badsubkey/ kitty/chain.json | exit !=0" { 349 declare -a OK_VARS=(nonce gasLimit difficulty forks alloc balance Block Hash) # 'name' can be blank... it's only for human consumption 350 declare -a NOTOK_VARS=(noneonce gasLim dificile knives allok bills Clock Cash) 351 352 mkdir -p $DATA_DIR/kitty 353 cp $BATS_TEST_DIRNAME/../../core/config/mainnet.json $DATA_DIR/kitty/chain.json 354 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/chain.json 355 356 counter=0 357 for var in "${OK_VARS[@]}" 358 do 359 sed -i.bu "s/${var}/${NOTOK_VARS[counter]}/" "$DATA_DIR/kitty/chain.json" 360 361 run $GETH_CMD --datadir $DATA_DIR --chain kitty console 362 echo "$output" 363 [ "$status" -ne 0 ] 364 if [ ! "$status" -ne 0 ]; then 365 echo "allowed invalid attribute: ${var}" 366 fi 367 368 freshconfig() 369 ((counter=counter+1)) 370 done 371 } 372 373 @test "--chain kitty @ sed -i s/value/badvalue/ kitty/chain.json | exit !=0" { 374 declare -a OK_VARS=(0x0000000000000042 0x0000000000000000000000000000000000000000000000000000000000001388 0x0000000000000000000000000000000000000000 enode homestead) # 'name' can be blank... it's only for human consumption 375 declare -a NOTOK_VARS=(Ox0000000000000042 Ox0000000000000000000000000000000000000000000000000000000000001388 0x000000000000000000000000000000000000000 ewok homeinbed) 376 377 mkdir -p $DATA_DIR/kitty 378 cp $BATS_TEST_DIRNAME/../../core/config/mainnet.json $DATA_DIR/kitty/chain.json 379 sed -i.bak s/mainnet/kitty/ $DATA_DIR/kitty/chain.json 380 381 counter=0 382 for var in "${OK_VARS[@]}" 383 do 384 sed -i.bu "s/${var}/${NOTOK_VARS[counter]}/" "$DATA_DIR/kitty/chain.json" 385 386 run $GETH_CMD --datadir $DATA_DIR --chain kitty console 387 echo "$output" 388 [ "$status" -ne 0 ] 389 if [ ! "$status" -ne 0 ]; then 390 echo "allowed invalid attribute: ${var}" 391 fi 392 393 freshconfig() 394 ((counter=counter+1)) 395 done 396 }