blockcache_test.go raw
1 package blockcache
2
3 import (
4 "os"
5 "testing"
6
7 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
8 )
9
10 // hashOf is a deterministic block hash for the tests.
11 func hashOf(tag byte) chainhash.Hash {
12 return chainhash.HashH([]byte{tag})
13 }
14
15 // blockOf is a block of the requested size, distinguished by the tag.
16 func blockOf(tag byte, size int) []byte {
17 out := make([]byte, size)
18 out[0] = tag
19
20 return out
21 }
22
23 func openTestCache(t *testing.T, maxBytes int64) (*Cache, string) {
24 t.Helper()
25
26 dir := t.TempDir()
27
28 cache, err := Open(Config{Dir: dir, MaxBytes: maxBytes})
29
30 if err != nil {
31 t.Fatal(err)
32 }
33
34 t.Cleanup(func() { _ = cache.Close() })
35
36 return cache, dir
37 }
38
39 // TestRoundTrip: a block that went in comes out byte for byte.
40 func TestRoundTrip(t *testing.T) {
41 cache, _ := openTestCache(t, 1<<20)
42
43 if err := cache.Put("btc", hashOf(1), blockOf(1, 100)); err != nil {
44 t.Fatal(err)
45 }
46
47 raw, ok, err := cache.Get("btc", hashOf(1))
48
49 if err != nil {
50 t.Fatal(err)
51 }
52
53 if !ok {
54 t.Fatal("a block that was just stored is a miss")
55 }
56
57 if len(raw) != 100 || raw[0] != 1 {
58 t.Fatalf("the block came back as %d bytes starting %d", len(raw), raw[0])
59 }
60 }
61
62 // TestAMissIsNotAnError: the cache is asked for blocks it does not hold, and that is the
63 // normal answer rather than a failure.
64 func TestAMissIsNotAnError(t *testing.T) {
65 cache, _ := openTestCache(t, 1<<20)
66
67 _, ok, err := cache.Get("btc", hashOf(9))
68
69 if err != nil {
70 t.Fatalf("a miss was reported as an error: %v", err)
71 }
72
73 if ok {
74 t.Fatal("a block that was never stored was found")
75 }
76 }
77
78 // TestTheOldestGoesFirst: the cache is bounded, and the blocks that go are the ones stored
79 // first, which is where the requests are not.
80 func TestTheOldestGoesFirst(t *testing.T) {
81 cache, _ := openTestCache(t, 250)
82
83 for tag := byte(1); tag <= 3; tag++ {
84 if err := cache.Put("btc", hashOf(tag), blockOf(tag, 100)); err != nil {
85 t.Fatal(err)
86 }
87 }
88
89 count, bytes := cache.Len()
90
91 if bytes > 250 {
92 t.Fatalf("cache holds %d bytes, bound is 250", bytes)
93 }
94
95 if count != 2 {
96 t.Fatalf("cache holds %d blocks, want the two newest", count)
97 }
98
99 for tag := byte(2); tag <= 3; tag++ {
100 if _, ok, _ := cache.Get("btc", hashOf(tag)); !ok {
101 t.Fatalf("block %d was evicted before block 1", tag)
102 }
103 }
104
105 if _, ok, _ := cache.Get("btc", hashOf(1)); ok {
106 t.Fatal("the oldest block survived a full cache")
107 }
108 }
109
110 // TestTheIndexSurvivesAReopen: a sidecar restarts and the blocks it paid to fetch are still
111 // there.
112 func TestTheIndexSurvivesAReopen(t *testing.T) {
113 dir := t.TempDir()
114
115 first, err := Open(Config{Dir: dir, MaxBytes: 1 << 20})
116
117 if err != nil {
118 t.Fatal(err)
119 }
120
121 if err := first.Put("blake", hashOf(4), blockOf(4, 64)); err != nil {
122 t.Fatal(err)
123 }
124
125 if err := first.Close(); err != nil {
126 t.Fatal(err)
127 }
128
129 second, err := Open(Config{Dir: dir, MaxBytes: 1 << 20})
130
131 if err != nil {
132 t.Fatal(err)
133 }
134
135 defer second.Close()
136
137 raw, ok, err := second.Get("blake", hashOf(4))
138
139 if err != nil || !ok {
140 t.Fatalf("the block did not survive the reopen: ok=%v err=%v", ok, err)
141 }
142
143 if len(raw) != 64 {
144 t.Fatalf("the block came back as %d bytes", len(raw))
145 }
146
147 count, bytes := second.Len()
148
149 if count != 1 || bytes != 64 {
150 t.Fatalf("index says %d blocks and %d bytes", count, bytes)
151 }
152 }
153
154 // TestAVanishedFileIsDropped: the index can outlive a file, and an entry with no bytes behind
155 // it is not a block.
156 func TestAVanishedFileIsDropped(t *testing.T) {
157 dir := t.TempDir()
158
159 first, err := Open(Config{Dir: dir, MaxBytes: 1 << 20})
160
161 if err != nil {
162 t.Fatal(err)
163 }
164
165 if err := first.Put("btc", hashOf(5), blockOf(5, 32)); err != nil {
166 t.Fatal(err)
167 }
168
169 if err := first.Close(); err != nil {
170 t.Fatal(err)
171 }
172
173 path, err := first.path("btc", hashOf(5).String())
174
175 if err != nil {
176 t.Fatal(err)
177 }
178
179 if err := os.Remove(path); err != nil {
180 t.Fatal(err)
181 }
182
183 second, err := Open(Config{Dir: dir, MaxBytes: 1 << 20})
184
185 if err != nil {
186 t.Fatal(err)
187 }
188
189 defer second.Close()
190
191 if count, _ := second.Len(); count != 0 {
192 t.Fatalf("index kept %d entries whose files are gone", count)
193 }
194 }
195
196 // TestANameThatIsAPathIsRefused: the chain name and the hash come from a request, so neither is
197 // allowed to be a path.
198 func TestANameThatIsAPathIsRefused(t *testing.T) {
199 cache, _ := openTestCache(t, 1<<20)
200
201 if _, _, err := cache.Get("../etc", hashOf(1)); err == nil {
202 t.Fatal("a chain name with a path in it was accepted")
203 }
204
205 if _, err := cache.path("btc", "../../etc/passwd"); err == nil {
206 t.Fatal("a block hash that is a path was accepted")
207 }
208
209 if err := cache.Put("../etc", hashOf(1), blockOf(1, 8)); err == nil {
210 t.Fatal("a chain name with a path in it was accepted on write")
211 }
212 }
213