github.com/theQRL/go-zond@v0.2.1/core/headerchain_test.go (about)

     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  
    17  package core
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"math/big"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/theQRL/go-zond/consensus/beacon"
    27  	"github.com/theQRL/go-zond/core/rawdb"
    28  	"github.com/theQRL/go-zond/core/types"
    29  	"github.com/theQRL/go-zond/params"
    30  	"github.com/theQRL/go-zond/trie"
    31  )
    32  
    33  func verifyUnbrokenCanonchain(hc *HeaderChain) error {
    34  	h := hc.CurrentHeader()
    35  	for {
    36  		canonHash := rawdb.ReadCanonicalHash(hc.chainDb, h.Number.Uint64())
    37  		if exp := h.Hash(); canonHash != exp {
    38  			return fmt.Errorf("Canon hash chain broken, block %d got %x, expected %x",
    39  				h.Number, canonHash[:8], exp[:8])
    40  		}
    41  		if h.Number.Uint64() == 0 {
    42  			break
    43  		}
    44  		h = hc.GetHeader(h.ParentHash, h.Number.Uint64()-1)
    45  	}
    46  	return nil
    47  }
    48  
    49  func testInsert(t *testing.T, hc *HeaderChain, chain []*types.Header, wantStatus WriteStatus, wantErr error) {
    50  	t.Helper()
    51  
    52  	status, err := hc.InsertHeaderChain(chain, time.Now())
    53  	if status != wantStatus {
    54  		t.Errorf("wrong write status from InsertHeaderChain: got %v, want %v", status, wantStatus)
    55  	}
    56  	// Always verify that the header chain is unbroken
    57  	if err := verifyUnbrokenCanonchain(hc); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if !errors.Is(err, wantErr) {
    61  		t.Fatalf("unexpected error from InsertHeaderChain: %v", err)
    62  	}
    63  }
    64  
    65  /*
    66  // This test checks status reporting of InsertHeaderChain.
    67  func TestHeaderInsertion(t *testing.T) {
    68  	var (
    69  		db    = rawdb.NewMemoryDatabase()
    70  		gspec = &Genesis{BaseFee: big.NewInt(params.InitialBaseFee), Config: params.AllBeaconProtocolChanges}
    71  	)
    72  	gspec.Commit(db, trie.NewDatabase(db, nil))
    73  	hc, err := NewHeaderChain(db, gspec.Config, beacon.NewFaker(), func() bool { return false })
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	// chain A: G->A1->A2...A128
    78  	genDb, chainA := makeHeaderChainWithGenesis(gspec, 128, beacon.NewFaker(), 10)
    79  	// chain B: G->A1->B1...B128
    80  	chainB := makeHeaderChain(gspec.Config, chainA[0], 128, beacon.NewFaker(), genDb, 10)
    81  
    82  	forker := NewForkChoice(hc, nil)
    83  	// Inserting 64 headers on an empty chain, expecting
    84  	// 1 callbacks, 1 canon-status, 0 sidestatus,
    85  	testInsert(t, hc, chainA[:64], CanonStatTy, nil, forker)
    86  
    87  	// Inserting 64 identical headers, expecting
    88  	// 0 callbacks, 0 canon-status, 0 sidestatus,
    89  	testInsert(t, hc, chainA[:64], NonStatTy, nil, forker)
    90  
    91  	// Inserting the same some old, some new headers
    92  	// 1 callbacks, 1 canon, 0 side
    93  	testInsert(t, hc, chainA[32:96], CanonStatTy, nil, forker)
    94  
    95  	// Inserting side blocks, but not overtaking the canon chain
    96  	testInsert(t, hc, chainB[0:32], SideStatTy, nil, forker)
    97  
    98  	// Inserting more side blocks, but we don't have the parent
    99  	testInsert(t, hc, chainB[34:36], NonStatTy, consensus.ErrUnknownAncestor, forker)
   100  
   101  	// Inserting more sideblocks, overtaking the canon chain
   102  	testInsert(t, hc, chainB[32:97], CanonStatTy, nil, forker)
   103  
   104  	// Inserting more A-headers, taking back the canonicality
   105  	testInsert(t, hc, chainA[90:100], CanonStatTy, nil, forker)
   106  
   107  	// And B becomes canon again
   108  	testInsert(t, hc, chainB[97:107], CanonStatTy, nil, forker)
   109  
   110  	// And B becomes even longer
   111  	testInsert(t, hc, chainB[107:128], CanonStatTy, nil, forker)
   112  }
   113  */
   114  
   115  // NOTE(rgeraldes24): the original test above is no longer valid
   116  // This test checks status reporting of InsertHeaderChain.
   117  func TestHeaderInsertion(t *testing.T) {
   118  	var (
   119  		db    = rawdb.NewMemoryDatabase()
   120  		gspec = &Genesis{BaseFee: big.NewInt(params.InitialBaseFee), Config: params.AllBeaconProtocolChanges}
   121  	)
   122  	gspec.Commit(db, trie.NewDatabase(db, nil))
   123  	hc, err := NewHeaderChain(db, gspec.Config, beacon.NewFaker(), func() bool { return false })
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	// chain A: G->A1->A2...A128
   128  	_, chainA := makeHeaderChainWithGenesis(gspec, 128, beacon.NewFaker(), 10)
   129  
   130  	// Inserting 64 headers on an empty chain, expecting
   131  	// 1 callbacks, 1 canon-status, 0 sidestatus,
   132  	testInsert(t, hc, chainA[:64], CanonStatTy, nil)
   133  
   134  	// Inserting 64 identical headers, expecting
   135  	// 0 callbacks, 0 canon-status, 0 sidestatus,
   136  	testInsert(t, hc, chainA[:64], NonStatTy, nil)
   137  
   138  	// Inserting the same some old, some new headers
   139  	// 1 callbacks, 1 canon, 0 side
   140  	testInsert(t, hc, chainA[32:96], CanonStatTy, nil)
   141  
   142  	// Inserting more A-headers, taking back the canonicality
   143  	testInsert(t, hc, chainA[90:100], CanonStatTy, nil)
   144  }