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