nodesynth_helpers_test.go raw
1 package utxostore
2
3 import (
4 "encoding/hex"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
12
13 "git.smesh.lol/btcd-lite/internal/utxo"
14 "git.smesh.lol/btcd-lite/internal/wire"
15 )
16
17 func nodeBody(t *testing.T, h uint32) []byte {
18 t.Helper()
19 raw, err := os.ReadFile(filepath.Join("..", "..", "utxobuild", "testdata", fmt.Sprintf("body-%03d.hex", h)))
20 if err != nil {
21 t.Skipf("no captured body %d", h)
22 }
23 b, err := hex.DecodeString(strings.TrimSpace(string(raw)))
24 if err != nil {
25 t.Fatal(err)
26 }
27 return b
28 }
29
30 // nodeEntriesAt4 is the set the first four node bodies build.
31 func nodeEntriesAt4(t *testing.T) []utxo.Entry {
32 t.Helper()
33 var set []utxo.Entry
34 for h := uint32(0); h < 4; h++ {
35 b, err := wire.ParseBlock(nodeBody(t, h))
36 if err != nil {
37 t.Fatal(err)
38 }
39 ts, err := utxo.BlockTouches(b, h, nil)
40 if err != nil {
41 t.Fatal(err)
42 }
43 set, err = utxo.ApplyRange(set, ts)
44 if err != nil {
45 t.Fatal(err)
46 }
47 }
48 return set
49 }
50
51 // nodeSecondRange applies the bodies 4..7 to base and returns the touches and
52 // the set they produce.
53 func nodeSecondRange(t *testing.T, base []utxo.Entry) ([]utxo.Touch, []utxo.Entry) {
54 t.Helper()
55 set := append([]utxo.Entry(nil), base...)
56 var ts []utxo.Touch
57 for h := uint32(4); h < 8; h++ {
58 b, err := wire.ParseBlock(nodeBody(t, h))
59 if err != nil {
60 t.Fatal(err)
61 }
62 lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) })
63 block, err := utxo.BlockTouches(b, h, lookup)
64 if err != nil {
65 t.Fatalf("body %d: %v", h, err)
66 }
67 ts = append(ts, block...)
68 set, err = utxo.ApplyRange(set, block)
69 if err != nil {
70 t.Fatalf("body %d apply: %v", h, err)
71 }
72 }
73 return ts, set
74 }
75
76 var _ = chainhash.Hash{}
77
78 // nodeScriptsAt4 is the set at 4 with the script hashes the entries carry, so a
79 // dump can show whether a difference is in the script or in the height.
80 func nodeScriptsAt4(t *testing.T) []utxo.Entry { return nodeEntriesAt4(t) }
81