verify_test.go raw
1 package verify
2
3 import (
4 "context"
5 "errors"
6 "strings"
7 "testing"
8
9 "git.smesh.lol/tinywallet/pkg/script"
10 "git.smesh.lol/tinywallet/pkg/tx"
11 )
12
13 // --- fake sources ---------------------------------------------------------------
14
15 type fake struct {
16 name string
17 raw []byte
18 found bool
19 height int64
20 tip Tip
21 fee int64
22 err error
23 }
24
25 func (f fake) Name() string { return f.name }
26
27 func (f fake) Tx(context.Context, [32]byte) (TxFact, error) {
28 if f.err != nil {
29 return TxFact{}, f.err
30 }
31
32 return TxFact{Found: f.found, Raw: f.raw, Height: f.height, Confirmations: 10}, nil
33 }
34
35 func (f fake) Tip(context.Context) (Tip, error) { return f.tip, nil }
36 func (f fake) FeeEstimate(context.Context) (int64, error) { return f.fee, nil }
37
38 func buildTx(t *testing.T, lockTime uint32, sequence uint32, outputs ...tx.Output) *tx.Tx {
39 t.Helper()
40
41 built := tx.New()
42 built.LockTime = lockTime
43 built.AddInput(tx.Outpoint{Hash: [32]byte{0x11}, Index: 0}, sequence)
44
45 for _, out := range outputs {
46 built.AddOutput(out.Value, out.Script)
47 }
48
49 return built
50 }
51
52 func rawOf(t *testing.T, built *tx.Tx) []byte {
53 t.Helper()
54
55 raw, err := built.Serialize()
56
57 if err != nil {
58 t.Fatal(err)
59 }
60
61 return raw
62 }
63
64 // --- the external engine --------------------------------------------------------
65
66 func TestQuorumNeedsTwoIndependentSources(t *testing.T) {
67 if _, err := NewFacts([]Source{fake{name: "a"}}, 1); err == nil {
68 t.Fatal("a single source was accepted as a quorum")
69 }
70
71 if _, err := NewFacts([]Source{fake{name: "a"}, fake{name: "a"}}, 2); err == nil {
72 t.Fatal("the same source name twice was accepted")
73 }
74 }
75
76 func TestTxAcceptedWhenQuorumAgreesAndBytesHashToOneID(t *testing.T) {
77 built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 1000, Script: []byte{0x51}})
78 raw := rawOf(t, built)
79 txid, err := TxidOf(raw)
80
81 if err != nil {
82 t.Fatal(err)
83 }
84
85 engine, err := NewFacts([]Source{
86 fake{name: "one", raw: raw, found: true, height: 962000, tip: Tip{Height: 962000, Hash: [32]byte{1}}, fee: 5},
87 fake{name: "two", raw: raw, found: true, height: 962000, tip: Tip{Height: 962000, Hash: [32]byte{2}}, fee: 6},
88 }, 2)
89
90 if err != nil {
91 t.Fatal(err)
92 }
93
94 answer, err := engine.Tx(context.Background(), txid)
95
96 if err != nil {
97 t.Fatal(err)
98 }
99
100 if !answer.Verified || !answer.Found || answer.Height != 962000 {
101 t.Fatalf("answer %+v", answer)
102 }
103 }
104
105 func TestTxRejectedWhenBytesDoNotHashToTheRequestedID(t *testing.T) {
106 built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 1, Script: []byte{0x51}})
107 other := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 2, Script: []byte{0x51}})
108
109 txid, err := TxidOf(rawOf(t, built))
110
111 if err != nil {
112 t.Fatal(err)
113 }
114
115 engine, err := NewFacts([]Source{
116 fake{name: "one", raw: rawOf(t, other), found: true, height: 1, tip: Tip{Hash: [32]byte{1}}},
117 fake{name: "two", raw: rawOf(t, other), found: true, height: 1, tip: Tip{Hash: [32]byte{2}}},
118 }, 2)
119
120 if err != nil {
121 t.Fatal(err)
122 }
123
124 if _, err := engine.Tx(context.Background(), txid); !errors.Is(err, ErrDisagree) {
125 t.Fatalf("err %v, want ErrDisagree", err)
126 }
127 }
128
129 func TestTxAbsentFromOneSourceIsDisagreement(t *testing.T) {
130 built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 1, Script: []byte{0x51}})
131 raw := rawOf(t, built)
132
133 txid, err := TxidOf(raw)
134
135 if err != nil {
136 t.Fatal(err)
137 }
138
139 engine, err := NewFacts([]Source{
140 fake{name: "one", raw: raw, found: true, height: 5, tip: Tip{Hash: [32]byte{1}}},
141 fake{name: "two", found: false, tip: Tip{Hash: [32]byte{2}}},
142 }, 2)
143
144 if err != nil {
145 t.Fatal(err)
146 }
147
148 if _, err := engine.Tx(context.Background(), txid); !errors.Is(err, ErrDisagree) {
149 t.Fatalf("err %v, want ErrDisagree", err)
150 }
151 }
152
153 func TestIndependenceWarnsWhenEverySourceSeesOneTip(t *testing.T) {
154 tip := Tip{Height: 900, Hash: [32]byte{7}}
155
156 engine, err := NewFacts([]Source{
157 fake{name: "one", tip: tip},
158 fake{name: "two", tip: tip},
159 }, 2)
160
161 if err != nil {
162 t.Fatal(err)
163 }
164
165 report, err := engine.Independence(context.Background())
166
167 if err != nil {
168 t.Fatal(err)
169 }
170
171 if report.DistinctTips != 1 {
172 t.Fatalf("independence %+v", report)
173 }
174
175 // The warning must not claim a shared backend, because one chain has one tip.
176 if len(report.Warnings) != 1 || strings.Contains(report.Warnings[0], "may be one backend") {
177 t.Fatalf("warnings %v", report.Warnings)
178 }
179 }
180
181 // --- the terms checks -----------------------------------------------------------
182
183 func testTerms(t *testing.T) (Terms, []byte) {
184 t.Helper()
185
186 key := func(b byte) []byte { return append([]byte{0x02}, make([]byte, 32)...) }
187
188 claimer := append([]byte{0x02}, make([]byte, 32)...)
189 claimer[1] = 1
190 refunder := append([]byte{0x02}, make([]byte, 32)...)
191 refunder[1] = 2
192
193 var hash [32]byte
194 hash[0] = 9
195
196 terms := Terms{
197 HashLock: hash, TimeoutBTC: 900, TimeoutXBT: 800,
198 AmountBTC: 100_000, AmountXBT: 100_000, FeeBTC: 500, FeeXBT: 500,
199 ClaimerBTC: claimer, RefunderBTC: refunder,
200 ClaimerXBT: claimer, RefunderXBT: refunder,
201 MinTimeoutGap: 24,
202 }
203
204 htlc, err := script.HTLC(claimer, refunder, hash, terms.TimeoutBTC)
205
206 if err != nil {
207 t.Fatal(err)
208 }
209
210 _ = key
211
212 return terms, htlc
213 }
214
215 func TestTermsFailOnWrongTimeout(t *testing.T) {
216 terms, htlc := testTerms(t)
217 leg := Leg{Chain: "btc", Timeout: 901, Amount: terms.AmountBTC, Fee: terms.FeeBTC,
218 HashLock: terms.HashLock, Claimer: terms.ClaimerBTC, Refunder: terms.RefunderBTC}
219
220 var report Report
221 terms.Verify(&report, []Leg{leg})
222
223 if report.Verdict != VerdictFail {
224 t.Fatalf("verdict %s, want fail", report.Verdict)
225 }
226
227 found := false
228
229 for _, check := range report.Checks {
230 if check.Name == "btc_timeout_matches_terms" && check.Status == Fail {
231 found = true
232 }
233 }
234
235 if !found {
236 t.Fatal("the timeout mismatch was not reported")
237 }
238
239 _ = htlc
240 }
241
242 func TestTermsFailOnAHiddenOutput(t *testing.T) {
243 terms, htlc := testTerms(t)
244
245 funding := buildTx(t, 0, tx.SequenceFinal,
246 tx.Output{Value: terms.AmountBTC, Script: htlc},
247 tx.Output{Value: 2_000, Script: []byte{0x76, 0xa9, 0x14}},
248 )
249
250 leg := Leg{Chain: "btc", Timeout: terms.TimeoutBTC, Amount: terms.AmountBTC, Fee: terms.FeeBTC,
251 HashLock: terms.HashLock, Claimer: terms.ClaimerBTC, Refunder: terms.RefunderBTC,
252 Funding: rawOf(t, funding)}
253
254 var report Report
255 terms.Verify(&report, []Leg{leg})
256
257 if report.Verdict != VerdictFail {
258 t.Fatalf("verdict %s, want fail", report.Verdict)
259 }
260 }
261
262 func TestTermsFailOnReusedHashlock(t *testing.T) {
263 terms, htlc := testTerms(t)
264
265 other := terms
266 other.HashLock[0] = 10
267
268 funding := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: terms.AmountBTC, Script: htlc})
269
270 leg := Leg{Chain: "btc", Timeout: terms.TimeoutBTC, Amount: terms.AmountBTC, Fee: terms.FeeBTC,
271 HashLock: other.HashLock, Claimer: terms.ClaimerBTC, Refunder: terms.RefunderBTC,
272 Funding: rawOf(t, funding)}
273
274 var report Report
275 terms.Verify(&report, []Leg{leg})
276
277 if report.Verdict != VerdictFail {
278 t.Fatalf("verdict %s, want fail", report.Verdict)
279 }
280 }
281
282 // --- fork constants -------------------------------------------------------------
283
284 func TestClockUsesObservedIntervals(t *testing.T) {
285 clock := NewClock([]int64{420, 600, 900})
286
287 if clock.Median != 600 || clock.Fastest != 420 || clock.Slowest != 900 {
288 t.Fatalf("clock %+v", clock)
289 }
290
291 expected, fast, slow := clock.Expected(6)
292
293 if expected != 3600 || fast != 2520 || slow != 5400 {
294 t.Fatalf("expected %d %d %d", expected, fast, slow)
295 }
296 }
297
298 func TestCoinbaseMaturityIsTheForksNotBitcoins(t *testing.T) {
299 if !ImmatureCoinbase(99) {
300 t.Fatal("an immature coinbase was accepted")
301 }
302
303 if ImmatureCoinbase(XBTCoinbaseMaturity) {
304 t.Fatal("a mature coinbase was refused")
305 }
306 }
307