fork_fixture_test.go raw

   1  package btcwire
   2  
   3  import (
   4  	_ "embed"
   5  	"encoding/hex"
   6  	"encoding/json"
   7  	"testing"
   8  
   9  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  10  )
  11  
  12  // fixture is real material from the forked chain: consecutive version 2 headers, harvested over
  13  // the Nostr tunnel with getheaders from the block the fork's own chainparams names as
  14  // defaultAssumeValid. That hash is a fixed point in the source, so the first header's prev is an
  15  // absolute anchor, and every following link then tests the proof-of-work hash against the chain
  16  // itself.
  17  type fixture struct {
  18  	Source       string   `json:"source"`
  19  	AnchorHeight uint32   `json:"anchor_height"`
  20  	AnchorHash   string   `json:"anchor_hash"`
  21  	FirstHeight  int32    `json:"first_height"`
  22  	Headers      []string `json:"headers"`
  23  }
  24  
  25  // The fixture is embedded rather than read from disk: the deployed client is built with TinyGo
  26  // for wasm, where the test runner has no working directory to read testdata from, and a test that
  27  // cannot run in the target build is a test that guards nothing there.
  28  //
  29  //go:embed testdata/blake-v2-headers.json
  30  var blakeFixture []byte
  31  
  32  func loadFixture(t *testing.T) (fixture, []BlockHeader) {
  33  	t.Helper()
  34  
  35  	body := blakeFixture
  36  
  37  	var f fixture
  38  
  39  	if err := json.Unmarshal(body, &f); err != nil {
  40  		t.Fatal(err)
  41  	}
  42  
  43  	headers := make([]BlockHeader, len(f.Headers))
  44  
  45  	for i, text := range f.Headers {
  46  		raw, err := hex.DecodeString(text)
  47  
  48  		if err != nil {
  49  			t.Fatalf("header %d is not hex: %v", i, err)
  50  		}
  51  
  52  		if len(raw) != BlockHeaderV2Size {
  53  			t.Fatalf("header %d is %d bytes, want %d", i, len(raw), BlockHeaderV2Size)
  54  		}
  55  
  56  		if err := headers[i].DeserializeV2(raw); err != nil {
  57  			t.Fatalf("header %d: %v", i, err)
  58  		}
  59  	}
  60  
  61  	return f, headers
  62  }
  63  
  64  // TestRealHeadersLinkToTheirOwnHashes is the proof that ForkHash is right: the chain's own links
  65  // are hashes of the previous header, so a single wrong byte anywhere in the construction breaks
  66  // every link at once.
  67  func TestRealHeadersLinkToTheirOwnHashes(t *testing.T) {
  68  	f, headers := loadFixture(t)
  69  
  70  	anchor, err := chainhash.NewHashFromStr(f.AnchorHash)
  71  
  72  	if err != nil {
  73  		t.Fatal(err)
  74  	}
  75  
  76  	if headers[0].PrevBlock != *anchor {
  77  		t.Fatalf("first header links to %s, want the chainparams anchor %s",
  78  			headers[0].PrevBlock, f.AnchorHash)
  79  	}
  80  
  81  	for i := range headers {
  82  		if got, want := headers[i].V2.Height, f.FirstHeight+int32(i); got != want {
  83  			t.Fatalf("header %d says height %d, want %d", i, got, want)
  84  		}
  85  
  86  		if i == 0 {
  87  			continue
  88  		}
  89  
  90  		hash, err := ForkHash(&headers[i-1])
  91  
  92  		if err != nil {
  93  			t.Fatalf("header %d: %v", i-1, err)
  94  		}
  95  
  96  		if hash != headers[i].PrevBlock {
  97  			t.Fatalf("header %d hashes to %s, but the next header links to %s",
  98  				i, hash, headers[i].PrevBlock)
  99  		}
 100  	}
 101  }
 102  
 103  // TestRealHeadersMeetTheirTargets: the harvested headers are real blocks, so each hash must sit
 104  // under the target its own bits field encodes. This catches a hash that is stable but wrong (for
 105  // example a mask applied in the wrong byte order).
 106  func TestRealHeadersMeetTheirTargets(t *testing.T) {
 107  	_, headers := loadFixture(t)
 108  
 109  	for i := range headers {
 110  		if !headers[i].CheckProofOfWork() {
 111  			t.Fatalf("header %d fails its own target", i)
 112  		}
 113  	}
 114  }
 115  
 116  // TestRealHeaderTimestampOffset: the fork puts time_on_wire on the wire and reconstructs the real
 117  // time from the offset flag. A block's timestamp is also the parent's, plus ten minutes at most
 118  // on this chain, so a broken reconstruction shows up as a jump.
 119  func TestRealHeaderTimestampOffset(t *testing.T) {
 120  	_, headers := loadFixture(t)
 121  
 122  	for i := 1; i < len(headers); i++ {
 123  		gap := int64(headers[i].Timestamp) - int64(headers[i-1].Timestamp)
 124  
 125  		if gap <= 0 || gap > 4*3600 {
 126  			t.Fatalf("header %d timestamp gap is %d seconds, which means the offset was not applied",
 127  				i, gap)
 128  		}
 129  	}
 130  }
 131