genesis.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/hexutil"
  23. "github.com/ethereum/go-ethereum/consensus/ethash"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. // cppEthereumGenesisSpec represents the genesis specification format used by the
  28. // C++ Ethereum implementation.
  29. type cppEthereumGenesisSpec struct {
  30. SealEngine string `json:"sealEngine"`
  31. Params struct {
  32. AccountStartNonce hexutil.Uint64 `json:"accountStartNonce"`
  33. HomesteadForkBlock hexutil.Uint64 `json:"homesteadForkBlock"`
  34. EIP150ForkBlock hexutil.Uint64 `json:"EIP150ForkBlock"`
  35. EIP158ForkBlock hexutil.Uint64 `json:"EIP158ForkBlock"`
  36. ByzantiumForkBlock hexutil.Uint64 `json:"byzantiumForkBlock"`
  37. ConstantinopleForkBlock hexutil.Uint64 `json:"constantinopleForkBlock"`
  38. NetworkID hexutil.Uint64 `json:"networkID"`
  39. ChainID hexutil.Uint64 `json:"chainID"`
  40. MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"`
  41. MinGasLimit hexutil.Uint64 `json:"minGasLimit"`
  42. MaxGasLimit hexutil.Uint64 `json:"maxGasLimit"`
  43. GasLimitBoundDivisor hexutil.Uint64 `json:"gasLimitBoundDivisor"`
  44. MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"`
  45. DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"`
  46. DurationLimit *hexutil.Big `json:"durationLimit"`
  47. BlockReward *hexutil.Big `json:"blockReward"`
  48. } `json:"params"`
  49. Genesis struct {
  50. Nonce hexutil.Bytes `json:"nonce"`
  51. Difficulty *hexutil.Big `json:"difficulty"`
  52. MixHash common.Hash `json:"mixHash"`
  53. Author common.Address `json:"author"`
  54. Timestamp hexutil.Uint64 `json:"timestamp"`
  55. ParentHash common.Hash `json:"parentHash"`
  56. ExtraData hexutil.Bytes `json:"extraData"`
  57. GasLimit hexutil.Uint64 `json:"gasLimit"`
  58. } `json:"genesis"`
  59. Accounts map[common.Address]*cppEthereumGenesisSpecAccount `json:"accounts"`
  60. }
  61. // cppEthereumGenesisSpecAccount is the prefunded genesis account and/or precompiled
  62. // contract definition.
  63. type cppEthereumGenesisSpecAccount struct {
  64. Balance *hexutil.Big `json:"balance"`
  65. Nonce uint64 `json:"nonce,omitempty"`
  66. Precompiled *cppEthereumGenesisSpecBuiltin `json:"precompiled,omitempty"`
  67. }
  68. // cppEthereumGenesisSpecBuiltin is the precompiled contract definition.
  69. type cppEthereumGenesisSpecBuiltin struct {
  70. Name string `json:"name,omitempty"`
  71. StartingBlock hexutil.Uint64 `json:"startingBlock,omitempty"`
  72. Linear *cppEthereumGenesisSpecLinearPricing `json:"linear,omitempty"`
  73. }
  74. type cppEthereumGenesisSpecLinearPricing struct {
  75. Base uint64 `json:"base"`
  76. Word uint64 `json:"word"`
  77. }
  78. // newCppEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
  79. // chain specification format.
  80. func newCppEthereumGenesisSpec(network string, genesis *core.Genesis) (*cppEthereumGenesisSpec, error) {
  81. // Only ethash is currently supported between go-ethereum and cpp-ethereum
  82. if genesis.Config.Ethash == nil {
  83. return nil, errors.New("unsupported consensus engine")
  84. }
  85. // Reconstruct the chain spec in Parity's format
  86. spec := &cppEthereumGenesisSpec{
  87. SealEngine: "Ethash",
  88. }
  89. spec.Params.AccountStartNonce = 0
  90. spec.Params.HomesteadForkBlock = (hexutil.Uint64)(genesis.Config.HomesteadBlock.Uint64())
  91. spec.Params.EIP150ForkBlock = (hexutil.Uint64)(genesis.Config.EIP150Block.Uint64())
  92. spec.Params.EIP158ForkBlock = (hexutil.Uint64)(genesis.Config.EIP158Block.Uint64())
  93. spec.Params.ByzantiumForkBlock = (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64())
  94. spec.Params.ConstantinopleForkBlock = (hexutil.Uint64)(math.MaxUint64)
  95. spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainId.Uint64())
  96. spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainId.Uint64())
  97. spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
  98. spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
  99. spec.Params.MaxGasLimit = (hexutil.Uint64)(math.MaxUint64)
  100. spec.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
  101. spec.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
  102. spec.Params.GasLimitBoundDivisor = (hexutil.Uint64)(params.GasLimitBoundDivisor)
  103. spec.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
  104. spec.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward)
  105. spec.Genesis.Nonce = (hexutil.Bytes)(make([]byte, 8))
  106. binary.LittleEndian.PutUint64(spec.Genesis.Nonce[:], genesis.Nonce)
  107. spec.Genesis.MixHash = genesis.Mixhash
  108. spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
  109. spec.Genesis.Author = genesis.Coinbase
  110. spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
  111. spec.Genesis.ParentHash = genesis.ParentHash
  112. spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
  113. spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
  114. spec.Accounts = make(map[common.Address]*cppEthereumGenesisSpecAccount)
  115. for address, account := range genesis.Alloc {
  116. spec.Accounts[address] = &cppEthereumGenesisSpecAccount{
  117. Balance: (*hexutil.Big)(account.Balance),
  118. Nonce: account.Nonce,
  119. }
  120. }
  121. spec.Accounts[common.BytesToAddress([]byte{1})].Precompiled = &cppEthereumGenesisSpecBuiltin{
  122. Name: "ecrecover", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 3000},
  123. }
  124. spec.Accounts[common.BytesToAddress([]byte{2})].Precompiled = &cppEthereumGenesisSpecBuiltin{
  125. Name: "sha256", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 60, Word: 12},
  126. }
  127. spec.Accounts[common.BytesToAddress([]byte{3})].Precompiled = &cppEthereumGenesisSpecBuiltin{
  128. Name: "ripemd160", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 600, Word: 120},
  129. }
  130. spec.Accounts[common.BytesToAddress([]byte{4})].Precompiled = &cppEthereumGenesisSpecBuiltin{
  131. Name: "identity", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 15, Word: 3},
  132. }
  133. if genesis.Config.ByzantiumBlock != nil {
  134. spec.Accounts[common.BytesToAddress([]byte{5})].Precompiled = &cppEthereumGenesisSpecBuiltin{
  135. Name: "modexp", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()),
  136. }
  137. spec.Accounts[common.BytesToAddress([]byte{6})].Precompiled = &cppEthereumGenesisSpecBuiltin{
  138. Name: "alt_bn128_G1_add", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), Linear: &cppEthereumGenesisSpecLinearPricing{Base: 500},
  139. }
  140. spec.Accounts[common.BytesToAddress([]byte{7})].Precompiled = &cppEthereumGenesisSpecBuiltin{
  141. Name: "alt_bn128_G1_mul", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), Linear: &cppEthereumGenesisSpecLinearPricing{Base: 40000},
  142. }
  143. spec.Accounts[common.BytesToAddress([]byte{8})].Precompiled = &cppEthereumGenesisSpecBuiltin{
  144. Name: "alt_bn128_pairing_product", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()),
  145. }
  146. }
  147. return spec, nil
  148. }
  149. // parityChainSpec is the chain specification format used by Parity.
  150. type parityChainSpec struct {
  151. Name string `json:"name"`
  152. Engine struct {
  153. Ethash struct {
  154. Params struct {
  155. MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"`
  156. DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"`
  157. DurationLimit *hexutil.Big `json:"durationLimit"`
  158. BlockReward *hexutil.Big `json:"blockReward"`
  159. HomesteadTransition uint64 `json:"homesteadTransition"`
  160. EIP150Transition uint64 `json:"eip150Transition"`
  161. EIP160Transition uint64 `json:"eip160Transition"`
  162. EIP161abcTransition uint64 `json:"eip161abcTransition"`
  163. EIP161dTransition uint64 `json:"eip161dTransition"`
  164. EIP649Reward *hexutil.Big `json:"eip649Reward"`
  165. EIP100bTransition uint64 `json:"eip100bTransition"`
  166. EIP649Transition uint64 `json:"eip649Transition"`
  167. } `json:"params"`
  168. } `json:"Ethash"`
  169. } `json:"engine"`
  170. Params struct {
  171. MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"`
  172. MinGasLimit hexutil.Uint64 `json:"minGasLimit"`
  173. GasLimitBoundDivisor hexutil.Uint64 `json:"gasLimitBoundDivisor"`
  174. NetworkID hexutil.Uint64 `json:"networkID"`
  175. MaxCodeSize uint64 `json:"maxCodeSize"`
  176. EIP155Transition uint64 `json:"eip155Transition"`
  177. EIP98Transition uint64 `json:"eip98Transition"`
  178. EIP86Transition uint64 `json:"eip86Transition"`
  179. EIP140Transition uint64 `json:"eip140Transition"`
  180. EIP211Transition uint64 `json:"eip211Transition"`
  181. EIP214Transition uint64 `json:"eip214Transition"`
  182. EIP658Transition uint64 `json:"eip658Transition"`
  183. } `json:"params"`
  184. Genesis struct {
  185. Seal struct {
  186. Ethereum struct {
  187. Nonce hexutil.Bytes `json:"nonce"`
  188. MixHash hexutil.Bytes `json:"mixHash"`
  189. } `json:"ethereum"`
  190. } `json:"seal"`
  191. Difficulty *hexutil.Big `json:"difficulty"`
  192. Author common.Address `json:"author"`
  193. Timestamp hexutil.Uint64 `json:"timestamp"`
  194. ParentHash common.Hash `json:"parentHash"`
  195. ExtraData hexutil.Bytes `json:"extraData"`
  196. GasLimit hexutil.Uint64 `json:"gasLimit"`
  197. } `json:"genesis"`
  198. Nodes []string `json:"nodes"`
  199. Accounts map[common.Address]*parityChainSpecAccount `json:"accounts"`
  200. }
  201. // parityChainSpecAccount is the prefunded genesis account and/or precompiled
  202. // contract definition.
  203. type parityChainSpecAccount struct {
  204. Balance *hexutil.Big `json:"balance"`
  205. Nonce uint64 `json:"nonce,omitempty"`
  206. Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"`
  207. }
  208. // parityChainSpecBuiltin is the precompiled contract definition.
  209. type parityChainSpecBuiltin struct {
  210. Name string `json:"name,omitempty"`
  211. ActivateAt uint64 `json:"activate_at,omitempty"`
  212. Pricing *parityChainSpecPricing `json:"pricing,omitempty"`
  213. }
  214. // parityChainSpecPricing represents the different pricing models that builtin
  215. // contracts might advertise using.
  216. type parityChainSpecPricing struct {
  217. Linear *parityChainSpecLinearPricing `json:"linear,omitempty"`
  218. ModExp *parityChainSpecModExpPricing `json:"modexp,omitempty"`
  219. AltBnPairing *parityChainSpecAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"`
  220. }
  221. type parityChainSpecLinearPricing struct {
  222. Base uint64 `json:"base"`
  223. Word uint64 `json:"word"`
  224. }
  225. type parityChainSpecModExpPricing struct {
  226. Divisor uint64 `json:"divisor"`
  227. }
  228. type parityChainSpecAltBnPairingPricing struct {
  229. Base uint64 `json:"base"`
  230. Pair uint64 `json:"pair"`
  231. }
  232. // newParityChainSpec converts a go-ethereum genesis block into a Parity specific
  233. // chain specification format.
  234. func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) {
  235. // Only ethash is currently supported between go-ethereum and Parity
  236. if genesis.Config.Ethash == nil {
  237. return nil, errors.New("unsupported consensus engine")
  238. }
  239. // Reconstruct the chain spec in Parity's format
  240. spec := &parityChainSpec{
  241. Name: network,
  242. Nodes: bootnodes,
  243. }
  244. spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
  245. spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
  246. spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
  247. spec.Engine.Ethash.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward)
  248. spec.Engine.Ethash.Params.HomesteadTransition = genesis.Config.HomesteadBlock.Uint64()
  249. spec.Engine.Ethash.Params.EIP150Transition = genesis.Config.EIP150Block.Uint64()
  250. spec.Engine.Ethash.Params.EIP160Transition = genesis.Config.EIP155Block.Uint64()
  251. spec.Engine.Ethash.Params.EIP161abcTransition = genesis.Config.EIP158Block.Uint64()
  252. spec.Engine.Ethash.Params.EIP161dTransition = genesis.Config.EIP158Block.Uint64()
  253. spec.Engine.Ethash.Params.EIP649Reward = (*hexutil.Big)(ethash.ByzantiumBlockReward)
  254. spec.Engine.Ethash.Params.EIP100bTransition = genesis.Config.ByzantiumBlock.Uint64()
  255. spec.Engine.Ethash.Params.EIP649Transition = genesis.Config.ByzantiumBlock.Uint64()
  256. spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
  257. spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
  258. spec.Params.GasLimitBoundDivisor = (hexutil.Uint64)(params.GasLimitBoundDivisor)
  259. spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainId.Uint64())
  260. spec.Params.MaxCodeSize = params.MaxCodeSize
  261. spec.Params.EIP155Transition = genesis.Config.EIP155Block.Uint64()
  262. spec.Params.EIP98Transition = math.MaxUint64
  263. spec.Params.EIP86Transition = math.MaxUint64
  264. spec.Params.EIP140Transition = genesis.Config.ByzantiumBlock.Uint64()
  265. spec.Params.EIP211Transition = genesis.Config.ByzantiumBlock.Uint64()
  266. spec.Params.EIP214Transition = genesis.Config.ByzantiumBlock.Uint64()
  267. spec.Params.EIP658Transition = genesis.Config.ByzantiumBlock.Uint64()
  268. spec.Genesis.Seal.Ethereum.Nonce = (hexutil.Bytes)(make([]byte, 8))
  269. binary.LittleEndian.PutUint64(spec.Genesis.Seal.Ethereum.Nonce[:], genesis.Nonce)
  270. spec.Genesis.Seal.Ethereum.MixHash = (hexutil.Bytes)(genesis.Mixhash[:])
  271. spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
  272. spec.Genesis.Author = genesis.Coinbase
  273. spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
  274. spec.Genesis.ParentHash = genesis.ParentHash
  275. spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
  276. spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)
  277. spec.Accounts = make(map[common.Address]*parityChainSpecAccount)
  278. for address, account := range genesis.Alloc {
  279. spec.Accounts[address] = &parityChainSpecAccount{
  280. Balance: (*hexutil.Big)(account.Balance),
  281. Nonce: account.Nonce,
  282. }
  283. }
  284. spec.Accounts[common.BytesToAddress([]byte{1})].Builtin = &parityChainSpecBuiltin{
  285. Name: "ecrecover", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}},
  286. }
  287. spec.Accounts[common.BytesToAddress([]byte{2})].Builtin = &parityChainSpecBuiltin{
  288. Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}},
  289. }
  290. spec.Accounts[common.BytesToAddress([]byte{3})].Builtin = &parityChainSpecBuiltin{
  291. Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}},
  292. }
  293. spec.Accounts[common.BytesToAddress([]byte{4})].Builtin = &parityChainSpecBuiltin{
  294. Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}},
  295. }
  296. if genesis.Config.ByzantiumBlock != nil {
  297. spec.Accounts[common.BytesToAddress([]byte{5})].Builtin = &parityChainSpecBuiltin{
  298. Name: "modexp", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{ModExp: &parityChainSpecModExpPricing{Divisor: 20}},
  299. }
  300. spec.Accounts[common.BytesToAddress([]byte{6})].Builtin = &parityChainSpecBuiltin{
  301. Name: "alt_bn128_add", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 500}},
  302. }
  303. spec.Accounts[common.BytesToAddress([]byte{7})].Builtin = &parityChainSpecBuiltin{
  304. Name: "alt_bn128_mul", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 40000}},
  305. }
  306. spec.Accounts[common.BytesToAddress([]byte{8})].Builtin = &parityChainSpecBuiltin{
  307. Name: "alt_bn128_pairing", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{AltBnPairing: &parityChainSpecAltBnPairingPricing{Base: 100000, Pair: 80000}},
  308. }
  309. }
  310. return spec, nil
  311. }
  312. // pyEthereumGenesisSpec represents the genesis specification format used by the
  313. // Python Ethereum implementation.
  314. type pyEthereumGenesisSpec struct {
  315. Nonce hexutil.Bytes `json:"nonce"`
  316. Timestamp hexutil.Uint64 `json:"timestamp"`
  317. ExtraData hexutil.Bytes `json:"extraData"`
  318. GasLimit hexutil.Uint64 `json:"gasLimit"`
  319. Difficulty *hexutil.Big `json:"difficulty"`
  320. Mixhash common.Hash `json:"mixhash"`
  321. Coinbase common.Address `json:"coinbase"`
  322. Alloc core.GenesisAlloc `json:"alloc"`
  323. ParentHash common.Hash `json:"parentHash"`
  324. }
  325. // newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
  326. // chain specification format.
  327. func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) {
  328. // Only ethash is currently supported between go-ethereum and pyethereum
  329. if genesis.Config.Ethash == nil {
  330. return nil, errors.New("unsupported consensus engine")
  331. }
  332. spec := &pyEthereumGenesisSpec{
  333. Timestamp: (hexutil.Uint64)(genesis.Timestamp),
  334. ExtraData: genesis.ExtraData,
  335. GasLimit: (hexutil.Uint64)(genesis.GasLimit),
  336. Difficulty: (*hexutil.Big)(genesis.Difficulty),
  337. Mixhash: genesis.Mixhash,
  338. Coinbase: genesis.Coinbase,
  339. Alloc: genesis.Alloc,
  340. ParentHash: genesis.ParentHash,
  341. }
  342. spec.Nonce = (hexutil.Bytes)(make([]byte, 8))
  343. binary.LittleEndian.PutUint64(spec.Nonce[:], genesis.Nonce)
  344. return spec, nil
  345. }