github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/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  	"github.com/electroneum/electroneum-sc/common"
    26  	"github.com/electroneum/electroneum-sc/common/hexutil"
    27  	math2 "github.com/electroneum/electroneum-sc/common/math"
    28  	"github.com/electroneum/electroneum-sc/consensus/ethash"
    29  	"github.com/electroneum/electroneum-sc/core"
    30  	"github.com/electroneum/electroneum-sc/core/types"
    31  	"github.com/electroneum/electroneum-sc/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  // parityChainSpec is the chain specification format used by Parity.
   228  type parityChainSpec struct {
   229  	Name    string `json:"name"`
   230  	Datadir string `json:"dataDir"`
   231  	Engine  struct {
   232  		Ethash struct {
   233  			Params struct {
   234  				MinimumDifficulty      *hexutil.Big      `json:"minimumDifficulty"`
   235  				DifficultyBoundDivisor *hexutil.Big      `json:"difficultyBoundDivisor"`
   236  				DurationLimit          *hexutil.Big      `json:"durationLimit"`
   237  				BlockReward            map[string]string `json:"blockReward"`
   238  				DifficultyBombDelays   map[string]string `json:"difficultyBombDelays"`
   239  				HomesteadTransition    hexutil.Uint64    `json:"homesteadTransition"`
   240  				EIP100bTransition      hexutil.Uint64    `json:"eip100bTransition"`
   241  			} `json:"params"`
   242  		} `json:"Ethash"`
   243  	} `json:"engine"`
   244  
   245  	Params struct {
   246  		AccountStartNonce         hexutil.Uint64       `json:"accountStartNonce"`
   247  		MaximumExtraDataSize      hexutil.Uint64       `json:"maximumExtraDataSize"`
   248  		MinGasLimit               hexutil.Uint64       `json:"minGasLimit"`
   249  		GasLimitBoundDivisor      math2.HexOrDecimal64 `json:"gasLimitBoundDivisor"`
   250  		NetworkID                 hexutil.Uint64       `json:"networkID"`
   251  		ChainID                   hexutil.Uint64       `json:"chainID"`
   252  		MaxCodeSize               hexutil.Uint64       `json:"maxCodeSize"`
   253  		MaxCodeSizeTransition     hexutil.Uint64       `json:"maxCodeSizeTransition"`
   254  		EIP98Transition           hexutil.Uint64       `json:"eip98Transition"`
   255  		EIP150Transition          hexutil.Uint64       `json:"eip150Transition"`
   256  		EIP160Transition          hexutil.Uint64       `json:"eip160Transition"`
   257  		EIP161abcTransition       hexutil.Uint64       `json:"eip161abcTransition"`
   258  		EIP161dTransition         hexutil.Uint64       `json:"eip161dTransition"`
   259  		EIP155Transition          hexutil.Uint64       `json:"eip155Transition"`
   260  		EIP140Transition          hexutil.Uint64       `json:"eip140Transition"`
   261  		EIP211Transition          hexutil.Uint64       `json:"eip211Transition"`
   262  		EIP214Transition          hexutil.Uint64       `json:"eip214Transition"`
   263  		EIP658Transition          hexutil.Uint64       `json:"eip658Transition"`
   264  		EIP145Transition          hexutil.Uint64       `json:"eip145Transition"`
   265  		EIP1014Transition         hexutil.Uint64       `json:"eip1014Transition"`
   266  		EIP1052Transition         hexutil.Uint64       `json:"eip1052Transition"`
   267  		EIP1283Transition         hexutil.Uint64       `json:"eip1283Transition"`
   268  		EIP1283DisableTransition  hexutil.Uint64       `json:"eip1283DisableTransition"`
   269  		EIP1283ReenableTransition hexutil.Uint64       `json:"eip1283ReenableTransition"`
   270  		EIP1344Transition         hexutil.Uint64       `json:"eip1344Transition"`
   271  		EIP1884Transition         hexutil.Uint64       `json:"eip1884Transition"`
   272  		EIP2028Transition         hexutil.Uint64       `json:"eip2028Transition"`
   273  	} `json:"params"`
   274  
   275  	Genesis struct {
   276  		Seal struct {
   277  			Ethereum struct {
   278  				Nonce   types.BlockNonce `json:"nonce"`
   279  				MixHash hexutil.Bytes    `json:"mixHash"`
   280  			} `json:"ethereum"`
   281  		} `json:"seal"`
   282  
   283  		Difficulty *hexutil.Big   `json:"difficulty"`
   284  		Author     common.Address `json:"author"`
   285  		Timestamp  hexutil.Uint64 `json:"timestamp"`
   286  		ParentHash common.Hash    `json:"parentHash"`
   287  		ExtraData  hexutil.Bytes  `json:"extraData"`
   288  		GasLimit   hexutil.Uint64 `json:"gasLimit"`
   289  	} `json:"genesis"`
   290  
   291  	Nodes    []string                                             `json:"nodes"`
   292  	Accounts map[common.UnprefixedAddress]*parityChainSpecAccount `json:"accounts"`
   293  }
   294  
   295  // parityChainSpecAccount is the prefunded genesis account and/or precompiled
   296  // contract definition.
   297  type parityChainSpecAccount struct {
   298  	Balance math2.HexOrDecimal256   `json:"balance"`
   299  	Nonce   math2.HexOrDecimal64    `json:"nonce,omitempty"`
   300  	Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"`
   301  }
   302  
   303  // parityChainSpecBuiltin is the precompiled contract definition.
   304  type parityChainSpecBuiltin struct {
   305  	Name       string       `json:"name"`                  // Each builtin should has it own name
   306  	Pricing    interface{}  `json:"pricing"`               // Each builtin should has it own price strategy
   307  	ActivateAt *hexutil.Big `json:"activate_at,omitempty"` // ActivateAt can't be omitted if empty, default means no fork
   308  }
   309  
   310  // parityChainSpecPricing represents the different pricing models that builtin
   311  // contracts might advertise using.
   312  type parityChainSpecPricing struct {
   313  	Linear *parityChainSpecLinearPricing `json:"linear,omitempty"`
   314  	ModExp *parityChainSpecModExpPricing `json:"modexp,omitempty"`
   315  
   316  	// Before the https://github.com/paritytech/parity-ethereum/pull/11039,
   317  	// Parity uses this format to config bn pairing price policy.
   318  	AltBnPairing *parityChainSepcAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"`
   319  
   320  	// Blake2F is the price per round of Blake2 compression
   321  	Blake2F *parityChainSpecBlakePricing `json:"blake2_f,omitempty"`
   322  }
   323  
   324  type parityChainSpecLinearPricing struct {
   325  	Base uint64 `json:"base"`
   326  	Word uint64 `json:"word"`
   327  }
   328  
   329  type parityChainSpecModExpPricing struct {
   330  	Divisor uint64 `json:"divisor"`
   331  }
   332  
   333  // parityChainSpecAltBnConstOperationPricing defines the price
   334  // policy for bn const operation(used after istanbul)
   335  type parityChainSpecAltBnConstOperationPricing struct {
   336  	Price uint64 `json:"price"`
   337  }
   338  
   339  // parityChainSepcAltBnPairingPricing defines the price policy
   340  // for bn pairing.
   341  type parityChainSepcAltBnPairingPricing struct {
   342  	Base uint64 `json:"base"`
   343  	Pair uint64 `json:"pair"`
   344  }
   345  
   346  // parityChainSpecBlakePricing defines the price policy for blake2 f
   347  // compression.
   348  type parityChainSpecBlakePricing struct {
   349  	GasPerRound uint64 `json:"gas_per_round"`
   350  }
   351  
   352  type parityChainSpecAlternativePrice struct {
   353  	AltBnConstOperationPrice *parityChainSpecAltBnConstOperationPricing `json:"alt_bn128_const_operations,omitempty"`
   354  	AltBnPairingPrice        *parityChainSepcAltBnPairingPricing        `json:"alt_bn128_pairing,omitempty"`
   355  }
   356  
   357  // parityChainSpecVersionedPricing represents a single version price policy.
   358  type parityChainSpecVersionedPricing struct {
   359  	Price *parityChainSpecAlternativePrice `json:"price,omitempty"`
   360  	Info  string                           `json:"info,omitempty"`
   361  }
   362  
   363  // newParityChainSpec converts a go-ethereum genesis block into a Parity specific
   364  // chain specification format.
   365  func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) {
   366  	// Only ethash is currently supported between go-ethereum and Parity
   367  	if genesis.Config.Ethash == nil {
   368  		return nil, errors.New("unsupported consensus engine")
   369  	}
   370  	// Reconstruct the chain spec in Parity's format
   371  	spec := &parityChainSpec{
   372  		Name:    network,
   373  		Nodes:   bootnodes,
   374  		Datadir: strings.ToLower(network),
   375  	}
   376  	spec.Engine.Ethash.Params.BlockReward = make(map[string]string)
   377  	spec.Engine.Ethash.Params.DifficultyBombDelays = make(map[string]string)
   378  	// Frontier
   379  	spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
   380  	spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
   381  	spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
   382  	spec.Engine.Ethash.Params.BlockReward["0x0"] = hexutil.EncodeBig(ethash.FrontierBlockReward)
   383  
   384  	// Homestead
   385  	spec.Engine.Ethash.Params.HomesteadTransition = hexutil.Uint64(genesis.Config.HomesteadBlock.Uint64())
   386  
   387  	// Tangerine Whistle : 150
   388  	// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-608.md
   389  	spec.Params.EIP150Transition = hexutil.Uint64(genesis.Config.EIP150Block.Uint64())
   390  
   391  	// Spurious Dragon: 155, 160, 161, 170
   392  	// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-607.md
   393  	spec.Params.EIP155Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
   394  	spec.Params.EIP160Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
   395  	spec.Params.EIP161abcTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
   396  	spec.Params.EIP161dTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
   397  
   398  	// Byzantium
   399  	if num := genesis.Config.ByzantiumBlock; num != nil {
   400  		spec.setByzantium(num)
   401  	}
   402  	// Constantinople
   403  	if num := genesis.Config.ConstantinopleBlock; num != nil {
   404  		spec.setConstantinople(num)
   405  	}
   406  	// ConstantinopleFix (remove eip-1283)
   407  	if num := genesis.Config.PetersburgBlock; num != nil {
   408  		spec.setConstantinopleFix(num)
   409  	}
   410  	// Istanbul
   411  	if num := genesis.Config.IstanbulBlock; num != nil {
   412  		spec.setIstanbul(num)
   413  	}
   414  	spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
   415  	spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
   416  	spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor)
   417  	spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
   418  	spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
   419  	spec.Params.MaxCodeSize = params.MaxCodeSize
   420  	// geth has it set from zero
   421  	spec.Params.MaxCodeSizeTransition = 0
   422  
   423  	// Disable this one
   424  	spec.Params.EIP98Transition = math.MaxInt64
   425  
   426  	spec.Genesis.Seal.Ethereum.Nonce = types.EncodeNonce(genesis.Nonce)
   427  	spec.Genesis.Seal.Ethereum.MixHash = genesis.Mixhash[:]
   428  	spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
   429  	spec.Genesis.Author = genesis.Coinbase
   430  	spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
   431  	spec.Genesis.ParentHash = genesis.ParentHash
   432  	spec.Genesis.ExtraData = genesis.ExtraData
   433  	spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
   434  
   435  	spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
   436  	for address, account := range genesis.Alloc {
   437  		bal := math2.HexOrDecimal256(*account.Balance)
   438  
   439  		spec.Accounts[common.UnprefixedAddress(address)] = &parityChainSpecAccount{
   440  			Balance: bal,
   441  			Nonce:   math2.HexOrDecimal64(account.Nonce),
   442  		}
   443  	}
   444  	spec.setPrecompile(1, &parityChainSpecBuiltin{Name: "ecrecover",
   445  		Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}}})
   446  
   447  	spec.setPrecompile(2, &parityChainSpecBuiltin{
   448  		Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}},
   449  	})
   450  	spec.setPrecompile(3, &parityChainSpecBuiltin{
   451  		Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}},
   452  	})
   453  	spec.setPrecompile(4, &parityChainSpecBuiltin{
   454  		Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}},
   455  	})
   456  	if genesis.Config.ByzantiumBlock != nil {
   457  		spec.setPrecompile(5, &parityChainSpecBuiltin{
   458  			Name:       "modexp",
   459  			ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   460  			Pricing: &parityChainSpecPricing{
   461  				ModExp: &parityChainSpecModExpPricing{Divisor: 20},
   462  			},
   463  		})
   464  		spec.setPrecompile(6, &parityChainSpecBuiltin{
   465  			Name:       "alt_bn128_add",
   466  			ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   467  			Pricing: &parityChainSpecPricing{
   468  				Linear: &parityChainSpecLinearPricing{Base: 500, Word: 0},
   469  			},
   470  		})
   471  		spec.setPrecompile(7, &parityChainSpecBuiltin{
   472  			Name:       "alt_bn128_mul",
   473  			ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   474  			Pricing: &parityChainSpecPricing{
   475  				Linear: &parityChainSpecLinearPricing{Base: 40000, Word: 0},
   476  			},
   477  		})
   478  		spec.setPrecompile(8, &parityChainSpecBuiltin{
   479  			Name:       "alt_bn128_pairing",
   480  			ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   481  			Pricing: &parityChainSpecPricing{
   482  				AltBnPairing: &parityChainSepcAltBnPairingPricing{Base: 100000, Pair: 80000},
   483  			},
   484  		})
   485  	}
   486  	if genesis.Config.IstanbulBlock != nil {
   487  		if genesis.Config.ByzantiumBlock == nil {
   488  			return nil, errors.New("invalid genesis, istanbul fork is enabled while byzantium is not")
   489  		}
   490  		spec.setPrecompile(6, &parityChainSpecBuiltin{
   491  			Name:       "alt_bn128_add",
   492  			ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   493  			Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{
   494  				(*hexutil.Big)(big.NewInt(0)): {
   495  					Price: &parityChainSpecAlternativePrice{
   496  						AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 500},
   497  					},
   498  				},
   499  				(*hexutil.Big)(genesis.Config.IstanbulBlock): {
   500  					Price: &parityChainSpecAlternativePrice{
   501  						AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 150},
   502  					},
   503  				},
   504  			},
   505  		})
   506  		spec.setPrecompile(7, &parityChainSpecBuiltin{
   507  			Name:       "alt_bn128_mul",
   508  			ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   509  			Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{
   510  				(*hexutil.Big)(big.NewInt(0)): {
   511  					Price: &parityChainSpecAlternativePrice{
   512  						AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 40000},
   513  					},
   514  				},
   515  				(*hexutil.Big)(genesis.Config.IstanbulBlock): {
   516  					Price: &parityChainSpecAlternativePrice{
   517  						AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 6000},
   518  					},
   519  				},
   520  			},
   521  		})
   522  		spec.setPrecompile(8, &parityChainSpecBuiltin{
   523  			Name:       "alt_bn128_pairing",
   524  			ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
   525  			Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{
   526  				(*hexutil.Big)(big.NewInt(0)): {
   527  					Price: &parityChainSpecAlternativePrice{
   528  						AltBnPairingPrice: &parityChainSepcAltBnPairingPricing{Base: 100000, Pair: 80000},
   529  					},
   530  				},
   531  				(*hexutil.Big)(genesis.Config.IstanbulBlock): {
   532  					Price: &parityChainSpecAlternativePrice{
   533  						AltBnPairingPrice: &parityChainSepcAltBnPairingPricing{Base: 45000, Pair: 34000},
   534  					},
   535  				},
   536  			},
   537  		})
   538  		spec.setPrecompile(9, &parityChainSpecBuiltin{
   539  			Name:       "blake2_f",
   540  			ActivateAt: (*hexutil.Big)(genesis.Config.IstanbulBlock),
   541  			Pricing: &parityChainSpecPricing{
   542  				Blake2F: &parityChainSpecBlakePricing{GasPerRound: 1},
   543  			},
   544  		})
   545  	}
   546  	return spec, nil
   547  }
   548  
   549  func (spec *parityChainSpec) setPrecompile(address byte, data *parityChainSpecBuiltin) {
   550  	if spec.Accounts == nil {
   551  		spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
   552  	}
   553  	a := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
   554  	if _, exist := spec.Accounts[a]; !exist {
   555  		spec.Accounts[a] = &parityChainSpecAccount{}
   556  	}
   557  	spec.Accounts[a].Builtin = data
   558  }
   559  
   560  func (spec *parityChainSpec) setByzantium(num *big.Int) {
   561  	spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ByzantiumBlockReward)
   562  	spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(3000000)
   563  	n := hexutil.Uint64(num.Uint64())
   564  	spec.Engine.Ethash.Params.EIP100bTransition = n
   565  	spec.Params.EIP140Transition = n
   566  	spec.Params.EIP211Transition = n
   567  	spec.Params.EIP214Transition = n
   568  	spec.Params.EIP658Transition = n
   569  }
   570  
   571  func (spec *parityChainSpec) setConstantinople(num *big.Int) {
   572  	spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ConstantinopleBlockReward)
   573  	spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(2000000)
   574  	n := hexutil.Uint64(num.Uint64())
   575  	spec.Params.EIP145Transition = n
   576  	spec.Params.EIP1014Transition = n
   577  	spec.Params.EIP1052Transition = n
   578  	spec.Params.EIP1283Transition = n
   579  }
   580  
   581  func (spec *parityChainSpec) setConstantinopleFix(num *big.Int) {
   582  	spec.Params.EIP1283DisableTransition = hexutil.Uint64(num.Uint64())
   583  }
   584  
   585  func (spec *parityChainSpec) setIstanbul(num *big.Int) {
   586  	spec.Params.EIP1344Transition = hexutil.Uint64(num.Uint64())
   587  	spec.Params.EIP1884Transition = hexutil.Uint64(num.Uint64())
   588  	spec.Params.EIP2028Transition = hexutil.Uint64(num.Uint64())
   589  	spec.Params.EIP1283ReenableTransition = hexutil.Uint64(num.Uint64())
   590  }
   591  
   592  // pyEthereumGenesisSpec represents the genesis specification format used by the
   593  // Python Ethereum implementation.
   594  type pyEthereumGenesisSpec struct {
   595  	Nonce      types.BlockNonce  `json:"nonce"`
   596  	Timestamp  hexutil.Uint64    `json:"timestamp"`
   597  	ExtraData  hexutil.Bytes     `json:"extraData"`
   598  	GasLimit   hexutil.Uint64    `json:"gasLimit"`
   599  	Difficulty *hexutil.Big      `json:"difficulty"`
   600  	Mixhash    common.Hash       `json:"mixhash"`
   601  	Coinbase   common.Address    `json:"coinbase"`
   602  	Alloc      core.GenesisAlloc `json:"alloc"`
   603  	ParentHash common.Hash       `json:"parentHash"`
   604  }
   605  
   606  // newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
   607  // chain specification format.
   608  func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) {
   609  	// Only ethash is currently supported between go-ethereum and pyethereum
   610  	if genesis.Config.Ethash == nil {
   611  		return nil, errors.New("unsupported consensus engine")
   612  	}
   613  	spec := &pyEthereumGenesisSpec{
   614  		Nonce:      types.EncodeNonce(genesis.Nonce),
   615  		Timestamp:  (hexutil.Uint64)(genesis.Timestamp),
   616  		ExtraData:  genesis.ExtraData,
   617  		GasLimit:   (hexutil.Uint64)(genesis.GasLimit),
   618  		Difficulty: (*hexutil.Big)(genesis.Difficulty),
   619  		Mixhash:    genesis.Mixhash,
   620  		Coinbase:   genesis.Coinbase,
   621  		Alloc:      genesis.Alloc,
   622  		ParentHash: genesis.ParentHash,
   623  	}
   624  	return spec, nil
   625  }