1 package gate
2 3 import (
4 "encoding/hex"
5 "encoding/json"
6 "path/filepath"
7 "testing"
8 9 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
10 11 "git.smesh.lol/btcd-lite/internal/chains"
12 "git.smesh.lol/btcd-lite/internal/filterbuild"
13 "git.smesh.lol/btcd-lite/internal/filterstore"
14 "git.smesh.lol/btcd-lite/internal/store"
15 )
16 17 // TestFiltersComputedFromBodies: every filter built from the bodies alone is
18 // the node's, including the ones whose element sets need the script of an
19 // output created in an earlier epoch. The scripts come from the table on disk,
20 // not from a map of every unspent output.
21 func TestFiltersComputedFromBodies(t *testing.T) {
22 r := nodeOn(t)
23 r.createWallet(t)
24 // A spend inside the first epoch of an output the same epoch made: the table
25 // holds what existed before the epoch, so this is the case only the epoch's
26 // own creation list can answer. The coinbase needs a hundred confirmations
27 // before it can be spent, and the spend is mined at 102, still inside 0..143.
28 r.mine(t, 101)
29 early := r.must(t, "getnewaddress")
30 if _, err := r.cli("sendtoaddress", early, "1"); err != nil {
31 t.Fatalf("sendtoaddress in the first epoch: %v", err)
32 }
33 r.mine(t, 1)
34 r.mine(t, 100)
35 // And a spend whose input was created in an earlier epoch, which the table
36 // answers.
37 to := r.must(t, "getnewaddress")
38 if _, err := r.cli("sendtoaddress", to, "1"); err != nil {
39 t.Fatalf("sendtoaddress: %v", err)
40 }
41 r.mine(t, 100)
42 tip := r.height(t)
43 44 // The script cache starts empty, which makes the build require the table for
45 // exactly the outputs created earlier in the chain.
46 t.Setenv("TMPDIR", t.TempDir())
47 bodies := rpcBodies(t, r, tip)
48 src := &mapSource{bodies: bodies}
49 dir := t.TempDir()
50 genesisHeader, err := chains.Regtest().Genesis()
51 if err != nil {
52 t.Fatal(err)
53 }
54 headers := anchoredHeaderStore(t, filepath.Join(dir, "headers"), genesisHeader.BlockHash())
55 filters, err := filterstore.Open(filepath.Join(dir, "filters"))
56 if err != nil {
57 t.Fatal(err)
58 }
59 defer filters.Close()
60 interval := chains.Regtest().Exported.Retarget
61 res, err := filterbuild.Build(filterbuild.Config{
62 Dir: filepath.Join(dir, "table"),
63 From: 0,
64 To: uint32(tip) + 1,
65 Interval: interval,
66 Blocks: src,
67 Filters: filters,
68 Folds: []*store.HeaderStore{headers},
69 Logf: t.Logf,
70 })
71 if err != nil {
72 t.Fatalf("build: %v", err)
73 }
74 if int(res.Filters) != tip+1 {
75 t.Fatalf("%d filters for %d heights", res.Filters, tip+1)
76 }
77 // The bound the design claims: one epoch's touches, not the chain's. Three
78 // epochs of 144, 144 and 12 blocks cannot exceed 144 creations each, and a
79 // walk that held the chain would show the sum.
80 // An epoch is 144 blocks of at most a coinbase and a payment, so two
81 // intervals is generous; a walk that held the chain would show the sum,
82 // which is 450 here and grows with every block.
83 if res.MaxCreated > 2*144 || res.MaxSpent > 2*144 {
84 t.Fatalf("the largest epoch held %d creations and %d spends, more than an epoch", res.MaxCreated, res.MaxSpent)
85 }
86 if res.Tables != 3 {
87 t.Fatalf("%d table rewrites for %d heights at interval %d", res.Tables, tip+1, interval)
88 }
89 for h := 0; h <= tip; h++ {
90 hash := r.must(t, "getblockhash", itoa(h))
91 hb, err := chainhash.NewHashFromStr(hash)
92 if err != nil {
93 t.Fatal(err)
94 }
95 got, err := filters.Get(*hb)
96 if err != nil {
97 t.Fatalf("height %d: %v", h, err)
98 }
99 var node struct {
100 Filter string `json:"filter"`
101 Header string `json:"header"`
102 }
103 raw, err := r.cli("getblockfilter", hash)
104 if err != nil {
105 t.Fatalf("getblockfilter %d: %v", h, err)
106 }
107 if err := json.Unmarshal([]byte(raw), &node); err != nil {
108 t.Fatalf("height %d: %v: %s", h, err, raw)
109 }
110 want, err := hex.DecodeString(node.Filter)
111 if err != nil {
112 t.Fatal(err)
113 }
114 if string(got) != string(want) {
115 t.Fatalf("height %d: the computed filter is not the node's", h)
116 }
117 fh, _ := headers.FilterHeader(uint32(h))
118 if fh.String() != node.Header {
119 t.Fatalf("height %d: fold %s, the node says %s", h, fh, node.Header)
120 }
121 }
122 t.Logf("%d filters computed from bodies in %d epochs, every byte and every fold the node's; the largest epoch held %d creations and %d spends",
123 res.Filters, res.Tables, res.MaxCreated, res.MaxSpent)
124 }
125