import_test.go raw

   1  package neutrino
   2  
   3  import (
   4  	"context"
   5  	"errors"
   6  	"net"
   7  	"strings"
   8  	"testing"
   9  
  10  	"git.smesh.lol/nostrino/pkg/btcwire"
  11  	"git.smesh.lol/nostrino/pkg/chain"
  12  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  13  )
  14  
  15  // TestAWrongCheckpointIsNamed: a checkpoint that is not on the chain is refused at the block
  16  // above it, and the error names the checkpoint so an operator knows which deployment value to
  17  // fix. The same check protects an import: a backup anchored at the wrong fork fails here.
  18  func TestAWrongCheckpointIsNamed(t *testing.T) {
  19  	profile := chain.Regtest
  20  	store := NewMemory()
  21  
  22  	checkpoint := Anchor{
  23  		Height:       30,
  24  		BlockHash:    chainhash.DoubleHashH([]byte("not this chain's block")),
  25  		FilterHeight: 30,
  26  		FilterHeader: chainhash.DoubleHashH([]byte("nor this filter header")),
  27  	}
  28  
  29  	if err := store.SetAnchor(checkpoint); err != nil {
  30  		t.Fatal(err)
  31  	}
  32  
  33  	client, err := New(Config{Profile: &profile, Store: store,
  34  		Dial: func(ctx context.Context) (net.Conn, error) { return nil, errors.New("no peer") }})
  35  
  36  	if err != nil {
  37  		t.Fatal(err)
  38  	}
  39  
  40  	var header btcwire.BlockHeader
  41  	header.PrevBlock = chainhash.DoubleHashH([]byte("some other parent"))
  42  
  43  	if _, err := client.ApplyHeaders(checkpoint.Height+1, []btcwire.BlockHeader{header}); err == nil {
  44  		t.Fatal("a header that does not link to the checkpoint was accepted")
  45  	} else if !strings.Contains(err.Error(), "checkpoint at 30") {
  46  		t.Fatalf("the error does not name the checkpoint: %v", err)
  47  	}
  48  }
  49