github.com/fly8eros/go-ethereum@v1.9.1/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.Uint64         `json:"homesteadForkBlock"`
    42  		DaoHardforkBlock        math2.HexOrDecimal64   `json:"daoHardforkBlock"`
    43  		EIP150ForkBlock         hexutil.Uint64         `json:"EIP150ForkBlock"`
    44  		EIP158ForkBlock         hexutil.Uint64         `json:"EIP158ForkBlock"`
    45  		ByzantiumForkBlock      hexutil.Uint64         `json:"byzantiumForkBlock"`
    46  		ConstantinopleForkBlock hexutil.Uint64         `json:"constantinopleForkBlock"`
    47  		MinGasLimit             hexutil.Uint64         `json:"minGasLimit"`
    48  		MaxGasLimit             hexutil.Uint64         `json:"maxGasLimit"`
    49  		TieBreakingGas          bool                   `json:"tieBreakingGas"`
    50  		GasLimitBoundDivisor    math2.HexOrDecimal64   `json:"gasLimitBoundDivisor"`
    51  		MinimumDifficulty       *hexutil.Big           `json:"minimumDifficulty"`
    52  		DifficultyBoundDivisor  *math2.HexOrDecimal256 `json:"difficultyBoundDivisor"`
    53  		DurationLimit           *math2.HexOrDecimal256 `json:"durationLimit"`
    54  		BlockReward             *hexutil.Big           `json:"blockReward"`
    55  		NetworkID               hexutil.Uint64         `json:"networkID"`
    56  		ChainID                 hexutil.Uint64         `json:"chainID"`
    57  		AllowFutureBlocks       bool                   `json:"allowFutureBlocks"`
    58  	} `json:"params"`
    59  
    60  	Genesis struct {
    61  		Nonce      hexutil.Bytes  `json:"nonce"`
    62  		Difficulty *hexutil.Big   `json:"difficulty"`
    63  		MixHash    common.Hash    `json:"mixHash"`
    64  		Author     common.Address `json:"author"`
    65  		Timestamp  hexutil.Uint64 `json:"timestamp"`
    66  		ParentHash common.Hash    `json:"parentHash"`
    67  		ExtraData  hexutil.Bytes  `json:"extraData"`
    68  		GasLimit   hexutil.Uint64 `json:"gasLimit"`
    69  	} `json:"genesis"`
    70  
    71  	Accounts map[common.UnprefixedAddress]*alethGenesisSpecAccount `json:"accounts"`
    72  }
    73  
    74  // alethGenesisSpecAccount is the prefunded genesis account and/or precompiled
    75  // contract definition.
    76  type alethGenesisSpecAccount struct {
    77  	Balance     *math2.HexOrDecimal256   `json:"balance"`
    78  	Nonce       uint64                   `json:"nonce,omitempty"`
    79  	Precompiled *alethGenesisSpecBuiltin `json:"precompiled,omitempty"`
    80  }
    81  
    82  // alethGenesisSpecBuiltin is the precompiled contract definition.
    83  type alethGenesisSpecBuiltin struct {
    84  	Name          string                         `json:"name,omitempty"`
    85  	StartingBlock hexutil.Uint64                 `json:"startingBlock,omitempty"`
    86  	Linear        *alethGenesisSpecLinearPricing `json:"linear,omitempty"`
    87  }
    88  
    89  type alethGenesisSpecLinearPricing struct {
    90  	Base uint64 `json:"base"`
    91  	Word uint64 `json:"word"`
    92  }
    93  
    94  // newAlethGenesisSpec converts a go-ethereum genesis block into a Aleth-specific
    95  // chain specification format.
    96  func newAlethGenesisSpec(network string, genesis *core.Genesis) (*alethGenesisSpec, error) {
    97  	// Only ethash is currently supported between go-ethereum and aleth
    98  	if genesis.Config.Ethash == nil {
    99  		return nil, errors.New("unsupported consensus engine")
   100  	}
   101  	// Reconstruct the chain spec in Aleth format
   102  	spec := &alethGenesisSpec{
   103  		SealEngine: "Ethash",
   104  	}
   105  	// Some defaults
   106  	spec.Params.AccountStartNonce = 0
   107  	spec.Params.TieBreakingGas = false
   108  	spec.Params.AllowFutureBlocks = false
   109  	spec.Params.DaoHardforkBlock = 0
   110  
   111  	spec.Params.HomesteadForkBlock = (hexutil.Uint64)(genesis.Config.HomesteadBlock.Uint64())
   112  	spec.Params.EIP150ForkBlock = (hexutil.Uint64)(genesis.Config.EIP150Block.Uint64())
   113  	spec.Params.EIP158ForkBlock = (hexutil.Uint64)(genesis.Config.EIP158Block.Uint64())
   114  
   115  	// Byzantium
   116  	if num := genesis.Config.ByzantiumBlock; num != nil {
   117  		spec.setByzantium(num)
   118  	}
   119  	// Constantinople
   120  	if num := genesis.Config.ConstantinopleBlock; num != nil {
   121  		spec.setConstantinople(num)
   122  	}
   123  
   124  	spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
   125  	spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
   126  	spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
   127  	spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
   128  	spec.Params.MaxGasLimit = (hexutil.Uint64)(math.MaxInt64)
   129  	spec.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
   130  	spec.Params.DifficultyBoundDivisor = (*math2.HexOrDecimal256)(params.DifficultyBoundDivisor)
   131  	spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor)
   132  	spec.Params.DurationLimit = (*math2.HexOrDecimal256)(params.DurationLimit)
   133  	spec.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward)
   134  
   135  	spec.Genesis.Nonce = (hexutil.Bytes)(make([]byte, 8))
   136  	binary.LittleEndian.PutUint64(spec.Genesis.Nonce[:], genesis.Nonce)
   137  
   138  	spec.Genesis.MixHash = genesis.Mixhash
   139  	spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
   140  	spec.Genesis.Author = genesis.Coinbase
   141  	spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
   142  	spec.Genesis.ParentHash = genesis.ParentHash
   143  	spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
   144  	spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
   145  
   146  	for address, account := range genesis.Alloc {
   147  		spec.setAccount(address, account)
   148  	}
   149  
   150  	spec.setPrecompile(1, &alethGenesisSpecBuiltin{Name: "ecrecover",
   151  		Linear: &alethGenesisSpecLinearPricing{Base: 3000}})
   152  	spec.setPrecompile(2, &alethGenesisSpecBuiltin{Name: "sha256",
   153  		Linear: &alethGenesisSpecLinearPricing{Base: 60, Word: 12}})
   154  	spec.setPrecompile(3, &alethGenesisSpecBuiltin{Name: "ripemd160",
   155  		Linear: &alethGenesisSpecLinearPricing{Base: 600, Word: 120}})
   156  	spec.setPrecompile(4, &alethGenesisSpecBuiltin{Name: "identity",
   157  		Linear: &alethGenesisSpecLinearPricing{Base: 15, Word: 3}})
   158  	if genesis.Config.ByzantiumBlock != nil {
   159  		spec.setPrecompile(5, &alethGenesisSpecBuiltin{Name: "modexp",
   160  			StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64())})
   161  		spec.setPrecompile(6, &alethGenesisSpecBuiltin{Name: "alt_bn128_G1_add",
   162  			StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()),
   163  			Linear:        &alethGenesisSpecLinearPricing{Base: 500}})
   164  		spec.setPrecompile(7, &alethGenesisSpecBuiltin{Name: "alt_bn128_G1_mul",
   165  			StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()),
   166  			Linear:        &alethGenesisSpecLinearPricing{Base: 40000}})
   167  		spec.setPrecompile(8, &alethGenesisSpecBuiltin{Name: "alt_bn128_pairing_product",
   168  			StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64())})
   169  	}
   170  	return spec, nil
   171  }
   172  
   173  func (spec *alethGenesisSpec) setPrecompile(address byte, data *alethGenesisSpecBuiltin) {
   174  	if spec.Accounts == nil {
   175  		spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount)
   176  	}
   177  	addr := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
   178  	if _, exist := spec.Accounts[addr]; !exist {
   179  		spec.Accounts[addr] = &alethGenesisSpecAccount{}
   180  	}
   181  	spec.Accounts[addr].Precompiled = data
   182  }
   183  
   184  func (spec *alethGenesisSpec) setAccount(address common.Address, account core.GenesisAccount) {
   185  	if spec.Accounts == nil {
   186  		spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount)
   187  	}
   188  
   189  	a, exist := spec.Accounts[common.UnprefixedAddress(address)]
   190  	if !exist {
   191  		a = &alethGenesisSpecAccount{}
   192  		spec.Accounts[common.UnprefixedAddress(address)] = a
   193  	}
   194  	a.Balance = (*math2.HexOrDecimal256)(account.Balance)
   195  	a.Nonce = account.Nonce
   196  
   197  }
   198  
   199  func (spec *alethGenesisSpec) setByzantium(num *big.Int) {
   200  	spec.Params.ByzantiumForkBlock = hexutil.Uint64(num.Uint64())
   201  }
   202  
   203  func (spec *alethGenesisSpec) setConstantinople(num *big.Int) {
   204  	spec.Params.ConstantinopleForkBlock = hexutil.Uint64(num.Uint64())
   205  }
   206  
   207  // parityChainSpec is the chain specification format used by Parity.
   208  type parityChainSpec struct {
   209  	Name    string `json:"name"`
   210  	Datadir string `json:"dataDir"`
   211  	Engine  struct {
   212  		Ethash struct {
   213  			Params struct {
   214  				MinimumDifficulty      *hexutil.Big      `json:"minimumDifficulty"`
   215  				DifficultyBoundDivisor *hexutil.Big      `json:"difficultyBoundDivisor"`
   216  				DurationLimit          *hexutil.Big      `json:"durationLimit"`
   217  				BlockReward            map[string]string `json:"blockReward"`
   218  				DifficultyBombDelays   map[string]string `json:"difficultyBombDelays"`
   219  				HomesteadTransition    hexutil.Uint64    `json:"homesteadTransition"`
   220  				EIP100bTransition      hexutil.Uint64    `json:"eip100bTransition"`
   221  			} `json:"params"`
   222  		} `json:"Ethash"`
   223  	} `json:"engine"`
   224  
   225  	Params struct {
   226  		AccountStartNonce        hexutil.Uint64       `json:"accountStartNonce"`
   227  		MaximumExtraDataSize     hexutil.Uint64       `json:"maximumExtraDataSize"`
   228  		MinGasLimit              hexutil.Uint64       `json:"minGasLimit"`
   229  		GasLimitBoundDivisor     math2.HexOrDecimal64 `json:"gasLimitBoundDivisor"`
   230  		NetworkID                hexutil.Uint64       `json:"networkID"`
   231  		ChainID                  hexutil.Uint64       `json:"chainID"`
   232  		MaxCodeSize              hexutil.Uint64       `json:"maxCodeSize"`
   233  		MaxCodeSizeTransition    hexutil.Uint64       `json:"maxCodeSizeTransition"`
   234  		EIP98Transition          hexutil.Uint64       `json:"eip98Transition"`
   235  		EIP150Transition         hexutil.Uint64       `json:"eip150Transition"`
   236  		EIP160Transition         hexutil.Uint64       `json:"eip160Transition"`
   237  		EIP161abcTransition      hexutil.Uint64       `json:"eip161abcTransition"`
   238  		EIP161dTransition        hexutil.Uint64       `json:"eip161dTransition"`
   239  		EIP155Transition         hexutil.Uint64       `json:"eip155Transition"`
   240  		EIP140Transition         hexutil.Uint64       `json:"eip140Transition"`
   241  		EIP211Transition         hexutil.Uint64       `json:"eip211Transition"`
   242  		EIP214Transition         hexutil.Uint64       `json:"eip214Transition"`
   243  		EIP658Transition         hexutil.Uint64       `json:"eip658Transition"`
   244  		EIP145Transition         hexutil.Uint64       `json:"eip145Transition"`
   245  		EIP1014Transition        hexutil.Uint64       `json:"eip1014Transition"`
   246  		EIP1052Transition        hexutil.Uint64       `json:"eip1052Transition"`
   247  		EIP1283Transition        hexutil.Uint64       `json:"eip1283Transition"`
   248  		EIP1283DisableTransition hexutil.Uint64       `json:"eip1283DisableTransition"`
   249  	} `json:"params"`
   250  
   251  	Genesis struct {
   252  		Seal struct {
   253  			Ethereum struct {
   254  				Nonce   hexutil.Bytes `json:"nonce"`
   255  				MixHash hexutil.Bytes `json:"mixHash"`
   256  			} `json:"ethereum"`
   257  		} `json:"seal"`
   258  
   259  		Difficulty *hexutil.Big   `json:"difficulty"`
   260  		Author     common.Address `json:"author"`
   261  		Timestamp  hexutil.Uint64 `json:"timestamp"`
   262  		ParentHash common.Hash    `json:"parentHash"`
   263  		ExtraData  hexutil.Bytes  `json:"extraData"`
   264  		GasLimit   hexutil.Uint64 `json:"gasLimit"`
   265  	} `json:"genesis"`
   266  
   267  	Nodes    []string                                             `json:"nodes"`
   268  	Accounts map[common.UnprefixedAddress]*parityChainSpecAccount `json:"accounts"`
   269  }
   270  
   271  // parityChainSpecAccount is the prefunded genesis account and/or precompiled
   272  // contract definition.
   273  type parityChainSpecAccount struct {
   274  	Balance math2.HexOrDecimal256   `json:"balance"`
   275  	Nonce   math2.HexOrDecimal64    `json:"nonce,omitempty"`
   276  	Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"`
   277  }
   278  
   279  // parityChainSpecBuiltin is the precompiled contract definition.
   280  type parityChainSpecBuiltin struct {
   281  	Name       string                  `json:"name,omitempty"`
   282  	ActivateAt math2.HexOrDecimal64    `json:"activate_at,omitempty"`
   283  	Pricing    *parityChainSpecPricing `json:"pricing,omitempty"`
   284  }
   285  
   286  // parityChainSpecPricing represents the different pricing models that builtin
   287  // contracts might advertise using.
   288  type parityChainSpecPricing struct {
   289  	Linear       *parityChainSpecLinearPricing       `json:"linear,omitempty"`
   290  	ModExp       *parityChainSpecModExpPricing       `json:"modexp,omitempty"`
   291  	AltBnPairing *parityChainSpecAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"`
   292  }
   293  
   294  type parityChainSpecLinearPricing struct {
   295  	Base uint64 `json:"base"`
   296  	Word uint64 `json:"word"`
   297  }
   298  
   299  type parityChainSpecModExpPricing struct {
   300  	Divisor uint64 `json:"divisor"`
   301  }
   302  
   303  type parityChainSpecAltBnPairingPricing struct {
   304  	Base uint64 `json:"base"`
   305  	Pair uint64 `json:"pair"`
   306  }
   307  
   308  // newParityChainSpec converts a go-ethereum genesis block into a Parity specific
   309  // chain specification format.
   310  func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) {
   311  	// Only ethash is currently supported between go-ethereum and Parity
   312  	if genesis.Config.Ethash == nil {
   313  		return nil, errors.New("unsupported consensus engine")
   314  	}
   315  	// Reconstruct the chain spec in Parity's format
   316  	spec := &parityChainSpec{
   317  		Name:    network,
   318  		Nodes:   bootnodes,
   319  		Datadir: strings.ToLower(network),
   320  	}
   321  	spec.Engine.Ethash.Params.BlockReward = make(map[string]string)
   322  	spec.Engine.Ethash.Params.DifficultyBombDelays = make(map[string]string)
   323  	// Frontier
   324  	spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
   325  	spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
   326  	spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
   327  	spec.Engine.Ethash.Params.BlockReward["0x0"] = hexutil.EncodeBig(ethash.FrontierBlockReward)
   328  
   329  	// Homestead
   330  	spec.Engine.Ethash.Params.HomesteadTransition = hexutil.Uint64(genesis.Config.HomesteadBlock.Uint64())
   331  
   332  	// Tangerine Whistle : 150
   333  	// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-608.md
   334  	spec.Params.EIP150Transition = hexutil.Uint64(genesis.Config.EIP150Block.Uint64())
   335  
   336  	// Spurious Dragon: 155, 160, 161, 170
   337  	// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-607.md
   338  	spec.Params.EIP155Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
   339  	spec.Params.EIP160Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
   340  	spec.Params.EIP161abcTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
   341  	spec.Params.EIP161dTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
   342  
   343  	// Byzantium
   344  	if num := genesis.Config.ByzantiumBlock; num != nil {
   345  		spec.setByzantium(num)
   346  	}
   347  	// Constantinople
   348  	if num := genesis.Config.ConstantinopleBlock; num != nil {
   349  		spec.setConstantinople(num)
   350  	}
   351  	// ConstantinopleFix (remove eip-1283)
   352  	if num := genesis.Config.PetersburgBlock; num != nil {
   353  		spec.setConstantinopleFix(num)
   354  	}
   355  
   356  	spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
   357  	spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
   358  	spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor)
   359  	spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
   360  	spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
   361  	spec.Params.MaxCodeSize = params.MaxCodeSize
   362  	// geth has it set from zero
   363  	spec.Params.MaxCodeSizeTransition = 0
   364  
   365  	// Disable this one
   366  	spec.Params.EIP98Transition = math.MaxInt64
   367  
   368  	spec.Genesis.Seal.Ethereum.Nonce = (hexutil.Bytes)(make([]byte, 8))
   369  	binary.LittleEndian.PutUint64(spec.Genesis.Seal.Ethereum.Nonce[:], genesis.Nonce)
   370  
   371  	spec.Genesis.Seal.Ethereum.MixHash = (hexutil.Bytes)(genesis.Mixhash[:])
   372  	spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
   373  	spec.Genesis.Author = genesis.Coinbase
   374  	spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
   375  	spec.Genesis.ParentHash = genesis.ParentHash
   376  	spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
   377  	spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
   378  
   379  	spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
   380  	for address, account := range genesis.Alloc {
   381  		bal := math2.HexOrDecimal256(*account.Balance)
   382  
   383  		spec.Accounts[common.UnprefixedAddress(address)] = &parityChainSpecAccount{
   384  			Balance: bal,
   385  			Nonce:   math2.HexOrDecimal64(account.Nonce),
   386  		}
   387  	}
   388  	spec.setPrecompile(1, &parityChainSpecBuiltin{Name: "ecrecover",
   389  		Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}}})
   390  
   391  	spec.setPrecompile(2, &parityChainSpecBuiltin{
   392  		Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}},
   393  	})
   394  	spec.setPrecompile(3, &parityChainSpecBuiltin{
   395  		Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}},
   396  	})
   397  	spec.setPrecompile(4, &parityChainSpecBuiltin{
   398  		Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}},
   399  	})
   400  	if genesis.Config.ByzantiumBlock != nil {
   401  		blnum := math2.HexOrDecimal64(genesis.Config.ByzantiumBlock.Uint64())
   402  		spec.setPrecompile(5, &parityChainSpecBuiltin{
   403  			Name: "modexp", ActivateAt: blnum, Pricing: &parityChainSpecPricing{ModExp: &parityChainSpecModExpPricing{Divisor: 20}},
   404  		})
   405  		spec.setPrecompile(6, &parityChainSpecBuiltin{
   406  			Name: "alt_bn128_add", ActivateAt: blnum, Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 500}},
   407  		})
   408  		spec.setPrecompile(7, &parityChainSpecBuiltin{
   409  			Name: "alt_bn128_mul", ActivateAt: blnum, Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 40000}},
   410  		})
   411  		spec.setPrecompile(8, &parityChainSpecBuiltin{
   412  			Name: "alt_bn128_pairing", ActivateAt: blnum, Pricing: &parityChainSpecPricing{AltBnPairing: &parityChainSpecAltBnPairingPricing{Base: 100000, Pair: 80000}},
   413  		})
   414  	}
   415  	return spec, nil
   416  }
   417  
   418  func (spec *parityChainSpec) setPrecompile(address byte, data *parityChainSpecBuiltin) {
   419  	if spec.Accounts == nil {
   420  		spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
   421  	}
   422  	a := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
   423  	if _, exist := spec.Accounts[a]; !exist {
   424  		spec.Accounts[a] = &parityChainSpecAccount{}
   425  	}
   426  	spec.Accounts[a].Builtin = data
   427  }
   428  
   429  func (spec *parityChainSpec) setByzantium(num *big.Int) {
   430  	spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ByzantiumBlockReward)
   431  	spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(3000000)
   432  	n := hexutil.Uint64(num.Uint64())
   433  	spec.Engine.Ethash.Params.EIP100bTransition = n
   434  	spec.Params.EIP140Transition = n
   435  	spec.Params.EIP211Transition = n
   436  	spec.Params.EIP214Transition = n
   437  	spec.Params.EIP658Transition = n
   438  }
   439  
   440  func (spec *parityChainSpec) setConstantinople(num *big.Int) {
   441  	spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ConstantinopleBlockReward)
   442  	spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(2000000)
   443  	n := hexutil.Uint64(num.Uint64())
   444  	spec.Params.EIP145Transition = n
   445  	spec.Params.EIP1014Transition = n
   446  	spec.Params.EIP1052Transition = n
   447  	spec.Params.EIP1283Transition = n
   448  }
   449  
   450  func (spec *parityChainSpec) setConstantinopleFix(num *big.Int) {
   451  	spec.Params.EIP1283DisableTransition = hexutil.Uint64(num.Uint64())
   452  }
   453  
   454  // pyEthereumGenesisSpec represents the genesis specification format used by the
   455  // Python Ethereum implementation.
   456  type pyEthereumGenesisSpec struct {
   457  	Nonce      hexutil.Bytes     `json:"nonce"`
   458  	Timestamp  hexutil.Uint64    `json:"timestamp"`
   459  	ExtraData  hexutil.Bytes     `json:"extraData"`
   460  	GasLimit   hexutil.Uint64    `json:"gasLimit"`
   461  	Difficulty *hexutil.Big      `json:"difficulty"`
   462  	Mixhash    common.Hash       `json:"mixhash"`
   463  	Coinbase   common.Address    `json:"coinbase"`
   464  	Alloc      core.GenesisAlloc `json:"alloc"`
   465  	ParentHash common.Hash       `json:"parentHash"`
   466  }
   467  
   468  // newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
   469  // chain specification format.
   470  func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) {
   471  	// Only ethash is currently supported between go-ethereum and pyethereum
   472  	if genesis.Config.Ethash == nil {
   473  		return nil, errors.New("unsupported consensus engine")
   474  	}
   475  	spec := &pyEthereumGenesisSpec{
   476  		Timestamp:  (hexutil.Uint64)(genesis.Timestamp),
   477  		ExtraData:  genesis.ExtraData,
   478  		GasLimit:   (hexutil.Uint64)(genesis.GasLimit),
   479  		Difficulty: (*hexutil.Big)(genesis.Difficulty),
   480  		Mixhash:    genesis.Mixhash,
   481  		Coinbase:   genesis.Coinbase,
   482  		Alloc:      genesis.Alloc,
   483  		ParentHash: genesis.ParentHash,
   484  	}
   485  	spec.Nonce = (hexutil.Bytes)(make([]byte, 8))
   486  	binary.LittleEndian.PutUint64(spec.Nonce[:], genesis.Nonce)
   487  
   488  	return spec, nil
   489  }