package neutrino import ( "path/filepath" "testing" "git.smesh.lol/nostrino/pkg/btcwire" ) // TestFileStoreRoundTripsForkHeaders: the stored bytes must reproduce the header's wire form, or // the reloaded header hashes to a different block. The time offset is the trap: a version 2 // header that uses one stores the offset on the wire and the resolved time in memory. func TestFileStoreRoundTripsForkHeaders(t *testing.T) { dir := t.TempDir() store, err := OpenFileStore(dir) if err != nil { t.Fatal(err) } var h btcwire.BlockHeader version := uint32(btcwire.HeaderV2Flag) | 0x20000000 h.Version = int32(version) h.Timestamp = 1_700_000_000 h.Bits = 0x1d00ffff h.Nonce = 9 h.V2 = &btcwire.HeaderV2{Flags: 4, TimeOffset: 90, Height: 961_640, TxCount: 3} h.V2.XorKey[0] = 0x77 h.PrevBlock[0] = 0x42 if err := store.SetAnchor(Anchor{Height: 961_639, BlockHash: h.PrevBlock, FilterHeight: -1}); err != nil { t.Fatal(err) } if err := store.PutHeaders(961_640, []btcwire.BlockHeader{h}); err != nil { t.Fatal(err) } want := h.BlockHash() if err := store.Close(); err != nil { t.Fatal(err) } reopened, err := OpenFileStore(filepath.Clean(dir)) if err != nil { t.Fatal(err) } defer reopened.Close() back, ok := reopened.Header(961_640) if !ok { t.Fatal("header did not come back") } if back.V2 == nil || back.V2.TimeOffset != 90 || back.V2.Height != 961_640 { t.Fatalf("fork fields changed on reload: %+v", back.V2) } if got := back.BlockHash(); got != want { t.Fatalf("reloaded header hashes to %s, want %s", got, want) } if back.TimeOnWire() != h.TimeOnWire() { t.Fatalf("wire time %d, want %d", back.TimeOnWire(), h.TimeOnWire()) } }