github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/cmd/puppeth/genesis.go (about)

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