github.com/klaytn/klaytn@v1.12.1/cmd/utils/nodecmd/genesis_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of go-ethereum. 4 // 5 // go-ethereum is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // go-ethereum is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from cmd/geth/genesis_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package nodecmd 22 23 import ( 24 "os" 25 "path/filepath" 26 "testing" 27 ) 28 29 var customGenesisTests = []struct { 30 genesis string 31 query []string 32 result []string 33 }{ 34 // Plain genesis file without anything extra 35 { 36 genesis: `{ 37 "alloc" : {}, 38 "blockScore" : "0x20000", 39 "extraData" : "0x0000000000000000000000000000000000000000000000000000000000000000f89af85494dddfb991127b43e209c2f8ed08b8b3d0b5843d3694195ba9cc787b00796a7ae6356e5b656d4360353794777fd033b5e3bcaad6006bc9f481ffed6b83cf5a94d473284239f704adccd24647c7ca132992a28973b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0", 40 "gasLimit" : "0x2fefd8", 41 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 42 "timestamp" : "0x00" 43 }`, 44 query: []string{"klay.getBlock(0).parentHash"}, 45 result: []string{"0x0000000000000000000000000000000000000000000000000000000000000000"}, 46 }, 47 // Genesis file with an empty chain configuration (ensure missing fields work) 48 { 49 genesis: `{ 50 "alloc" : {}, 51 "blockScore" : "0x20000", 52 "extraData" : "0x0000000000000000000000000000000000000000000000000000000000000000f89af85494dddfb991127b43e209c2f8ed08b8b3d0b5843d3694195ba9cc787b00796a7ae6356e5b656d4360353794777fd033b5e3bcaad6006bc9f481ffed6b83cf5a94d473284239f704adccd24647c7ca132992a28973b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0", 53 "gasLimit" : "0x2fefd8", 54 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 55 "timestamp" : "0x00", 56 "config" : {} 57 }`, 58 query: []string{"klay.getBlock(0).parentHash"}, 59 result: []string{"0x0000000000000000000000000000000000000000000000000000000000000000"}, 60 }, 61 // Genesis file with specific chain configurations 62 { 63 genesis: `{ 64 "alloc" : {}, 65 "blockScore" : "0x20000", 66 "extraData" : "0x0000000000000000000000000000000000000000000000000000000000000000f89af85494dddfb991127b43e209c2f8ed08b8b3d0b5843d3694195ba9cc787b00796a7ae6356e5b656d4360353794777fd033b5e3bcaad6006bc9f481ffed6b83cf5a94d473284239f704adccd24647c7ca132992a28973b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0", 67 "gasLimit" : "0x2fefd8", 68 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 69 "timestamp" : "0x00", 70 "config" : { 71 "homesteadBlock" : 314, 72 "daoForkBlock" : 141, 73 "daoForkSupport" : true 74 } 75 }`, 76 query: []string{"klay.getBlock(0).parentHash"}, 77 result: []string{"0x0000000000000000000000000000000000000000000000000000000000000000"}, 78 }, 79 { 80 genesis: `{ 81 "config": { 82 "chainId": 1000, 83 "istanbul": { 84 "epoch": 30, 85 "policy": 0, 86 "sub": 22 87 }, 88 "unitPrice": 0, 89 "deriveShaImpl": 2, 90 "governance": null 91 }, 92 "timestamp": "0x5da3fdfd", 93 "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000f89af8549413c9a496fb7b84ea6bf39f3602658c41f0dd7a51947cf8c6a6a6a4fbb9b846527b5d762b278adfe7369438b99937e557a21d9ab9b6d9cfc07498b50660d09442b1d1fccee0e7b51a5091c532eb2b92a1e49296b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0", 94 "governanceData": null, 95 "blockScore": "0x1", 96 "alloc": { 97 "04d67289f30351fc46c161eb3c588d07c2995367": { 98 "balance": "0x446c3b15f9926687d2c40534fdb564000000000000" 99 }, 100 "13c9a496fb7b84ea6bf39f3602658c41f0dd7a51": { 101 "balance": "0x446c3b15f9926687d2c40534fdb564000000000000" 102 }, 103 "2913ecaec7da798466611271a27b53836f20b108": { 104 "balance": "0x446c3b15f9926687d2c40534fdb564000000000000" 105 }, 106 "30208f32c70e8b53a67ea171c8720cbfe32888ff": { 107 "balance": "0x446c3b15f9926687d2c40534fdb564000000000000" 108 } 109 }, 110 "number": "0x0", 111 "gasUsed": "0x0", 112 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" 113 }`, 114 query: []string{"klay.getBlock(0).parentHash", "klay.getBlock(0).blockScore", "klay.getBlock(0).extraData", "klay.chainId", "governance.getParams()[\"governance.deriveshaimpl\"]", "governance.getParams()[\"governance.unitprice\"]", "governance.getParams()[\"governance.governancemode\"]", "governance.getParams()[\"governance.governingnode\"]", "governance.getParams()[\"reward.deferredtxfee\"]", "governance.getParams()[\"reward.minimumstake\"]", "governance.getParams()[\"reward.mintingamount\"]", "governance.getParams()[\"reward.proposerupdateinterval\"]", "governance.getParams()[\"reward.ratio\"]", "governance.getParams()[\"reward.stakingupdateinterval\"]", "governance.getParams()[\"reward.useginicoeff\"]", "governance.getParams()[\"istanbul.epoch\"]", "governance.getParams()[\"istanbul.policy\"]", "governance.getParams()[\"istanbul.committeesize\"]"}, 115 result: []string{"0x0000000000000000000000000000000000000000000000000000000000000000", "0x1", "0x0000000000000000000000000000000000000000000000000000000000000000f89af8549413c9a496fb7b84ea6bf39f3602658c41f0dd7a51947cf8c6a6a6a4fbb9b846527b5d762b278adfe7369438b99937e557a21d9ab9b6d9cfc07498b50660d09442b1d1fccee0e7b51a5091c532eb2b92a1e49296b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0", "1000", "2", "0", "none", "0x0000000000000000000000000000000000000000", "false", "2000000", "0", "3600", "100/0/0", "86400", "false", "30", "0", "22"}, 116 }, 117 { 118 genesis: `{ 119 "config":{ 120 "chainId":2019, 121 "istanbul":{ 122 "epoch":30, 123 "policy":2, 124 "sub":13 125 }, 126 "unitPrice":25000000000, 127 "deriveShaImpl":2, 128 "governance":{ 129 "governingNode":"0xdddfb991127b43e209c2f8ed08b8b3d0b5843d36", 130 "governanceMode":"single", 131 "reward":{ 132 "mintingAmount":9600000000000000000, 133 "ratio":"34/54/12", 134 "useGiniCoeff":false, 135 "deferredTxFee":true, 136 "stakingUpdateInterval":60, 137 "proposerUpdateInterval":30, 138 "minimumStake":5000000 139 } 140 } 141 }, 142 "timestamp":"0x5ce33d6e", 143 "extraData":"0x0000000000000000000000000000000000000000000000000000000000000000f89af85494dddfb991127b43e209c2f8ed08b8b3d0b5843d3694195ba9cc787b00796a7ae6356e5b656d4360353794777fd033b5e3bcaad6006bc9f481ffed6b83cf5a94d473284239f704adccd24647c7ca132992a28973b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0", 144 "governanceData":null, 145 "blockScore":"0x1", 146 "alloc":{ 147 "195ba9cc787b00796a7ae6356e5b656d43603537":{"balance":"0x446c3b15f9926687d2c40534fdb564000000000000"}, 148 "777fd033b5e3bcaad6006bc9f481ffed6b83cf5a":{"balance":"0x446c3b15f9926687d2c40534fdb564000000000000"}, 149 "d473284239f704adccd24647c7ca132992a28973":{"balance":"0x446c3b15f9926687d2c40534fdb564000000000000"}, 150 "dddfb991127b43e209c2f8ed08b8b3d0b5843d36":{"balance":"0x446c3b15f9926687d2c40534fdb564000000000000"}, 151 "f4316f69d9522667c0674afcd8638288489f0333":{"balance":"0x446c3b15f9926687d2c40534fdb564000000000000"} 152 }, 153 "number":"0x0", 154 "gasUsed":"0x0", 155 "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000" 156 }`, 157 query: []string{"klay.getBlock(0).parentHash", "klay.getBlock(0).blockScore", "klay.getBlock(0).extraData", "klay.chainId", "governance.getParams()[\"governance.deriveshaimpl\"]", "governance.getParams()[\"governance.unitprice\"]", "governance.getParams()[\"governance.governancemode\"]", "governance.getParams()[\"governance.governingnode\"]", "governance.getParams()[\"reward.deferredtxfee\"]", "governance.getParams()[\"reward.minimumstake\"]", "governance.getParams()[\"reward.mintingamount\"]", "governance.getParams()[\"reward.proposerupdateinterval\"]", "governance.getParams()[\"reward.ratio\"]", "governance.getParams()[\"reward.stakingupdateinterval\"]", "governance.getParams()[\"reward.useginicoeff\"]", "governance.getParams()[\"istanbul.epoch\"]", "governance.getParams()[\"istanbul.policy\"]", "governance.getParams()[\"istanbul.committeesize\"]"}, 158 result: []string{"0x0000000000000000000000000000000000000000000000000000000000000000", "0x1", "0x0000000000000000000000000000000000000000000000000000000000000000f89af85494dddfb991127b43e209c2f8ed08b8b3d0b5843d3694195ba9cc787b00796a7ae6356e5b656d4360353794777fd033b5e3bcaad6006bc9f481ffed6b83cf5a94d473284239f704adccd24647c7ca132992a28973b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0", "2019", "2", "25000000000", "single", "0xdddfb991127b43e209c2f8ed08b8b3d0b5843d36", "true", "5000000", "9600000000000000000", "30", "34/54/12", "60", "false", "30", "2", "13"}, 159 }, 160 } 161 162 // Tests that initializing Klay with a custom genesis block and chain definitions 163 // work properly. 164 func TestCustomGenesis(t *testing.T) { 165 for i, tt := range customGenesisTests { 166 // Create a temporary data directory to use and inspect later 167 datadir := tmpdir(t) 168 defer os.RemoveAll(datadir) 169 170 // Initialize the data directory with the custom genesis block 171 json := filepath.Join(datadir, "genesis.json") 172 if err := os.WriteFile(json, []byte(tt.genesis), 0o600); err != nil { 173 t.Fatalf("test %d: failed to write genesis file: %v", i, err) 174 } 175 runKlay(t, "klay-test", "--datadir", datadir, "--verbosity", "0", "init", json).WaitExit() 176 177 // Query the custom genesis block 178 if len(tt.query) != len(tt.result) { 179 t.Errorf("Test cases are wrong, #query: %v, #result, %v", len(tt.query), len(tt.result)) 180 } 181 for idx, query := range tt.query { 182 klay := runKlay(t, 183 "klay-test", "--datadir", datadir, "--maxconnections", "0", "--port", "0", 184 "--nodiscover", "--nat", "none", "--ipcdisable", "--ntp.disable", 185 "--exec", query, "--verbosity", "0", "console") 186 klay.ExpectRegexp(tt.result[idx]) 187 klay.ExpectExit() 188 } 189 } 190 }