scan.go raw
1 package domain
2
3 import (
4 "context"
5 "fmt"
6 "sort"
7
8 "git.smesh.lol/tinywallet/pkg/scanner"
9 )
10
11 // ScanBackwards is the wallet-driven scan: filters from the verified tip downwards, stopping at
12 // the wallet's floor or after a gap of blocks with no match.
13 //
14 // Headers and filter headers are not downloaded here. That is the chain's job, done forward in the
15 // background before a seed exists. This only spends bandwidth on filters the wallet's scripts can
16 // use, newest first, because a wallet's spendable coins are recent far more often than not.
17 func (d *Domain) ScanBackwards(ctx context.Context, name string, window, gap uint32) (Report, error) {
18 link, err := d.link(name)
19
20 if err != nil {
21 return Report{}, err
22 }
23
24 derived, err := d.walletOf(name)
25
26 if err != nil {
27 return Report{}, err
28 }
29
30 meta, err := d.meta(name)
31
32 if err != nil {
33 return Report{}, err
34 }
35
36 scripts, err := d.scanScripts(name, derived, meta, DefaultLookahead)
37
38 if err != nil {
39 return Report{}, err
40 }
41
42 position, err := link.Client.Position()
43
44 if err != nil {
45 return Report{}, fmt.Errorf("domain: %q: position: %w", name, err)
46 }
47
48 report := Report{Chain: name, Scripts: len(scripts)}
49 adapter := &scanChain{link: link, scripts: scripts}
50
51 result, err := scanner.Backwards(ctx, adapter, scripts, scanner.Options{
52 Floor: meta.FilterFrom,
53 Window: window,
54 Gap: gap,
55 OnMatch: func(ctx context.Context, height uint32) error {
56 raw, err := link.Client.Block(height)
57
58 if err != nil {
59 return fmt.Errorf("domain: %q: block %d: %w", name, height, err)
60 }
61
62 if _, err := d.scanBlock(derived, raw, height); err != nil {
63 return fmt.Errorf("domain: %q: scanning block %d: %w", name, height, err)
64 }
65
66 return nil
67 },
68 })
69
70 report.Matched = append([]uint32{}, result.Matched...)
71 sort.Slice(report.Matched, func(i, j int) bool { return report.Matched[i] < report.Matched[j] })
72 report.New = report.Matched
73 report.From = result.Lowest
74 report.Position = position
75
76 return report, err
77 }
78
79 // scanChain adapts one chain's client to what a backwards walk needs.
80 type scanChain struct {
81 link Link
82 scripts [][]byte
83 }
84
85 func (s *scanChain) Tip(context.Context) (uint32, error) {
86 position, err := s.link.Client.Position()
87
88 if err != nil {
89 return 0, err
90 }
91
92 return position.Tip, nil
93 }
94
95 // Fetch downloads one window. The walk asks for ascending ranges, so the client is given exactly
96 // that and nothing wider.
97 func (s *scanChain) Fetch(ctx context.Context, from, to uint32) error {
98 return s.link.Client.FetchFilters(from, to)
99 }
100
101 // Match returns the window's matches. The chain's matcher covers everything it has stored, so the
102 // result is narrowed to the window: a match outside it belongs to another window of the walk.
103 func (s *scanChain) Match(_ context.Context, from, to uint32, _ [][]byte) ([]uint32, error) {
104 heights, err := s.link.Client.Match(s.scripts)
105
106 if err != nil {
107 return nil, err
108 }
109
110 out := make([]uint32, 0, len(heights))
111
112 for _, height := range heights {
113 if height >= from && height <= to {
114 out = append(out, height)
115 }
116 }
117
118 return out, nil
119 }
120