genesis.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. addr := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
  160. if _, exist := spec.Accounts[addr]; !exist {
  161. spec.Accounts[addr] = &alethGenesisSpecAccount{}
  162. }
  163. spec.Accounts[addr].Precompiled = data
  164. }
  165. func (spec *alethGenesisSpec) setAccount(address common.Address, account core.GenesisAccount) {
  166. if spec.Accounts == nil {
  167. spec.Accounts = make(map[common.UnprefixedAddress]*alethGenesisSpecAccount)
  168. }
  169. a, exist := spec.Accounts[common.UnprefixedAddress(address)]
  170. if !exist {
  171. a = &alethGenesisSpecAccount{}
  172. spec.Accounts[common.UnprefixedAddress(address)] = a
  173. }
  174. a.Balance = (*math2.HexOrDecimal256)(account.Balance)
  175. a.Nonce = account.Nonce
  176. }
  177. func (spec *alethGenesisSpec) setByzantium(num *big.Int) {
  178. spec.Params.ByzantiumForkBlock = hexutil.Uint64(num.Uint64())
  179. }
  180. func (spec *alethGenesisSpec) setConstantinople(num *big.Int) {
  181. spec.Params.ConstantinopleForkBlock = hexutil.Uint64(num.Uint64())
  182. }
  183. // parityChainSpec is the chain specification format used by Parity.
  184. type parityChainSpec struct {
  185. Name string `json:"name"`
  186. Datadir string `json:"dataDir"`
  187. Engine struct {
  188. Ethash struct {
  189. Params struct {
  190. MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"`
  191. DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"`
  192. DurationLimit *hexutil.Big `json:"durationLimit"`
  193. BlockReward map[string]string `json:"blockReward"`
  194. DifficultyBombDelays map[string]string `json:"difficultyBombDelays"`
  195. HomesteadTransition hexutil.Uint64 `json:"homesteadTransition"`
  196. EIP100bTransition hexutil.Uint64 `json:"eip100bTransition"`
  197. } `json:"params"`
  198. } `json:"Ethash"`
  199. } `json:"engine"`
  200. Params struct {
  201. AccountStartNonce hexutil.Uint64 `json:"accountStartNonce"`
  202. MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"`
  203. MinGasLimit hexutil.Uint64 `json:"minGasLimit"`
  204. GasLimitBoundDivisor math2.HexOrDecimal64 `json:"gasLimitBoundDivisor"`
  205. NetworkID hexutil.Uint64 `json:"networkID"`
  206. ChainID hexutil.Uint64 `json:"chainID"`
  207. MaxCodeSize hexutil.Uint64 `json:"maxCodeSize"`
  208. MaxCodeSizeTransition hexutil.Uint64 `json:"maxCodeSizeTransition"`
  209. EIP98Transition hexutil.Uint64 `json:"eip98Transition"`
  210. EIP150Transition hexutil.Uint64 `json:"eip150Transition"`
  211. EIP160Transition hexutil.Uint64 `json:"eip160Transition"`
  212. EIP161abcTransition hexutil.Uint64 `json:"eip161abcTransition"`
  213. EIP161dTransition hexutil.Uint64 `json:"eip161dTransition"`
  214. EIP155Transition hexutil.Uint64 `json:"eip155Transition"`
  215. EIP140Transition hexutil.Uint64 `json:"eip140Transition"`
  216. EIP211Transition hexutil.Uint64 `json:"eip211Transition"`
  217. EIP214Transition hexutil.Uint64 `json:"eip214Transition"`
  218. EIP658Transition hexutil.Uint64 `json:"eip658Transition"`
  219. EIP145Transition hexutil.Uint64 `json:"eip145Transition"`
  220. EIP1014Transition hexutil.Uint64 `json:"eip1014Transition"`
  221. EIP1052Transition hexutil.Uint64 `json:"eip1052Transition"`
  222. EIP1283Transition hexutil.Uint64 `json:"eip1283Transition"`
  223. EIP1283DisableTransition hexutil.Uint64 `json:"eip1283DisableTransition"`
  224. } `json:"params"`
  225. Genesis struct {
  226. Seal struct {
  227. Ethereum struct {
  228. Nonce hexutil.Bytes `json:"nonce"`
  229. MixHash hexutil.Bytes `json:"mixHash"`
  230. } `json:"ethereum"`
  231. } `json:"seal"`
  232. Difficulty *hexutil.Big `json:"difficulty"`
  233. Author common.Address `json:"author"`
  234. Timestamp hexutil.Uint64 `json:"timestamp"`
  235. ParentHash common.Hash `json:"parentHash"`
  236. ExtraData hexutil.Bytes `json:"extraData"`
  237. GasLimit hexutil.Uint64 `json:"gasLimit"`
  238. } `json:"genesis"`
  239. Nodes []string `json:"nodes"`
  240. Accounts map[common.UnprefixedAddress]*parityChainSpecAccount `json:"accounts"`
  241. }
  242. // parityChainSpecAccount is the prefunded genesis account and/or precompiled
  243. // contract definition.
  244. type parityChainSpecAccount struct {
  245. Balance math2.HexOrDecimal256 `json:"balance"`
  246. Nonce math2.HexOrDecimal64 `json:"nonce,omitempty"`
  247. Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"`
  248. }
  249. // parityChainSpecBuiltin is the precompiled contract definition.
  250. type parityChainSpecBuiltin struct {
  251. Name string `json:"name,omitempty"`
  252. ActivateAt math2.HexOrDecimal64 `json:"activate_at,omitempty"`
  253. Pricing *parityChainSpecPricing `json:"pricing,omitempty"`
  254. }
  255. // parityChainSpecPricing represents the different pricing models that builtin
  256. // contracts might advertise using.
  257. type parityChainSpecPricing struct {
  258. Linear *parityChainSpecLinearPricing `json:"linear,omitempty"`
  259. ModExp *parityChainSpecModExpPricing `json:"modexp,omitempty"`
  260. AltBnPairing *parityChainSpecAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"`
  261. }
  262. type parityChainSpecLinearPricing struct {
  263. Base uint64 `json:"base"`
  264. Word uint64 `json:"word"`
  265. }
  266. type parityChainSpecModExpPricing struct {
  267. Divisor uint64 `json:"divisor"`
  268. }
  269. type parityChainSpecAltBnPairingPricing struct {
  270. Base uint64 `json:"base"`
  271. Pair uint64 `json:"pair"`
  272. }
  273. // newParityChainSpec converts a go-ethereum genesis block into a Parity specific
  274. // chain specification format.
  275. func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) {
  276. // Only ethash is currently supported between go-ethereum and Parity
  277. if genesis.Config.Ethash == nil {
  278. return nil, errors.New("unsupported consensus engine")
  279. }
  280. // Reconstruct the chain spec in Parity's format
  281. spec := &parityChainSpec{
  282. Name: network,
  283. Nodes: bootnodes,
  284. Datadir: strings.ToLower(network),
  285. }
  286. spec.Engine.Ethash.Params.BlockReward = make(map[string]string)
  287. spec.Engine.Ethash.Params.DifficultyBombDelays = make(map[string]string)
  288. // Frontier
  289. spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
  290. spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
  291. spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
  292. spec.Engine.Ethash.Params.BlockReward["0x0"] = hexutil.EncodeBig(ethash.FrontierBlockReward)
  293. // Homestead
  294. spec.Engine.Ethash.Params.HomesteadTransition = hexutil.Uint64(genesis.Config.HomesteadBlock.Uint64())
  295. // Tangerine Whistle : 150
  296. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-608.md
  297. spec.Params.EIP150Transition = hexutil.Uint64(genesis.Config.EIP150Block.Uint64())
  298. // Spurious Dragon: 155, 160, 161, 170
  299. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-607.md
  300. spec.Params.EIP155Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
  301. spec.Params.EIP160Transition = hexutil.Uint64(genesis.Config.EIP155Block.Uint64())
  302. spec.Params.EIP161abcTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
  303. spec.Params.EIP161dTransition = hexutil.Uint64(genesis.Config.EIP158Block.Uint64())
  304. // Byzantium
  305. if num := genesis.Config.ByzantiumBlock; num != nil {
  306. spec.setByzantium(num)
  307. }
  308. // Constantinople
  309. if num := genesis.Config.ConstantinopleBlock; num != nil {
  310. spec.setConstantinople(num)
  311. }
  312. // ConstantinopleFix (remove eip-1283)
  313. if num := genesis.Config.PetersburgBlock; num != nil {
  314. spec.setConstantinopleFix(num)
  315. }
  316. spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
  317. spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
  318. spec.Params.GasLimitBoundDivisor = (math2.HexOrDecimal64)(params.GasLimitBoundDivisor)
  319. spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
  320. spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
  321. spec.Params.MaxCodeSize = params.MaxCodeSize
  322. // geth has it set from zero
  323. spec.Params.MaxCodeSizeTransition = 0
  324. // Disable this one
  325. spec.Params.EIP98Transition = math.MaxInt64
  326. spec.Genesis.Seal.Ethereum.Nonce = (hexutil.Bytes)(make([]byte, 8))
  327. binary.LittleEndian.PutUint64(spec.Genesis.Seal.Ethereum.Nonce[:], genesis.Nonce)
  328. spec.Genesis.Seal.Ethereum.MixHash = (hexutil.Bytes)(genesis.Mixhash[:])
  329. spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
  330. spec.Genesis.Author = genesis.Coinbase
  331. spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
  332. spec.Genesis.ParentHash = genesis.ParentHash
  333. spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
  334. spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
  335. spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
  336. for address, account := range genesis.Alloc {
  337. bal := math2.HexOrDecimal256(*account.Balance)
  338. spec.Accounts[common.UnprefixedAddress(address)] = &parityChainSpecAccount{
  339. Balance: bal,
  340. Nonce: math2.HexOrDecimal64(account.Nonce),
  341. }
  342. }
  343. spec.setPrecompile(1, &parityChainSpecBuiltin{Name: "ecrecover",
  344. Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}}})
  345. spec.setPrecompile(2, &parityChainSpecBuiltin{
  346. Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}},
  347. })
  348. spec.setPrecompile(3, &parityChainSpecBuiltin{
  349. Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}},
  350. })
  351. spec.setPrecompile(4, &parityChainSpecBuiltin{
  352. Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}},
  353. })
  354. if genesis.Config.ByzantiumBlock != nil {
  355. blnum := math2.HexOrDecimal64(genesis.Config.ByzantiumBlock.Uint64())
  356. spec.setPrecompile(5, &parityChainSpecBuiltin{
  357. Name: "modexp", ActivateAt: blnum, Pricing: &parityChainSpecPricing{ModExp: &parityChainSpecModExpPricing{Divisor: 20}},
  358. })
  359. spec.setPrecompile(6, &parityChainSpecBuiltin{
  360. Name: "alt_bn128_add", ActivateAt: blnum, Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 500}},
  361. })
  362. spec.setPrecompile(7, &parityChainSpecBuiltin{
  363. Name: "alt_bn128_mul", ActivateAt: blnum, Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 40000}},
  364. })
  365. spec.setPrecompile(8, &parityChainSpecBuiltin{
  366. Name: "alt_bn128_pairing", ActivateAt: blnum, Pricing: &parityChainSpecPricing{AltBnPairing: &parityChainSpecAltBnPairingPricing{Base: 100000, Pair: 80000}},
  367. })
  368. }
  369. return spec, nil
  370. }
  371. func (spec *parityChainSpec) setPrecompile(address byte, data *parityChainSpecBuiltin) {
  372. if spec.Accounts == nil {
  373. spec.Accounts = make(map[common.UnprefixedAddress]*parityChainSpecAccount)
  374. }
  375. a := common.UnprefixedAddress(common.BytesToAddress([]byte{address}))
  376. if _, exist := spec.Accounts[a]; !exist {
  377. spec.Accounts[a] = &parityChainSpecAccount{}
  378. }
  379. spec.Accounts[a].Builtin = data
  380. }
  381. func (spec *parityChainSpec) setByzantium(num *big.Int) {
  382. spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ByzantiumBlockReward)
  383. spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(3000000)
  384. n := hexutil.Uint64(num.Uint64())
  385. spec.Engine.Ethash.Params.EIP100bTransition = n
  386. spec.Params.EIP140Transition = n
  387. spec.Params.EIP211Transition = n
  388. spec.Params.EIP214Transition = n
  389. spec.Params.EIP658Transition = n
  390. }
  391. func (spec *parityChainSpec) setConstantinople(num *big.Int) {
  392. spec.Engine.Ethash.Params.BlockReward[hexutil.EncodeBig(num)] = hexutil.EncodeBig(ethash.ConstantinopleBlockReward)
  393. spec.Engine.Ethash.Params.DifficultyBombDelays[hexutil.EncodeBig(num)] = hexutil.EncodeUint64(2000000)
  394. n := hexutil.Uint64(num.Uint64())
  395. spec.Params.EIP145Transition = n
  396. spec.Params.EIP1014Transition = n
  397. spec.Params.EIP1052Transition = n
  398. spec.Params.EIP1283Transition = n
  399. }
  400. func (spec *parityChainSpec) setConstantinopleFix(num *big.Int) {
  401. spec.Params.EIP1283DisableTransition = hexutil.Uint64(num.Uint64())
  402. }
  403. // pyEthereumGenesisSpec represents the genesis specification format used by the
  404. // Python Ethereum implementation.
  405. type pyEthereumGenesisSpec struct {
  406. Nonce hexutil.Bytes `json:"nonce"`
  407. Timestamp hexutil.Uint64 `json:"timestamp"`
  408. ExtraData hexutil.Bytes `json:"extraData"`
  409. GasLimit hexutil.Uint64 `json:"gasLimit"`
  410. Difficulty *hexutil.Big `json:"difficulty"`
  411. Mixhash common.Hash `json:"mixhash"`
  412. Coinbase common.Address `json:"coinbase"`
  413. Alloc core.GenesisAlloc `json:"alloc"`
  414. ParentHash common.Hash `json:"parentHash"`
  415. }
  416. // newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
  417. // chain specification format.
  418. func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) {
  419. // Only ethash is currently supported between go-ethereum and pyethereum
  420. if genesis.Config.Ethash == nil {
  421. return nil, errors.New("unsupported consensus engine")
  422. }
  423. spec := &pyEthereumGenesisSpec{
  424. Timestamp: (hexutil.Uint64)(genesis.Timestamp),
  425. ExtraData: genesis.ExtraData,
  426. GasLimit: (hexutil.Uint64)(genesis.GasLimit),
  427. Difficulty: (*hexutil.Big)(genesis.Difficulty),
  428. Mixhash: genesis.Mixhash,
  429. Coinbase: genesis.Coinbase,
  430. Alloc: genesis.Alloc,
  431. ParentHash: genesis.ParentHash,
  432. }
  433. spec.Nonce = (hexutil.Bytes)(make([]byte, 8))
  434. binary.LittleEndian.PutUint64(spec.Nonce[:], genesis.Nonce)
  435. return spec, nil
  436. }