package neutrino import ( "testing" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/crypto/chainhash" ) // chainOf builds n linked headers whose hashes are the chain a fetch answers // from. Header 0 commits to a zero previous hash. func chainOf(n int) []btcwire.BlockHeader { out := make([]btcwire.BlockHeader, n) var prev chainhash.Hash for i := range out { h := btcwire.BlockHeader{ Version: 1, PrevBlock: prev, Timestamp: uint32(1600000000 + i*600), Bits: 0x207fffff, Nonce: uint32(i), } out[i] = h prev = h.BlockHash() } return out } // chainFetcher is the durable side of a windowed store: it holds every header // and counts what it was asked for. type chainFetcher struct { headers []btcwire.BlockHeader reads int } func (f *chainFetcher) FetchHeader(height uint32) (btcwire.BlockHeader, bool) { f.reads++ if int(height) >= len(f.headers) { return btcwire.BlockHeader{}, false } return f.headers[height], true } func (f *chainFetcher) FetchFilterHeader(uint32) (chainhash.Hash, bool) { return chainhash.Hash{}, false } func (f *chainFetcher) FetchFilterHash(uint32) (chainhash.Hash, bool) { return chainhash.Hash{}, false } // TestWindowRetainsOnlyItsTail is the property the browser store needs: the map // stops growing, the tip is still the true tip, and every height below the // window is answered by the fetcher. func TestWindowRetainsOnlyItsTail(t *testing.T) { chain := chainOf(100) m := NewMemory() f := &chainFetcher{headers: chain} m.SetFetcher(f) m.SetWindow(10) if err := m.SetAnchor(Anchor{Height: 0, BlockHash: chain[0].BlockHash(), FilterHeight: -1}); err != nil { t.Fatalf("anchor: %v", err) } m.PutHeaders(0, chain[:1]) if err := m.PutHeaders(1, chain[1:]); err != nil { t.Fatalf("put: %v", err) } tip, tipHash, ok := m.HeaderTip() if !ok || tip != 99 || tipHash != chain[99].BlockHash() { t.Fatalf("tip = %d %s %v, want 99 %s", tip, tipHash, ok, chain[99].BlockHash()) } m.mu.RLock() retained := len(m.headers) low := m.low m.mu.RUnlock() if retained > 10 { t.Fatalf("retained %d headers, window is 10", retained) } if low != 90 { t.Fatalf("lowest retained height is %d, want 90", low) } for h := uint32(0); h < 100; h++ { got, ok := m.Header(h) if !ok { t.Fatalf("header %d missing", h) } if got.BlockHash() != chain[h].BlockHash() { t.Fatalf("header %d is %s, want %s", h, got.BlockHash(), chain[h].BlockHash()) } } if f.reads == 0 { t.Fatal("no read reached the fetcher") } // A reorg below the window is refused: the fork point is not held any more. if err := m.Truncate(50); err == nil { t.Fatal("truncate below the retained window was accepted") } if err := m.Truncate(95); err != nil { t.Fatalf("truncate inside the window: %v", err) } tip, _, _ = m.HeaderTip() if tip != 94 { t.Fatalf("tip after truncate = %d, want 94", tip) } } // TestAdoptTailInstallsTheTip checks the open path: a store that read only the // newest heights from disk reports the right tip and still answers below it. func TestAdoptTailInstallsTheTip(t *testing.T) { chain := chainOf(50) m := NewMemory() m.SetFetcher(&chainFetcher{headers: chain}) m.SetWindow(8) if err := m.SetAnchor(Anchor{Height: 0, BlockHash: chain[0].BlockHash(), FilterHeight: -1}); err != nil { t.Fatalf("anchor: %v", err) } m.AdoptHeaders(42, chain[42:]) tip, tipHash, ok := m.HeaderTip() if !ok || tip != 49 || tipHash != chain[49].BlockHash() { t.Fatalf("tip = %d %s %v, want 49", tip, tipHash, ok) } for h := uint32(0); h < 50; h++ { if _, ok := m.Header(h); !ok { t.Fatalf("header %d missing", h) } } }