window_test.go raw
1 package neutrino
2
3 import (
4 "testing"
5
6 "git.smesh.lol/nostrino/pkg/btcwire"
7 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
8 )
9
10 // chainOf builds n linked headers whose hashes are the chain a fetch answers
11 // from. Header 0 commits to a zero previous hash.
12 func chainOf(n int) []btcwire.BlockHeader {
13 out := make([]btcwire.BlockHeader, n)
14 var prev chainhash.Hash
15 for i := range out {
16 h := btcwire.BlockHeader{
17 Version: 1,
18 PrevBlock: prev,
19 Timestamp: uint32(1600000000 + i*600),
20 Bits: 0x207fffff,
21 Nonce: uint32(i),
22 }
23 out[i] = h
24 prev = h.BlockHash()
25 }
26 return out
27 }
28
29 // chainFetcher is the durable side of a windowed store: it holds every header
30 // and counts what it was asked for.
31 type chainFetcher struct {
32 headers []btcwire.BlockHeader
33 reads int
34 }
35
36 func (f *chainFetcher) FetchHeader(height uint32) (btcwire.BlockHeader, bool) {
37 f.reads++
38 if int(height) >= len(f.headers) {
39 return btcwire.BlockHeader{}, false
40 }
41 return f.headers[height], true
42 }
43
44 func (f *chainFetcher) FetchFilterHeader(uint32) (chainhash.Hash, bool) {
45 return chainhash.Hash{}, false
46 }
47
48 func (f *chainFetcher) FetchFilterHash(uint32) (chainhash.Hash, bool) {
49 return chainhash.Hash{}, false
50 }
51
52 // TestWindowRetainsOnlyItsTail is the property the browser store needs: the map
53 // stops growing, the tip is still the true tip, and every height below the
54 // window is answered by the fetcher.
55 func TestWindowRetainsOnlyItsTail(t *testing.T) {
56 chain := chainOf(100)
57 m := NewMemory()
58 f := &chainFetcher{headers: chain}
59 m.SetFetcher(f)
60 m.SetWindow(10)
61
62 if err := m.SetAnchor(Anchor{Height: 0, BlockHash: chain[0].BlockHash(), FilterHeight: -1}); err != nil {
63 t.Fatalf("anchor: %v", err)
64 }
65 m.PutHeaders(0, chain[:1])
66 if err := m.PutHeaders(1, chain[1:]); err != nil {
67 t.Fatalf("put: %v", err)
68 }
69
70 tip, tipHash, ok := m.HeaderTip()
71 if !ok || tip != 99 || tipHash != chain[99].BlockHash() {
72 t.Fatalf("tip = %d %s %v, want 99 %s", tip, tipHash, ok, chain[99].BlockHash())
73 }
74
75 m.mu.RLock()
76 retained := len(m.headers)
77 low := m.low
78 m.mu.RUnlock()
79 if retained > 10 {
80 t.Fatalf("retained %d headers, window is 10", retained)
81 }
82 if low != 90 {
83 t.Fatalf("lowest retained height is %d, want 90", low)
84 }
85
86 for h := uint32(0); h < 100; h++ {
87 got, ok := m.Header(h)
88 if !ok {
89 t.Fatalf("header %d missing", h)
90 }
91 if got.BlockHash() != chain[h].BlockHash() {
92 t.Fatalf("header %d is %s, want %s", h, got.BlockHash(), chain[h].BlockHash())
93 }
94 }
95 if f.reads == 0 {
96 t.Fatal("no read reached the fetcher")
97 }
98
99 // A reorg below the window is refused: the fork point is not held any more.
100 if err := m.Truncate(50); err == nil {
101 t.Fatal("truncate below the retained window was accepted")
102 }
103 if err := m.Truncate(95); err != nil {
104 t.Fatalf("truncate inside the window: %v", err)
105 }
106 tip, _, _ = m.HeaderTip()
107 if tip != 94 {
108 t.Fatalf("tip after truncate = %d, want 94", tip)
109 }
110 }
111
112 // TestAdoptTailInstallsTheTip checks the open path: a store that read only the
113 // newest heights from disk reports the right tip and still answers below it.
114 func TestAdoptTailInstallsTheTip(t *testing.T) {
115 chain := chainOf(50)
116 m := NewMemory()
117 m.SetFetcher(&chainFetcher{headers: chain})
118 m.SetWindow(8)
119 if err := m.SetAnchor(Anchor{Height: 0, BlockHash: chain[0].BlockHash(), FilterHeight: -1}); err != nil {
120 t.Fatalf("anchor: %v", err)
121 }
122 m.AdoptHeaders(42, chain[42:])
123
124 tip, tipHash, ok := m.HeaderTip()
125 if !ok || tip != 49 || tipHash != chain[49].BlockHash() {
126 t.Fatalf("tip = %d %s %v, want 49", tip, tipHash, ok)
127 }
128 for h := uint32(0); h < 50; h++ {
129 if _, ok := m.Header(h); !ok {
130 t.Fatalf("header %d missing", h)
131 }
132 }
133 }
134