package gate import ( "encoding/hex" "encoding/json" "path/filepath" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/chains" "git.smesh.lol/btcd-lite/internal/filterbuild" "git.smesh.lol/btcd-lite/internal/filterstore" "git.smesh.lol/btcd-lite/internal/store" ) // TestFiltersComputedFromBodies: every filter built from the bodies alone is // the node's, including the ones whose element sets need the script of an // output created in an earlier epoch. The scripts come from the table on disk, // not from a map of every unspent output. func TestFiltersComputedFromBodies(t *testing.T) { r := nodeOn(t) r.createWallet(t) // A spend inside the first epoch of an output the same epoch made: the table // holds what existed before the epoch, so this is the case only the epoch's // own creation list can answer. The coinbase needs a hundred confirmations // before it can be spent, and the spend is mined at 102, still inside 0..143. r.mine(t, 101) early := r.must(t, "getnewaddress") if _, err := r.cli("sendtoaddress", early, "1"); err != nil { t.Fatalf("sendtoaddress in the first epoch: %v", err) } r.mine(t, 1) r.mine(t, 100) // And a spend whose input was created in an earlier epoch, which the table // answers. to := r.must(t, "getnewaddress") if _, err := r.cli("sendtoaddress", to, "1"); err != nil { t.Fatalf("sendtoaddress: %v", err) } r.mine(t, 100) tip := r.height(t) // The script cache starts empty, which makes the build require the table for // exactly the outputs created earlier in the chain. t.Setenv("TMPDIR", t.TempDir()) bodies := rpcBodies(t, r, tip) src := &mapSource{bodies: bodies} dir := t.TempDir() genesisHeader, err := chains.Regtest().Genesis() if err != nil { t.Fatal(err) } headers := anchoredHeaderStore(t, filepath.Join(dir, "headers"), genesisHeader.BlockHash()) filters, err := filterstore.Open(filepath.Join(dir, "filters")) if err != nil { t.Fatal(err) } defer filters.Close() interval := chains.Regtest().Exported.Retarget res, err := filterbuild.Build(filterbuild.Config{ Dir: filepath.Join(dir, "table"), From: 0, To: uint32(tip) + 1, Interval: interval, Blocks: src, Filters: filters, Folds: []*store.HeaderStore{headers}, Logf: t.Logf, }) if err != nil { t.Fatalf("build: %v", err) } if int(res.Filters) != tip+1 { t.Fatalf("%d filters for %d heights", res.Filters, tip+1) } // The bound the design claims: one epoch's touches, not the chain's. Three // epochs of 144, 144 and 12 blocks cannot exceed 144 creations each, and a // walk that held the chain would show the sum. // An epoch is 144 blocks of at most a coinbase and a payment, so two // intervals is generous; a walk that held the chain would show the sum, // which is 450 here and grows with every block. if res.MaxCreated > 2*144 || res.MaxSpent > 2*144 { t.Fatalf("the largest epoch held %d creations and %d spends, more than an epoch", res.MaxCreated, res.MaxSpent) } if res.Tables != 3 { t.Fatalf("%d table rewrites for %d heights at interval %d", res.Tables, tip+1, interval) } for h := 0; h <= tip; h++ { hash := r.must(t, "getblockhash", itoa(h)) hb, err := chainhash.NewHashFromStr(hash) if err != nil { t.Fatal(err) } got, err := filters.Get(*hb) if err != nil { t.Fatalf("height %d: %v", h, err) } var node struct { Filter string `json:"filter"` Header string `json:"header"` } raw, err := r.cli("getblockfilter", hash) if err != nil { t.Fatalf("getblockfilter %d: %v", h, err) } if err := json.Unmarshal([]byte(raw), &node); err != nil { t.Fatalf("height %d: %v: %s", h, err, raw) } want, err := hex.DecodeString(node.Filter) if err != nil { t.Fatal(err) } if string(got) != string(want) { t.Fatalf("height %d: the computed filter is not the node's", h) } fh, _ := headers.FilterHeader(uint32(h)) if fh.String() != node.Header { t.Fatalf("height %d: fold %s, the node says %s", h, fh, node.Header) } } 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", res.Filters, res.Tables, res.MaxCreated, res.MaxSpent) }