// Package scanner walks a chain's compact filters backwards from the tip. // // A wallet usually does not know its history, and it does not need to: the coins it can spend are // recent far more often than not. So the useful order is newest first, stopping once the scan has // passed the wallet's activity, and the useful unit is a window — BIP157 ranges ascend, so a // backwards walk is a descending series of ascending windows. // // Headers and filter headers are not this package's business: they are verified forward once, in // the background, and give every window a chain to stand on. package scanner import ( "context" "errors" "fmt" ) // ErrNoTip is a chain that has not synced far enough to scan. var ErrNoTip = errors.New("scanner: the chain has no verified tip") // Chain is what a backwards walk needs from a light client. type Chain interface { // Tip is the highest height whose filter header is verified. Tip(ctx context.Context) (uint32, error) // Fetch downloads and verifies the filters for [from, to], inclusive. It may be asked for // the same window twice after a failure; it must be idempotent. Fetch(ctx context.Context, from, to uint32) error // Match returns the heights in [from, to] whose filters mention one of the scripts. Match(ctx context.Context, from, to uint32, scripts [][]byte) ([]uint32, error) } // Options bounds a walk. type Options struct { // Floor is the lowest height to scan, usually the wallet's birthday or a chain checkpoint. // Zero means the walk may go to genesis. Floor uint32 // Window is how many blocks one fetch covers. Zero means 500. Window uint32 // Gap is how many consecutive blocks whose filters do not mention any of the wallet's scripts // end the walk: the point where the wallet's activity has clearly been passed. It is not // about empty blocks — every block has a coinbase — but about blocks with nothing for these // keys. Zero means no gap stop, which is what a full rescan wants. Gap uint32 // OnMatch receives each matched height, in descending discovery order. OnMatch func(ctx context.Context, height uint32) error } // Result is what a walk covered. type Result struct { // Scanned is how many blocks were fetched and examined. Scanned uint32 // Lowest is the lowest height reached. Lowest uint32 // Matched is every matching height found, newest first. Matched []uint32 // Stopped says why: "floor", "gap" or "tip". Stopped string } // Backwards walks from the tip down to the floor, or until a gap of blocks with no match. // // The walk is chunked on purpose: a single BIP157 request cannot span more than a thousand blocks, // and asking for the whole range would be one enormous request the node disconnects for. Each // window is fetched ascending, then examined, and the next window starts below it. func Backwards(ctx context.Context, chain Chain, scripts [][]byte, opts Options) (Result, error) { var result Result if opts.Window == 0 { opts.Window = 500 } if len(scripts) == 0 { return result, errors.New("scanner: no scripts to look for") } tip, err := chain.Tip(ctx) if err != nil { return result, err } if tip == 0 { return result, fmt.Errorf("%w: tip is zero", ErrNoTip) } end := tip var sinceMatch uint32 for { if ctx.Err() != nil { return result, ctx.Err() } start := uint32(0) if end >= opts.Window { start = end - opts.Window + 1 } if start < opts.Floor { start = opts.Floor } if err := chain.Fetch(ctx, start, end); err != nil { return result, fmt.Errorf("scanner: fetch %d..%d: %w", start, end, err) } matched, err := chain.Match(ctx, start, end, scripts) if err != nil { return result, fmt.Errorf("scanner: match %d..%d: %w", start, end, err) } // Newest first, so a caller can act on the most likely coin before the walk finishes. for i := len(matched) - 1; i >= 0; i-- { height := matched[i] result.Matched = append(result.Matched, height) if opts.OnMatch != nil { if err := opts.OnMatch(ctx, height); err != nil { return result, err } } } scanned := end - start + 1 result.Scanned += scanned result.Lowest = start if len(matched) > 0 { sinceMatch = 0 } else { sinceMatch += scanned } switch { case start == opts.Floor: result.Stopped = "floor" return result, nil case opts.Gap > 0 && sinceMatch >= opts.Gap: result.Stopped = "gap" return result, nil } if start == 0 { result.Stopped = "tip" return result, nil } end = start - 1 } }