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