github.com/bcskill/bcschain/v3@v3.4.9-beta2/cmd/gochain/genesis_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "testing" 24 ) 25 26 type customGenesisTest struct { 27 genesis string 28 query string 29 result string 30 } 31 32 var customGenesisTests = map[string]customGenesisTest{ 33 // Genesis file with an empty chain configuration (ensure missing fields work) 34 "empty": { 35 genesis: `{ 36 "alloc" : {}, 37 "coinbase" : "0x0000000000000000000000000000000000000000", 38 "difficulty" : "0x20000", 39 "extraData" : "", 40 "gasLimit" : "0x2fefd8", 41 "nonce" : "0x0125864321546982", 42 "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 43 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 44 "timestamp" : "0x00", 45 "config" : { 46 "clique": { 47 "period": 5, 48 "epoch": 3000 49 } 50 } 51 }`, 52 query: "eth.getBlock(0).nonce", 53 result: "0x0125864321546982", 54 }, 55 // Genesis file with specific chain configurations 56 "specific": { 57 genesis: `{ 58 "alloc" : {}, 59 "coinbase" : "0x0000000000000000000000000000000000000000", 60 "difficulty" : "0x20000", 61 "extraData" : "", 62 "gasLimit" : "0x2fefd8", 63 "nonce" : "0x0000159876215648", 64 "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 65 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 66 "timestamp" : "0x00", 67 "config" : { 68 "homesteadBlock" : 314, 69 "clique": { 70 "period": 5, 71 "epoch": 3000 72 } 73 } 74 }`, 75 query: "eth.getBlock(0).nonce", 76 result: "0x0000159876215648", 77 }, 78 } 79 80 // Tests that initializing GoChain with a custom genesis block and chain definitions 81 // work properly. 82 func TestCustomGenesis(t *testing.T) { 83 for name, test := range customGenesisTests { 84 t.Run(name, test.run) 85 } 86 } 87 func (test customGenesisTest) run(t *testing.T) { 88 // Create a temporary data directory to use and inspect later 89 datadir := tmpdir(t) 90 defer os.RemoveAll(datadir) 91 92 // Initialize the data directory with the custom genesis block 93 json := filepath.Join(datadir, "genesis.json") 94 if err := ioutil.WriteFile(json, []byte(test.genesis), 0600); err != nil { 95 t.Fatalf("failed to write genesis file: %v", err) 96 } 97 runGoChain(t, "--datadir", datadir, "init", json).WaitExit() 98 99 // Query the custom genesis block 100 geth := runGoChain(t, 101 "--datadir", datadir, "--maxpeers", "0", "--port", "0", 102 "--nodiscover", "--nat", "none", "--ipcdisable", 103 "--exec", test.query, "console") 104 geth.ExpectRegexp(test.result) 105 geth.ExpectExit() 106 }