github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/core/headerchain_test.go (about)

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