headerchain_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2020 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. "fmt"
  20. "math/big"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/consensus"
  24. "github.com/ethereum/go-ethereum/consensus/ethash"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/params"
  29. )
  30. func verifyUnbrokenCanonchain(hc *HeaderChain) error {
  31. h := hc.CurrentHeader()
  32. for {
  33. canonHash := rawdb.ReadCanonicalHash(hc.chainDb, h.Number.Uint64())
  34. if exp := h.Hash(); canonHash != exp {
  35. return fmt.Errorf("Canon hash chain broken, block %d got %x, expected %x",
  36. h.Number, canonHash[:8], exp[:8])
  37. }
  38. // Verify that we have the TD
  39. if td := rawdb.ReadTd(hc.chainDb, canonHash, h.Number.Uint64()); td == nil {
  40. return fmt.Errorf("Canon TD missing at block %d", h.Number)
  41. }
  42. if h.Number.Uint64() == 0 {
  43. break
  44. }
  45. h = hc.GetHeader(h.ParentHash, h.Number.Uint64()-1)
  46. }
  47. return nil
  48. }
  49. func testInsert(t *testing.T, hc *HeaderChain, chain []*types.Header, wantStatus WriteStatus, wantErr error, forker *ForkChoice) {
  50. t.Helper()
  51. status, err := hc.InsertHeaderChain(chain, time.Now(), forker)
  52. if status != wantStatus {
  53. t.Errorf("wrong write status from InsertHeaderChain: got %v, want %v", status, wantStatus)
  54. }
  55. // Always verify that the header chain is unbroken
  56. if err := verifyUnbrokenCanonchain(hc); err != nil {
  57. t.Fatal(err)
  58. }
  59. if !errors.Is(err, wantErr) {
  60. t.Fatalf("unexpected error from InsertHeaderChain: %v", err)
  61. }
  62. }
  63. // This test checks status reporting of InsertHeaderChain.
  64. func TestHeaderInsertion(t *testing.T) {
  65. var (
  66. db = rawdb.NewMemoryDatabase()
  67. genesis = (&Genesis{BaseFee: big.NewInt(params.InitialBaseFee)}).MustCommit(db)
  68. )
  69. hc, err := NewHeaderChain(db, params.AllEthashProtocolChanges, ethash.NewFaker(), func() bool { return false })
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. // chain A: G->A1->A2...A128
  74. chainA := makeHeaderChain(genesis.Header(), 128, ethash.NewFaker(), db, 10)
  75. // chain B: G->A1->B1...B128
  76. chainB := makeHeaderChain(chainA[0], 128, ethash.NewFaker(), db, 10)
  77. log.Root().SetHandler(log.StdoutHandler)
  78. forker := NewForkChoice(hc, nil)
  79. // Inserting 64 headers on an empty chain, expecting
  80. // 1 callbacks, 1 canon-status, 0 sidestatus,
  81. testInsert(t, hc, chainA[:64], CanonStatTy, nil, forker)
  82. // Inserting 64 identical headers, expecting
  83. // 0 callbacks, 0 canon-status, 0 sidestatus,
  84. testInsert(t, hc, chainA[:64], NonStatTy, nil, forker)
  85. // Inserting the same some old, some new headers
  86. // 1 callbacks, 1 canon, 0 side
  87. testInsert(t, hc, chainA[32:96], CanonStatTy, nil, forker)
  88. // Inserting side blocks, but not overtaking the canon chain
  89. testInsert(t, hc, chainB[0:32], SideStatTy, nil, forker)
  90. // Inserting more side blocks, but we don't have the parent
  91. testInsert(t, hc, chainB[34:36], NonStatTy, consensus.ErrUnknownAncestor, forker)
  92. // Inserting more sideblocks, overtaking the canon chain
  93. testInsert(t, hc, chainB[32:97], CanonStatTy, nil, forker)
  94. // Inserting more A-headers, taking back the canonicality
  95. testInsert(t, hc, chainA[90:100], CanonStatTy, nil, forker)
  96. // And B becomes canon again
  97. testInsert(t, hc, chainB[97:107], CanonStatTy, nil, forker)
  98. // And B becomes even longer
  99. testInsert(t, hc, chainB[107:128], CanonStatTy, nil, forker)
  100. }