github.com/mister-meeseeks/go-ethereum@v1.9.7/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  	"math/big"
    24  	"strings"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/common/hexutil"
    28  	math2 "github.com/ethereum/go-ethereum/common/math"
    29  	"github.com/ethereum/go-ethereum/consensus/ethash"
    30  	"github.com/ethereum/go-ethereum/core"
    31  	"github.com/ethereum/go-ethereum/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      hexutil.Bytes  `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 = (hexutil.Bytes)(make([]byte, 8))
   150  	binary.LittleEndian.PutUint64(spec.Genesis.Nonce[:], genesis.Nonce)
   151  
   152  	spec.Genesis.MixHash = genesis.Mixhash
   153  	spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
   154  	spec.Genesis.Author = genesis.Coinbase
   155  	spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
   156  	spec.Genesis.ParentHash = genesis.ParentHash
   157  	spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
   158  	spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
   159  
   160  	for address, account := range genesis.Alloc {
   161  		spec.setAccount(address, account)
   162  	}
   163  
   164  	spec.setPrecompile(1, &alethGenesisSpecBuiltin{Name: "ecrecover",
   165  		Linear: &alethGenesisSpecLinearPricing{Base: 3000}})
   166  	spec.setPrecompile(2, &alethGenesisSpecBuiltin{Name: "sha256",
   167  		Linear: &alethGenesisSpecLinearPricing{Base: 60, Word: 12}})
   168  	spec.setPrecompile(3, &alethGenesisSpecBuiltin{Name: "ripemd160",
   169  		Linear: &alethGenesisSpecLinearPricing{Base: 600, Word: 120}})
   170  	spec.setPrecompile(4, &alethGenesisSpecBuiltin{Name: "identity",
   171  		Linear: &alethGenesisSpecLinearPricing{Base: 15, Word: 3}})
   172  	if genesis.Config.ByzantiumBlock != nil {
   173  		spec.setPrecompile(5, &alethGenesisSpecBuiltin{Name: "modexp",
   174  			StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock)})
   175  		spec.setPrecompile(6, &alethGenesisSpecBuiltin{Name: "alt_bn128_G1_add",
   176  			StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   177  			Linear:        &alethGenesisSpecLinearPricing{Base: 500}})
   178  		spec.setPrecompile(7, &alethGenesisSpecBuiltin{Name: "alt_bn128_G1_mul",
   179  			StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   180  			Linear:        &alethGenesisSpecLinearPricing{Base: 40000}})
   181  		spec.setPrecompile(8, &alethGenesisSpecBuiltin{Name: "alt_bn128_pairing_product",
   182  			StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock)})
   183  	}
   184  	if genesis.Config.IstanbulBlock != nil {
   185  		if genesis.Config.ByzantiumBlock == nil {
   186  			return nil, errors.New("invalid genesis, istanbul fork is enabled while byzantium is not")
   187  		}
   188  		spec.setPrecompile(6, &alethGenesisSpecBuiltin{
   189  			Name:          "alt_bn128_G1_add",
   190  			StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   191  		}) // Aleth hardcoded the gas policy
   192  		spec.setPrecompile(7, &alethGenesisSpecBuiltin{
   193  			Name:          "alt_bn128_G1_mul",
   194  			StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   195  		}) // Aleth hardcoded the gas policy
   196  		spec.setPrecompile(9, &alethGenesisSpecBuiltin{
   197  			Name:          "blake2_compression",
   198  			StartingBlock: (*hexutil.Big)(genesis.Config.IstanbulBlock),
   199  		})
   200  	}
   201  	return spec, nil
   202  }
   203  
   204  func (spec *alethGenesisSpec) setPrecompile(address byte, data *alethGenesisSpecBuiltin) {
   205  	if spec.Accounts == nil {
   206  		spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount)
   207  	}
   208  	addr := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
   209  	if _, exist := spec.Accounts[addr]; !exist {
   210  		spec.Accounts[addr] = &alethGenesisSpecAccount{}
   211  	}
   212  	spec.Accounts[addr].Precompiled = data
   213  }
   214  
   215  func (spec *alethGenesisSpec) setAccount(address common.Address, account core.GenesisAccount) {
   216  	if spec.Accounts == nil {
   217  		spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount)
   218  	}
   219  
   220  	a, exist := spec.Accounts[common.UnprefixedAddress(address)]
   221  	if !exist {
   222  		a = &alethGenesisSpecAccount{}
   223  		spec.Accounts[common.UnprefixedAddress(address)] = a
   224  	}
   225  	a.Balance = (*math2.HexOrDecimal256)(account.Balance)
   226  	a.Nonce = account.Nonce
   227  
   228  }
   229  
   230  // parityChainSpec is the chain specification format used by Parity.
   231  type parityChainSpec struct {
   232  	Name    string `json:"name"`
   233  	Datadir string `json:"dataDir"`
   234  	Engine  struct {
   235  		Ethash struct {
   236  			Params struct {
   237  				MinimumDifficulty      *hexutil.Big      `json:"minimumDifficulty"`
   238  				DifficultyBoundDivisor *hexutil.Big      `json:"difficultyBoundDivisor"`
   239  				DurationLimit          *hexutil.Big      `json:"durationLimit"`
   240  				BlockReward            map[string]string `json:"blockReward"`
   241  				DifficultyBombDelays   map[string]string `json:"difficultyBombDelays"`
   242  				HomesteadTransition    hexutil.Uint64    `json:"homesteadTransition"`
   243  				EIP100bTransition      hexutil.Uint64    `json:"eip100bTransition"`
   244  			} `json:"params"`
   245  		} `json:"Ethash"`
   246  	} `json:"engine"`
   247  
   248  	Params struct {
   249  		AccountStartNonce         hexutil.Uint64       `json:"accountStartNonce"`
   250  		MaximumExtraDataSize      hexutil.Uint64       `json:"maximumExtraDataSize"`
   251  		MinGasLimit               hexutil.Uint64       `json:"minGasLimit"`
   252  		GasLimitBoundDivisor      math2.HexOrDecimal64 `json:"gasLimitBoundDivisor"`
   253  		NetworkID                 hexutil.Uint64       `json:"networkID"`
   254  		ChainID                   hexutil.Uint64       `json:"chainID"`
   255  		MaxCodeSize               hexutil.Uint64       `json:"maxCodeSize"`
   256  		MaxCodeSizeTransition     hexutil.Uint64       `json:"maxCodeSizeTransition"`
   257  		EIP98Transition           hexutil.Uint64       `json:"eip98Transition"`
   258  		EIP150Transition          hexutil.Uint64       `json:"eip150Transition"`
   259  		EIP160Transition          hexutil.Uint64       `json:"eip160Transition"`
   260  		EIP161abcTransition       hexutil.Uint64       `json:"eip161abcTransition"`
   261  		EIP161dTransition         hexutil.Uint64       `json:"eip161dTransition"`
   262  		EIP155Transition          hexutil.Uint64       `json:"eip155Transition"`
   263  		EIP140Transition          hexutil.Uint64       `json:"eip140Transition"`
   264  		EIP211Transition          hexutil.Uint64       `json:"eip211Transition"`
   265  		EIP214Transition          hexutil.Uint64       `json:"eip214Transition"`
   266  		EIP658Transition          hexutil.Uint64       `json:"eip658Transition"`
   267  		EIP145Transition          hexutil.Uint64       `json:"eip145Transition"`
   268  		EIP1014Transition         hexutil.Uint64       `json:"eip1014Transition"`
   269  		EIP1052Transition         hexutil.Uint64       `json:"eip1052Transition"`
   270  		EIP1283Transition         hexutil.Uint64       `json:"eip1283Transition"`
   271  		EIP1283DisableTransition  hexutil.Uint64       `json:"eip1283DisableTransition"`
   272  		EIP1283ReenableTransition hexutil.Uint64       `json:"eip1283ReenableTransition"`
   273  		EIP1344Transition         hexutil.Uint64       `json:"eip1344Transition"`
   274  		EIP1884Transition         hexutil.Uint64       `json:"eip1884Transition"`
   275  		EIP2028Transition         hexutil.Uint64       `json:"eip2028Transition"`
   276  	} `json:"params"`
   277  
   278  	Genesis struct {
   279  		Seal struct {
   280  			Ethereum struct {
   281  				Nonce   hexutil.Bytes `json:"nonce"`
   282  				MixHash hexutil.Bytes `json:"mixHash"`
   283  			} `json:"ethereum"`
   284  		} `json:"seal"`
   285  
   286  		Difficulty *hexutil.Big   `json:"difficulty"`
   287  		Author     common.Address `json:"author"`
   288  		Timestamp  hexutil.Uint64 `json:"timestamp"`
   289  		ParentHash common.Hash    `json:"parentHash"`
   290  		ExtraData  hexutil.Bytes  `json:"extraData"`
   291  		GasLimit   hexutil.Uint64 `json:"gasLimit"`
   292  	} `json:"genesis"`
   293  
   294  	Nodes    []string                                             `json:"nodes"`
   295  	Accounts map[common.UnprefixedAddress]*parityChainSpecAccount `json:"accounts"`
   296  }
   297  
   298  // parityChainSpecAccount is the prefunded genesis account and/or precompiled
   299  // contract definition.
   300  type parityChainSpecAccount struct {
   301  	Balance math2.HexOrDecimal256   `json:"balance"`
   302  	Nonce   math2.HexOrDecimal64    `json:"nonce,omitempty"`
   303  	Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"`
   304  }
   305  
   306  // parityChainSpecBuiltin is the precompiled contract definition.
   307  type parityChainSpecBuiltin struct {
   308  	Name              string                  `json:"name"`                         // Each builtin should has it own name
   309  	Pricing           *parityChainSpecPricing `json:"pricing"`                      // Each builtin should has it own price strategy
   310  	ActivateAt        *hexutil.Big            `json:"activate_at,omitempty"`        // ActivateAt can't be omitted if empty, default means no fork
   311  	EIP1108Transition *hexutil.Big            `json:"eip1108_transition,omitempty"` // EIP1108Transition can't be omitted if empty, default means no fork
   312  }
   313  
   314  // parityChainSpecPricing represents the different pricing models that builtin
   315  // contracts might advertise using.
   316  type parityChainSpecPricing struct {
   317  	Linear              *parityChainSpecLinearPricing              `json:"linear,omitempty"`
   318  	ModExp              *parityChainSpecModExpPricing              `json:"modexp,omitempty"`
   319  	AltBnPairing        *parityChainSpecAltBnPairingPricing        `json:"alt_bn128_pairing,omitempty"`
   320  	AltBnConstOperation *parityChainSpecAltBnConstOperationPricing `json:"alt_bn128_const_operations,omitempty"`
   321  
   322  	// Blake2F is the price per round of Blake2 compression
   323  	Blake2F *parityChainSpecBlakePricing `json:"blake2_f,omitempty"`
   324  }
   325  
   326  type parityChainSpecLinearPricing struct {
   327  	Base uint64 `json:"base"`
   328  	Word uint64 `json:"word"`
   329  }
   330  
   331  type parityChainSpecModExpPricing struct {
   332  	Divisor uint64 `json:"divisor"`
   333  }
   334  
   335  type parityChainSpecAltBnConstOperationPricing struct {
   336  	Price                  uint64 `json:"price"`
   337  	EIP1108TransitionPrice uint64 `json:"eip1108_transition_price,omitempty"` // Before Istanbul fork, this field is nil
   338  }
   339  
   340  type parityChainSpecAltBnPairingPricing struct {
   341  	Base                  uint64 `json:"base"`
   342  	Pair                  uint64 `json:"pair"`
   343  	EIP1108TransitionBase uint64 `json:"eip1108_transition_base,omitempty"` // Before Istanbul fork, this field is nil
   344  	EIP1108TransitionPair uint64 `json:"eip1108_transition_pair,omitempty"` // Before Istanbul fork, this field is nil
   345  }
   346  
   347  type parityChainSpecBlakePricing struct {
   348  	GasPerRound uint64 `json:"gas_per_round"`
   349  }
   350  
   351  // newParityChainSpec converts a go-ethereum genesis block into a Parity specific
   352  // chain specification format.
   353  func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) {
   354  	// Only ethash is currently supported between go-ethereum and Parity
   355  	if genesis.Config.Ethash == nil {
   356  		return nil, errors.New("unsupported consensus engine")
   357  	}
   358  	// Reconstruct the chain spec in Parity's format
   359  	spec := &parityChainSpec{
   360  		Name:    network,
   361  		Nodes:   bootnodes,
   362  		Datadir: strings.ToLower(network),
   363  	}
   364  	spec.Engine.Ethash.Params.BlockReward = make(map[string]string)
   365  	spec.Engine.Ethash.Params.DifficultyBombDelays = make(map[string]string)
   366  	// Frontier
   367  	spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
   368  	spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
   369  	spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
   370  	spec.Engine.Ethash.Params.BlockReward["0x0"] = hexutil.EncodeBig(ethash.FrontierBlockReward)
   371  
   372  	// Homestead
   373  	spec.Engine.Ethash.Params.HomesteadTransition = hexutil.Uint64(genesis.Config.HomesteadBlock.Uint64())
   374  
   375  	// Tangerine Whistle : 150
   376  	// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-608.md
   377  	spec.Params.EIP150Transition = hexutil.Uint64(genesis.Config.EIP150Block.Uint64())
   378  
   379  	// Spurious Dragon: 155, 160, 161, 170
   380  	// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-607.md
   381  	spec.Params.EIP155Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
   382  	spec.Params.EIP160Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
   383  	spec.Params.EIP161abcTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
   384  	spec.Params.EIP161dTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
   385  
   386  	// Byzantium
   387  	if num := genesis.Config.ByzantiumBlock; num != nil {
   388  		spec.setByzantium(num)
   389  	}
   390  	// Constantinople
   391  	if num := genesis.Config.ConstantinopleBlock; num != nil {
   392  		spec.setConstantinople(num)
   393  	}
   394  	// ConstantinopleFix (remove eip-1283)
   395  	if num := genesis.Config.PetersburgBlock; num != nil {
   396  		spec.setConstantinopleFix(num)
   397  	}
   398  	// Istanbul
   399  	if num := genesis.Config.IstanbulBlock; num != nil {
   400  		spec.setIstanbul(num)
   401  	}
   402  	spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
   403  	spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
   404  	spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor)
   405  	spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
   406  	spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
   407  	spec.Params.MaxCodeSize = params.MaxCodeSize
   408  	// geth has it set from zero
   409  	spec.Params.MaxCodeSizeTransition = 0
   410  
   411  	// Disable this one
   412  	spec.Params.EIP98Transition = math.MaxInt64
   413  
   414  	spec.Genesis.Seal.Ethereum.Nonce = (hexutil.Bytes)(make([]byte, 8))
   415  	binary.LittleEndian.PutUint64(spec.Genesis.Seal.Ethereum.Nonce[:], genesis.Nonce)
   416  
   417  	spec.Genesis.Seal.Ethereum.MixHash = (hexutil.Bytes)(genesis.Mixhash[:])
   418  	spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
   419  	spec.Genesis.Author = genesis.Coinbase
   420  	spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
   421  	spec.Genesis.ParentHash = genesis.ParentHash
   422  	spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
   423  	spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
   424  
   425  	spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
   426  	for address, account := range genesis.Alloc {
   427  		bal := math2.HexOrDecimal256(*account.Balance)
   428  
   429  		spec.Accounts[common.UnprefixedAddress(address)] = &parityChainSpecAccount{
   430  			Balance: bal,
   431  			Nonce:   math2.HexOrDecimal64(account.Nonce),
   432  		}
   433  	}
   434  	spec.setPrecompile(1, &parityChainSpecBuiltin{Name: "ecrecover",
   435  		Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}}})
   436  
   437  	spec.setPrecompile(2, &parityChainSpecBuiltin{
   438  		Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}},
   439  	})
   440  	spec.setPrecompile(3, &parityChainSpecBuiltin{
   441  		Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}},
   442  	})
   443  	spec.setPrecompile(4, &parityChainSpecBuiltin{
   444  		Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}},
   445  	})
   446  	if genesis.Config.ByzantiumBlock != nil {
   447  		spec.setPrecompile(5, &parityChainSpecBuiltin{
   448  			Name: "modexp", ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), Pricing: &parityChainSpecPricing{ModExp: &parityChainSpecModExpPricing{Divisor: 20}},
   449  		})
   450  		spec.setPrecompile(6, &parityChainSpecBuiltin{
   451  			Name: "alt_bn128_add", ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), Pricing: &parityChainSpecPricing{AltBnConstOperation: &parityChainSpecAltBnConstOperationPricing{Price: 500}},
   452  		})
   453  		spec.setPrecompile(7, &parityChainSpecBuiltin{
   454  			Name: "alt_bn128_mul", ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), Pricing: &parityChainSpecPricing{AltBnConstOperation: &parityChainSpecAltBnConstOperationPricing{Price: 40000}},
   455  		})
   456  		spec.setPrecompile(8, &parityChainSpecBuiltin{
   457  			Name: "alt_bn128_pairing", ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), Pricing: &parityChainSpecPricing{AltBnPairing: &parityChainSpecAltBnPairingPricing{Base: 100000, Pair: 80000}},
   458  		})
   459  	}
   460  	if genesis.Config.IstanbulBlock != nil {
   461  		if genesis.Config.ByzantiumBlock == nil {
   462  			return nil, errors.New("invalid genesis, istanbul fork is enabled while byzantium is not")
   463  		}
   464  		spec.setPrecompile(6, &parityChainSpecBuiltin{
   465  			Name: "alt_bn128_add", ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), EIP1108Transition: (*hexutil.Big)(genesis.Config.IstanbulBlock), Pricing: &parityChainSpecPricing{AltBnConstOperation: &parityChainSpecAltBnConstOperationPricing{Price: 500, EIP1108TransitionPrice: 150}},
   466  		})
   467  		spec.setPrecompile(7, &parityChainSpecBuiltin{
   468  			Name: "alt_bn128_mul", ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), EIP1108Transition: (*hexutil.Big)(genesis.Config.IstanbulBlock), Pricing: &parityChainSpecPricing{AltBnConstOperation: &parityChainSpecAltBnConstOperationPricing{Price: 40000, EIP1108TransitionPrice: 6000}},
   469  		})
   470  		spec.setPrecompile(8, &parityChainSpecBuiltin{
   471  			Name: "alt_bn128_pairing", ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock), EIP1108Transition: (*hexutil.Big)(genesis.Config.IstanbulBlock), Pricing: &parityChainSpecPricing{AltBnPairing: &parityChainSpecAltBnPairingPricing{Base: 100000, Pair: 80000, EIP1108TransitionBase: 45000, EIP1108TransitionPair: 34000}},
   472  		})
   473  		spec.setPrecompile(9, &parityChainSpecBuiltin{
   474  			Name: "blake2_f", ActivateAt: (*hexutil.Big)(genesis.Config.IstanbulBlock), Pricing: &parityChainSpecPricing{Blake2F: &parityChainSpecBlakePricing{GasPerRound: 1}},
   475  		})
   476  	}
   477  	return spec, nil
   478  }
   479  
   480  func (spec *parityChainSpec) setPrecompile(address byte, data *parityChainSpecBuiltin) {
   481  	if spec.Accounts == nil {
   482  		spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
   483  	}
   484  	a := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
   485  	if _, exist := spec.Accounts[a]; !exist {
   486  		spec.Accounts[a] = &parityChainSpecAccount{}
   487  	}
   488  	spec.Accounts[a].Builtin = data
   489  }
   490  
   491  func (spec *parityChainSpec) setByzantium(num *big.Int) {
   492  	spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ByzantiumBlockReward)
   493  	spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(3000000)
   494  	n := hexutil.Uint64(num.Uint64())
   495  	spec.Engine.Ethash.Params.EIP100bTransition = n
   496  	spec.Params.EIP140Transition = n
   497  	spec.Params.EIP211Transition = n
   498  	spec.Params.EIP214Transition = n
   499  	spec.Params.EIP658Transition = n
   500  }
   501  
   502  func (spec *parityChainSpec) setConstantinople(num *big.Int) {
   503  	spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ConstantinopleBlockReward)
   504  	spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(2000000)
   505  	n := hexutil.Uint64(num.Uint64())
   506  	spec.Params.EIP145Transition = n
   507  	spec.Params.EIP1014Transition = n
   508  	spec.Params.EIP1052Transition = n
   509  	spec.Params.EIP1283Transition = n
   510  }
   511  
   512  func (spec *parityChainSpec) setConstantinopleFix(num *big.Int) {
   513  	spec.Params.EIP1283DisableTransition = hexutil.Uint64(num.Uint64())
   514  }
   515  
   516  func (spec *parityChainSpec) setIstanbul(num *big.Int) {
   517  	// spec.Params.EIP152Transition = hexutil.Uint64(num.Uint64())
   518  	// spec.Params.EIP1108Transition = hexutil.Uint64(num.Uint64())
   519  	spec.Params.EIP1344Transition = hexutil.Uint64(num.Uint64())
   520  	spec.Params.EIP1884Transition = hexutil.Uint64(num.Uint64())
   521  	spec.Params.EIP2028Transition = hexutil.Uint64(num.Uint64())
   522  	spec.Params.EIP1283ReenableTransition = hexutil.Uint64(num.Uint64())
   523  }
   524  
   525  // pyEthereumGenesisSpec represents the genesis specification format used by the
   526  // Python Ethereum implementation.
   527  type pyEthereumGenesisSpec struct {
   528  	Nonce      hexutil.Bytes     `json:"nonce"`
   529  	Timestamp  hexutil.Uint64    `json:"timestamp"`
   530  	ExtraData  hexutil.Bytes     `json:"extraData"`
   531  	GasLimit   hexutil.Uint64    `json:"gasLimit"`
   532  	Difficulty *hexutil.Big      `json:"difficulty"`
   533  	Mixhash    common.Hash       `json:"mixhash"`
   534  	Coinbase   common.Address    `json:"coinbase"`
   535  	Alloc      core.GenesisAlloc `json:"alloc"`
   536  	ParentHash common.Hash       `json:"parentHash"`
   537  }
   538  
   539  // newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
   540  // chain specification format.
   541  func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) {
   542  	// Only ethash is currently supported between go-ethereum and pyethereum
   543  	if genesis.Config.Ethash == nil {
   544  		return nil, errors.New("unsupported consensus engine")
   545  	}
   546  	spec := &pyEthereumGenesisSpec{
   547  		Timestamp:  (hexutil.Uint64)(genesis.Timestamp),
   548  		ExtraData:  genesis.ExtraData,
   549  		GasLimit:   (hexutil.Uint64)(genesis.GasLimit),
   550  		Difficulty: (*hexutil.Big)(genesis.Difficulty),
   551  		Mixhash:    genesis.Mixhash,
   552  		Coinbase:   genesis.Coinbase,
   553  		Alloc:      genesis.Alloc,
   554  		ParentHash: genesis.ParentHash,
   555  	}
   556  	spec.Nonce = (hexutil.Bytes)(make([]byte, 8))
   557  	binary.LittleEndian.PutUint64(spec.Nonce[:], genesis.Nonce)
   558  
   559  	return spec, nil
   560  }