genesis_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 = DefaultTestnetGenesisBlock().ToBlock(nil)
  35. if block.Hash() != params.TestnetGenesisHash {
  36. t.Errorf("wrong testnet genesis hash, got %v, want %v", block.Hash(), params.TestnetGenesisHash)
  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: "mainnet block in DB, genesis == nil",
  76. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  77. DefaultGenesisBlock().MustCommit(db)
  78. return SetupGenesisBlock(db, nil)
  79. },
  80. wantHash: params.MainnetGenesisHash,
  81. wantConfig: params.MainnetChainConfig,
  82. },
  83. {
  84. name: "custom block in DB, genesis == nil",
  85. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  86. customg.MustCommit(db)
  87. return SetupGenesisBlock(db, nil)
  88. },
  89. wantHash: customghash,
  90. wantConfig: customg.Config,
  91. },
  92. {
  93. name: "custom block in DB, genesis == testnet",
  94. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  95. customg.MustCommit(db)
  96. return SetupGenesisBlock(db, DefaultTestnetGenesisBlock())
  97. },
  98. wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash},
  99. wantHash: params.TestnetGenesisHash,
  100. wantConfig: params.TestnetChainConfig,
  101. },
  102. {
  103. name: "compatible config in DB",
  104. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  105. oldcustomg.MustCommit(db)
  106. return SetupGenesisBlock(db, &customg)
  107. },
  108. wantHash: customghash,
  109. wantConfig: customg.Config,
  110. },
  111. {
  112. name: "incompatible config in DB",
  113. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  114. // Commit the 'old' genesis block with Homestead transition at #2.
  115. // Advance to block #4, past the homestead transition block of customg.
  116. genesis := oldcustomg.MustCommit(db)
  117. bc, _ := NewBlockChain(db, nil, oldcustomg.Config, ethash.NewFullFaker(), vm.Config{})
  118. defer bc.Stop()
  119. blocks, _ := GenerateChain(oldcustomg.Config, genesis, ethash.NewFaker(), db, 4, nil)
  120. bc.InsertChain(blocks)
  121. bc.CurrentBlock()
  122. // This should return a compatibility error.
  123. return SetupGenesisBlock(db, &customg)
  124. },
  125. wantHash: customghash,
  126. wantConfig: customg.Config,
  127. wantErr: &params.ConfigCompatError{
  128. What: "Homestead fork block",
  129. StoredConfig: big.NewInt(2),
  130. NewConfig: big.NewInt(3),
  131. RewindTo: 1,
  132. },
  133. },
  134. }
  135. for _, test := range tests {
  136. db := ethdb.NewMemDatabase()
  137. config, hash, err := test.fn(db)
  138. // Check the return values.
  139. if !reflect.DeepEqual(err, test.wantErr) {
  140. spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
  141. t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
  142. }
  143. if !reflect.DeepEqual(config, test.wantConfig) {
  144. t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig)
  145. }
  146. if hash != test.wantHash {
  147. t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
  148. } else if err == nil {
  149. // Check database content.
  150. stored := rawdb.ReadBlock(db, test.wantHash, 0)
  151. if stored.Hash() != test.wantHash {
  152. t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
  153. }
  154. }
  155. }
  156. }