xbt_test.go raw

   1  package chain_test
   2  
   3  import (
   4  	"testing"
   5  
   6  	"git.smesh.lol/nostrino/pkg/chain"
   7  )
   8  
   9  // TestXBTProfileCarriesTheFork pins the fork's three moving parts to the values the node's source
  10  // and a direct peer's chain agree on. A light client that starts with any of them wrong either
  11  // syncs the wrong chain or cannot get past the fork.
  12  func TestXBTProfileCarriesTheFork(t *testing.T) {
  13  	profile, err := chain.ByName("blake")
  14  
  15  	if err != nil {
  16  		t.Fatal(err)
  17  	}
  18  
  19  	if err := profile.Validate(); err != nil {
  20  		t.Fatalf("xbt profile does not validate: %v", err)
  21  	}
  22  
  23  	if profile.HeaderV2Height != 961640 {
  24  		t.Fatalf("fork height %d, want 961640", profile.HeaderV2Height)
  25  	}
  26  
  27  	if profile.TargetShift != 22 {
  28  		t.Fatalf("target shift %d, want 22", profile.TargetShift)
  29  	}
  30  
  31  	if profile.Checkpoint == nil {
  32  		t.Fatal("no checkpoint: a first sync would start at genesis")
  33  	}
  34  
  35  	if profile.Checkpoint.Height != 961631 {
  36  		t.Fatalf("checkpoint height %d, want 961631", profile.Checkpoint.Height)
  37  	}
  38  
  39  	if got, want := profile.Checkpoint.BlockHash.String(),
  40  		"00000000000000000000807f9dc917442a67910426d79ebb2f8aa2149327ce8a"; got != want {
  41  		t.Fatalf("checkpoint block hash %s, want %s", got, want)
  42  	}
  43  
  44  	if got, want := profile.Checkpoint.FilterHeader.String(),
  45  		"323d85f60703ce8b32e56bcaab8ccbe1b121abe7669aed933f557767194bb3f6"; got != want {
  46  		t.Fatalf("checkpoint filter header %s, want %s", got, want)
  47  	}
  48  
  49  	// The fork shares Bitcoin's network parameters: that is why the transport profile is the
  50  	// same and only the consensus above the fork differs.
  51  	btc, err := chain.ByName("btc")
  52  
  53  	if err != nil {
  54  		t.Fatal(err)
  55  	}
  56  
  57  	if profile.Magic != btc.Magic || profile.GenesisHash != btc.GenesisHash {
  58  		t.Fatal("the fork must keep Bitcoin's magic and genesis")
  59  	}
  60  
  61  	if profile.Bech32HRP != btc.Bech32HRP || profile.CoinType != btc.CoinType {
  62  		t.Fatal("the fork must keep Bitcoin's address space")
  63  	}
  64  }
  65  
  66  // TestProfileJSONRoundTripsTheFork: a profile is how the fork reaches a browser wallet or another
  67  // operator, so the fork fields have to survive the file.
  68  func TestProfileJSONRoundTripsTheFork(t *testing.T) {
  69  	profile, err := chain.ByName("blake")
  70  
  71  	if err != nil {
  72  		t.Fatal(err)
  73  	}
  74  
  75  	body, err := chain.MarshalProfile(profile)
  76  
  77  	if err != nil {
  78  		t.Fatal(err)
  79  	}
  80  
  81  	back, err := chain.ParseProfile(body)
  82  
  83  	if err != nil {
  84  		t.Fatal(err)
  85  	}
  86  
  87  	if back.HeaderV2Height != profile.HeaderV2Height || back.TargetShift != profile.TargetShift {
  88  		t.Fatal("the fork fields did not survive the JSON round trip")
  89  	}
  90  
  91  	if back.Checkpoint == nil || back.Checkpoint.FilterHeader != profile.Checkpoint.FilterHeader {
  92  		t.Fatal("the checkpoint did not survive the JSON round trip")
  93  	}
  94  }
  95