1 package relay
2 3 import (
4 "context"
5 "testing"
6 "time"
7 8 "git.smesh.lol/nostrino/pkg/btcwire"
9 "git.smesh.lol/nostrino/pkg/chain"
10 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
11 "git.smesh.lol/nostrino/pkg/neutrino"
12 13 "git.smesh.lol/btcd-lite/internal/filter"
14 "git.smesh.lol/btcd-lite/internal/store"
15 "git.smesh.lol/btcd-lite/internal/wire"
16 )
17 18 // TestFollowSettlesFromTheStore: a header store with a filter index and a
19 // window holding a transaction whose script the block funds. Follow must take
20 // the entry to Seen from the stored filter, and to Spent when the store's
21 // answer says the inputs went there.
22 func TestFollowSettlesFromTheStore(t *testing.T) {
23 dir := t.TempDir()
24 headers, err := store.NewHeaderStore(dir)
25 if err != nil {
26 t.Fatal(err)
27 }
28 defer headers.Close()
29 genesis := btcwire.BlockHeader{}
30 anchor := neutrino.Anchor{Height: 0, BlockHash: genesis.BlockHash(), FilterHeight: 0}
31 if err := headers.SetAnchor(anchor); err != nil {
32 t.Fatal(err)
33 }
34 script := []byte{0x51, 0x33}
35 raw, tx := mkTx(5, script)
36 blk := filterTestBlock(script)
37 // The filter is keyed by the block's own hash, and the header record the
38 // store holds must be that block's header for the match to mean anything.
39 var hdr btcwire.BlockHeader
40 // The header record and the block's own 80 bytes are the same header: the
41 // fixture derives one from the other and puts the previous block in both,
42 // so the hash the filter is keyed by is the hash the store reports.
43 derived := btcwire.BlockHeader{PrevBlock: genesis.BlockHash(), MerkleRoot: blk.MerkleRoot(), Timestamp: 1}
44 enc := derived.Serialize()
45 copy(blk.Header[:], enc[:])
46 if err := hdr.Deserialize(blk.Header[:]); err != nil {
47 t.Fatal(err)
48 }
49 hdr.PrevBlock = genesis.BlockHash()
50 // The records run from the anchor height, so the genesis header is the
51 // first record and the block that funds the script is the second.
52 if err := headers.PutHeaders(0, []btcwire.BlockHeader{genesis}); err != nil {
53 t.Fatal(err)
54 }
55 if err := headers.PutHeaders(1, []btcwire.BlockHeader{hdr}); err != nil {
56 t.Fatal(err)
57 }
58 flt, err := filter.Build(blk, nil)
59 if err != nil {
60 t.Fatal(err)
61 }
62 fh := filter.Hash(flt)
63 fhdr := filter.Fold(fh, anchor.FilterHeader)
64 if err := headers.PutFilterHeaders(1, []chainhash.Hash{fh}, []chainhash.Hash{fhdr}); err != nil {
65 t.Fatal(err)
66 }
67 if err := headers.PutFilter(1, hdr.BlockHash(), fh, flt); err != nil {
68 t.Fatal(err)
69 }
70 w := NewWindow(1 << 20)
71 w.Add(&Entry{TxID: tx.TxID(), Raw: raw, Scripts: ScriptsOf(tx), Inputs: InputsOf(tx), Size: len(raw)})
72 r, err := New(RelayConfig{Profile: &chain.Profile{Name: "x", Magic: 0xDAB5BFFA}, Window: w, Peers: []string{"127.0.0.1:1"}})
73 if err != nil {
74 t.Fatal(err)
75 }
76 // One pass of the loop, driven by a context that ends after the ticker's
77 // interval.
78 spent := LookupFromSet(map[wire.OutPoint]uint32{tx.TxIn[0].PreviousOutPoint: 1})
79 go r.Follow(context.Background(), headers, spent, 5*time.Millisecond)
80 deadline := time.Now().Add(2 * time.Second)
81 for time.Now().Before(deadline) {
82 if e, ok := w.Get(tx.TxID()); ok && e.State == Spent {
83 return
84 }
85 time.Sleep(10 * time.Millisecond)
86 }
87 e, _ := w.Get(tx.TxID())
88 t.Fatalf("the entry never settled: %+v", e)
89 }
90