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