github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/cmd/geth/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 "os" 21 "path/filepath" 22 "testing" 23 ) 24 25 var customGenesisTests = []struct { 26 genesis string 27 query string 28 result string 29 }{ 30 // Genesis file with an empty chain configuration (ensure missing fields work) 31 { 32 genesis: `{ 33 "alloc" : {}, 34 "coinbase" : "0x0000000000000000000000000000000000000000", 35 "difficulty" : "0x20000", 36 "extraData" : "", 37 "gasLimit" : "0x2fefd8", 38 "nonce" : "0x0000000000001338", 39 "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 40 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 41 "timestamp" : "0x00", 42 "config" : {} 43 }`, 44 query: "eth.getBlock(0).nonce", 45 result: "0x0000000000001338", 46 }, 47 // Genesis file with specific chain configurations 48 { 49 genesis: `{ 50 "alloc" : {}, 51 "coinbase" : "0x0000000000000000000000000000000000000000", 52 "difficulty" : "0x20000", 53 "extraData" : "", 54 "gasLimit" : "0x2fefd8", 55 "nonce" : "0x0000000000001339", 56 "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 57 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 58 "timestamp" : "0x00", 59 "config" : { 60 "homesteadBlock" : 42, 61 "daoForkBlock" : 141, 62 "daoForkSupport" : true 63 } 64 }`, 65 query: "eth.getBlock(0).nonce", 66 result: "0x0000000000001339", 67 }, 68 } 69 70 // Tests that initializing Geth with a custom genesis block and chain definitions 71 // work properly. 72 func TestCustomGenesis(t *testing.T) { 73 for i, tt := range customGenesisTests { 74 // Create a temporary data directory to use and inspect later 75 datadir := t.TempDir() 76 77 // Initialize the data directory with the custom genesis block 78 json := filepath.Join(datadir, "genesis.json") 79 if err := os.WriteFile(json, []byte(tt.genesis), 0600); err != nil { 80 t.Fatalf("test %d: failed to write genesis file: %v", i, err) 81 } 82 runGeth(t, "--datadir", datadir, "init", json).WaitExit() 83 84 // Query the custom genesis block 85 geth := runGeth(t, "--networkid", "1337", "--syncmode=full", "--cache", "16", 86 "--datadir", datadir, "--maxpeers", "0", "--port", "0", "--authrpc.port", "0", 87 "--nodiscover", "--nat", "none", "--ipcdisable", 88 "--exec", tt.query, "console") 89 geth.ExpectRegexp(tt.result) 90 geth.ExpectExit() 91 } 92 }