genesis.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. package main
  17. import (
  18. "encoding/binary"
  19. "errors"
  20. "math"
  21. "math/big"
  22. "strings"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/common/hexutil"
  25. math2 "github.com/ethereum/go-ethereum/common/math"
  26. "github.com/ethereum/go-ethereum/consensus/ethash"
  27. "github.com/ethereum/go-ethereum/core"
  28. "github.com/ethereum/go-ethereum/params"
  29. )
  30. // alethGenesisSpec represents the genesis specification format used by the
  31. // C++ Ethereum implementation.
  32. type alethGenesisSpec struct {
  33. SealEngine string `json:"sealEngine"`
  34. Params struct {
  35. AccountStartNonce math2.HexOrDecimal64 `json:"accountStartNonce"`
  36. MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"`
  37. HomesteadForkBlock *hexutil.Big `json:"homesteadForkBlock,omitempty"`
  38. DaoHardforkBlock math2.HexOrDecimal64 `json:"daoHardforkBlock"`
  39. EIP150ForkBlock *hexutil.Big `json:"EIP150ForkBlock,omitempty"`
  40. EIP158ForkBlock *hexutil.Big `json:"EIP158ForkBlock,omitempty"`
  41. ByzantiumForkBlock *hexutil.Big `json:"byzantiumForkBlock,omitempty"`
  42. ConstantinopleForkBlock *hexutil.Big `json:"constantinopleForkBlock,omitempty"`
  43. ConstantinopleFixForkBlock *hexutil.Big `json:"constantinopleFixForkBlock,omitempty"`
  44. IstanbulForkBlock *hexutil.Big `json:"istanbulForkBlock,omitempty"`
  45. MinGasLimit hexutil.Uint64 `json:"minGasLimit"`
  46. MaxGasLimit hexutil.Uint64 `json:"maxGasLimit"`
  47. TieBreakingGas bool `json:"tieBreakingGas"`
  48. GasLimitBoundDivisor math2.HexOrDecimal64 `json:"gasLimitBoundDivisor"`
  49. MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"`
  50. DifficultyBoundDivisor *math2.HexOrDecimal256 `json:"difficultyBoundDivisor"`
  51. DurationLimit *math2.HexOrDecimal256 `json:"durationLimit"`
  52. BlockReward *hexutil.Big `json:"blockReward"`
  53. NetworkID hexutil.Uint64 `json:"networkID"`
  54. ChainID hexutil.Uint64 `json:"chainID"`
  55. AllowFutureBlocks bool `json:"allowFutureBlocks"`
  56. } `json:"params"`
  57. Genesis struct {
  58. Nonce hexutil.Bytes `json:"nonce"`
  59. Difficulty *hexutil.Big `json:"difficulty"`
  60. MixHash common.Hash `json:"mixHash"`
  61. Author common.Address `json:"author"`
  62. Timestamp hexutil.Uint64 `json:"timestamp"`
  63. ParentHash common.Hash `json:"parentHash"`
  64. ExtraData hexutil.Bytes `json:"extraData"`
  65. GasLimit hexutil.Uint64 `json:"gasLimit"`
  66. } `json:"genesis"`
  67. Accounts map[common.UnprefixedAddress]*alethGenesisSpecAccount `json:"accounts"`
  68. }
  69. // alethGenesisSpecAccount is the prefunded genesis account and/or precompiled
  70. // contract definition.
  71. type alethGenesisSpecAccount struct {
  72. Balance *math2.HexOrDecimal256 `json:"balance,omitempty"`
  73. Nonce uint64 `json:"nonce,omitempty"`
  74. Precompiled *alethGenesisSpecBuiltin `json:"precompiled,omitempty"`
  75. }
  76. // alethGenesisSpecBuiltin is the precompiled contract definition.
  77. type alethGenesisSpecBuiltin struct {
  78. Name string `json:"name,omitempty"`
  79. StartingBlock *hexutil.Big `json:"startingBlock,omitempty"`
  80. Linear *alethGenesisSpecLinearPricing `json:"linear,omitempty"`
  81. }
  82. type alethGenesisSpecLinearPricing struct {
  83. Base uint64 `json:"base"`
  84. Word uint64 `json:"word"`
  85. }
  86. // newAlethGenesisSpec converts a go-ethereum genesis block into a Aleth-specific
  87. // chain specification format.
  88. func newAlethGenesisSpec(network string, genesis *core.Genesis) (*alethGenesisSpec, error) {
  89. // Only ethash is currently supported between go-ethereum and aleth
  90. if genesis.Config.Ethash == nil {
  91. return nil, errors.New("unsupported consensus engine")
  92. }
  93. // Reconstruct the chain spec in Aleth format
  94. spec := &alethGenesisSpec{
  95. SealEngine: "Ethash",
  96. }
  97. // Some defaults
  98. spec.Params.AccountStartNonce = 0
  99. spec.Params.TieBreakingGas = false
  100. spec.Params.AllowFutureBlocks = false
  101. // Dao hardfork block is a special one. The fork block is listed as 0 in the
  102. // config but aleth will sync with ETC clients up until the actual dao hard
  103. // fork block.
  104. spec.Params.DaoHardforkBlock = 0
  105. if num := genesis.Config.HomesteadBlock; num != nil {
  106. spec.Params.HomesteadForkBlock = (*hexutil.Big)(num)
  107. }
  108. if num := genesis.Config.EIP150Block; num != nil {
  109. spec.Params.EIP150ForkBlock = (*hexutil.Big)(num)
  110. }
  111. if num := genesis.Config.EIP158Block; num != nil {
  112. spec.Params.EIP158ForkBlock = (*hexutil.Big)(num)
  113. }
  114. if num := genesis.Config.ByzantiumBlock; num != nil {
  115. spec.Params.ByzantiumForkBlock = (*hexutil.Big)(num)
  116. }
  117. if num := genesis.Config.ConstantinopleBlock; num != nil {
  118. spec.Params.ConstantinopleForkBlock = (*hexutil.Big)(num)
  119. }
  120. if num := genesis.Config.PetersburgBlock; num != nil {
  121. spec.Params.ConstantinopleFixForkBlock = (*hexutil.Big)(num)
  122. }
  123. if num := genesis.Config.IstanbulBlock; num != nil {
  124. spec.Params.IstanbulForkBlock = (*hexutil.Big)(num)
  125. }
  126. spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
  127. spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
  128. spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
  129. spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
  130. spec.Params.MaxGasLimit = (hexutil.Uint64)(math.MaxInt64)
  131. spec.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
  132. spec.Params.DifficultyBoundDivisor = (*math2.HexOrDecimal256)(params.DifficultyBoundDivisor)
  133. spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor)
  134. spec.Params.DurationLimit = (*math2.HexOrDecimal256)(params.DurationLimit)
  135. spec.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward)
  136. spec.Genesis.Nonce = (hexutil.Bytes)(make([]byte, 8))
  137. binary.LittleEndian.PutUint64(spec.Genesis.Nonce[:], genesis.Nonce)
  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. for address, account := range genesis.Alloc {
  146. spec.setAccount(address, account)
  147. }
  148. spec.setPrecompile(1, &alethGenesisSpecBuiltin{Name: "ecrecover",
  149. Linear: &alethGenesisSpecLinearPricing{Base: 3000}})
  150. spec.setPrecompile(2, &alethGenesisSpecBuiltin{Name: "sha256",
  151. Linear: &alethGenesisSpecLinearPricing{Base: 60, Word: 12}})
  152. spec.setPrecompile(3, &alethGenesisSpecBuiltin{Name: "ripemd160",
  153. Linear: &alethGenesisSpecLinearPricing{Base: 600, Word: 120}})
  154. spec.setPrecompile(4, &alethGenesisSpecBuiltin{Name: "identity",
  155. Linear: &alethGenesisSpecLinearPricing{Base: 15, Word: 3}})
  156. if genesis.Config.ByzantiumBlock != nil {
  157. spec.setPrecompile(5, &alethGenesisSpecBuiltin{Name: "modexp",
  158. StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock)})
  159. spec.setPrecompile(6, &alethGenesisSpecBuiltin{Name: "alt_bn128_G1_add",
  160. StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  161. Linear: &alethGenesisSpecLinearPricing{Base: 500}})
  162. spec.setPrecompile(7, &alethGenesisSpecBuiltin{Name: "alt_bn128_G1_mul",
  163. StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  164. Linear: &alethGenesisSpecLinearPricing{Base: 40000}})
  165. spec.setPrecompile(8, &alethGenesisSpecBuiltin{Name: "alt_bn128_pairing_product",
  166. StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock)})
  167. }
  168. if genesis.Config.IstanbulBlock != nil {
  169. if genesis.Config.ByzantiumBlock == nil {
  170. return nil, errors.New("invalid genesis, istanbul fork is enabled while byzantium is not")
  171. }
  172. spec.setPrecompile(6, &alethGenesisSpecBuiltin{
  173. Name: "alt_bn128_G1_add",
  174. StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  175. }) // Aleth hardcoded the gas policy
  176. spec.setPrecompile(7, &alethGenesisSpecBuiltin{
  177. Name: "alt_bn128_G1_mul",
  178. StartingBlock: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  179. }) // Aleth hardcoded the gas policy
  180. spec.setPrecompile(9, &alethGenesisSpecBuiltin{
  181. Name: "blake2_compression",
  182. StartingBlock: (*hexutil.Big)(genesis.Config.IstanbulBlock),
  183. })
  184. }
  185. return spec, nil
  186. }
  187. func (spec *alethGenesisSpec) setPrecompile(address byte, data *alethGenesisSpecBuiltin) {
  188. if spec.Accounts == nil {
  189. spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount)
  190. }
  191. addr := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
  192. if _, exist := spec.Accounts[addr]; !exist {
  193. spec.Accounts[addr] = &alethGenesisSpecAccount{}
  194. }
  195. spec.Accounts[addr].Precompiled = data
  196. }
  197. func (spec *alethGenesisSpec) setAccount(address common.Address, account core.GenesisAccount) {
  198. if spec.Accounts == nil {
  199. spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount)
  200. }
  201. a, exist := spec.Accounts[common.UnprefixedAddress(address)]
  202. if !exist {
  203. a = &alethGenesisSpecAccount{}
  204. spec.Accounts[common.UnprefixedAddress(address)] = a
  205. }
  206. a.Balance = (*math2.HexOrDecimal256)(account.Balance)
  207. a.Nonce = account.Nonce
  208. }
  209. // parityChainSpec is the chain specification format used by Parity.
  210. type parityChainSpec struct {
  211. Name string `json:"name"`
  212. Datadir string `json:"dataDir"`
  213. Engine struct {
  214. Ethash struct {
  215. Params struct {
  216. MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"`
  217. DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"`
  218. DurationLimit *hexutil.Big `json:"durationLimit"`
  219. BlockReward map[string]string `json:"blockReward"`
  220. DifficultyBombDelays map[string]string `json:"difficultyBombDelays"`
  221. HomesteadTransition hexutil.Uint64 `json:"homesteadTransition"`
  222. EIP100bTransition hexutil.Uint64 `json:"eip100bTransition"`
  223. } `json:"params"`
  224. } `json:"Ethash"`
  225. } `json:"engine"`
  226. Params struct {
  227. AccountStartNonce hexutil.Uint64 `json:"accountStartNonce"`
  228. MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"`
  229. MinGasLimit hexutil.Uint64 `json:"minGasLimit"`
  230. GasLimitBoundDivisor math2.HexOrDecimal64 `json:"gasLimitBoundDivisor"`
  231. NetworkID hexutil.Uint64 `json:"networkID"`
  232. ChainID hexutil.Uint64 `json:"chainID"`
  233. MaxCodeSize hexutil.Uint64 `json:"maxCodeSize"`
  234. MaxCodeSizeTransition hexutil.Uint64 `json:"maxCodeSizeTransition"`
  235. EIP98Transition hexutil.Uint64 `json:"eip98Transition"`
  236. EIP150Transition hexutil.Uint64 `json:"eip150Transition"`
  237. EIP160Transition hexutil.Uint64 `json:"eip160Transition"`
  238. EIP161abcTransition hexutil.Uint64 `json:"eip161abcTransition"`
  239. EIP161dTransition hexutil.Uint64 `json:"eip161dTransition"`
  240. EIP155Transition hexutil.Uint64 `json:"eip155Transition"`
  241. EIP140Transition hexutil.Uint64 `json:"eip140Transition"`
  242. EIP211Transition hexutil.Uint64 `json:"eip211Transition"`
  243. EIP214Transition hexutil.Uint64 `json:"eip214Transition"`
  244. EIP658Transition hexutil.Uint64 `json:"eip658Transition"`
  245. EIP145Transition hexutil.Uint64 `json:"eip145Transition"`
  246. EIP1014Transition hexutil.Uint64 `json:"eip1014Transition"`
  247. EIP1052Transition hexutil.Uint64 `json:"eip1052Transition"`
  248. EIP1283Transition hexutil.Uint64 `json:"eip1283Transition"`
  249. EIP1283DisableTransition hexutil.Uint64 `json:"eip1283DisableTransition"`
  250. EIP1283ReenableTransition hexutil.Uint64 `json:"eip1283ReenableTransition"`
  251. EIP1344Transition hexutil.Uint64 `json:"eip1344Transition"`
  252. EIP1884Transition hexutil.Uint64 `json:"eip1884Transition"`
  253. EIP2028Transition hexutil.Uint64 `json:"eip2028Transition"`
  254. } `json:"params"`
  255. Genesis struct {
  256. Seal struct {
  257. Ethereum struct {
  258. Nonce hexutil.Bytes `json:"nonce"`
  259. MixHash hexutil.Bytes `json:"mixHash"`
  260. } `json:"ethereum"`
  261. } `json:"seal"`
  262. Difficulty *hexutil.Big `json:"difficulty"`
  263. Author common.Address `json:"author"`
  264. Timestamp hexutil.Uint64 `json:"timestamp"`
  265. ParentHash common.Hash `json:"parentHash"`
  266. ExtraData hexutil.Bytes `json:"extraData"`
  267. GasLimit hexutil.Uint64 `json:"gasLimit"`
  268. } `json:"genesis"`
  269. Nodes []string `json:"nodes"`
  270. Accounts map[common.UnprefixedAddress]*parityChainSpecAccount `json:"accounts"`
  271. }
  272. // parityChainSpecAccount is the prefunded genesis account and/or precompiled
  273. // contract definition.
  274. type parityChainSpecAccount struct {
  275. Balance math2.HexOrDecimal256 `json:"balance"`
  276. Nonce math2.HexOrDecimal64 `json:"nonce,omitempty"`
  277. Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"`
  278. }
  279. // parityChainSpecBuiltin is the precompiled contract definition.
  280. type parityChainSpecBuiltin struct {
  281. Name string `json:"name"` // Each builtin should has it own name
  282. Pricing interface{} `json:"pricing"` // Each builtin should has it own price strategy
  283. ActivateAt *hexutil.Big `json:"activate_at,omitempty"` // ActivateAt can't be omitted if empty, default means no fork
  284. }
  285. // parityChainSpecPricing represents the different pricing models that builtin
  286. // contracts might advertise using.
  287. type parityChainSpecPricing struct {
  288. Linear *parityChainSpecLinearPricing `json:"linear,omitempty"`
  289. ModExp *parityChainSpecModExpPricing `json:"modexp,omitempty"`
  290. // Before the https://github.com/paritytech/parity-ethereum/pull/11039,
  291. // Parity uses this format to config bn pairing price policy.
  292. AltBnPairing *parityChainSepcAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"`
  293. // Blake2F is the price per round of Blake2 compression
  294. Blake2F *parityChainSpecBlakePricing `json:"blake2_f,omitempty"`
  295. }
  296. type parityChainSpecLinearPricing struct {
  297. Base uint64 `json:"base"`
  298. Word uint64 `json:"word"`
  299. }
  300. type parityChainSpecModExpPricing struct {
  301. Divisor uint64 `json:"divisor"`
  302. }
  303. // parityChainSpecAltBnConstOperationPricing defines the price
  304. // policy for bn const operation(used after istanbul)
  305. type parityChainSpecAltBnConstOperationPricing struct {
  306. Price uint64 `json:"price"`
  307. }
  308. // parityChainSepcAltBnPairingPricing defines the price policy
  309. // for bn pairing.
  310. type parityChainSepcAltBnPairingPricing struct {
  311. Base uint64 `json:"base"`
  312. Pair uint64 `json:"pair"`
  313. }
  314. // parityChainSpecBlakePricing defines the price policy for blake2 f
  315. // compression.
  316. type parityChainSpecBlakePricing struct {
  317. GasPerRound uint64 `json:"gas_per_round"`
  318. }
  319. type parityChainSpecAlternativePrice struct {
  320. AltBnConstOperationPrice *parityChainSpecAltBnConstOperationPricing `json:"alt_bn128_const_operations,omitempty"`
  321. AltBnPairingPrice *parityChainSepcAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"`
  322. }
  323. // parityChainSpecVersionedPricing represents a single version price policy.
  324. type parityChainSpecVersionedPricing struct {
  325. Price *parityChainSpecAlternativePrice `json:"price,omitempty"`
  326. Info string `json:"info,omitempty"`
  327. }
  328. // newParityChainSpec converts a go-ethereum genesis block into a Parity specific
  329. // chain specification format.
  330. func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) {
  331. // Only ethash is currently supported between go-ethereum and Parity
  332. if genesis.Config.Ethash == nil {
  333. return nil, errors.New("unsupported consensus engine")
  334. }
  335. // Reconstruct the chain spec in Parity's format
  336. spec := &parityChainSpec{
  337. Name: network,
  338. Nodes: bootnodes,
  339. Datadir: strings.ToLower(network),
  340. }
  341. spec.Engine.Ethash.Params.BlockReward = make(map[string]string)
  342. spec.Engine.Ethash.Params.DifficultyBombDelays = make(map[string]string)
  343. // Frontier
  344. spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
  345. spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
  346. spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
  347. spec.Engine.Ethash.Params.BlockReward["0x0"] = hexutil.EncodeBig(ethash.FrontierBlockReward)
  348. // Homestead
  349. spec.Engine.Ethash.Params.HomesteadTransition = hexutil.Uint64(genesis.Config.HomesteadBlock.Uint64())
  350. // Tangerine Whistle : 150
  351. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-608.md
  352. spec.Params.EIP150Transition = hexutil.Uint64(genesis.Config.EIP150Block.Uint64())
  353. // Spurious Dragon: 155, 160, 161, 170
  354. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-607.md
  355. spec.Params.EIP155Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
  356. spec.Params.EIP160Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
  357. spec.Params.EIP161abcTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
  358. spec.Params.EIP161dTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
  359. // Byzantium
  360. if num := genesis.Config.ByzantiumBlock; num != nil {
  361. spec.setByzantium(num)
  362. }
  363. // Constantinople
  364. if num := genesis.Config.ConstantinopleBlock; num != nil {
  365. spec.setConstantinople(num)
  366. }
  367. // ConstantinopleFix (remove eip-1283)
  368. if num := genesis.Config.PetersburgBlock; num != nil {
  369. spec.setConstantinopleFix(num)
  370. }
  371. // Istanbul
  372. if num := genesis.Config.IstanbulBlock; num != nil {
  373. spec.setIstanbul(num)
  374. }
  375. spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
  376. spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
  377. spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor)
  378. spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
  379. spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
  380. spec.Params.MaxCodeSize = params.MaxCodeSize
  381. // geth has it set from zero
  382. spec.Params.MaxCodeSizeTransition = 0
  383. // Disable this one
  384. spec.Params.EIP98Transition = math.MaxInt64
  385. spec.Genesis.Seal.Ethereum.Nonce = (hexutil.Bytes)(make([]byte, 8))
  386. binary.LittleEndian.PutUint64(spec.Genesis.Seal.Ethereum.Nonce[:], genesis.Nonce)
  387. spec.Genesis.Seal.Ethereum.MixHash = (hexutil.Bytes)(genesis.Mixhash[:])
  388. spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
  389. spec.Genesis.Author = genesis.Coinbase
  390. spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
  391. spec.Genesis.ParentHash = genesis.ParentHash
  392. spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
  393. spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
  394. spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
  395. for address, account := range genesis.Alloc {
  396. bal := math2.HexOrDecimal256(*account.Balance)
  397. spec.Accounts[common.UnprefixedAddress(address)] = &parityChainSpecAccount{
  398. Balance: bal,
  399. Nonce: math2.HexOrDecimal64(account.Nonce),
  400. }
  401. }
  402. spec.setPrecompile(1, &parityChainSpecBuiltin{Name: "ecrecover",
  403. Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}}})
  404. spec.setPrecompile(2, &parityChainSpecBuiltin{
  405. Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}},
  406. })
  407. spec.setPrecompile(3, &parityChainSpecBuiltin{
  408. Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}},
  409. })
  410. spec.setPrecompile(4, &parityChainSpecBuiltin{
  411. Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}},
  412. })
  413. if genesis.Config.ByzantiumBlock != nil {
  414. spec.setPrecompile(5, &parityChainSpecBuiltin{
  415. Name: "modexp",
  416. ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  417. Pricing: &parityChainSpecPricing{
  418. ModExp: &parityChainSpecModExpPricing{Divisor: 20},
  419. },
  420. })
  421. spec.setPrecompile(6, &parityChainSpecBuiltin{
  422. Name: "alt_bn128_add",
  423. ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  424. Pricing: &parityChainSpecPricing{
  425. Linear: &parityChainSpecLinearPricing{Base: 500, Word: 0},
  426. },
  427. })
  428. spec.setPrecompile(7, &parityChainSpecBuiltin{
  429. Name: "alt_bn128_mul",
  430. ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  431. Pricing: &parityChainSpecPricing{
  432. Linear: &parityChainSpecLinearPricing{Base: 40000, Word: 0},
  433. },
  434. })
  435. spec.setPrecompile(8, &parityChainSpecBuiltin{
  436. Name: "alt_bn128_pairing",
  437. ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  438. Pricing: &parityChainSpecPricing{
  439. AltBnPairing: &parityChainSepcAltBnPairingPricing{Base: 100000, Pair: 80000},
  440. },
  441. })
  442. }
  443. if genesis.Config.IstanbulBlock != nil {
  444. if genesis.Config.ByzantiumBlock == nil {
  445. return nil, errors.New("invalid genesis, istanbul fork is enabled while byzantium is not")
  446. }
  447. spec.setPrecompile(6, &parityChainSpecBuiltin{
  448. Name: "alt_bn128_add",
  449. ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  450. Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{
  451. (*hexutil.Big)(big.NewInt(0)): {
  452. Price: &parityChainSpecAlternativePrice{
  453. AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 500},
  454. },
  455. },
  456. (*hexutil.Big)(genesis.Config.IstanbulBlock): {
  457. Price: &parityChainSpecAlternativePrice{
  458. AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 150},
  459. },
  460. },
  461. },
  462. })
  463. spec.setPrecompile(7, &parityChainSpecBuiltin{
  464. Name: "alt_bn128_mul",
  465. ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  466. Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{
  467. (*hexutil.Big)(big.NewInt(0)): {
  468. Price: &parityChainSpecAlternativePrice{
  469. AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 40000},
  470. },
  471. },
  472. (*hexutil.Big)(genesis.Config.IstanbulBlock): {
  473. Price: &parityChainSpecAlternativePrice{
  474. AltBnConstOperationPrice: &parityChainSpecAltBnConstOperationPricing{Price: 6000},
  475. },
  476. },
  477. },
  478. })
  479. spec.setPrecompile(8, &parityChainSpecBuiltin{
  480. Name: "alt_bn128_pairing",
  481. ActivateAt: (*hexutil.Big)(genesis.Config.ByzantiumBlock),
  482. Pricing: map[*hexutil.Big]*parityChainSpecVersionedPricing{
  483. (*hexutil.Big)(big.NewInt(0)): {
  484. Price: &parityChainSpecAlternativePrice{
  485. AltBnPairingPrice: &parityChainSepcAltBnPairingPricing{Base: 100000, Pair: 80000},
  486. },
  487. },
  488. (*hexutil.Big)(genesis.Config.IstanbulBlock): {
  489. Price: &parityChainSpecAlternativePrice{
  490. AltBnPairingPrice: &parityChainSepcAltBnPairingPricing{Base: 45000, Pair: 34000},
  491. },
  492. },
  493. },
  494. })
  495. spec.setPrecompile(9, &parityChainSpecBuiltin{
  496. Name: "blake2_f",
  497. ActivateAt: (*hexutil.Big)(genesis.Config.IstanbulBlock),
  498. Pricing: &parityChainSpecPricing{
  499. Blake2F: &parityChainSpecBlakePricing{GasPerRound: 1},
  500. },
  501. })
  502. }
  503. return spec, nil
  504. }
  505. func (spec *parityChainSpec) setPrecompile(address byte, data *parityChainSpecBuiltin) {
  506. if spec.Accounts == nil {
  507. spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
  508. }
  509. a := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
  510. if _, exist := spec.Accounts[a]; !exist {
  511. spec.Accounts[a] = &parityChainSpecAccount{}
  512. }
  513. spec.Accounts[a].Builtin = data
  514. }
  515. func (spec *parityChainSpec) setByzantium(num *big.Int) {
  516. spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ByzantiumBlockReward)
  517. spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(3000000)
  518. n := hexutil.Uint64(num.Uint64())
  519. spec.Engine.Ethash.Params.EIP100bTransition = n
  520. spec.Params.EIP140Transition = n
  521. spec.Params.EIP211Transition = n
  522. spec.Params.EIP214Transition = n
  523. spec.Params.EIP658Transition = n
  524. }
  525. func (spec *parityChainSpec) setConstantinople(num *big.Int) {
  526. spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ConstantinopleBlockReward)
  527. spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(2000000)
  528. n := hexutil.Uint64(num.Uint64())
  529. spec.Params.EIP145Transition = n
  530. spec.Params.EIP1014Transition = n
  531. spec.Params.EIP1052Transition = n
  532. spec.Params.EIP1283Transition = n
  533. }
  534. func (spec *parityChainSpec) setConstantinopleFix(num *big.Int) {
  535. spec.Params.EIP1283DisableTransition = hexutil.Uint64(num.Uint64())
  536. }
  537. func (spec *parityChainSpec) setIstanbul(num *big.Int) {
  538. spec.Params.EIP1344Transition = hexutil.Uint64(num.Uint64())
  539. spec.Params.EIP1884Transition = hexutil.Uint64(num.Uint64())
  540. spec.Params.EIP2028Transition = hexutil.Uint64(num.Uint64())
  541. spec.Params.EIP1283ReenableTransition = hexutil.Uint64(num.Uint64())
  542. }
  543. // pyEthereumGenesisSpec represents the genesis specification format used by the
  544. // Python Ethereum implementation.
  545. type pyEthereumGenesisSpec struct {
  546. Nonce hexutil.Bytes `json:"nonce"`
  547. Timestamp hexutil.Uint64 `json:"timestamp"`
  548. ExtraData hexutil.Bytes `json:"extraData"`
  549. GasLimit hexutil.Uint64 `json:"gasLimit"`
  550. Difficulty *hexutil.Big `json:"difficulty"`
  551. Mixhash common.Hash `json:"mixhash"`
  552. Coinbase common.Address `json:"coinbase"`
  553. Alloc core.GenesisAlloc `json:"alloc"`
  554. ParentHash common.Hash `json:"parentHash"`
  555. }
  556. // newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
  557. // chain specification format.
  558. func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) {
  559. // Only ethash is currently supported between go-ethereum and pyethereum
  560. if genesis.Config.Ethash == nil {
  561. return nil, errors.New("unsupported consensus engine")
  562. }
  563. spec := &pyEthereumGenesisSpec{
  564. Timestamp: (hexutil.Uint64)(genesis.Timestamp),
  565. ExtraData: genesis.ExtraData,
  566. GasLimit: (hexutil.Uint64)(genesis.GasLimit),
  567. Difficulty: (*hexutil.Big)(genesis.Difficulty),
  568. Mixhash: genesis.Mixhash,
  569. Coinbase: genesis.Coinbase,
  570. Alloc: genesis.Alloc,
  571. ParentHash: genesis.ParentHash,
  572. }
  573. spec.Nonce = (hexutil.Bytes)(make([]byte, 8))
  574. binary.LittleEndian.PutUint64(spec.Nonce[:], genesis.Nonce)
  575. return spec, nil
  576. }