• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 9915780197

13 Jul 2024 12:30AM UTC coverage: 49.268% (-9.1%) from 58.413%
9915780197

push

github

web-flow
Merge pull request #8653 from ProofOfKeags/fn-prim

DynComms [0/n]: `fn` package additions

92837 of 188433 relevant lines covered (49.27%)

1.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/chainntnfs/neutrinonotify/neutrino.go
1
package neutrinonotify
2

3
import (
4
        "errors"
5
        "fmt"
6
        "strings"
7
        "sync"
8
        "sync/atomic"
9
        "time"
10

11
        "github.com/btcsuite/btcd/btcjson"
12
        "github.com/btcsuite/btcd/btcutil"
13
        "github.com/btcsuite/btcd/btcutil/gcs/builder"
14
        "github.com/btcsuite/btcd/chaincfg/chainhash"
15
        "github.com/btcsuite/btcd/rpcclient"
16
        "github.com/btcsuite/btcd/txscript"
17
        "github.com/btcsuite/btcd/wire"
18
        "github.com/lightninglabs/neutrino"
19
        "github.com/lightninglabs/neutrino/headerfs"
20
        "github.com/lightningnetwork/lnd/blockcache"
21
        "github.com/lightningnetwork/lnd/chainntnfs"
22
        "github.com/lightningnetwork/lnd/lntypes"
23
        "github.com/lightningnetwork/lnd/queue"
24
)
25

26
const (
27
        // notifierType uniquely identifies this concrete implementation of the
28
        // ChainNotifier interface.
29
        notifierType = "neutrino"
30
)
31

32
// NeutrinoNotifier is a version of ChainNotifier that's backed by the neutrino
33
// Bitcoin light client. Unlike other implementations, this implementation
34
// speaks directly to the p2p network. As a result, this implementation of the
35
// ChainNotifier interface is much more light weight that other implementation
36
// which rely of receiving notification over an RPC interface backed by a
37
// running full node.
38
//
39
// TODO(roasbeef): heavily consolidate with NeutrinoNotifier code
40
//   - maybe combine into single package?
41
type NeutrinoNotifier struct {
42
        epochClientCounter uint64 // To be used atomically.
43

44
        start   sync.Once
45
        active  int32 // To be used atomically.
46
        stopped int32 // To be used atomically.
47

48
        bestBlockMtx sync.RWMutex
49
        bestBlock    chainntnfs.BlockEpoch
50

51
        p2pNode   *neutrino.ChainService
52
        chainView *neutrino.Rescan
53

54
        chainConn *NeutrinoChainConn
55

56
        notificationCancels  chan interface{}
57
        notificationRegistry chan interface{}
58

59
        txNotifier *chainntnfs.TxNotifier
60

61
        blockEpochClients map[uint64]*blockEpochRegistration
62

63
        rescanErr <-chan error
64

65
        chainUpdates chan *filteredBlock
66

67
        txUpdates *queue.ConcurrentQueue
68

69
        // spendHintCache is a cache used to query and update the latest height
70
        // hints for an outpoint. Each height hint represents the earliest
71
        // height at which the outpoint could have been spent within the chain.
72
        spendHintCache chainntnfs.SpendHintCache
73

74
        // confirmHintCache is a cache used to query the latest height hints for
75
        // a transaction. Each height hint represents the earliest height at
76
        // which the transaction could have confirmed within the chain.
77
        confirmHintCache chainntnfs.ConfirmHintCache
78

79
        // blockCache is an LRU block cache.
80
        blockCache *blockcache.BlockCache
81

82
        wg   sync.WaitGroup
83
        quit chan struct{}
84
}
85

86
// Ensure NeutrinoNotifier implements the ChainNotifier interface at compile time.
87
var _ chainntnfs.ChainNotifier = (*NeutrinoNotifier)(nil)
88

89
// New creates a new instance of the NeutrinoNotifier concrete implementation
90
// of the ChainNotifier interface.
91
//
92
// NOTE: The passed neutrino node should already be running and active before
93
// being passed into this function.
94
func New(node *neutrino.ChainService, spendHintCache chainntnfs.SpendHintCache,
95
        confirmHintCache chainntnfs.ConfirmHintCache,
96
        blockCache *blockcache.BlockCache) *NeutrinoNotifier {
×
97

×
98
        return &NeutrinoNotifier{
×
99
                notificationCancels:  make(chan interface{}),
×
100
                notificationRegistry: make(chan interface{}),
×
101

×
102
                blockEpochClients: make(map[uint64]*blockEpochRegistration),
×
103

×
104
                p2pNode:   node,
×
105
                chainConn: &NeutrinoChainConn{node},
×
106

×
107
                rescanErr: make(chan error),
×
108

×
109
                chainUpdates: make(chan *filteredBlock, 100),
×
110

×
111
                txUpdates: queue.NewConcurrentQueue(10),
×
112

×
113
                spendHintCache:   spendHintCache,
×
114
                confirmHintCache: confirmHintCache,
×
115

×
116
                blockCache: blockCache,
×
117

×
118
                quit: make(chan struct{}),
×
119
        }
×
120
}
×
121

122
// Start contacts the running neutrino light client and kicks off an initial
123
// empty rescan.
124
func (n *NeutrinoNotifier) Start() error {
×
125
        var startErr error
×
126
        n.start.Do(func() {
×
127
                startErr = n.startNotifier()
×
128
        })
×
129
        return startErr
×
130
}
131

132
// Stop shuts down the NeutrinoNotifier.
133
func (n *NeutrinoNotifier) Stop() error {
×
134
        // Already shutting down?
×
135
        if atomic.AddInt32(&n.stopped, 1) != 1 {
×
136
                return nil
×
137
        }
×
138

139
        chainntnfs.Log.Info("neutrino notifier shutting down...")
×
140
        defer chainntnfs.Log.Debug("neutrino notifier shutdown complete")
×
141

×
142
        close(n.quit)
×
143
        n.wg.Wait()
×
144

×
145
        n.txUpdates.Stop()
×
146

×
147
        // Notify all pending clients of our shutdown by closing the related
×
148
        // notification channels.
×
149
        for _, epochClient := range n.blockEpochClients {
×
150
                close(epochClient.cancelChan)
×
151
                epochClient.wg.Wait()
×
152

×
153
                close(epochClient.epochChan)
×
154
        }
×
155
        n.txNotifier.TearDown()
×
156

×
157
        return nil
×
158
}
159

160
// Started returns true if this instance has been started, and false otherwise.
161
func (n *NeutrinoNotifier) Started() bool {
×
162
        return atomic.LoadInt32(&n.active) != 0
×
163
}
×
164

165
func (n *NeutrinoNotifier) startNotifier() error {
×
166
        // Start our concurrent queues before starting the rescan, to ensure
×
167
        // onFilteredBlockConnected and onRelavantTx callbacks won't be
×
168
        // blocked.
×
169
        n.txUpdates.Start()
×
170

×
171
        // First, we'll obtain the latest block height of the p2p node. We'll
×
172
        // start the auto-rescan from this point. Once a caller actually wishes
×
173
        // to register a chain view, the rescan state will be rewound
×
174
        // accordingly.
×
175
        startingPoint, err := n.p2pNode.BestBlock()
×
176
        if err != nil {
×
177
                n.txUpdates.Stop()
×
178
                return err
×
179
        }
×
180
        startingHeader, err := n.p2pNode.GetBlockHeader(
×
181
                &startingPoint.Hash,
×
182
        )
×
183
        if err != nil {
×
184
                n.txUpdates.Stop()
×
185
                return err
×
186
        }
×
187

188
        n.bestBlock.Hash = &startingPoint.Hash
×
189
        n.bestBlock.Height = startingPoint.Height
×
190
        n.bestBlock.BlockHeader = startingHeader
×
191

×
192
        n.txNotifier = chainntnfs.NewTxNotifier(
×
193
                uint32(n.bestBlock.Height), chainntnfs.ReorgSafetyLimit,
×
194
                n.confirmHintCache, n.spendHintCache,
×
195
        )
×
196

×
197
        // Next, we'll create our set of rescan options. Currently it's
×
198
        // required that a user MUST set an addr/outpoint/txid when creating a
×
199
        // rescan. To get around this, we'll add a "zero" outpoint, that won't
×
200
        // actually be matched.
×
201
        var zeroInput neutrino.InputWithScript
×
202
        rescanOptions := []neutrino.RescanOption{
×
203
                neutrino.StartBlock(startingPoint),
×
204
                neutrino.QuitChan(n.quit),
×
205
                neutrino.NotificationHandlers(
×
206
                        rpcclient.NotificationHandlers{
×
207
                                OnFilteredBlockConnected:    n.onFilteredBlockConnected,
×
208
                                OnFilteredBlockDisconnected: n.onFilteredBlockDisconnected,
×
209
                                OnRedeemingTx:               n.onRelevantTx,
×
210
                        },
×
211
                ),
×
212
                neutrino.WatchInputs(zeroInput),
×
213
        }
×
214

×
215
        // Finally, we'll create our rescan struct, start it, and launch all
×
216
        // the goroutines we need to operate this ChainNotifier instance.
×
217
        n.chainView = neutrino.NewRescan(
×
218
                &neutrino.RescanChainSource{
×
219
                        ChainService: n.p2pNode,
×
220
                },
×
221
                rescanOptions...,
×
222
        )
×
223
        n.rescanErr = n.chainView.Start()
×
224

×
225
        n.wg.Add(1)
×
226
        go n.notificationDispatcher()
×
227

×
228
        // Set the active flag now that we've completed the full
×
229
        // startup.
×
230
        atomic.StoreInt32(&n.active, 1)
×
231

×
232
        return nil
×
233
}
234

235
// filteredBlock represents a new block which has been connected to the main
236
// chain. The slice of transactions will only be populated if the block
237
// includes a transaction that confirmed one of our watched txids, or spends
238
// one of the outputs currently being watched.
239
type filteredBlock struct {
240
        header *wire.BlockHeader
241
        hash   chainhash.Hash
242
        height uint32
243
        txns   []*btcutil.Tx
244

245
        // connected is true if this update is a new block and false if it is a
246
        // disconnected block.
247
        connect bool
248
}
249

250
// rescanFilterUpdate represents a request that will be sent to the
251
// notificaionRegistry in order to prevent race conditions between the filter
252
// update and new block notifications.
253
type rescanFilterUpdate struct {
254
        updateOptions []neutrino.UpdateOption
255
        errChan       chan error
256
}
257

258
// onFilteredBlockConnected is a callback which is executed each a new block is
259
// connected to the end of the main chain.
260
func (n *NeutrinoNotifier) onFilteredBlockConnected(height int32,
261
        header *wire.BlockHeader, txns []*btcutil.Tx) {
×
262

×
263
        // Append this new chain update to the end of the queue of new chain
×
264
        // updates.
×
265
        select {
×
266
        case n.chainUpdates <- &filteredBlock{
267
                hash:    header.BlockHash(),
268
                height:  uint32(height),
269
                txns:    txns,
270
                header:  header,
271
                connect: true,
272
        }:
×
273
        case <-n.quit:
×
274
        }
275
}
276

277
// onFilteredBlockDisconnected is a callback which is executed each time a new
278
// block has been disconnected from the end of the mainchain due to a re-org.
279
func (n *NeutrinoNotifier) onFilteredBlockDisconnected(height int32,
280
        header *wire.BlockHeader) {
×
281

×
282
        // Append this new chain update to the end of the queue of new chain
×
283
        // disconnects.
×
284
        select {
×
285
        case n.chainUpdates <- &filteredBlock{
286
                hash:    header.BlockHash(),
287
                height:  uint32(height),
288
                connect: false,
289
        }:
×
290
        case <-n.quit:
×
291
        }
292
}
293

294
// relevantTx represents a relevant transaction to the notifier that fulfills
295
// any outstanding spend requests.
296
type relevantTx struct {
297
        tx      *btcutil.Tx
298
        details *btcjson.BlockDetails
299
}
300

301
// onRelevantTx is a callback that proxies relevant transaction notifications
302
// from the backend to the notifier's main event handler.
303
func (n *NeutrinoNotifier) onRelevantTx(tx *btcutil.Tx, details *btcjson.BlockDetails) {
×
304
        select {
×
305
        case n.txUpdates.ChanIn() <- &relevantTx{tx, details}:
×
306
        case <-n.quit:
×
307
        }
308
}
309

310
// connectFilteredBlock is called when we receive a filteredBlock from the
311
// backend. If the block is ahead of what we're expecting, we'll attempt to
312
// catch up and then process the block.
313
func (n *NeutrinoNotifier) connectFilteredBlock(update *filteredBlock) {
×
314
        n.bestBlockMtx.Lock()
×
315
        defer n.bestBlockMtx.Unlock()
×
316

×
317
        if update.height != uint32(n.bestBlock.Height+1) {
×
318
                chainntnfs.Log.Infof("Missed blocks, attempting to catch up")
×
319

×
320
                _, missedBlocks, err := chainntnfs.HandleMissedBlocks(
×
321
                        n.chainConn, n.txNotifier, n.bestBlock,
×
322
                        int32(update.height), false,
×
323
                )
×
324
                if err != nil {
×
325
                        chainntnfs.Log.Error(err)
×
326
                        return
×
327
                }
×
328

329
                for _, block := range missedBlocks {
×
330
                        filteredBlock, err := n.getFilteredBlock(block)
×
331
                        if err != nil {
×
332
                                chainntnfs.Log.Error(err)
×
333
                                return
×
334
                        }
×
335
                        err = n.handleBlockConnected(filteredBlock)
×
336
                        if err != nil {
×
337
                                chainntnfs.Log.Error(err)
×
338
                                return
×
339
                        }
×
340
                }
341
        }
342

343
        err := n.handleBlockConnected(update)
×
344
        if err != nil {
×
345
                chainntnfs.Log.Error(err)
×
346
        }
×
347
}
348

349
// disconnectFilteredBlock is called when our disconnected filtered block
350
// callback is fired. It attempts to rewind the chain to before the
351
// disconnection and updates our best block.
352
func (n *NeutrinoNotifier) disconnectFilteredBlock(update *filteredBlock) {
×
353
        n.bestBlockMtx.Lock()
×
354
        defer n.bestBlockMtx.Unlock()
×
355

×
356
        if update.height != uint32(n.bestBlock.Height) {
×
357
                chainntnfs.Log.Infof("Missed disconnected blocks, attempting" +
×
358
                        " to catch up")
×
359
        }
×
360
        newBestBlock, err := chainntnfs.RewindChain(n.chainConn, n.txNotifier,
×
361
                n.bestBlock, int32(update.height-1),
×
362
        )
×
363
        if err != nil {
×
364
                chainntnfs.Log.Errorf("Unable to rewind chain from height %d"+
×
365
                        "to height %d: %v", n.bestBlock.Height,
×
366
                        update.height-1, err,
×
367
                )
×
368
        }
×
369

370
        n.bestBlock = newBestBlock
×
371
}
372

373
// drainChainUpdates is called after updating the filter. It reads every
374
// buffered item off the chan and returns when no more are available. It is
375
// used to ensure that callers performing a historical scan properly update
376
// their EndHeight to scan blocks that did not have the filter applied at
377
// processing time. Without this, a race condition exists that could allow a
378
// spend or confirmation notification to be missed. It is unlikely this would
379
// occur in a real-world scenario, and instead would manifest itself in tests.
380
func (n *NeutrinoNotifier) drainChainUpdates() {
×
381
        for {
×
382
                select {
×
383
                case update := <-n.chainUpdates:
×
384
                        if update.connect {
×
385
                                n.connectFilteredBlock(update)
×
386
                                break
×
387
                        }
388
                        n.disconnectFilteredBlock(update)
×
389
                default:
×
390
                        return
×
391
                }
392
        }
393
}
394

395
// notificationDispatcher is the primary goroutine which handles client
396
// notification registrations, as well as notification dispatches.
397
func (n *NeutrinoNotifier) notificationDispatcher() {
×
398
        defer n.wg.Done()
×
399

×
400
        for {
×
401
                select {
×
402
                case cancelMsg := <-n.notificationCancels:
×
403
                        switch msg := cancelMsg.(type) {
×
404
                        case *epochCancel:
×
405
                                chainntnfs.Log.Infof("Cancelling epoch "+
×
406
                                        "notification, epoch_id=%v", msg.epochID)
×
407

×
408
                                // First, we'll lookup the original
×
409
                                // registration in order to stop the active
×
410
                                // queue goroutine.
×
411
                                reg := n.blockEpochClients[msg.epochID]
×
412
                                reg.epochQueue.Stop()
×
413

×
414
                                // Next, close the cancel channel for this
×
415
                                // specific client, and wait for the client to
×
416
                                // exit.
×
417
                                close(n.blockEpochClients[msg.epochID].cancelChan)
×
418
                                n.blockEpochClients[msg.epochID].wg.Wait()
×
419

×
420
                                // Once the client has exited, we can then
×
421
                                // safely close the channel used to send epoch
×
422
                                // notifications, in order to notify any
×
423
                                // listeners that the intent has been
×
424
                                // canceled.
×
425
                                close(n.blockEpochClients[msg.epochID].epochChan)
×
426
                                delete(n.blockEpochClients, msg.epochID)
×
427
                        }
428

429
                case registerMsg := <-n.notificationRegistry:
×
430
                        switch msg := registerMsg.(type) {
×
431
                        case *chainntnfs.HistoricalConfDispatch:
×
432
                                // We'll start a historical rescan chain of the
×
433
                                // chain asynchronously to prevent blocking
×
434
                                // potentially long rescans.
×
435
                                n.wg.Add(1)
×
436

×
437
                                //nolint:lll
×
438
                                go func(msg *chainntnfs.HistoricalConfDispatch) {
×
439
                                        defer n.wg.Done()
×
440

×
441
                                        confDetails, err := n.historicalConfDetails(
×
442
                                                msg.ConfRequest,
×
443
                                                msg.StartHeight, msg.EndHeight,
×
444
                                        )
×
445
                                        if err != nil {
×
446
                                                chainntnfs.Log.Error(err)
×
447
                                                return
×
448
                                        }
×
449

450
                                        // If the historical dispatch finished
451
                                        // without error, we will invoke
452
                                        // UpdateConfDetails even if none were
453
                                        // found. This allows the notifier to
454
                                        // begin safely updating the height hint
455
                                        // cache at tip, since any pending
456
                                        // rescans have now completed.
457
                                        err = n.txNotifier.UpdateConfDetails(
×
458
                                                msg.ConfRequest, confDetails,
×
459
                                        )
×
460
                                        if err != nil {
×
461
                                                chainntnfs.Log.Error(err)
×
462
                                        }
×
463
                                }(msg)
464

465
                        case *blockEpochRegistration:
×
466
                                chainntnfs.Log.Infof("New block epoch subscription")
×
467

×
468
                                n.blockEpochClients[msg.epochID] = msg
×
469

×
470
                                // If the client did not provide their best
×
471
                                // known block, then we'll immediately dispatch
×
472
                                // a notification for the current tip.
×
473
                                if msg.bestBlock == nil {
×
474
                                        n.notifyBlockEpochClient(
×
475
                                                msg, n.bestBlock.Height,
×
476
                                                n.bestBlock.Hash,
×
477
                                                n.bestBlock.BlockHeader,
×
478
                                        )
×
479

×
480
                                        msg.errorChan <- nil
×
481
                                        continue
×
482
                                }
483

484
                                // Otherwise, we'll attempt to deliver the
485
                                // backlog of notifications from their best
486
                                // known block.
487
                                n.bestBlockMtx.Lock()
×
488
                                bestHeight := n.bestBlock.Height
×
489
                                n.bestBlockMtx.Unlock()
×
490

×
491
                                missedBlocks, err := chainntnfs.GetClientMissedBlocks(
×
492
                                        n.chainConn, msg.bestBlock, bestHeight,
×
493
                                        false,
×
494
                                )
×
495
                                if err != nil {
×
496
                                        msg.errorChan <- err
×
497
                                        continue
×
498
                                }
499

500
                                for _, block := range missedBlocks {
×
501
                                        n.notifyBlockEpochClient(
×
502
                                                msg, block.Height, block.Hash,
×
503
                                                block.BlockHeader,
×
504
                                        )
×
505
                                }
×
506

507
                                msg.errorChan <- nil
×
508

509
                        case *rescanFilterUpdate:
×
510
                                err := n.chainView.Update(msg.updateOptions...)
×
511
                                if err != nil {
×
512
                                        chainntnfs.Log.Errorf("Unable to "+
×
513
                                                "update rescan filter: %v", err)
×
514
                                }
×
515

516
                                // Drain the chainUpdates chan so the caller
517
                                // listening on errChan can be sure that
518
                                // updates after receiving the error will have
519
                                // the filter applied. This allows the caller
520
                                // to update their EndHeight if they're
521
                                // performing a historical scan.
522
                                n.drainChainUpdates()
×
523

×
524
                                // After draining, send the error to the
×
525
                                // caller.
×
526
                                msg.errChan <- err
×
527
                        }
528

529
                case item := <-n.chainUpdates:
×
530
                        update := item
×
531
                        if update.connect {
×
532
                                n.connectFilteredBlock(update)
×
533
                                continue
×
534
                        }
535

536
                        n.disconnectFilteredBlock(update)
×
537

538
                case txUpdate := <-n.txUpdates.ChanOut():
×
539
                        // A new relevant transaction notification has been
×
540
                        // received from the backend. We'll attempt to process
×
541
                        // it to determine if it fulfills any outstanding
×
542
                        // confirmation and/or spend requests and dispatch
×
543
                        // notifications for them.
×
544
                        update := txUpdate.(*relevantTx)
×
545
                        err := n.txNotifier.ProcessRelevantSpendTx(
×
546
                                update.tx, uint32(update.details.Height),
×
547
                        )
×
548
                        if err != nil {
×
549
                                chainntnfs.Log.Errorf("Unable to process "+
×
550
                                        "transaction %v: %v", update.tx.Hash(),
×
551
                                        err)
×
552
                        }
×
553

554
                case err := <-n.rescanErr:
×
555
                        chainntnfs.Log.Errorf("Error during rescan: %v", err)
×
556

557
                case <-n.quit:
×
558
                        return
×
559

560
                }
561
        }
562
}
563

564
// historicalConfDetails looks up whether a confirmation request (txid/output
565
// script) has already been included in a block in the active chain and, if so,
566
// returns details about said block.
567
func (n *NeutrinoNotifier) historicalConfDetails(confRequest chainntnfs.ConfRequest,
568
        startHeight, endHeight uint32) (*chainntnfs.TxConfirmation, error) {
×
569

×
570
        // Starting from the height hint, we'll walk forwards in the chain to
×
571
        // see if this transaction/output script has already been confirmed.
×
572
        for scanHeight := endHeight; scanHeight >= startHeight && scanHeight > 0; scanHeight-- {
×
573
                // Ensure we haven't been requested to shut down before
×
574
                // processing the next height.
×
575
                select {
×
576
                case <-n.quit:
×
577
                        return nil, chainntnfs.ErrChainNotifierShuttingDown
×
578
                default:
×
579
                }
580

581
                // First, we'll fetch the block header for this height so we
582
                // can compute the current block hash.
583
                blockHash, err := n.p2pNode.GetBlockHash(int64(scanHeight))
×
584
                if err != nil {
×
585
                        return nil, fmt.Errorf("unable to get header for "+
×
586
                                "height=%v: %w", scanHeight, err)
×
587
                }
×
588

589
                // With the hash computed, we can now fetch the basic filter for this
590
                // height. Since the range of required items is known we avoid
591
                // roundtrips by requesting a batched response and save bandwidth by
592
                // limiting the max number of items per batch. Since neutrino populates
593
                // its underline filters cache with the batch response, the next call
594
                // will execute a network query only once per batch and not on every
595
                // iteration.
596
                regFilter, err := n.p2pNode.GetCFilter(
×
597
                        *blockHash, wire.GCSFilterRegular,
×
598
                        neutrino.NumRetries(5),
×
599
                        neutrino.OptimisticReverseBatch(),
×
600
                        neutrino.MaxBatchSize(int64(scanHeight-startHeight+1)),
×
601
                )
×
602
                if err != nil {
×
603
                        return nil, fmt.Errorf("unable to retrieve regular "+
×
604
                                "filter for height=%v: %w", scanHeight, err)
×
605
                }
×
606

607
                // In the case that the filter exists, we'll attempt to see if
608
                // any element in it matches our target public key script.
609
                key := builder.DeriveKey(blockHash)
×
610
                match, err := regFilter.Match(key, confRequest.PkScript.Script())
×
611
                if err != nil {
×
612
                        return nil, fmt.Errorf("unable to query filter: %w",
×
613
                                err)
×
614
                }
×
615

616
                // If there's no match, then we can continue forward to the
617
                // next block.
618
                if !match {
×
619
                        continue
×
620
                }
621

622
                // In the case that we do have a match, we'll fetch the block
623
                // from the network so we can find the positional data required
624
                // to send the proper response.
625
                block, err := n.GetBlock(*blockHash)
×
626
                if err != nil {
×
627
                        return nil, fmt.Errorf("unable to get block from "+
×
628
                                "network: %w", err)
×
629
                }
×
630

631
                // For every transaction in the block, check which one matches
632
                // our request. If we find one that does, we can dispatch its
633
                // confirmation details.
634
                for i, tx := range block.Transactions() {
×
635
                        if !confRequest.MatchesTx(tx.MsgTx()) {
×
636
                                continue
×
637
                        }
638

639
                        return &chainntnfs.TxConfirmation{
×
640
                                Tx:          tx.MsgTx().Copy(),
×
641
                                BlockHash:   blockHash,
×
642
                                BlockHeight: scanHeight,
×
643
                                TxIndex:     uint32(i),
×
644
                                Block:       block.MsgBlock(),
×
645
                        }, nil
×
646
                }
647
        }
648

649
        return nil, nil
×
650
}
651

652
// handleBlockConnected applies a chain update for a new block. Any watched
653
// transactions included this block will processed to either send notifications
654
// now or after numConfirmations confs.
655
//
656
// NOTE: This method must be called with the bestBlockMtx lock held.
657
func (n *NeutrinoNotifier) handleBlockConnected(newBlock *filteredBlock) error {
×
658
        // We'll extend the txNotifier's height with the information of this
×
659
        // new block, which will handle all of the notification logic for us.
×
660
        //
×
661
        // We actually need the _full_ block here as well in order to be able
×
662
        // to send the full block back up to the client. The neutrino client
×
663
        // itself will only dispatch a block if one of the items we're looking
×
664
        // for matches, so ultimately passing it the full block will still only
×
665
        // result in the items we care about being dispatched.
×
666
        rawBlock, err := n.GetBlock(newBlock.hash)
×
667
        if err != nil {
×
668
                return fmt.Errorf("unable to get full block: %w", err)
×
669
        }
×
670
        err = n.txNotifier.ConnectTip(rawBlock, newBlock.height)
×
671
        if err != nil {
×
672
                return fmt.Errorf("unable to connect tip: %w", err)
×
673
        }
×
674

675
        chainntnfs.Log.Infof("New block: height=%v, sha=%v", newBlock.height,
×
676
                newBlock.hash)
×
677

×
678
        // Now that we've guaranteed the new block extends the txNotifier's
×
679
        // current tip, we'll proceed to dispatch notifications to all of our
×
680
        // registered clients whom have had notifications fulfilled. Before
×
681
        // doing so, we'll make sure update our in memory state in order to
×
682
        // satisfy any client requests based upon the new block.
×
683
        n.bestBlock.Hash = &newBlock.hash
×
684
        n.bestBlock.Height = int32(newBlock.height)
×
685
        n.bestBlock.BlockHeader = newBlock.header
×
686

×
687
        n.notifyBlockEpochs(
×
688
                int32(newBlock.height), &newBlock.hash, newBlock.header,
×
689
        )
×
690
        return n.txNotifier.NotifyHeight(newBlock.height)
×
691
}
692

693
// getFilteredBlock is a utility to retrieve the full filtered block from a block epoch.
694
func (n *NeutrinoNotifier) getFilteredBlock(epoch chainntnfs.BlockEpoch) (*filteredBlock, error) {
×
695
        rawBlock, err := n.GetBlock(*epoch.Hash)
×
696
        if err != nil {
×
697
                return nil, fmt.Errorf("unable to get block: %w", err)
×
698
        }
×
699

700
        txns := rawBlock.Transactions()
×
701

×
702
        block := &filteredBlock{
×
703
                hash:    *epoch.Hash,
×
704
                height:  uint32(epoch.Height),
×
705
                header:  &rawBlock.MsgBlock().Header,
×
706
                txns:    txns,
×
707
                connect: true,
×
708
        }
×
709
        return block, nil
×
710
}
711

712
// notifyBlockEpochs notifies all registered block epoch clients of the newly
713
// connected block to the main chain.
714
func (n *NeutrinoNotifier) notifyBlockEpochs(newHeight int32, newSha *chainhash.Hash,
715
        blockHeader *wire.BlockHeader) {
×
716

×
717
        for _, client := range n.blockEpochClients {
×
718
                n.notifyBlockEpochClient(client, newHeight, newSha, blockHeader)
×
719
        }
×
720
}
721

722
// notifyBlockEpochClient sends a registered block epoch client a notification
723
// about a specific block.
724
func (n *NeutrinoNotifier) notifyBlockEpochClient(epochClient *blockEpochRegistration,
725
        height int32, sha *chainhash.Hash, blockHeader *wire.BlockHeader) {
×
726

×
727
        epoch := &chainntnfs.BlockEpoch{
×
728
                Height:      height,
×
729
                Hash:        sha,
×
730
                BlockHeader: blockHeader,
×
731
        }
×
732

×
733
        select {
×
734
        case epochClient.epochQueue.ChanIn() <- epoch:
×
735
        case <-epochClient.cancelChan:
×
736
        case <-n.quit:
×
737
        }
738
}
739

740
// RegisterSpendNtfn registers an intent to be notified once the target
741
// outpoint/output script has been spent by a transaction on-chain. When
742
// intending to be notified of the spend of an output script, a nil outpoint
743
// must be used. The heightHint should represent the earliest height in the
744
// chain of the transaction that spent the outpoint/output script.
745
//
746
// Once a spend of has been detected, the details of the spending event will be
747
// sent across the 'Spend' channel.
748
func (n *NeutrinoNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
749
        pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
×
750

×
751
        // Register the conf notification with the TxNotifier. A non-nil value
×
752
        // for `dispatch` will be returned if we are required to perform a
×
753
        // manual scan for the confirmation. Otherwise the notifier will begin
×
754
        // watching at tip for the transaction to confirm.
×
755
        ntfn, err := n.txNotifier.RegisterSpend(outpoint, pkScript, heightHint)
×
756
        if err != nil {
×
757
                return nil, err
×
758
        }
×
759

760
        // To determine whether this outpoint has been spent on-chain, we'll
761
        // update our filter to watch for the transaction at tip and we'll also
762
        // dispatch a historical rescan to determine if it has been spent in the
763
        // past.
764
        //
765
        // We'll update our filter first to ensure we can immediately detect the
766
        // spend at tip.
767
        if outpoint == nil {
×
768
                outpoint = &chainntnfs.ZeroOutPoint
×
769
        }
×
770
        inputToWatch := neutrino.InputWithScript{
×
771
                OutPoint: *outpoint,
×
772
                PkScript: pkScript,
×
773
        }
×
774
        updateOptions := []neutrino.UpdateOption{
×
775
                neutrino.AddInputs(inputToWatch),
×
776
                neutrino.DisableDisconnectedNtfns(true),
×
777
        }
×
778

×
779
        // We'll use the txNotifier's tip as the starting point of our filter
×
780
        // update. In the case of an output script spend request, we'll check if
×
781
        // we should perform a historical rescan and start from there, as we
×
782
        // cannot do so with GetUtxo since it matches outpoints.
×
783
        rewindHeight := ntfn.Height
×
784
        if ntfn.HistoricalDispatch != nil && *outpoint == chainntnfs.ZeroOutPoint {
×
785
                rewindHeight = ntfn.HistoricalDispatch.StartHeight
×
786
        }
×
787
        updateOptions = append(updateOptions, neutrino.Rewind(rewindHeight))
×
788

×
789
        errChan := make(chan error, 1)
×
790
        select {
×
791
        case n.notificationRegistry <- &rescanFilterUpdate{
792
                updateOptions: updateOptions,
793
                errChan:       errChan,
794
        }:
×
795
        case <-n.quit:
×
796
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
797
        }
798

799
        select {
×
800
        case err = <-errChan:
×
801
        case <-n.quit:
×
802
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
803
        }
804
        if err != nil {
×
805
                return nil, fmt.Errorf("unable to update filter: %w", err)
×
806
        }
×
807

808
        // If the txNotifier didn't return any details to perform a historical
809
        // scan of the chain, or if we already performed one like in the case of
810
        // output script spend requests, then we can return early as there's
811
        // nothing left for us to do.
812
        if ntfn.HistoricalDispatch == nil || *outpoint == chainntnfs.ZeroOutPoint {
×
813
                return ntfn.Event, nil
×
814
        }
×
815

816
        // Grab the current best height as the height may have been updated
817
        // while we were draining the chainUpdates queue.
818
        n.bestBlockMtx.RLock()
×
819
        currentHeight := uint32(n.bestBlock.Height)
×
820
        n.bestBlockMtx.RUnlock()
×
821

×
822
        ntfn.HistoricalDispatch.EndHeight = currentHeight
×
823

×
824
        // With the filter updated, we'll dispatch our historical rescan to
×
825
        // ensure we detect the spend if it happened in the past.
×
826
        n.wg.Add(1)
×
827
        go func() {
×
828
                defer n.wg.Done()
×
829

×
830
                // We'll ensure that neutrino is caught up to the starting
×
831
                // height before we attempt to fetch the UTXO from the chain.
×
832
                // If we're behind, then we may miss a notification dispatch.
×
833
                for {
×
834
                        n.bestBlockMtx.RLock()
×
835
                        currentHeight := uint32(n.bestBlock.Height)
×
836
                        n.bestBlockMtx.RUnlock()
×
837

×
838
                        if currentHeight >= ntfn.HistoricalDispatch.StartHeight {
×
839
                                break
×
840
                        }
841

842
                        select {
×
843
                        case <-time.After(time.Millisecond * 200):
×
844
                        case <-n.quit:
×
845
                                return
×
846
                        }
847
                }
848

849
                spendReport, err := n.p2pNode.GetUtxo(
×
850
                        neutrino.WatchInputs(inputToWatch),
×
851
                        neutrino.StartBlock(&headerfs.BlockStamp{
×
852
                                Height: int32(ntfn.HistoricalDispatch.StartHeight),
×
853
                        }),
×
854
                        neutrino.EndBlock(&headerfs.BlockStamp{
×
855
                                Height: int32(ntfn.HistoricalDispatch.EndHeight),
×
856
                        }),
×
857
                        neutrino.ProgressHandler(func(processedHeight uint32) {
×
858
                                // We persist the rescan progress to achieve incremental
×
859
                                // behavior across restarts, otherwise long rescans may
×
860
                                // start from the beginning with every restart.
×
861
                                err := n.spendHintCache.CommitSpendHint(
×
862
                                        processedHeight,
×
863
                                        ntfn.HistoricalDispatch.SpendRequest)
×
864
                                if err != nil {
×
865
                                        chainntnfs.Log.Errorf("Failed to update rescan "+
×
866
                                                "progress: %v", err)
×
867
                                }
×
868
                        }),
869
                        neutrino.QuitChan(n.quit),
870
                )
871
                if err != nil && !strings.Contains(err.Error(), "not found") {
×
872
                        chainntnfs.Log.Errorf("Failed getting UTXO: %v", err)
×
873
                        return
×
874
                }
×
875

876
                // If a spend report was returned, and the transaction is present, then
877
                // this means that the output is already spent.
878
                var spendDetails *chainntnfs.SpendDetail
×
879
                if spendReport != nil && spendReport.SpendingTx != nil {
×
880
                        spendingTxHash := spendReport.SpendingTx.TxHash()
×
881
                        spendDetails = &chainntnfs.SpendDetail{
×
882
                                SpentOutPoint:     outpoint,
×
883
                                SpenderTxHash:     &spendingTxHash,
×
884
                                SpendingTx:        spendReport.SpendingTx,
×
885
                                SpenderInputIndex: spendReport.SpendingInputIndex,
×
886
                                SpendingHeight:    int32(spendReport.SpendingTxHeight),
×
887
                        }
×
888
                }
×
889

890
                // Finally, no matter whether the rescan found a spend in the past or
891
                // not, we'll mark our historical rescan as complete to ensure the
892
                // outpoint's spend hint gets updated upon connected/disconnected
893
                // blocks.
894
                err = n.txNotifier.UpdateSpendDetails(
×
895
                        ntfn.HistoricalDispatch.SpendRequest, spendDetails,
×
896
                )
×
897
                if err != nil {
×
898
                        chainntnfs.Log.Errorf("Failed to update spend details: %v", err)
×
899
                        return
×
900
                }
×
901
        }()
902

903
        return ntfn.Event, nil
×
904
}
905

906
// RegisterConfirmationsNtfn registers an intent to be notified once the target
907
// txid/output script has reached numConfs confirmations on-chain. When
908
// intending to be notified of the confirmation of an output script, a nil txid
909
// must be used. The heightHint should represent the earliest height at which
910
// the txid/output script could have been included in the chain.
911
//
912
// Progress on the number of confirmations left can be read from the 'Updates'
913
// channel. Once it has reached all of its confirmations, a notification will be
914
// sent across the 'Confirmed' channel.
915
func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
916
        pkScript []byte, numConfs, heightHint uint32,
917
        opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent, error) {
×
918

×
919
        // Register the conf notification with the TxNotifier. A non-nil value
×
920
        // for `dispatch` will be returned if we are required to perform a
×
921
        // manual scan for the confirmation. Otherwise the notifier will begin
×
922
        // watching at tip for the transaction to confirm.
×
923
        ntfn, err := n.txNotifier.RegisterConf(
×
924
                txid, pkScript, numConfs, heightHint, opts...,
×
925
        )
×
926
        if err != nil {
×
927
                return nil, err
×
928
        }
×
929

930
        // To determine whether this transaction has confirmed on-chain, we'll
931
        // update our filter to watch for the transaction at tip and we'll also
932
        // dispatch a historical rescan to determine if it has confirmed in the
933
        // past.
934
        //
935
        // We'll update our filter first to ensure we can immediately detect the
936
        // confirmation at tip. To do so, we'll map the script into an address
937
        // type so we can instruct neutrino to match if the transaction
938
        // containing the script is found in a block.
939
        params := n.p2pNode.ChainParams()
×
940
        _, addrs, _, err := txscript.ExtractPkScriptAddrs(pkScript, &params)
×
941
        if err != nil {
×
942
                return nil, fmt.Errorf("unable to extract script: %w", err)
×
943
        }
×
944

945
        // We'll send the filter update request to the notifier's main event
946
        // handler and wait for its response.
947
        errChan := make(chan error, 1)
×
948
        select {
×
949
        case n.notificationRegistry <- &rescanFilterUpdate{
950
                updateOptions: []neutrino.UpdateOption{
951
                        neutrino.AddAddrs(addrs...),
952
                        neutrino.Rewind(ntfn.Height),
953
                        neutrino.DisableDisconnectedNtfns(true),
954
                },
955
                errChan: errChan,
956
        }:
×
957
        case <-n.quit:
×
958
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
959
        }
960

961
        select {
×
962
        case err = <-errChan:
×
963
        case <-n.quit:
×
964
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
965
        }
966
        if err != nil {
×
967
                return nil, fmt.Errorf("unable to update filter: %w", err)
×
968
        }
×
969

970
        // If a historical rescan was not requested by the txNotifier, then we
971
        // can return to the caller.
972
        if ntfn.HistoricalDispatch == nil {
×
973
                return ntfn.Event, nil
×
974
        }
×
975

976
        // Grab the current best height as the height may have been updated
977
        // while we were draining the chainUpdates queue.
978
        n.bestBlockMtx.RLock()
×
979
        currentHeight := uint32(n.bestBlock.Height)
×
980
        n.bestBlockMtx.RUnlock()
×
981

×
982
        ntfn.HistoricalDispatch.EndHeight = currentHeight
×
983

×
984
        // Finally, with the filter updated, we can dispatch the historical
×
985
        // rescan to ensure we can detect if the event happened in the past.
×
986
        select {
×
987
        case n.notificationRegistry <- ntfn.HistoricalDispatch:
×
988
        case <-n.quit:
×
989
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
990
        }
991

992
        return ntfn.Event, nil
×
993
}
994

995
// GetBlock is used to retrieve the block with the given hash. Since the block
996
// cache used by neutrino will be the same as that used by LND (since it is
997
// passed to neutrino on initialisation), the neutrino GetBlock method can be
998
// called directly since it already uses the block cache. However, neutrino
999
// does not lock the block cache mutex for the given block hash and so that is
1000
// done here.
1001
func (n *NeutrinoNotifier) GetBlock(hash chainhash.Hash) (
1002
        *btcutil.Block, error) {
×
1003

×
1004
        n.blockCache.HashMutex.Lock(lntypes.Hash(hash))
×
1005
        defer n.blockCache.HashMutex.Unlock(lntypes.Hash(hash))
×
1006

×
1007
        return n.p2pNode.GetBlock(hash)
×
1008
}
×
1009

1010
// blockEpochRegistration represents a client's intent to receive a
1011
// notification with each newly connected block.
1012
type blockEpochRegistration struct {
1013
        epochID uint64
1014

1015
        epochChan chan *chainntnfs.BlockEpoch
1016

1017
        epochQueue *queue.ConcurrentQueue
1018

1019
        cancelChan chan struct{}
1020

1021
        bestBlock *chainntnfs.BlockEpoch
1022

1023
        errorChan chan error
1024

1025
        wg sync.WaitGroup
1026
}
1027

1028
// epochCancel is a message sent to the NeutrinoNotifier when a client wishes
1029
// to cancel an outstanding epoch notification that has yet to be dispatched.
1030
type epochCancel struct {
1031
        epochID uint64
1032
}
1033

1034
// RegisterBlockEpochNtfn returns a BlockEpochEvent which subscribes the
1035
// caller to receive notifications, of each new block connected to the main
1036
// chain. Clients have the option of passing in their best known block, which
1037
// the notifier uses to check if they are behind on blocks and catch them up. If
1038
// they do not provide one, then a notification will be dispatched immediately
1039
// for the current tip of the chain upon a successful registration.
1040
func (n *NeutrinoNotifier) RegisterBlockEpochNtfn(
1041
        bestBlock *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error) {
×
1042

×
1043
        reg := &blockEpochRegistration{
×
1044
                epochQueue: queue.NewConcurrentQueue(20),
×
1045
                epochChan:  make(chan *chainntnfs.BlockEpoch, 20),
×
1046
                cancelChan: make(chan struct{}),
×
1047
                epochID:    atomic.AddUint64(&n.epochClientCounter, 1),
×
1048
                bestBlock:  bestBlock,
×
1049
                errorChan:  make(chan error, 1),
×
1050
        }
×
1051
        reg.epochQueue.Start()
×
1052

×
1053
        // Before we send the request to the main goroutine, we'll launch a new
×
1054
        // goroutine to proxy items added to our queue to the client itself.
×
1055
        // This ensures that all notifications are received *in order*.
×
1056
        reg.wg.Add(1)
×
1057
        go func() {
×
1058
                defer reg.wg.Done()
×
1059

×
1060
                for {
×
1061
                        select {
×
1062
                        case ntfn := <-reg.epochQueue.ChanOut():
×
1063
                                blockNtfn := ntfn.(*chainntnfs.BlockEpoch)
×
1064
                                select {
×
1065
                                case reg.epochChan <- blockNtfn:
×
1066

1067
                                case <-reg.cancelChan:
×
1068
                                        return
×
1069

1070
                                case <-n.quit:
×
1071
                                        return
×
1072
                                }
1073

1074
                        case <-reg.cancelChan:
×
1075
                                return
×
1076

1077
                        case <-n.quit:
×
1078
                                return
×
1079
                        }
1080
                }
1081
        }()
1082

1083
        select {
×
1084
        case <-n.quit:
×
1085
                // As we're exiting before the registration could be sent,
×
1086
                // we'll stop the queue now ourselves.
×
1087
                reg.epochQueue.Stop()
×
1088

×
1089
                return nil, errors.New("chainntnfs: system interrupt while " +
×
1090
                        "attempting to register for block epoch notification.")
×
1091
        case n.notificationRegistry <- reg:
×
1092
                return &chainntnfs.BlockEpochEvent{
×
1093
                        Epochs: reg.epochChan,
×
1094
                        Cancel: func() {
×
1095
                                cancel := &epochCancel{
×
1096
                                        epochID: reg.epochID,
×
1097
                                }
×
1098

×
1099
                                // Submit epoch cancellation to notification dispatcher.
×
1100
                                select {
×
1101
                                case n.notificationCancels <- cancel:
×
1102
                                        // Cancellation is being handled, drain the epoch channel until it is
×
1103
                                        // closed before yielding to caller.
×
1104
                                        for {
×
1105
                                                select {
×
1106
                                                case _, ok := <-reg.epochChan:
×
1107
                                                        if !ok {
×
1108
                                                                return
×
1109
                                                        }
×
1110
                                                case <-n.quit:
×
1111
                                                        return
×
1112
                                                }
1113
                                        }
1114
                                case <-n.quit:
×
1115
                                }
1116
                        },
1117
                }, nil
1118
        }
1119
}
1120

1121
// NeutrinoChainConn is a wrapper around neutrino's chain backend in order
1122
// to satisfy the chainntnfs.ChainConn interface.
1123
type NeutrinoChainConn struct {
1124
        p2pNode *neutrino.ChainService
1125
}
1126

1127
// GetBlockHeader returns the block header for a hash.
1128
func (n *NeutrinoChainConn) GetBlockHeader(blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
×
1129
        return n.p2pNode.GetBlockHeader(blockHash)
×
1130
}
×
1131

1132
// GetBlockHeaderVerbose returns a verbose block header result for a hash. This
1133
// result only contains the height with a nil hash.
1134
func (n *NeutrinoChainConn) GetBlockHeaderVerbose(blockHash *chainhash.Hash) (
1135
        *btcjson.GetBlockHeaderVerboseResult, error) {
×
1136

×
1137
        height, err := n.p2pNode.GetBlockHeight(blockHash)
×
1138
        if err != nil {
×
1139
                return nil, err
×
1140
        }
×
1141
        // Since only the height is used from the result, leave the hash nil.
1142
        return &btcjson.GetBlockHeaderVerboseResult{Height: int32(height)}, nil
×
1143
}
1144

1145
// GetBlockHash returns the hash from a block height.
1146
func (n *NeutrinoChainConn) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
×
1147
        return n.p2pNode.GetBlockHash(blockHeight)
×
1148
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc