1 package relay
2 3 import (
4 "context"
5 "time"
6 7 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
8 9 "git.smesh.lol/btcd-lite/internal/store"
10 )
11 12 // Follow settles the window as the chain advances: every new block's verified
13 // filter is matched against the window, and a block that matches takes the
14 // entries it touches to Seen or Spent.
15 //
16 // It reads the filter chain the instance already holds and verifies it the way
17 // every other reader does: the filter's hash against the filter hash the
18 // header chain commits to. A filter that does not verify ends the pass rather
19 // than being used.
20 func (r *Relay) Follow(ctx context.Context, headers *store.HeaderStore, lookup SpentAt, every time.Duration) {
21 if every <= 0 {
22 every = 15 * time.Second
23 }
24 t := time.NewTicker(every)
25 defer t.Stop()
26 // The pass starts at the base filter's height: an entry filed after the
27 // base was built is still found by the block whose filter already
28 // existed, because the window is settled against every block the instance
29 // holds and the states keep their first evidence.
30 next := uint32(0)
31 if a, ok := headers.Anchor(); ok && a.FilterHeight >= 0 {
32 next = uint32(a.FilterHeight) + 1
33 }
34 for ctx.Err() == nil {
35 select {
36 case <-ctx.Done():
37 return
38 case <-t.C:
39 }
40 tip, ok := headers.FilterTip()
41 if !ok {
42 continue
43 }
44 // The walk starts above the anchor, because the anchor's own filter
45 // header and filter are the trusted starting point and not records; a
46 // height with no filter in the store is where the store's coverage
47 // ends, and an entry filed above it settles when the blocks arrive.
48 for h := next; h <= tip; h++ {
49 flt, ok := headers.Filter(h)
50 if !ok {
51 break
52 }
53 want, ok := headers.FilterHash(h)
54 if !ok || chainhash.DoubleHashH(flt) != want {
55 r.cfg.Logf("relay: filter at %d does not verify; not settling with it", h)
56 next = h
57 break
58 }
59 header, ok := headers.Header(h)
60 if !ok {
61 next = h
62 break
63 }
64 // The block hash the filter was keyed with is the header's, in
65 // internal order: Header returns the record neutrino holds.
66 blockHash := header.BlockHash()
67 for _, e := range r.window.Settle(h, blockHash, flt, lookup) {
68 r.cfg.Logf("relay: %s is %s at %d", e.TxID, e.State, h)
69 }
70 next = h + 1
71 }
72 }
73 }
74 75 // Rebroadcast re-announces the window's unconfirmed transactions every age,
76 // jittered per instance so two instances do not announce in lockstep.
77 func (r *Relay) Rebroadcast(ctx context.Context, age time.Duration) {
78 if age <= 0 {
79 age = 45 * time.Minute
80 }
81 t := time.NewTicker(age / 3)
82 defer t.Stop()
83 for ctx.Err() == nil {
84 select {
85 case <-ctx.Done():
86 return
87 case <-t.C:
88 }
89 due := r.window.Due(age)
90 if len(due) == 0 {
91 continue
92 }
93 ids := make([]chainhash.Hash, len(due))
94 for i, e := range due {
95 ids[i] = e.TxID
96 }
97 if n, err := r.Announce(ctx, ids); err != nil {
98 r.cfg.Logf("relay: rebroadcast: %v", err)
99 } else if n > 0 {
100 for _, e := range due {
101 r.window.Announced(e.TxID)
102 }
103 r.cfg.Logf("relay: re-announced %d transactions to %d peers", len(ids), n)
104 }
105 }
106 }
107