genesis.go 26 KB

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