github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/cmd/puppeth/genesis.go (about) 1 // Copyright 2017 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 "encoding/binary" 21 "errors" 22 "math" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/common/hexutil" 26 "github.com/ethereum/go-ethereum/consensus/ethash" 27 "github.com/ethereum/go-ethereum/core" 28 "github.com/ethereum/go-ethereum/params" 29 ) 30 31 // cppEthereumGenesisSpec represents the genesis specification format used by the 32 // C++ Ethereum implementation. 33 type cppEthereumGenesisSpec struct { 34 SealEngine string `json:"sealEngine"` 35 Params struct { 36 AccountStartNonce hexutil.Uint64 `json:"accountStartNonce"` 37 HomesteadForkBlock hexutil.Uint64 `json:"homesteadForkBlock"` 38 EIP150ForkBlock hexutil.Uint64 `json:"EIP150ForkBlock"` 39 EIP158ForkBlock hexutil.Uint64 `json:"EIP158ForkBlock"` 40 ByzantiumForkBlock hexutil.Uint64 `json:"byzantiumForkBlock"` 41 ConstantinopleForkBlock hexutil.Uint64 `json:"constantinopleForkBlock"` 42 NetworkID hexutil.Uint64 `json:"networkID"` 43 ChainID hexutil.Uint64 `json:"chainID"` 44 MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"` 45 MinGasLimit hexutil.Uint64 `json:"minGasLimit"` 46 MaxGasLimit hexutil.Uint64 `json:"maxGasLimit"` 47 GasLimitBoundDivisor hexutil.Uint64 `json:"gasLimitBoundDivisor"` 48 MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"` 49 DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"` 50 DurationLimit *hexutil.Big `json:"durationLimit"` 51 BlockReward *hexutil.Big `json:"blockReward"` 52 } `json:"params"` 53 54 // 创世区块类,后面对应的是配置文件json字符串 55 Genesis struct { 56 Nonce hexutil.Bytes `json:"nonce"` // 与mixHash结合用于满足POW条件 57 Difficulty *hexutil.Big `json:"difficulty"` // POW挖矿难度值 58 MixHash common.Hash `json:"mixHash"` // 与nonce结合用于满足POW条件 59 Author common.Address `json:"author"` 60 Timestamp hexutil.Uint64 `json:"timestamp"` // 时间戳 61 ParentHash common.Hash `json:"parentHash"` // 父区块哈希 62 ExtraData hexutil.Bytes `json:"extraData"` // 与此区块相关的其他数据 63 GasLimit hexutil.Uint64 `json:"gasLimit"` // 每个区块的Gas限制 64 } `json:"genesis"` 65 66 Accounts map[common.Address]*cppEthereumGenesisSpecAccount `json:"accounts"` 67 } 68 69 // cppEthereumGenesisSpecAccount is the prefunded genesis account and/or precompiled 70 // contract definition. 71 type cppEthereumGenesisSpecAccount struct { 72 Balance *hexutil.Big `json:"balance"` 73 Nonce uint64 `json:"nonce,omitempty"` 74 Precompiled *cppEthereumGenesisSpecBuiltin `json:"precompiled,omitempty"` 75 } 76 77 // cppEthereumGenesisSpecBuiltin is the precompiled contract definition. 78 type cppEthereumGenesisSpecBuiltin struct { 79 Name string `json:"name,omitempty"` 80 StartingBlock hexutil.Uint64 `json:"startingBlock,omitempty"` 81 Linear *cppEthereumGenesisSpecLinearPricing `json:"linear,omitempty"` 82 } 83 84 type cppEthereumGenesisSpecLinearPricing struct { 85 Base uint64 `json:"base"` 86 Word uint64 `json:"word"` 87 } 88 89 // newCppEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific 90 // chain specification format. 91 func newCppEthereumGenesisSpec(network string, genesis *core.Genesis) (*cppEthereumGenesisSpec, error) { 92 // Only ethash is currently supported between go-ethereum and cpp-ethereum 93 if genesis.Config.Ethash == nil { 94 return nil, errors.New("unsupported consensus engine") 95 } 96 // Reconstruct the chain spec in Parity's format 97 spec := &cppEthereumGenesisSpec{ 98 SealEngine: "Ethash", 99 } 100 spec.Params.AccountStartNonce = 0 101 spec.Params.HomesteadForkBlock = (hexutil.Uint64)(genesis.Config.HomesteadBlock.Uint64()) 102 spec.Params.EIP150ForkBlock = (hexutil.Uint64)(genesis.Config.EIP150Block.Uint64()) 103 spec.Params.EIP158ForkBlock = (hexutil.Uint64)(genesis.Config.EIP158Block.Uint64()) 104 spec.Params.ByzantiumForkBlock = (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()) 105 spec.Params.ConstantinopleForkBlock = (hexutil.Uint64)(math.MaxUint64) 106 107 spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 108 spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 109 110 spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize) 111 spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit) 112 spec.Params.MaxGasLimit = (hexutil.Uint64)(math.MaxUint64) 113 spec.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty) 114 spec.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor) 115 spec.Params.GasLimitBoundDivisor = (hexutil.Uint64)(params.GasLimitBoundDivisor) 116 spec.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit) 117 spec.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward) 118 119 spec.Genesis.Nonce = (hexutil.Bytes)(make([]byte, 8)) 120 binary.LittleEndian.PutUint64(spec.Genesis.Nonce[:], genesis.Nonce) 121 122 spec.Genesis.MixHash = genesis.Mixhash 123 spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty) 124 spec.Genesis.Author = genesis.Coinbase 125 spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp) 126 spec.Genesis.ParentHash = genesis.ParentHash 127 spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData) 128 spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit) 129 130 spec.Accounts = make(map[common.Address]*cppEthereumGenesisSpecAccount) 131 for address, account := range genesis.Alloc { 132 spec.Accounts[address] = &cppEthereumGenesisSpecAccount{ 133 Balance: (*hexutil.Big)(account.Balance), 134 Nonce: account.Nonce, 135 } 136 } 137 spec.Accounts[common.BytesToAddress([]byte{1})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 138 Name: "ecrecover", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 3000}, 139 } 140 spec.Accounts[common.BytesToAddress([]byte{2})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 141 Name: "sha256", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 60, Word: 12}, 142 } 143 spec.Accounts[common.BytesToAddress([]byte{3})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 144 Name: "ripemd160", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 600, Word: 120}, 145 } 146 spec.Accounts[common.BytesToAddress([]byte{4})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 147 Name: "identity", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 15, Word: 3}, 148 } 149 if genesis.Config.ByzantiumBlock != nil { 150 spec.Accounts[common.BytesToAddress([]byte{5})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 151 Name: "modexp", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), 152 } 153 spec.Accounts[common.BytesToAddress([]byte{6})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 154 Name: "alt_bn128_G1_add", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), Linear: &cppEthereumGenesisSpecLinearPricing{Base: 500}, 155 } 156 spec.Accounts[common.BytesToAddress([]byte{7})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 157 Name: "alt_bn128_G1_mul", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), Linear: &cppEthereumGenesisSpecLinearPricing{Base: 40000}, 158 } 159 spec.Accounts[common.BytesToAddress([]byte{8})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 160 Name: "alt_bn128_pairing_product", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), 161 } 162 } 163 return spec, nil 164 } 165 166 // parityChainSpec is the chain specification format used by Parity. 167 type parityChainSpec struct { 168 Name string `json:"name"` 169 Engine struct { 170 Ethash struct { 171 Params struct { 172 MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"` 173 DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"` 174 DurationLimit *hexutil.Big `json:"durationLimit"` 175 BlockReward *hexutil.Big `json:"blockReward"` 176 HomesteadTransition uint64 `json:"homesteadTransition"` 177 EIP150Transition uint64 `json:"eip150Transition"` 178 EIP160Transition uint64 `json:"eip160Transition"` 179 EIP161abcTransition uint64 `json:"eip161abcTransition"` 180 EIP161dTransition uint64 `json:"eip161dTransition"` 181 EIP649Reward *hexutil.Big `json:"eip649Reward"` 182 EIP100bTransition uint64 `json:"eip100bTransition"` 183 EIP649Transition uint64 `json:"eip649Transition"` 184 } `json:"params"` 185 } `json:"Ethash"` 186 } `json:"engine"` 187 188 Params struct { 189 MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"` 190 MinGasLimit hexutil.Uint64 `json:"minGasLimit"` 191 GasLimitBoundDivisor hexutil.Uint64 `json:"gasLimitBoundDivisor"` 192 NetworkID hexutil.Uint64 `json:"networkID"` 193 MaxCodeSize uint64 `json:"maxCodeSize"` 194 EIP155Transition uint64 `json:"eip155Transition"` 195 EIP98Transition uint64 `json:"eip98Transition"` 196 EIP86Transition uint64 `json:"eip86Transition"` 197 EIP140Transition uint64 `json:"eip140Transition"` 198 EIP211Transition uint64 `json:"eip211Transition"` 199 EIP214Transition uint64 `json:"eip214Transition"` 200 EIP658Transition uint64 `json:"eip658Transition"` 201 } `json:"params"` 202 203 Genesis struct { 204 Seal struct { 205 Ethereum struct { 206 Nonce hexutil.Bytes `json:"nonce"` 207 MixHash hexutil.Bytes `json:"mixHash"` 208 } `json:"ethereum"` 209 } `json:"seal"` 210 211 Difficulty *hexutil.Big `json:"difficulty"` 212 Author common.Address `json:"author"` 213 Timestamp hexutil.Uint64 `json:"timestamp"` 214 ParentHash common.Hash `json:"parentHash"` 215 ExtraData hexutil.Bytes `json:"extraData"` 216 GasLimit hexutil.Uint64 `json:"gasLimit"` 217 } `json:"genesis"` 218 219 Nodes []string `json:"nodes"` 220 Accounts map[common.Address]*parityChainSpecAccount `json:"accounts"` 221 } 222 223 // parityChainSpecAccount is the prefunded genesis account and/or precompiled 224 // contract definition. 225 type parityChainSpecAccount struct { 226 Balance *hexutil.Big `json:"balance"` 227 Nonce uint64 `json:"nonce,omitempty"` 228 Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"` 229 } 230 231 // parityChainSpecBuiltin is the precompiled contract definition. 232 type parityChainSpecBuiltin struct { 233 Name string `json:"name,omitempty"` 234 ActivateAt uint64 `json:"activate_at,omitempty"` 235 Pricing *parityChainSpecPricing `json:"pricing,omitempty"` 236 } 237 238 // parityChainSpecPricing represents the different pricing models that builtin 239 // contracts might advertise using. 240 type parityChainSpecPricing struct { 241 Linear *parityChainSpecLinearPricing `json:"linear,omitempty"` 242 ModExp *parityChainSpecModExpPricing `json:"modexp,omitempty"` 243 AltBnPairing *parityChainSpecAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"` 244 } 245 246 type parityChainSpecLinearPricing struct { 247 Base uint64 `json:"base"` 248 Word uint64 `json:"word"` 249 } 250 251 type parityChainSpecModExpPricing struct { 252 Divisor uint64 `json:"divisor"` 253 } 254 255 type parityChainSpecAltBnPairingPricing struct { 256 Base uint64 `json:"base"` 257 Pair uint64 `json:"pair"` 258 } 259 260 // newParityChainSpec converts a go-ethereum genesis block into a Parity specific 261 // chain specification format. 262 func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) { 263 // Only ethash is currently supported between go-ethereum and Parity 264 if genesis.Config.Ethash == nil { 265 return nil, errors.New("unsupported consensus engine") 266 } 267 // Reconstruct the chain spec in Parity's format 268 spec := &parityChainSpec{ 269 Name: network, 270 Nodes: bootnodes, 271 } 272 spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty) 273 spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor) 274 spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit) 275 spec.Engine.Ethash.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward) 276 spec.Engine.Ethash.Params.HomesteadTransition = genesis.Config.HomesteadBlock.Uint64() 277 spec.Engine.Ethash.Params.EIP150Transition = genesis.Config.EIP150Block.Uint64() 278 spec.Engine.Ethash.Params.EIP160Transition = genesis.Config.EIP155Block.Uint64() 279 spec.Engine.Ethash.Params.EIP161abcTransition = genesis.Config.EIP158Block.Uint64() 280 spec.Engine.Ethash.Params.EIP161dTransition = genesis.Config.EIP158Block.Uint64() 281 spec.Engine.Ethash.Params.EIP649Reward = (*hexutil.Big)(ethash.ByzantiumBlockReward) 282 spec.Engine.Ethash.Params.EIP100bTransition = genesis.Config.ByzantiumBlock.Uint64() 283 spec.Engine.Ethash.Params.EIP649Transition = genesis.Config.ByzantiumBlock.Uint64() 284 285 spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize) 286 spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit) 287 spec.Params.GasLimitBoundDivisor = (hexutil.Uint64)(params.GasLimitBoundDivisor) 288 spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 289 spec.Params.MaxCodeSize = params.MaxCodeSize 290 spec.Params.EIP155Transition = genesis.Config.EIP155Block.Uint64() 291 spec.Params.EIP98Transition = math.MaxUint64 292 spec.Params.EIP86Transition = math.MaxUint64 293 spec.Params.EIP140Transition = genesis.Config.ByzantiumBlock.Uint64() 294 spec.Params.EIP211Transition = genesis.Config.ByzantiumBlock.Uint64() 295 spec.Params.EIP214Transition = genesis.Config.ByzantiumBlock.Uint64() 296 spec.Params.EIP658Transition = genesis.Config.ByzantiumBlock.Uint64() 297 298 spec.Genesis.Seal.Ethereum.Nonce = (hexutil.Bytes)(make([]byte, 8)) 299 binary.LittleEndian.PutUint64(spec.Genesis.Seal.Ethereum.Nonce[:], genesis.Nonce) 300 301 spec.Genesis.Seal.Ethereum.MixHash = (hexutil.Bytes)(genesis.Mixhash[:]) 302 spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty) 303 spec.Genesis.Author = genesis.Coinbase 304 spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp) 305 spec.Genesis.ParentHash = genesis.ParentHash 306 spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData) 307 spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit) 308 309 spec.Accounts = make(map[common.Address]*parityChainSpecAccount) 310 for address, account := range genesis.Alloc { 311 spec.Accounts[address] = &parityChainSpecAccount{ 312 Balance: (*hexutil.Big)(account.Balance), 313 Nonce: account.Nonce, 314 } 315 } 316 spec.Accounts[common.BytesToAddress([]byte{1})].Builtin = &parityChainSpecBuiltin{ 317 Name: "ecrecover", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}}, 318 } 319 spec.Accounts[common.BytesToAddress([]byte{2})].Builtin = &parityChainSpecBuiltin{ 320 Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}}, 321 } 322 spec.Accounts[common.BytesToAddress([]byte{3})].Builtin = &parityChainSpecBuiltin{ 323 Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}}, 324 } 325 spec.Accounts[common.BytesToAddress([]byte{4})].Builtin = &parityChainSpecBuiltin{ 326 Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}}, 327 } 328 if genesis.Config.ByzantiumBlock != nil { 329 spec.Accounts[common.BytesToAddress([]byte{5})].Builtin = &parityChainSpecBuiltin{ 330 Name: "modexp", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{ModExp: &parityChainSpecModExpPricing{Divisor: 20}}, 331 } 332 spec.Accounts[common.BytesToAddress([]byte{6})].Builtin = &parityChainSpecBuiltin{ 333 Name: "alt_bn128_add", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 500}}, 334 } 335 spec.Accounts[common.BytesToAddress([]byte{7})].Builtin = &parityChainSpecBuiltin{ 336 Name: "alt_bn128_mul", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 40000}}, 337 } 338 spec.Accounts[common.BytesToAddress([]byte{8})].Builtin = &parityChainSpecBuiltin{ 339 Name: "alt_bn128_pairing", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{AltBnPairing: &parityChainSpecAltBnPairingPricing{Base: 100000, Pair: 80000}}, 340 } 341 } 342 return spec, nil 343 } 344 345 // pyEthereumGenesisSpec represents the genesis specification format used by the 346 // Python Ethereum implementation. 347 type pyEthereumGenesisSpec struct { 348 Nonce hexutil.Bytes `json:"nonce"` 349 Timestamp hexutil.Uint64 `json:"timestamp"` 350 ExtraData hexutil.Bytes `json:"extraData"` 351 GasLimit hexutil.Uint64 `json:"gasLimit"` 352 Difficulty *hexutil.Big `json:"difficulty"` 353 Mixhash common.Hash `json:"mixhash"` 354 Coinbase common.Address `json:"coinbase"` 355 Alloc core.GenesisAlloc `json:"alloc"` 356 ParentHash common.Hash `json:"parentHash"` 357 } 358 359 // newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific 360 // chain specification format. 361 func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) { 362 // Only ethash is currently supported between go-ethereum and pyethereum 363 if genesis.Config.Ethash == nil { 364 return nil, errors.New("unsupported consensus engine") 365 } 366 spec := &pyEthereumGenesisSpec{ 367 Timestamp: (hexutil.Uint64)(genesis.Timestamp), 368 ExtraData: genesis.ExtraData, 369 GasLimit: (hexutil.Uint64)(genesis.GasLimit), 370 Difficulty: (*hexutil.Big)(genesis.Difficulty), 371 Mixhash: genesis.Mixhash, 372 Coinbase: genesis.Coinbase, 373 Alloc: genesis.Alloc, 374 ParentHash: genesis.ParentHash, 375 } 376 spec.Nonce = (hexutil.Bytes)(make([]byte, 8)) 377 binary.LittleEndian.PutUint64(spec.Nonce[:], genesis.Nonce) 378 379 return spec, nil 380 }