genesis_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "math/big"
  19. "reflect"
  20. "testing"
  21. "github.com/davecgh/go-spew/spew"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/consensus/ethash"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/ethdb"
  27. "github.com/ethereum/go-ethereum/params"
  28. )
  29. func TestDefaultGenesisBlock(t *testing.T) {
  30. block := DefaultGenesisBlock().ToBlock(nil)
  31. if block.Hash() != params.MainnetGenesisHash {
  32. t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
  33. }
  34. block = DefaultRopstenGenesisBlock().ToBlock(nil)
  35. if block.Hash() != params.RopstenGenesisHash {
  36. t.Errorf("wrong ropsten genesis hash, got %v, want %v", block.Hash(), params.RopstenGenesisHash)
  37. }
  38. }
  39. func TestSetupGenesis(t *testing.T) {
  40. var (
  41. customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50")
  42. customg = Genesis{
  43. Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3)},
  44. Alloc: GenesisAlloc{
  45. {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
  46. },
  47. }
  48. oldcustomg = customg
  49. )
  50. oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
  51. tests := []struct {
  52. name string
  53. fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error)
  54. wantConfig *params.ChainConfig
  55. wantHash common.Hash
  56. wantErr error
  57. }{
  58. {
  59. name: "genesis without ChainConfig",
  60. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  61. return SetupGenesisBlock(db, new(Genesis))
  62. },
  63. wantErr: errGenesisNoConfig,
  64. wantConfig: params.AllEthashProtocolChanges,
  65. },
  66. {
  67. name: "no block in DB, genesis == nil",
  68. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  69. return SetupGenesisBlock(db, nil)
  70. },
  71. wantHash: params.MainnetGenesisHash,
  72. wantConfig: params.MainnetChainConfig,
  73. },
  74. {
  75. name: "custom block in DB, genesis == nil",
  76. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  77. customg.MustCommit(db)
  78. return SetupGenesisBlock(db, nil)
  79. },
  80. wantHash: customghash,
  81. wantConfig: customg.Config,
  82. },
  83. {
  84. name: "custom block in DB, genesis == ropsten",
  85. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  86. customg.MustCommit(db)
  87. return SetupGenesisBlock(db, DefaultRopstenGenesisBlock())
  88. },
  89. wantErr: &GenesisMismatchError{Stored: customghash, New: params.RopstenGenesisHash},
  90. wantHash: params.RopstenGenesisHash,
  91. wantConfig: params.RopstenChainConfig,
  92. },
  93. {
  94. name: "compatible config in DB",
  95. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  96. oldcustomg.MustCommit(db)
  97. return SetupGenesisBlock(db, &customg)
  98. },
  99. wantHash: customghash,
  100. wantConfig: customg.Config,
  101. },
  102. {
  103. name: "incompatible config in DB",
  104. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  105. // Commit the 'old' genesis block with Homestead transition at #2.
  106. // Advance to block #4, past the homestead transition block of customg.
  107. genesis := oldcustomg.MustCommit(db)
  108. bc, _ := NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{}, nil, nil)
  109. defer bc.Stop()
  110. blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil)
  111. bc.InsertChain(blocks)
  112. bc.CurrentBlock()
  113. // This should return a compatibility error.
  114. return SetupGenesisBlock(db, &customg)
  115. },
  116. wantHash: customghash,
  117. wantConfig: customg.Config,
  118. wantErr: &params.ConfigCompatError{
  119. What: "Homestead fork block",
  120. StoredConfig: big.NewInt(2),
  121. NewConfig: big.NewInt(3),
  122. RewindTo: 1,
  123. },
  124. },
  125. }
  126. for _, test := range tests {
  127. db := rawdb.NewMemoryDatabase()
  128. config, hash, err := test.fn(db)
  129. // Check the return values.
  130. if !reflect.DeepEqual(err, test.wantErr) {
  131. spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
  132. t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
  133. }
  134. if !reflect.DeepEqual(config, test.wantConfig) {
  135. t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig)
  136. }
  137. if hash != test.wantHash {
  138. t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
  139. } else if err == nil {
  140. // Check database content.
  141. stored := rawdb.ReadBlock(db, test.wantHash, 0)
  142. if stored.Hash() != test.wantHash {
  143. t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
  144. }
  145. }
  146. }
  147. }
  148. // TestGenesisHashes checks the congruity of default genesis data to corresponding hardcoded genesis hash values.
  149. func TestGenesisHashes(t *testing.T) {
  150. cases := []struct {
  151. genesis *Genesis
  152. hash common.Hash
  153. }{
  154. {
  155. genesis: DefaultGenesisBlock(),
  156. hash: params.MainnetGenesisHash,
  157. },
  158. {
  159. genesis: DefaultGoerliGenesisBlock(),
  160. hash: params.GoerliGenesisHash,
  161. },
  162. {
  163. genesis: DefaultRopstenGenesisBlock(),
  164. hash: params.RopstenGenesisHash,
  165. },
  166. {
  167. genesis: DefaultRinkebyGenesisBlock(),
  168. hash: params.RinkebyGenesisHash,
  169. },
  170. {
  171. genesis: DefaultYoloV3GenesisBlock(),
  172. hash: params.YoloV3GenesisHash,
  173. },
  174. }
  175. for i, c := range cases {
  176. b := c.genesis.MustCommit(rawdb.NewMemoryDatabase())
  177. if got := b.Hash(); got != c.hash {
  178. t.Errorf("case: %d, want: %s, got: %s", i, c.hash.Hex(), got.Hex())
  179. }
  180. }
  181. }