package btcwire import ( _ "embed" "encoding/hex" "encoding/json" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" ) // fixture is real material from the forked chain: consecutive version 2 headers, harvested over // the Nostr tunnel with getheaders from the block the fork's own chainparams names as // defaultAssumeValid. That hash is a fixed point in the source, so the first header's prev is an // absolute anchor, and every following link then tests the proof-of-work hash against the chain // itself. type fixture struct { Source string `json:"source"` AnchorHeight uint32 `json:"anchor_height"` AnchorHash string `json:"anchor_hash"` FirstHeight int32 `json:"first_height"` Headers []string `json:"headers"` } // The fixture is embedded rather than read from disk: the deployed client is built with TinyGo // for wasm, where the test runner has no working directory to read testdata from, and a test that // cannot run in the target build is a test that guards nothing there. // //go:embed testdata/blake-v2-headers.json var blakeFixture []byte func loadFixture(t *testing.T) (fixture, []BlockHeader) { t.Helper() body := blakeFixture var f fixture if err := json.Unmarshal(body, &f); err != nil { t.Fatal(err) } headers := make([]BlockHeader, len(f.Headers)) for i, text := range f.Headers { raw, err := hex.DecodeString(text) if err != nil { t.Fatalf("header %d is not hex: %v", i, err) } if len(raw) != BlockHeaderV2Size { t.Fatalf("header %d is %d bytes, want %d", i, len(raw), BlockHeaderV2Size) } if err := headers[i].DeserializeV2(raw); err != nil { t.Fatalf("header %d: %v", i, err) } } return f, headers } // TestRealHeadersLinkToTheirOwnHashes is the proof that ForkHash is right: the chain's own links // are hashes of the previous header, so a single wrong byte anywhere in the construction breaks // every link at once. func TestRealHeadersLinkToTheirOwnHashes(t *testing.T) { f, headers := loadFixture(t) anchor, err := chainhash.NewHashFromStr(f.AnchorHash) if err != nil { t.Fatal(err) } if headers[0].PrevBlock != *anchor { t.Fatalf("first header links to %s, want the chainparams anchor %s", headers[0].PrevBlock, f.AnchorHash) } for i := range headers { if got, want := headers[i].V2.Height, f.FirstHeight+int32(i); got != want { t.Fatalf("header %d says height %d, want %d", i, got, want) } if i == 0 { continue } hash, err := ForkHash(&headers[i-1]) if err != nil { t.Fatalf("header %d: %v", i-1, err) } if hash != headers[i].PrevBlock { t.Fatalf("header %d hashes to %s, but the next header links to %s", i, hash, headers[i].PrevBlock) } } } // TestRealHeadersMeetTheirTargets: the harvested headers are real blocks, so each hash must sit // under the target its own bits field encodes. This catches a hash that is stable but wrong (for // example a mask applied in the wrong byte order). func TestRealHeadersMeetTheirTargets(t *testing.T) { _, headers := loadFixture(t) for i := range headers { if !headers[i].CheckProofOfWork() { t.Fatalf("header %d fails its own target", i) } } } // TestRealHeaderTimestampOffset: the fork puts time_on_wire on the wire and reconstructs the real // time from the offset flag. A block's timestamp is also the parent's, plus ten minutes at most // on this chain, so a broken reconstruction shows up as a jump. func TestRealHeaderTimestampOffset(t *testing.T) { _, headers := loadFixture(t) for i := 1; i < len(headers); i++ { gap := int64(headers[i].Timestamp) - int64(headers[i-1].Timestamp) if gap <= 0 || gap > 4*3600 { t.Fatalf("header %d timestamp gap is %d seconds, which means the offset was not applied", i, gap) } } }