genesis_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. "errors"
  19. "math/big"
  20. "reflect"
  21. "testing"
  22. "github.com/davecgh/go-spew/spew"
  23. "github.com/ethereum/go-ethereum/common"
  24. //"github.com/ethereum/go-ethereum/consensus/ethash"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. //"github.com/ethereum/go-ethereum/core/vm"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/params"
  29. )
  30. func TestDefaultGenesisBlock(t *testing.T) {
  31. block := DefaultGenesisBlock().ToBlock(nil)
  32. if block.Hash() != params.MainnetGenesisHash {
  33. t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
  34. }
  35. block = DefaultRopstenGenesisBlock().ToBlock(nil)
  36. if block.Hash() != params.RopstenGenesisHash {
  37. t.Errorf("wrong ropsten genesis hash, got %v, want %v", block.Hash(), params.RopstenGenesisHash)
  38. }
  39. }
  40. func TestSetupGenesis(t *testing.T) {
  41. // Quorum: customized test cases for quorum
  42. var (
  43. customg = Genesis{
  44. Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3), IsQuorum: true},
  45. Alloc: GenesisAlloc{
  46. {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
  47. },
  48. }
  49. oldcustomg = customg
  50. )
  51. oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2)}
  52. tests := []struct {
  53. name string
  54. fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error)
  55. wantConfig *params.ChainConfig
  56. wantHash common.Hash
  57. wantErr error
  58. }{
  59. {
  60. name: "genesis without ChainConfig",
  61. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  62. return SetupGenesisBlock(db, new(Genesis))
  63. },
  64. wantErr: errGenesisNoConfig,
  65. wantConfig: params.AllEthashProtocolChanges,
  66. },
  67. {
  68. name: "no block in DB, genesis == nil",
  69. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  70. return SetupGenesisBlock(db, nil)
  71. },
  72. wantHash: params.MainnetGenesisHash,
  73. wantConfig: params.MainnetChainConfig,
  74. },
  75. {
  76. name: "mainnet block in DB, genesis == nil",
  77. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  78. DefaultGenesisBlock().MustCommit(db)
  79. return SetupGenesisBlock(db, nil)
  80. },
  81. wantHash: params.MainnetGenesisHash,
  82. wantConfig: params.MainnetChainConfig,
  83. },
  84. {
  85. name: "genesis with incorrect SizeLimit",
  86. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  87. customg.Config.TransactionSizeLimit = 100000
  88. customg.Config.MaxCodeSize = 32
  89. return SetupGenesisBlock(db, &customg)
  90. },
  91. wantErr: errors.New("Genesis transaction size limit must be between 32 and 128"),
  92. wantConfig: customg.Config,
  93. },
  94. {
  95. name: "genesis with incorrect max code size ",
  96. fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
  97. customg.Config.TransactionSizeLimit = 64
  98. customg.Config.MaxCodeSize = 100000
  99. return SetupGenesisBlock(db, &customg)
  100. },
  101. wantErr: errors.New("Genesis max code size must be between 24 and 128"),
  102. wantConfig: customg.Config,
  103. },
  104. }
  105. for _, test := range tests {
  106. db := rawdb.NewMemoryDatabase()
  107. config, hash, err := test.fn(db)
  108. // Check the return values.
  109. if !reflect.DeepEqual(err, test.wantErr) {
  110. spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
  111. t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
  112. }
  113. if !reflect.DeepEqual(config, test.wantConfig) {
  114. t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig)
  115. }
  116. if hash != test.wantHash {
  117. t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
  118. } else if err == nil {
  119. // Check database content.
  120. stored := rawdb.ReadBlock(db, test.wantHash, 0)
  121. if stored.Hash() != test.wantHash {
  122. t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
  123. }
  124. }
  125. }
  126. }
  127. // TestGenesisHashes checks the congruity of default genesis data to corresponding hardcoded genesis hash values.
  128. func TestGenesisHashes(t *testing.T) {
  129. cases := []struct {
  130. genesis *Genesis
  131. hash common.Hash
  132. }{
  133. {
  134. genesis: DefaultGenesisBlock(),
  135. hash: params.MainnetGenesisHash,
  136. },
  137. {
  138. genesis: DefaultGoerliGenesisBlock(),
  139. hash: params.GoerliGenesisHash,
  140. },
  141. {
  142. genesis: DefaultRopstenGenesisBlock(),
  143. hash: params.RopstenGenesisHash,
  144. },
  145. {
  146. genesis: DefaultRinkebyGenesisBlock(),
  147. hash: params.RinkebyGenesisHash,
  148. },
  149. {
  150. genesis: DefaultYoloV3GenesisBlock(),
  151. hash: params.YoloV3GenesisHash,
  152. },
  153. }
  154. for i, c := range cases {
  155. b := c.genesis.MustCommit(rawdb.NewMemoryDatabase())
  156. if got := b.Hash(); got != c.hash {
  157. t.Errorf("case: %d, want: %s, got: %s", i, c.hash.Hex(), got.Hex())
  158. }
  159. }
  160. }