github.com/btcsuite/btcd@v0.24.0/blockchain/blockindex_test.go (about)

     1  // Copyright (c) 2023 The utreexo developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package blockchain
     6  
     7  import (
     8  	"math/rand"
     9  	"testing"
    10  )
    11  
    12  func TestAncestor(t *testing.T) {
    13  	height := 500_000
    14  	blockNodes := chainedNodes(nil, height)
    15  
    16  	for i, blockNode := range blockNodes {
    17  		// Grab a random node that's a child of this node
    18  		// and try to fetch the current blockNode with Ancestor.
    19  		randNode := blockNodes[rand.Intn(height-i)+i]
    20  		got := randNode.Ancestor(blockNode.height)
    21  
    22  		// See if we got the right one.
    23  		if got.hash != blockNode.hash {
    24  			t.Fatalf("expected ancestor at height %d "+
    25  				"but got a node at height %d",
    26  				blockNode.height, got.height)
    27  		}
    28  
    29  		// Gensis doesn't have ancestors so skip the check below.
    30  		if blockNode.height == 0 {
    31  			continue
    32  		}
    33  
    34  		// The ancestors are deterministic so check that this node's
    35  		// ancestor is the correct one.
    36  		if blockNode.ancestor.height != getAncestorHeight(blockNode.height) {
    37  			t.Fatalf("expected anestor at height %d, but it was at %d",
    38  				getAncestorHeight(blockNode.height),
    39  				blockNode.ancestor.height)
    40  		}
    41  	}
    42  }