github.com/core-coin/go-core/v2@v2.1.9/cmd/gocore/genesis_test.go (about) 1 // Copyright 2023 by the Authors 2 // This file is part of go-core. 3 // 4 // go-core 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-core 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-core. 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 var customGenesisTests = []struct { 27 genesis string 28 query string 29 result string 30 }{ 31 // Plain genesis file without anything extra 32 { 33 genesis: `{ 34 "alloc" : {}, 35 "coinbase" : "0x0000000000000000000000000000000000000000", 36 "difficulty" : "0x20000", 37 "extraData" : "", 38 "energyLimit" : "0x2fefd8", 39 "nonce" : "0x0000000000000047", 40 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 41 "timestamp" : "0x00" 42 }`, 43 query: "xcb.getBlock(0).nonce", 44 result: "0x0000000000000047", 45 }, 46 // Genesis file with an empty chain configuration (ensure missing fields work) 47 { 48 genesis: `{ 49 "alloc" : {}, 50 "coinbase" : "0x0000000000000000000000000000000000000000", 51 "difficulty" : "0x20000", 52 "extraData" : "", 53 "energyLimit" : "0x2fefd8", 54 "nonce" : "0x0000000000000047", 55 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 56 "timestamp" : "0x00", 57 "config" : {} 58 }`, 59 query: "xcb.getBlock(0).nonce", 60 result: "0x0000000000000047", 61 }, 62 // Genesis file with specific chain configurations 63 { 64 genesis: `{ 65 "alloc" : {}, 66 "coinbase" : "0x0000000000000000000000000000000000000000", 67 "difficulty" : "0x20000", 68 "extraData" : "", 69 "energyLimit" : "0x2fefd8", 70 "nonce" : "0x0000000000000047", 71 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 72 "timestamp" : "0x00", 73 "config" : {}, 74 }`, 75 query: "xcb.getBlock(0).nonce", 76 result: "0x0000000000000047", 77 }, 78 } 79 80 // Tests that initializing Gocore with a custom genesis block and chain definitions 81 // work properly. 82 func TestCustomGenesis(t *testing.T) { 83 for i, tt := range customGenesisTests { 84 // Create a temporary data directory to use and inspect later 85 datadir := tmpdir(t) 86 defer os.RemoveAll(datadir) 87 88 // Initialize the data directory with the custom genesis block 89 json := filepath.Join(datadir, "genesis.json") 90 if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil { 91 t.Fatalf("test %d: failed to write genesis file: %v", i, err) 92 } 93 runGocore(t, "--datadir", datadir, "init", json).WaitExit() 94 95 // Query the custom genesis block 96 gocore := runGocore(t, 97 "--datadir", datadir, "--maxpeers", "0", "--port", "0", 98 "--nodiscover", "--nat", "none", "--ipcdisable", 99 "--exec", tt.query, "console") 100 gocore.ExpectRegexp(tt.result) 101 gocore.ExpectExit() 102 } 103 }