genesis.go 20 KB

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