gitee.com/liu-zhao234568/cntest@v1.0.0/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 "errors" 21 "math" 22 "math/big" 23 "strings" 24 25 "gitee.com/liu-zhao234568/cntest/common" 26 "gitee.com/liu-zhao234568/cntest/common/hexutil" 27 math2 "gitee.com/liu-zhao234568/cntest/common/math" 28 "gitee.com/liu-zhao234568/cntest/consensus/ethash" 29 "gitee.com/liu-zhao234568/cntest/core" 30 "gitee.com/liu-zhao234568/cntest/core/types" 31 "gitee.com/liu-zhao234568/cntest/params" 32 ) 33 34 // alethGenesisSpec represents the genesis specification format used by the 35 // C++ Ethereum implementation. 36 type alethGenesisSpec struct { 37 SealEngine string `json:"sealEngine"` 38 Params struct { 39 AccountStartNonce math2.HexOrDecimal64 `json:"accountStartNonce"` 40 MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"` 41 HomesteadForkBlock *hexutil.Big `json:"homesteadForkBlock,omitempty"` 42 DaoHardforkBlock math2.HexOrDecimal64 `json:"daoHardforkBlock"` 43 EIP150ForkBlock *hexutil.Big `json:"EIP150ForkBlock,omitempty"` 44 EIP158ForkBlock *hexutil.Big `json:"EIP158ForkBlock,omitempty"` 45 ByzantiumForkBlock *hexutil.Big `json:"byzantiumForkBlock,omitempty"` 46 ConstantinopleForkBlock *hexutil.Big `json:"constantinopleForkBlock,omitempty"` 47 ConstantinopleFixForkBlock *hexutil.Big `json:"constantinopleFixForkBlock,omitempty"` 48 IstanbulForkBlock *hexutil.Big `json:"istanbulForkBlock,omitempty"` 49 MinGasLimit hexutil.Uint64 `json:"minGasLimit"` 50 MaxGasLimit hexutil.Uint64 `json:"maxGasLimit"` 51 TieBreakingGas bool `json:"tieBreakingGas"` 52 GasLimitBoundDivisor math2.HexOrDecimal64 `json:"gasLimitBoundDivisor"` 53 MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"` 54 DifficultyBoundDivisor *math2.HexOrDecimal256 `json:"difficultyBoundDivisor"` 55 DurationLimit *math2.HexOrDecimal256 `json:"durationLimit"` 56 BlockReward *hexutil.Big `json:"blockReward"` 57 NetworkID hexutil.Uint64 `json:"networkID"` 58 ChainID hexutil.Uint64 `json:"chainID"` 59 AllowFutureBlocks bool `json:"allowFutureBlocks"` 60 } `json:"params"` 61 62 Genesis struct { 63 Nonce types.BlockNonce `json:"nonce"` 64 Difficulty *hexutil.Big `json:"difficulty"` 65 MixHash common.Hash `json:"mixHash"` 66 Author common.Address `json:"author"` 67 Timestamp hexutil.Uint64 `json:"timestamp"` 68 ParentHash common.Hash `json:"parentHash"` 69 ExtraData hexutil.Bytes `json:"extraData"` 70 GasLimit hexutil.Uint64 `json:"gasLimit"` 71 } `json:"genesis"` 72 73 Accounts map[common.UnprefixedAddress]*alethGenesisSpecAccount `json:"accounts"` 74 } 75 76 // alethGenesisSpecAccount is the prefunded genesis account and/or precompiled 77 // contract definition. 78 type alethGenesisSpecAccount struct { 79 Balance *math2.HexOrDecimal256 `json:"balance,omitempty"` 80 Nonce uint64 `json:"nonce,omitempty"` 81 Precompiled *alethGenesisSpecBuiltin `json:"precompiled,omitempty"` 82 } 83 84 // alethGenesisSpecBuiltin is the precompiled contract definition. 85 type alethGenesisSpecBuiltin struct { 86 Name string `json:"name,omitempty"` 87 StartingBlock *hexutil.Big `json:"startingBlock,omitempty"` 88 Linear *alethGenesisSpecLinearPricing `json:"linear,omitempty"` 89 } 90 91 type alethGenesisSpecLinearPricing struct { 92 Base uint64 `json:"base"` 93 Word uint64 `json:"word"` 94 } 95 96 // newAlethGenesisSpec converts a go-ethereum genesis block into a Aleth-specific 97 // chain specification format. 98 func newAlethGenesisSpec(network string, genesis *core.Genesis) (*alethGenesisSpec, error) { 99 // Only ethash is currently supported between go-ethereum and aleth 100 if genesis.Config.Ethash == nil { 101 return nil, errors.New("unsupported consensus engine") 102 } 103 // Reconstruct the chain spec in Aleth format 104 spec := &alethGenesisSpec{ 105 SealEngine: "Ethash", 106 } 107 // Some defaults 108 spec.Params.AccountStartNonce = 0 109 spec.Params.TieBreakingGas = false 110 spec.Params.AllowFutureBlocks = false 111 112 // Dao hardfork block is a special one. The fork block is listed as 0 in the 113 // config but aleth will sync with ETC clients up until the actual dao hard 114 // fork block. 115 spec.Params.DaoHardforkBlock = 0 116 117 if num := genesis.Config.HomesteadBlock; num != nil { 118 spec.Params.HomesteadForkBlock = (*hexutil.Big)(num) 119 } 120 if num := genesis.Config.EIP150Block; num != nil { 121 spec.Params.EIP150ForkBlock = (*hexutil.Big)(num) 122 } 123 if num := genesis.Config.EIP158Block; num != nil { 124 spec.Params.EIP158ForkBlock = (*hexutil.Big)(num) 125 } 126 if num := genesis.Config.ByzantiumBlock; num != nil { 127 spec.Params.ByzantiumForkBlock = (*hexutil.Big)(num) 128 } 129 if num := genesis.Config.ConstantinopleBlock; num != nil { 130 spec.Params.ConstantinopleForkBlock = (*hexutil.Big)(num) 131 } 132 if num := genesis.Config.PetersburgBlock; num != nil { 133 spec.Params.ConstantinopleFixForkBlock = (*hexutil.Big)(num) 134 } 135 if num := genesis.Config.IstanbulBlock; num != nil { 136 spec.Params.IstanbulForkBlock = (*hexutil.Big)(num) 137 } 138 spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 139 spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 140 spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize) 141 spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit) 142 spec.Params.MaxGasLimit = (hexutil.Uint64)(math.MaxInt64) 143 spec.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty) 144 spec.Params.DifficultyBoundDivisor = (*math2.HexOrDecimal256)(params.DifficultyBoundDivisor) 145 spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor) 146 spec.Params.DurationLimit = (*math2.HexOrDecimal256)(params.DurationLimit) 147 spec.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward) 148 149 spec.Genesis.Nonce = types.EncodeNonce(genesis.Nonce) 150 spec.Genesis.MixHash = genesis.Mixhash 151 spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty) 152 spec.Genesis.Author = genesis.Coinbase 153 spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp) 154 spec.Genesis.ParentHash = genesis.ParentHash 155 spec.Genesis.ExtraData = genesis.ExtraData 156 spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit) 157 158 for address, account := range genesis.Alloc { 159 spec.setAccount(address, account) 160 } 161 162 spec.setPrecompile(1, &alethGenesisSpecBuiltin{Name: "ecrecover", 163 Linear: &alethGenesisSpecLinearPricing{Base: 3000}}) 164 spec.setPrecompile(2, &alethGenesisSpecBuiltin{Name: "sha256", 165 Linear: &alethGenesisSpecLinearPricing{Base: 60, Word: 12}}) 166 spec.setPrecompile(3, &alethGenesisSpecBuiltin{Name: "ripemd160", 167 Linear: &alethGenesisSpecLinearPricing{Base: 600, Word: 120}}) 168 spec.setPrecompile(4, &alethGenesisSpecBuiltin{Name: "identity", 169 Linear: &alethGenesisSpecLinearPricing{Base: 15, Word: 3}}) 170 if genesis.Config.ByzantiumBlock != nil { 171 spec.setPrecompile(5, &alethGenesisSpecBuiltin{Name: "modexp", 172 StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock)}) 173 spec.setPrecompile(6, &alethGenesisSpecBuiltin{Name: "alt_bn128_G1_add", 174 StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 175 Linear: &alethGenesisSpecLinearPricing{Base: 500}}) 176 spec.setPrecompile(7, &alethGenesisSpecBuiltin{Name: "alt_bn128_G1_mul", 177 StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 178 Linear: &alethGenesisSpecLinearPricing{Base: 40000}}) 179 spec.setPrecompile(8, &alethGenesisSpecBuiltin{Name: "alt_bn128_pairing_product", 180 StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock)}) 181 } 182 if genesis.Config.IstanbulBlock != nil { 183 if genesis.Config.ByzantiumBlock == nil { 184 return nil, errors.New("invalid genesis, istanbul fork is enabled while byzantium is not") 185 } 186 spec.setPrecompile(6, &alethGenesisSpecBuiltin{ 187 Name: "alt_bn128_G1_add", 188 StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 189 }) // Aleth hardcoded the gas policy 190 spec.setPrecompile(7, &alethGenesisSpecBuiltin{ 191 Name: "alt_bn128_G1_mul", 192 StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 193 }) // Aleth hardcoded the gas policy 194 spec.setPrecompile(9, &alethGenesisSpecBuiltin{ 195 Name: "blake2_compression", 196 StartingBlock: (*hexutil.Big)(genesis.Config.IstanbulBlock), 197 }) 198 } 199 return spec, nil 200 } 201 202 func (spec *alethGenesisSpec) setPrecompile(address byte, data *alethGenesisSpecBuiltin) { 203 if spec.Accounts == nil { 204 spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount) 205 } 206 addr := common.UnprefixedAddress(common.BytesToAddress([]byte{address})) 207 if _, exist := spec.Accounts[addr]; !exist { 208 spec.Accounts[addr] = &alethGenesisSpecAccount{} 209 } 210 spec.Accounts[addr].Precompiled = data 211 } 212 213 func (spec *alethGenesisSpec) setAccount(address common.Address, account core.GenesisAccount) { 214 if spec.Accounts == nil { 215 spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount) 216 } 217 218 a, exist := spec.Accounts[common.UnprefixedAddress(address)] 219 if !exist { 220 a = &alethGenesisSpecAccount{} 221 spec.Accounts[common.UnprefixedAddress(address)] = a 222 } 223 a.Balance = (*math2.HexOrDecimal256)(account.Balance) 224 a.Nonce = account.Nonce 225 226 } 227 228 // parityChainSpec is the chain specification format used by Parity. 229 type parityChainSpec struct { 230 Name string `json:"name"` 231 Datadir string `json:"dataDir"` 232 Engine struct { 233 Ethash struct { 234 Params struct { 235 MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"` 236 DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"` 237 DurationLimit *hexutil.Big `json:"durationLimit"` 238 BlockReward map[string]string `json:"blockReward"` 239 DifficultyBombDelays map[string]string `json:"difficultyBombDelays"` 240 HomesteadTransition hexutil.Uint64 `json:"homesteadTransition"` 241 EIP100bTransition hexutil.Uint64 `json:"eip100bTransition"` 242 } `json:"params"` 243 } `json:"Ethash"` 244 } `json:"engine"` 245 246 Params struct { 247 AccountStartNonce hexutil.Uint64 `json:"accountStartNonce"` 248 MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"` 249 MinGasLimit hexutil.Uint64 `json:"minGasLimit"` 250 GasLimitBoundDivisor math2.HexOrDecimal64 `json:"gasLimitBoundDivisor"` 251 NetworkID hexutil.Uint64 `json:"networkID"` 252 ChainID hexutil.Uint64 `json:"chainID"` 253 MaxCodeSize hexutil.Uint64 `json:"maxCodeSize"` 254 MaxCodeSizeTransition hexutil.Uint64 `json:"maxCodeSizeTransition"` 255 EIP98Transition hexutil.Uint64 `json:"eip98Transition"` 256 EIP150Transition hexutil.Uint64 `json:"eip150Transition"` 257 EIP160Transition hexutil.Uint64 `json:"eip160Transition"` 258 EIP161abcTransition hexutil.Uint64 `json:"eip161abcTransition"` 259 EIP161dTransition hexutil.Uint64 `json:"eip161dTransition"` 260 EIP155Transition hexutil.Uint64 `json:"eip155Transition"` 261 EIP140Transition hexutil.Uint64 `json:"eip140Transition"` 262 EIP211Transition hexutil.Uint64 `json:"eip211Transition"` 263 EIP214Transition hexutil.Uint64 `json:"eip214Transition"` 264 EIP658Transition hexutil.Uint64 `json:"eip658Transition"` 265 EIP145Transition hexutil.Uint64 `json:"eip145Transition"` 266 EIP1014Transition hexutil.Uint64 `json:"eip1014Transition"` 267 EIP1052Transition hexutil.Uint64 `json:"eip1052Transition"` 268 EIP1283Transition hexutil.Uint64 `json:"eip1283Transition"` 269 EIP1283DisableTransition hexutil.Uint64 `json:"eip1283DisableTransition"` 270 EIP1283ReenableTransition hexutil.Uint64 `json:"eip1283ReenableTransition"` 271 EIP1344Transition hexutil.Uint64 `json:"eip1344Transition"` 272 EIP1884Transition hexutil.Uint64 `json:"eip1884Transition"` 273 EIP2028Transition hexutil.Uint64 `json:"eip2028Transition"` 274 } `json:"params"` 275 276 Genesis struct { 277 Seal struct { 278 Ethereum struct { 279 Nonce types.BlockNonce `json:"nonce"` 280 MixHash hexutil.Bytes `json:"mixHash"` 281 } `json:"ethereum"` 282 } `json:"seal"` 283 284 Difficulty *hexutil.Big `json:"difficulty"` 285 Author common.Address `json:"author"` 286 Timestamp hexutil.Uint64 `json:"timestamp"` 287 ParentHash common.Hash `json:"parentHash"` 288 ExtraData hexutil.Bytes `json:"extraData"` 289 GasLimit hexutil.Uint64 `json:"gasLimit"` 290 } `json:"genesis"` 291 292 Nodes []string `json:"nodes"` 293 Accounts map[common.UnprefixedAddress]*parityChainSpecAccount `json:"accounts"` 294 } 295 296 // parityChainSpecAccount is the prefunded genesis account and/or precompiled 297 // contract definition. 298 type parityChainSpecAccount struct { 299 Balance math2.HexOrDecimal256 `json:"balance"` 300 Nonce math2.HexOrDecimal64 `json:"nonce,omitempty"` 301 Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"` 302 } 303 304 // parityChainSpecBuiltin is the precompiled contract definition. 305 type parityChainSpecBuiltin struct { 306 Name string `json:"name"` // Each builtin should has it own name 307 Pricing interface{} `json:"pricing"` // Each builtin should has it own price strategy 308 ActivateAt *hexutil.Big `json:"activate_at,omitempty"` // ActivateAt can't be omitted if empty, default means no fork 309 } 310 311 // parityChainSpecPricing represents the different pricing models that builtin 312 // contracts might advertise using. 313 type parityChainSpecPricing struct { 314 Linear *parityChainSpecLinearPricing `json:"linear,omitempty"` 315 ModExp *parityChainSpecModExpPricing `json:"modexp,omitempty"` 316 317 // Before the https://github.com/paritytech/parity-ethereum/pull/11039, 318 // Parity uses this format to config bn pairing price policy. 319 AltBnPairing *parityChainSepcAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"` 320 321 // Blake2F is the price per round of Blake2 compression 322 Blake2F *parityChainSpecBlakePricing `json:"blake2_f,omitempty"` 323 } 324 325 type parityChainSpecLinearPricing struct { 326 Base uint64 `json:"base"` 327 Word uint64 `json:"word"` 328 } 329 330 type parityChainSpecModExpPricing struct { 331 Divisor uint64 `json:"divisor"` 332 } 333 334 // parityChainSpecAltBnConstOperationPricing defines the price 335 // policy for bn const operation(used after istanbul) 336 type parityChainSpecAltBnConstOperationPricing struct { 337 Price uint64 `json:"price"` 338 } 339 340 // parityChainSepcAltBnPairingPricing defines the price policy 341 // for bn pairing. 342 type parityChainSepcAltBnPairingPricing struct { 343 Base uint64 `json:"base"` 344 Pair uint64 `json:"pair"` 345 } 346 347 // parityChainSpecBlakePricing defines the price policy for blake2 f 348 // compression. 349 type parityChainSpecBlakePricing struct { 350 GasPerRound uint64 `json:"gas_per_round"` 351 } 352 353 type parityChainSpecAlternativePrice struct { 354 AltBnConstOperationPrice *parityChainSpecAltBnConstOperationPricing `json:"alt_bn128_const_operations,omitempty"` 355 AltBnPairingPrice *parityChainSepcAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"` 356 } 357 358 // parityChainSpecVersionedPricing represents a single version price policy. 359 type parityChainSpecVersionedPricing struct { 360 Price *parityChainSpecAlternativePrice `json:"price,omitempty"` 361 Info string `json:"info,omitempty"` 362 } 363 364 // newParityChainSpec converts a go-ethereum genesis block into a Parity specific 365 // chain specification format. 366 func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) { 367 // Only ethash is currently supported between go-ethereum and Parity 368 if genesis.Config.Ethash == nil { 369 return nil, errors.New("unsupported consensus engine") 370 } 371 // Reconstruct the chain spec in Parity's format 372 spec := &parityChainSpec{ 373 Name: network, 374 Nodes: bootnodes, 375 Datadir: strings.ToLower(network), 376 } 377 spec.Engine.Ethash.Params.BlockReward = make(map[string]string) 378 spec.Engine.Ethash.Params.DifficultyBombDelays = make(map[string]string) 379 // Frontier 380 spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty) 381 spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor) 382 spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit) 383 spec.Engine.Ethash.Params.BlockReward["0x0"] = hexutil.EncodeBig(ethash.FrontierBlockReward) 384 385 // Homestead 386 spec.Engine.Ethash.Params.HomesteadTransition = hexutil.Uint64(genesis.Config.HomesteadBlock.Uint64()) 387 388 // Tangerine Whistle : 150 389 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-608.md 390 spec.Params.EIP150Transition = hexutil.Uint64(genesis.Config.EIP150Block.Uint64()) 391 392 // Spurious Dragon: 155, 160, 161, 170 393 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-607.md 394 spec.Params.EIP155Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64()) 395 spec.Params.EIP160Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64()) 396 spec.Params.EIP161abcTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64()) 397 spec.Params.EIP161dTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64()) 398 399 // Byzantium 400 if num := genesis.Config.ByzantiumBlock; num != nil { 401 spec.setByzantium(num) 402 } 403 // Constantinople 404 if num := genesis.Config.ConstantinopleBlock; num != nil { 405 spec.setConstantinople(num) 406 } 407 // ConstantinopleFix (remove eip-1283) 408 if num := genesis.Config.PetersburgBlock; num != nil { 409 spec.setConstantinopleFix(num) 410 } 411 // Istanbul 412 if num := genesis.Config.IstanbulBlock; num != nil { 413 spec.setIstanbul(num) 414 } 415 spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize) 416 spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit) 417 spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor) 418 spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 419 spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 420 spec.Params.MaxCodeSize = params.MaxCodeSize 421 // geth has it set from zero 422 spec.Params.MaxCodeSizeTransition = 0 423 424 // Disable this one 425 spec.Params.EIP98Transition = math.MaxInt64 426 427 spec.Genesis.Seal.Ethereum.Nonce = types.EncodeNonce(genesis.Nonce) 428 spec.Genesis.Seal.Ethereum.MixHash = genesis.Mixhash[:] 429 spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty) 430 spec.Genesis.Author = genesis.Coinbase 431 spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp) 432 spec.Genesis.ParentHash = genesis.ParentHash 433 spec.Genesis.ExtraData = genesis.ExtraData 434 spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit) 435 436 spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount) 437 for address, account := range genesis.Alloc { 438 bal := math2.HexOrDecimal256(*account.Balance) 439 440 spec.Accounts[common.UnprefixedAddress(address)] = &parityChainSpecAccount{ 441 Balance: bal, 442 Nonce: math2.HexOrDecimal64(account.Nonce), 443 } 444 } 445 spec.setPrecompile(1, &parityChainSpecBuiltin{Name: "ecrecover", 446 Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}}}) 447 448 spec.setPrecompile(2, &parityChainSpecBuiltin{ 449 Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}}, 450 }) 451 spec.setPrecompile(3, &parityChainSpecBuiltin{ 452 Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}}, 453 }) 454 spec.setPrecompile(4, &parityChainSpecBuiltin{ 455 Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}}, 456 }) 457 if genesis.Config.ByzantiumBlock != nil { 458 spec.setPrecompile(5, &parityChainSpecBuiltin{ 459 Name: "modexp", 460 ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 461 Pricing: &parityChainSpecPricing{ 462 ModExp: &parityChainSpecModExpPricing{Divisor: 20}, 463 }, 464 }) 465 spec.setPrecompile(6, &parityChainSpecBuiltin{ 466 Name: "alt_bn128_add", 467 ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 468 Pricing: &parityChainSpecPricing{ 469 Linear: &parityChainSpecLinearPricing{Base: 500, Word: 0}, 470 }, 471 }) 472 spec.setPrecompile(7, &parityChainSpecBuiltin{ 473 Name: "alt_bn128_mul", 474 ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 475 Pricing: &parityChainSpecPricing{ 476 Linear: &parityChainSpecLinearPricing{Base: 40000, Word: 0}, 477 }, 478 }) 479 spec.setPrecompile(8, &parityChainSpecBuiltin{ 480 Name: "alt_bn128_pairing", 481 ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 482 Pricing: &parityChainSpecPricing{ 483 AltBnPairing: &parityChainSepcAltBnPairingPricing{Base: 100000, Pair: 80000}, 484 }, 485 }) 486 } 487 if genesis.Config.IstanbulBlock != nil { 488 if genesis.Config.ByzantiumBlock == nil { 489 return nil, errors.New("invalid genesis, istanbul fork is enabled while byzantium is not") 490 } 491 spec.setPrecompile(6, &parityChainSpecBuiltin{ 492 Name: "alt_bn128_add", 493 ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 494 Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{ 495 (*hexutil.Big)(big.NewInt(0)): { 496 Price: &parityChainSpecAlternativePrice{ 497 AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 500}, 498 }, 499 }, 500 (*hexutil.Big)(genesis.Config.IstanbulBlock): { 501 Price: &parityChainSpecAlternativePrice{ 502 AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 150}, 503 }, 504 }, 505 }, 506 }) 507 spec.setPrecompile(7, &parityChainSpecBuiltin{ 508 Name: "alt_bn128_mul", 509 ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 510 Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{ 511 (*hexutil.Big)(big.NewInt(0)): { 512 Price: &parityChainSpecAlternativePrice{ 513 AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 40000}, 514 }, 515 }, 516 (*hexutil.Big)(genesis.Config.IstanbulBlock): { 517 Price: &parityChainSpecAlternativePrice{ 518 AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 6000}, 519 }, 520 }, 521 }, 522 }) 523 spec.setPrecompile(8, &parityChainSpecBuiltin{ 524 Name: "alt_bn128_pairing", 525 ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), 526 Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{ 527 (*hexutil.Big)(big.NewInt(0)): { 528 Price: &parityChainSpecAlternativePrice{ 529 AltBnPairingPrice: &parityChainSepcAltBnPairingPricing{Base: 100000, Pair: 80000}, 530 }, 531 }, 532 (*hexutil.Big)(genesis.Config.IstanbulBlock): { 533 Price: &parityChainSpecAlternativePrice{ 534 AltBnPairingPrice: &parityChainSepcAltBnPairingPricing{Base: 45000, Pair: 34000}, 535 }, 536 }, 537 }, 538 }) 539 spec.setPrecompile(9, &parityChainSpecBuiltin{ 540 Name: "blake2_f", 541 ActivateAt: (*hexutil.Big)(genesis.Config.IstanbulBlock), 542 Pricing: &parityChainSpecPricing{ 543 Blake2F: &parityChainSpecBlakePricing{GasPerRound: 1}, 544 }, 545 }) 546 } 547 return spec, nil 548 } 549 550 func (spec *parityChainSpec) setPrecompile(address byte, data *parityChainSpecBuiltin) { 551 if spec.Accounts == nil { 552 spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount) 553 } 554 a := common.UnprefixedAddress(common.BytesToAddress([]byte{address})) 555 if _, exist := spec.Accounts[a]; !exist { 556 spec.Accounts[a] = &parityChainSpecAccount{} 557 } 558 spec.Accounts[a].Builtin = data 559 } 560 561 func (spec *parityChainSpec) setByzantium(num *big.Int) { 562 spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ByzantiumBlockReward) 563 spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(3000000) 564 n := hexutil.Uint64(num.Uint64()) 565 spec.Engine.Ethash.Params.EIP100bTransition = n 566 spec.Params.EIP140Transition = n 567 spec.Params.EIP211Transition = n 568 spec.Params.EIP214Transition = n 569 spec.Params.EIP658Transition = n 570 } 571 572 func (spec *parityChainSpec) setConstantinople(num *big.Int) { 573 spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ConstantinopleBlockReward) 574 spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(2000000) 575 n := hexutil.Uint64(num.Uint64()) 576 spec.Params.EIP145Transition = n 577 spec.Params.EIP1014Transition = n 578 spec.Params.EIP1052Transition = n 579 spec.Params.EIP1283Transition = n 580 } 581 582 func (spec *parityChainSpec) setConstantinopleFix(num *big.Int) { 583 spec.Params.EIP1283DisableTransition = hexutil.Uint64(num.Uint64()) 584 } 585 586 func (spec *parityChainSpec) setIstanbul(num *big.Int) { 587 spec.Params.EIP1344Transition = hexutil.Uint64(num.Uint64()) 588 spec.Params.EIP1884Transition = hexutil.Uint64(num.Uint64()) 589 spec.Params.EIP2028Transition = hexutil.Uint64(num.Uint64()) 590 spec.Params.EIP1283ReenableTransition = hexutil.Uint64(num.Uint64()) 591 } 592 593 // pyEthereumGenesisSpec represents the genesis specification format used by the 594 // Python Ethereum implementation. 595 type pyEthereumGenesisSpec struct { 596 Nonce types.BlockNonce `json:"nonce"` 597 Timestamp hexutil.Uint64 `json:"timestamp"` 598 ExtraData hexutil.Bytes `json:"extraData"` 599 GasLimit hexutil.Uint64 `json:"gasLimit"` 600 Difficulty *hexutil.Big `json:"difficulty"` 601 Mixhash common.Hash `json:"mixhash"` 602 Coinbase common.Address `json:"coinbase"` 603 Alloc core.GenesisAlloc `json:"alloc"` 604 ParentHash common.Hash `json:"parentHash"` 605 } 606 607 // newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific 608 // chain specification format. 609 func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) { 610 // Only ethash is currently supported between go-ethereum and pyethereum 611 if genesis.Config.Ethash == nil { 612 return nil, errors.New("unsupported consensus engine") 613 } 614 spec := &pyEthereumGenesisSpec{ 615 Nonce: types.EncodeNonce(genesis.Nonce), 616 Timestamp: (hexutil.Uint64)(genesis.Timestamp), 617 ExtraData: genesis.ExtraData, 618 GasLimit: (hexutil.Uint64)(genesis.GasLimit), 619 Difficulty: (*hexutil.Big)(genesis.Difficulty), 620 Mixhash: genesis.Mixhash, 621 Coinbase: genesis.Coinbase, 622 Alloc: genesis.Alloc, 623 ParentHash: genesis.ParentHash, 624 } 625 return spec, nil 626 }