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

lightningnetwork / lnd / 14493620719

16 Apr 2025 01:13PM UTC coverage: 58.577% (-10.5%) from 69.084%
14493620719

Pull #9722

github

web-flow
Merge 08088bb15 into 06f1ef47f
Pull Request #9722: Change RPC call order for the btcd notifier

3 of 11 new or added lines in 1 file covered. (27.27%)

28287 existing lines in 450 files now uncovered.

97134 of 165824 relevant lines covered (58.58%)

1.82 hits per line

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

70.43
/chainntnfs/btcdnotify/btcd.go
1
package btcdnotify
2

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

10
        "github.com/btcsuite/btcd/btcjson"
11
        "github.com/btcsuite/btcd/btcutil"
12
        "github.com/btcsuite/btcd/chaincfg"
13
        "github.com/btcsuite/btcd/chaincfg/chainhash"
14
        "github.com/btcsuite/btcd/rpcclient"
15
        "github.com/btcsuite/btcd/txscript"
16
        "github.com/btcsuite/btcd/wire"
17
        "github.com/btcsuite/btcwallet/chain"
18
        "github.com/lightningnetwork/lnd/blockcache"
19
        "github.com/lightningnetwork/lnd/chainntnfs"
20
        "github.com/lightningnetwork/lnd/fn/v2"
21
        "github.com/lightningnetwork/lnd/queue"
22
)
23

24
const (
25
        // notifierType uniquely identifies this concrete implementation of the
26
        // ChainNotifier interface.
27
        notifierType = "btcd"
28
)
29

30
// chainUpdate encapsulates an update to the current main chain. This struct is
31
// used as an element within an unbounded queue in order to avoid blocking the
32
// main rpc dispatch rule.
33
type chainUpdate struct {
34
        blockHash   *chainhash.Hash
35
        blockHeight int32
36

37
        // connected is true if this update is a new block and false if it is a
38
        // disconnected block.
39
        connect bool
40
}
41

42
// txUpdate encapsulates a transaction related notification sent from btcd to
43
// the registered RPC client. This struct is used as an element within an
44
// unbounded queue in order to avoid blocking the main rpc dispatch rule.
45
type txUpdate struct {
46
        tx      *btcutil.Tx
47
        details *btcjson.BlockDetails
48
}
49

50
// TODO(roasbeef): generalize struct below:
51
//  * move chans to config, allow outside callers to handle send conditions
52

53
// BtcdNotifier implements the ChainNotifier interface using btcd's websockets
54
// notifications. Multiple concurrent clients are supported. All notifications
55
// are achieved via non-blocking sends on client channels.
56
type BtcdNotifier struct {
57
        epochClientCounter uint64 // To be used atomically.
58

59
        start   sync.Once
60
        active  int32 // To be used atomically.
61
        stopped int32 // To be used atomically.
62

63
        chainConn   *chain.RPCClient
64
        chainParams *chaincfg.Params
65

66
        notificationCancels  chan interface{}
67
        notificationRegistry chan interface{}
68

69
        txNotifier *chainntnfs.TxNotifier
70

71
        blockEpochClients map[uint64]*blockEpochRegistration
72

73
        bestBlock chainntnfs.BlockEpoch
74

75
        // blockCache is a LRU block cache.
76
        blockCache *blockcache.BlockCache
77

78
        chainUpdates *queue.ConcurrentQueue
79
        txUpdates    *queue.ConcurrentQueue
80

81
        // spendHintCache is a cache used to query and update the latest height
82
        // hints for an outpoint. Each height hint represents the earliest
83
        // height at which the outpoint could have been spent within the chain.
84
        spendHintCache chainntnfs.SpendHintCache
85

86
        // confirmHintCache is a cache used to query the latest height hints for
87
        // a transaction. Each height hint represents the earliest height at
88
        // which the transaction could have confirmed within the chain.
89
        confirmHintCache chainntnfs.ConfirmHintCache
90

91
        // memNotifier notifies clients of events related to the mempool.
92
        memNotifier *chainntnfs.MempoolNotifier
93

94
        wg   sync.WaitGroup
95
        quit chan struct{}
96
}
97

98
// Ensure BtcdNotifier implements the ChainNotifier interface at compile time.
99
var _ chainntnfs.ChainNotifier = (*BtcdNotifier)(nil)
100

101
// Ensure BtcdNotifier implements the MempoolWatcher interface at compile time.
102
var _ chainntnfs.MempoolWatcher = (*BtcdNotifier)(nil)
103

104
// New returns a new BtcdNotifier instance. This function assumes the btcd node
105
// detailed in the passed configuration is already running, and willing to
106
// accept new websockets clients.
107
func New(config *rpcclient.ConnConfig, chainParams *chaincfg.Params,
108
        spendHintCache chainntnfs.SpendHintCache,
109
        confirmHintCache chainntnfs.ConfirmHintCache,
110
        blockCache *blockcache.BlockCache) (*BtcdNotifier, error) {
1✔
111

1✔
112
        notifier := &BtcdNotifier{
1✔
113
                chainParams: chainParams,
1✔
114

1✔
115
                notificationCancels:  make(chan interface{}),
1✔
116
                notificationRegistry: make(chan interface{}),
1✔
117

1✔
118
                blockEpochClients: make(map[uint64]*blockEpochRegistration),
1✔
119

1✔
120
                chainUpdates: queue.NewConcurrentQueue(10),
1✔
121
                txUpdates:    queue.NewConcurrentQueue(10),
1✔
122

1✔
123
                spendHintCache:   spendHintCache,
1✔
124
                confirmHintCache: confirmHintCache,
1✔
125

1✔
126
                blockCache:  blockCache,
1✔
127
                memNotifier: chainntnfs.NewMempoolNotifier(),
1✔
128

1✔
129
                quit: make(chan struct{}),
1✔
130
        }
1✔
131

1✔
132
        ntfnCallbacks := &rpcclient.NotificationHandlers{
1✔
133
                OnBlockConnected:    notifier.onBlockConnected,
1✔
134
                OnBlockDisconnected: notifier.onBlockDisconnected,
1✔
135
                OnRedeemingTx:       notifier.onRedeemingTx,
1✔
136
        }
1✔
137

1✔
138
        rpcCfg := &chain.RPCClientConfig{
1✔
139
                ReconnectAttempts:    20,
1✔
140
                Conn:                 config,
1✔
141
                Chain:                chainParams,
1✔
142
                NotificationHandlers: ntfnCallbacks,
1✔
143
        }
1✔
144

1✔
145
        chainRPC, err := chain.NewRPCClientWithConfig(rpcCfg)
1✔
146
        if err != nil {
1✔
147
                return nil, err
×
148
        }
×
149

150
        notifier.chainConn = chainRPC
1✔
151

1✔
152
        return notifier, nil
1✔
153
}
154

155
// Start connects to the running btcd node over websockets, registers for block
156
// notifications, and finally launches all related helper goroutines.
157
func (b *BtcdNotifier) Start() error {
1✔
158
        var startErr error
1✔
159
        b.start.Do(func() {
2✔
160
                startErr = b.startNotifier()
1✔
161
        })
1✔
162

163
        return startErr
1✔
164
}
165

166
// Started returns true if this instance has been started, and false otherwise.
167
func (b *BtcdNotifier) Started() bool {
1✔
168
        return atomic.LoadInt32(&b.active) != 0
1✔
169
}
1✔
170

171
// Stop shutsdown the BtcdNotifier.
172
func (b *BtcdNotifier) Stop() error {
1✔
173
        // Already shutting down?
1✔
174
        if atomic.AddInt32(&b.stopped, 1) != 1 {
1✔
175
                return nil
×
176
        }
×
177

178
        chainntnfs.Log.Info("btcd notifier shutting down...")
1✔
179
        defer chainntnfs.Log.Debug("btcd notifier shutdown complete")
1✔
180

1✔
181
        // Shutdown the rpc client, this gracefully disconnects from btcd, and
1✔
182
        // cleans up all related resources.
1✔
183
        b.chainConn.Stop()
1✔
184

1✔
185
        close(b.quit)
1✔
186
        b.wg.Wait()
1✔
187

1✔
188
        b.chainUpdates.Stop()
1✔
189
        b.txUpdates.Stop()
1✔
190

1✔
191
        // Notify all pending clients of our shutdown by closing the related
1✔
192
        // notification channels.
1✔
193
        for _, epochClient := range b.blockEpochClients {
2✔
194
                close(epochClient.cancelChan)
1✔
195
                epochClient.wg.Wait()
1✔
196

1✔
197
                close(epochClient.epochChan)
1✔
198
        }
1✔
199
        b.txNotifier.TearDown()
1✔
200

1✔
201
        // Stop the mempool notifier.
1✔
202
        b.memNotifier.TearDown()
1✔
203

1✔
204
        return nil
1✔
205
}
206

207
// startNotifier is the main starting point for the BtcdNotifier. It connects
208
// to btcd and start the main dispatcher goroutine.
209
func (b *BtcdNotifier) startNotifier() error {
1✔
210
        chainntnfs.Log.Infof("btcd notifier starting...")
1✔
211

1✔
212
        // Start our concurrent queues before starting the chain connection, to
1✔
213
        // ensure onBlockConnected and onRedeemingTx callbacks won't be
1✔
214
        // blocked.
1✔
215
        b.chainUpdates.Start()
1✔
216
        b.txUpdates.Start()
1✔
217

1✔
218
        // Connect to btcd, and register for notifications on connected, and
1✔
219
        // disconnected blocks.
1✔
220
        if err := b.chainConn.Connect(20); err != nil {
1✔
221
                b.txUpdates.Stop()
×
222
                b.chainUpdates.Stop()
×
223
                return err
×
224
        }
×
225

226
        // Before we fetch the best block we need to register the notifications
227
        // otherwise we might think we are at a prior block but the
228
        // notification for the blocks were registered after the best block.
229
        if err := b.chainConn.NotifyBlocks(); err != nil {
1✔
NEW
230
                b.txUpdates.Stop()
×
NEW
231
                b.chainUpdates.Stop()
×
NEW
232
                return err
×
NEW
233
        }
×
234

235
        currentHash, currentHeight, err := b.chainConn.GetBestBlock()
1✔
236
        if err != nil {
1✔
NEW
237
                b.txUpdates.Stop()
×
NEW
238
                b.chainUpdates.Stop()
×
NEW
239
                return err
×
NEW
240
        }
×
241

242
        bestBlock, err := b.chainConn.GetBlock(currentHash)
1✔
243
        if err != nil {
1✔
244
                b.txUpdates.Stop()
×
245
                b.chainUpdates.Stop()
×
246
                return err
×
UNCOV
247
        }
×
248

249
        b.txNotifier = chainntnfs.NewTxNotifier(
1✔
250
                uint32(currentHeight), chainntnfs.ReorgSafetyLimit,
1✔
251
                b.confirmHintCache, b.spendHintCache,
1✔
252
        )
1✔
253

1✔
254
        b.bestBlock = chainntnfs.BlockEpoch{
1✔
255
                Height:      currentHeight,
1✔
256
                Hash:        currentHash,
1✔
257
                BlockHeader: &bestBlock.Header,
1✔
258
        }
1✔
259

1✔
260
        b.wg.Add(1)
1✔
261
        go b.notificationDispatcher()
1✔
262

1✔
263
        // Set the active flag now that we've completed the full
1✔
264
        // startup.
1✔
265
        atomic.StoreInt32(&b.active, 1)
1✔
266

1✔
267
        chainntnfs.Log.Debugf("btcd notifier started")
1✔
268

1✔
269
        return nil
1✔
270
}
271

272
// onBlockConnected implements on OnBlockConnected callback for rpcclient.
273
// Ingesting a block updates the wallet's internal utxo state based on the
274
// outputs created and destroyed within each block.
275
func (b *BtcdNotifier) onBlockConnected(hash *chainhash.Hash, height int32, t time.Time) {
1✔
276
        // Append this new chain update to the end of the queue of new chain
1✔
277
        // updates.
1✔
278
        select {
1✔
279
        case b.chainUpdates.ChanIn() <- &chainUpdate{
280
                blockHash:   hash,
281
                blockHeight: height,
282
                connect:     true,
283
        }:
1✔
UNCOV
284
        case <-b.quit:
×
UNCOV
285
                return
×
286
        }
287
}
288

289
// filteredBlock represents a new block which has been connected to the main
290
// chain. The slice of transactions will only be populated if the block
291
// includes a transaction that confirmed one of our watched txids, or spends
292
// one of the outputs currently being watched.
293
//
294
// TODO(halseth): this is currently used for complete blocks. Change to use
295
// onFilteredBlockConnected and onFilteredBlockDisconnected, making it easier
296
// to unify with the Neutrino implementation.
297
type filteredBlock struct {
298
        hash   chainhash.Hash
299
        height uint32
300
        block  *btcutil.Block
301

302
        // connected is true if this update is a new block and false if it is a
303
        // disconnected block.
304
        connect bool
305
}
306

307
// onBlockDisconnected implements on OnBlockDisconnected callback for rpcclient.
308
func (b *BtcdNotifier) onBlockDisconnected(hash *chainhash.Hash, height int32, t time.Time) {
1✔
309
        // Append this new chain update to the end of the queue of new chain
1✔
310
        // updates.
1✔
311
        select {
1✔
312
        case b.chainUpdates.ChanIn() <- &chainUpdate{
313
                blockHash:   hash,
314
                blockHeight: height,
315
                connect:     false,
316
        }:
1✔
UNCOV
317
        case <-b.quit:
×
UNCOV
318
                return
×
319
        }
320
}
321

322
// onRedeemingTx implements on OnRedeemingTx callback for rpcclient.
323
func (b *BtcdNotifier) onRedeemingTx(tx *btcutil.Tx, details *btcjson.BlockDetails) {
1✔
324
        // Append this new transaction update to the end of the queue of new
1✔
325
        // chain updates.
1✔
326
        select {
1✔
327
        case b.txUpdates.ChanIn() <- &txUpdate{tx, details}:
1✔
UNCOV
328
        case <-b.quit:
×
UNCOV
329
                return
×
330
        }
331
}
332

333
// notificationDispatcher is the primary goroutine which handles client
334
// notification registrations, as well as notification dispatches.
335
func (b *BtcdNotifier) notificationDispatcher() {
1✔
336
        defer b.wg.Done()
1✔
337

1✔
338
out:
1✔
339
        for {
2✔
340
                select {
1✔
341
                case cancelMsg := <-b.notificationCancels:
1✔
342
                        switch msg := cancelMsg.(type) {
1✔
343
                        case *epochCancel:
1✔
344
                                chainntnfs.Log.Infof("Cancelling epoch "+
1✔
345
                                        "notification, epoch_id=%v", msg.epochID)
1✔
346

1✔
347
                                // First, we'll lookup the original
1✔
348
                                // registration in order to stop the active
1✔
349
                                // queue goroutine.
1✔
350
                                reg := b.blockEpochClients[msg.epochID]
1✔
351
                                reg.epochQueue.Stop()
1✔
352

1✔
353
                                // Next, close the cancel channel for this
1✔
354
                                // specific client, and wait for the client to
1✔
355
                                // exit.
1✔
356
                                close(b.blockEpochClients[msg.epochID].cancelChan)
1✔
357
                                b.blockEpochClients[msg.epochID].wg.Wait()
1✔
358

1✔
359
                                // Once the client has exited, we can then
1✔
360
                                // safely close the channel used to send epoch
1✔
361
                                // notifications, in order to notify any
1✔
362
                                // listeners that the intent has been
1✔
363
                                // canceled.
1✔
364
                                close(b.blockEpochClients[msg.epochID].epochChan)
1✔
365
                                delete(b.blockEpochClients, msg.epochID)
1✔
366
                        }
367
                case registerMsg := <-b.notificationRegistry:
1✔
368
                        switch msg := registerMsg.(type) {
1✔
369
                        case *chainntnfs.HistoricalConfDispatch:
1✔
370
                                // Look up whether the transaction/output script
1✔
371
                                // has already confirmed in the active chain.
1✔
372
                                // We'll do this in a goroutine to prevent
1✔
373
                                // blocking potentially long rescans.
1✔
374
                                //
1✔
375
                                // TODO(wilmer): add retry logic if rescan fails?
1✔
376
                                b.wg.Add(1)
1✔
377

1✔
378
                                //nolint:ll
1✔
379
                                go func(msg *chainntnfs.HistoricalConfDispatch) {
2✔
380
                                        defer b.wg.Done()
1✔
381

1✔
382
                                        confDetails, _, err := b.historicalConfDetails(
1✔
383
                                                msg.ConfRequest,
1✔
384
                                                msg.StartHeight, msg.EndHeight,
1✔
385
                                        )
1✔
386
                                        if err != nil {
1✔
UNCOV
387
                                                chainntnfs.Log.Error(err)
×
UNCOV
388
                                                return
×
UNCOV
389
                                        }
×
390

391
                                        // If the historical dispatch finished
392
                                        // without error, we will invoke
393
                                        // UpdateConfDetails even if none were
394
                                        // found. This allows the notifier to
395
                                        // begin safely updating the height hint
396
                                        // cache at tip, since any pending
397
                                        // rescans have now completed.
398
                                        err = b.txNotifier.UpdateConfDetails(
1✔
399
                                                msg.ConfRequest, confDetails,
1✔
400
                                        )
1✔
401
                                        if err != nil {
1✔
UNCOV
402
                                                chainntnfs.Log.Error(err)
×
UNCOV
403
                                        }
×
404
                                }(msg)
405

406
                        case *blockEpochRegistration:
1✔
407
                                chainntnfs.Log.Infof("New block epoch subscription")
1✔
408

1✔
409
                                b.blockEpochClients[msg.epochID] = msg
1✔
410

1✔
411
                                // If the client did not provide their best
1✔
412
                                // known block, then we'll immediately dispatch
1✔
413
                                // a notification for the current tip.
1✔
414
                                if msg.bestBlock == nil {
2✔
415
                                        b.notifyBlockEpochClient(
1✔
416
                                                msg, b.bestBlock.Height,
1✔
417
                                                b.bestBlock.Hash,
1✔
418
                                                b.bestBlock.BlockHeader,
1✔
419
                                        )
1✔
420

1✔
421
                                        msg.errorChan <- nil
1✔
422
                                        continue
1✔
423
                                }
424

425
                                // Otherwise, we'll attempt to deliver the
426
                                // backlog of notifications from their best
427
                                // known block.
428
                                missedBlocks, err := chainntnfs.GetClientMissedBlocks(
1✔
429
                                        b.chainConn, msg.bestBlock,
1✔
430
                                        b.bestBlock.Height, true,
1✔
431
                                )
1✔
432
                                if err != nil {
1✔
UNCOV
433
                                        msg.errorChan <- err
×
UNCOV
434
                                        continue
×
435
                                }
436

437
                                for _, block := range missedBlocks {
1✔
UNCOV
438
                                        b.notifyBlockEpochClient(
×
439
                                                msg, block.Height, block.Hash,
×
440
                                                block.BlockHeader,
×
UNCOV
441
                                        )
×
UNCOV
442
                                }
×
443

444
                                msg.errorChan <- nil
1✔
445
                        }
446

447
                case item := <-b.chainUpdates.ChanOut():
1✔
448
                        update := item.(*chainUpdate)
1✔
449
                        if update.connect {
2✔
450
                                blockHeader, err := b.chainConn.GetBlockHeader(
1✔
451
                                        update.blockHash,
1✔
452
                                )
1✔
453
                                if err != nil {
1✔
UNCOV
454
                                        chainntnfs.Log.Errorf("Unable to fetch "+
×
UNCOV
455
                                                "block header: %v", err)
×
UNCOV
456
                                        continue
×
457
                                }
458

459
                                if blockHeader.PrevBlock != *b.bestBlock.Hash {
1✔
UNCOV
460
                                        // Handle the case where the notifier
×
UNCOV
461
                                        // missed some blocks from its chain
×
UNCOV
462
                                        // backend
×
UNCOV
463
                                        chainntnfs.Log.Infof("Missed blocks, " +
×
UNCOV
464
                                                "attempting to catch up")
×
UNCOV
465
                                        newBestBlock, missedBlocks, err :=
×
UNCOV
466
                                                chainntnfs.HandleMissedBlocks(
×
UNCOV
467
                                                        b.chainConn,
×
UNCOV
468
                                                        b.txNotifier,
×
UNCOV
469
                                                        b.bestBlock,
×
UNCOV
470
                                                        update.blockHeight,
×
UNCOV
471
                                                        true,
×
UNCOV
472
                                                )
×
UNCOV
473
                                        if err != nil {
×
UNCOV
474
                                                // Set the bestBlock here in case
×
UNCOV
475
                                                // a catch up partially completed.
×
UNCOV
476
                                                b.bestBlock = newBestBlock
×
UNCOV
477
                                                chainntnfs.Log.Error(err)
×
UNCOV
478
                                                continue
×
479
                                        }
480

UNCOV
481
                                        for _, block := range missedBlocks {
×
UNCOV
482
                                                err := b.handleBlockConnected(block)
×
UNCOV
483
                                                if err != nil {
×
UNCOV
484
                                                        chainntnfs.Log.Error(err)
×
UNCOV
485
                                                        continue out
×
486
                                                }
487
                                        }
488
                                }
489

490
                                newBlock := chainntnfs.BlockEpoch{
1✔
491
                                        Height:      update.blockHeight,
1✔
492
                                        Hash:        update.blockHash,
1✔
493
                                        BlockHeader: blockHeader,
1✔
494
                                }
1✔
495
                                if err := b.handleBlockConnected(newBlock); err != nil {
1✔
UNCOV
496
                                        chainntnfs.Log.Error(err)
×
UNCOV
497
                                }
×
498
                                continue
1✔
499
                        }
500

501
                        if update.blockHeight != b.bestBlock.Height {
2✔
502
                                chainntnfs.Log.Infof("Missed disconnected" +
1✔
503
                                        "blocks, attempting to catch up")
1✔
504
                        }
1✔
505

506
                        newBestBlock, err := chainntnfs.RewindChain(
1✔
507
                                b.chainConn, b.txNotifier, b.bestBlock,
1✔
508
                                update.blockHeight-1,
1✔
509
                        )
1✔
510
                        if err != nil {
2✔
511
                                chainntnfs.Log.Errorf("Unable to rewind chain "+
1✔
512
                                        "from height %d to height %d: %v",
1✔
513
                                        b.bestBlock.Height, update.blockHeight-1, err)
1✔
514
                        }
1✔
515

516
                        // Set the bestBlock here in case a chain rewind
517
                        // partially completed.
518
                        b.bestBlock = newBestBlock
1✔
519

520
                case item := <-b.txUpdates.ChanOut():
1✔
521
                        newSpend := item.(*txUpdate)
1✔
522
                        tx := newSpend.tx
1✔
523

1✔
524
                        // Init values.
1✔
525
                        isMempool := false
1✔
526
                        height := uint32(0)
1✔
527

1✔
528
                        // Unwrap values.
1✔
529
                        if newSpend.details == nil {
2✔
530
                                isMempool = true
1✔
531
                        } else {
2✔
532
                                height = uint32(newSpend.details.Height)
1✔
533
                        }
1✔
534

535
                        // Handle the transaction.
536
                        b.handleRelevantTx(tx, isMempool, height)
1✔
537

538
                case <-b.quit:
1✔
539
                        break out
1✔
540
                }
541
        }
542
}
543

544
// handleRelevantTx handles a new transaction that has been seen either in a
545
// block or in the mempool. If in mempool, it will ask the mempool notifier to
546
// handle it. If in a block, it will ask the txNotifier to handle it, and
547
// cancel any relevant subscriptions made in the mempool.
548
func (b *BtcdNotifier) handleRelevantTx(tx *btcutil.Tx,
549
        mempool bool, height uint32) {
1✔
550

1✔
551
        // If this is a mempool spend, we'll ask the mempool notifier to handle
1✔
552
        // it.
1✔
553
        if mempool {
2✔
554
                err := b.memNotifier.ProcessRelevantSpendTx(tx)
1✔
555
                if err != nil {
1✔
UNCOV
556
                        chainntnfs.Log.Errorf("Unable to process transaction "+
×
UNCOV
557
                                "%v: %v", tx.Hash(), err)
×
UNCOV
558
                }
×
559

560
                return
1✔
561
        }
562

563
        // Otherwise this is a confirmed spend, and we'll ask the tx notifier
564
        // to handle it.
565
        err := b.txNotifier.ProcessRelevantSpendTx(tx, height)
1✔
566
        if err != nil {
1✔
UNCOV
567
                chainntnfs.Log.Errorf("Unable to process transaction %v: %v",
×
UNCOV
568
                        tx.Hash(), err)
×
UNCOV
569

×
UNCOV
570
                return
×
UNCOV
571
        }
×
572

573
        // Once the tx is processed, we will ask the memNotifier to unsubscribe
574
        // the input.
575
        //
576
        // NOTE(yy): we could build it into txNotifier.ProcessRelevantSpendTx,
577
        // but choose to implement it here so we can easily decouple the two
578
        // notifiers in the future.
579
        b.memNotifier.UnsubsribeConfirmedSpentTx(tx)
1✔
580
}
581

582
// historicalConfDetails looks up whether a confirmation request (txid/output
583
// script) has already been included in a block in the active chain and, if so,
584
// returns details about said block.
585
func (b *BtcdNotifier) historicalConfDetails(confRequest chainntnfs.ConfRequest,
586
        startHeight, endHeight uint32) (*chainntnfs.TxConfirmation,
587
        chainntnfs.TxConfStatus, error) {
1✔
588

1✔
589
        // If a txid was not provided, then we should dispatch upon seeing the
1✔
590
        // script on-chain, so we'll short-circuit straight to scanning manually
1✔
591
        // as there doesn't exist a script index to query.
1✔
592
        if confRequest.TxID == chainntnfs.ZeroHash {
1✔
UNCOV
593
                return b.confDetailsManually(
×
UNCOV
594
                        confRequest, startHeight, endHeight,
×
UNCOV
595
                )
×
UNCOV
596
        }
×
597

598
        // Otherwise, we'll dispatch upon seeing a transaction on-chain with the
599
        // given hash.
600
        //
601
        // We'll first attempt to retrieve the transaction using the node's
602
        // txindex.
603
        txNotFoundErr := "No information available about transaction"
1✔
604
        txConf, txStatus, err := chainntnfs.ConfDetailsFromTxIndex(
1✔
605
                b.chainConn, confRequest, txNotFoundErr,
1✔
606
        )
1✔
607

1✔
608
        // We'll then check the status of the transaction lookup returned to
1✔
609
        // determine whether we should proceed with any fallback methods.
1✔
610
        switch {
1✔
611

612
        // We failed querying the index for the transaction, fall back to
613
        // scanning manually.
UNCOV
614
        case err != nil:
×
UNCOV
615
                chainntnfs.Log.Debugf("Unable to determine confirmation of %v "+
×
UNCOV
616
                        "through the backend's txindex (%v), scanning manually",
×
UNCOV
617
                        confRequest.TxID, err)
×
UNCOV
618

×
UNCOV
619
                return b.confDetailsManually(
×
UNCOV
620
                        confRequest, startHeight, endHeight,
×
UNCOV
621
                )
×
622

623
        // The transaction was found within the node's mempool.
624
        case txStatus == chainntnfs.TxFoundMempool:
1✔
625

626
        // The transaction was found within the node's txindex.
627
        case txStatus == chainntnfs.TxFoundIndex:
1✔
628

629
        // The transaction was not found within the node's mempool or txindex.
630
        case txStatus == chainntnfs.TxNotFoundIndex:
1✔
631

632
        // Unexpected txStatus returned.
UNCOV
633
        default:
×
UNCOV
634
                return nil, txStatus,
×
UNCOV
635
                        fmt.Errorf("Got unexpected txConfStatus: %v", txStatus)
×
636
        }
637

638
        return txConf, txStatus, nil
1✔
639
}
640

641
// confDetailsManually looks up whether a transaction/output script has already
642
// been included in a block in the active chain by scanning the chain's blocks
643
// within the given range. If the transaction/output script is found, its
644
// confirmation details are returned. Otherwise, nil is returned.
645
func (b *BtcdNotifier) confDetailsManually(confRequest chainntnfs.ConfRequest,
646
        startHeight, endHeight uint32) (*chainntnfs.TxConfirmation,
UNCOV
647
        chainntnfs.TxConfStatus, error) {
×
UNCOV
648

×
UNCOV
649
        // Begin scanning blocks at every height to determine where the
×
UNCOV
650
        // transaction was included in.
×
UNCOV
651
        for height := endHeight; height >= startHeight && height > 0; height-- {
×
UNCOV
652
                // Ensure we haven't been requested to shut down before
×
UNCOV
653
                // processing the next height.
×
UNCOV
654
                select {
×
UNCOV
655
                case <-b.quit:
×
UNCOV
656
                        return nil, chainntnfs.TxNotFoundManually,
×
UNCOV
657
                                chainntnfs.ErrChainNotifierShuttingDown
×
UNCOV
658
                default:
×
659
                }
660

661
                blockHash, err := b.chainConn.GetBlockHash(int64(height))
×
662
                if err != nil {
×
663
                        return nil, chainntnfs.TxNotFoundManually,
×
UNCOV
664
                                fmt.Errorf("unable to get hash from block "+
×
UNCOV
665
                                        "with height %d", height)
×
UNCOV
666
                }
×
667

668
                // TODO: fetch the neutrino filters instead.
669
                block, err := b.GetBlock(blockHash)
×
670
                if err != nil {
×
671
                        return nil, chainntnfs.TxNotFoundManually,
×
672
                                fmt.Errorf("unable to get block with hash "+
×
UNCOV
673
                                        "%v: %v", blockHash, err)
×
UNCOV
674
                }
×
675

676
                // For every transaction in the block, check which one matches
677
                // our request. If we find one that does, we can dispatch its
678
                // confirmation details.
679
                for txIndex, tx := range block.Transactions {
×
680
                        if !confRequest.MatchesTx(tx) {
×
UNCOV
681
                                continue
×
682
                        }
683

UNCOV
684
                        return &chainntnfs.TxConfirmation{
×
UNCOV
685
                                Tx:          tx.Copy(),
×
UNCOV
686
                                BlockHash:   blockHash,
×
UNCOV
687
                                BlockHeight: height,
×
UNCOV
688
                                TxIndex:     uint32(txIndex),
×
UNCOV
689
                                Block:       block,
×
UNCOV
690
                        }, chainntnfs.TxFoundManually, nil
×
691
                }
692
        }
693

694
        // If we reach here, then we were not able to find the transaction
695
        // within a block, so we avoid returning an error.
UNCOV
696
        return nil, chainntnfs.TxNotFoundManually, nil
×
697
}
698

699
// handleBlockConnected applies a chain update for a new block. Any watched
700
// transactions included this block will processed to either send notifications
701
// now or after numConfirmations confs.
702
// TODO(halseth): this is reusing the neutrino notifier implementation, unify
703
// them.
704
func (b *BtcdNotifier) handleBlockConnected(epoch chainntnfs.BlockEpoch) error {
1✔
705
        // First, we'll fetch the raw block as we'll need to gather all the
1✔
706
        // transactions to determine whether any are relevant to our registered
1✔
707
        // clients.
1✔
708
        rawBlock, err := b.GetBlock(epoch.Hash)
1✔
709
        if err != nil {
1✔
UNCOV
710
                return fmt.Errorf("unable to get block: %w", err)
×
UNCOV
711
        }
×
712
        newBlock := &filteredBlock{
1✔
713
                hash:    *epoch.Hash,
1✔
714
                height:  uint32(epoch.Height),
1✔
715
                block:   btcutil.NewBlock(rawBlock),
1✔
716
                connect: true,
1✔
717
        }
1✔
718

1✔
719
        // We'll then extend the txNotifier's height with the information of
1✔
720
        // this new block, which will handle all of the notification logic for
1✔
721
        // us.
1✔
722
        err = b.txNotifier.ConnectTip(newBlock.block, newBlock.height)
1✔
723
        if err != nil {
1✔
UNCOV
724
                return fmt.Errorf("unable to connect tip: %w", err)
×
UNCOV
725
        }
×
726

727
        chainntnfs.Log.Infof("New block: height=%v, sha=%v", epoch.Height,
1✔
728
                epoch.Hash)
1✔
729

1✔
730
        // Now that we've guaranteed the new block extends the txNotifier's
1✔
731
        // current tip, we'll proceed to dispatch notifications to all of our
1✔
732
        // registered clients whom have had notifications fulfilled. Before
1✔
733
        // doing so, we'll make sure update our in memory state in order to
1✔
734
        // satisfy any client requests based upon the new block.
1✔
735
        b.bestBlock = epoch
1✔
736

1✔
737
        err = b.txNotifier.NotifyHeight(uint32(epoch.Height))
1✔
738
        if err != nil {
1✔
UNCOV
739
                return fmt.Errorf("unable to notify height: %w", err)
×
UNCOV
740
        }
×
741

742
        b.notifyBlockEpochs(
1✔
743
                epoch.Height, epoch.Hash, epoch.BlockHeader,
1✔
744
        )
1✔
745

1✔
746
        return nil
1✔
747
}
748

749
// notifyBlockEpochs notifies all registered block epoch clients of the newly
750
// connected block to the main chain.
751
func (b *BtcdNotifier) notifyBlockEpochs(newHeight int32,
752
        newSha *chainhash.Hash, blockHeader *wire.BlockHeader) {
1✔
753

1✔
754
        for _, client := range b.blockEpochClients {
2✔
755
                b.notifyBlockEpochClient(
1✔
756
                        client, newHeight, newSha, blockHeader,
1✔
757
                )
1✔
758
        }
1✔
759
}
760

761
// notifyBlockEpochClient sends a registered block epoch client a notification
762
// about a specific block.
763
func (b *BtcdNotifier) notifyBlockEpochClient(epochClient *blockEpochRegistration,
764
        height int32, sha *chainhash.Hash, blockHeader *wire.BlockHeader) {
1✔
765

1✔
766
        epoch := &chainntnfs.BlockEpoch{
1✔
767
                Height:      height,
1✔
768
                Hash:        sha,
1✔
769
                BlockHeader: blockHeader,
1✔
770
        }
1✔
771

1✔
772
        select {
1✔
773
        case epochClient.epochQueue.ChanIn() <- epoch:
1✔
UNCOV
774
        case <-epochClient.cancelChan:
×
UNCOV
775
        case <-b.quit:
×
776
        }
777
}
778

779
// RegisterSpendNtfn registers an intent to be notified once the target
780
// outpoint/output script has been spent by a transaction on-chain. When
781
// intending to be notified of the spend of an output script, a nil outpoint
782
// must be used. The heightHint should represent the earliest height in the
783
// chain of the transaction that spent the outpoint/output script.
784
//
785
// Once a spend of has been detected, the details of the spending event will be
786
// sent across the 'Spend' channel.
787
func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
788
        pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
1✔
789

1✔
790
        // Register the conf notification with the TxNotifier. A non-nil value
1✔
791
        // for `dispatch` will be returned if we are required to perform a
1✔
792
        // manual scan for the confirmation. Otherwise the notifier will begin
1✔
793
        // watching at tip for the transaction to confirm.
1✔
794
        ntfn, err := b.txNotifier.RegisterSpend(outpoint, pkScript, heightHint)
1✔
795
        if err != nil {
2✔
796
                return nil, err
1✔
797
        }
1✔
798

799
        // We'll then request the backend to notify us when it has detected the
800
        // outpoint/output script as spent.
801
        //
802
        // TODO(wilmer): use LoadFilter API instead.
803
        if outpoint == nil || *outpoint == chainntnfs.ZeroOutPoint {
1✔
UNCOV
804
                _, addrs, _, err := txscript.ExtractPkScriptAddrs(
×
UNCOV
805
                        pkScript, b.chainParams,
×
UNCOV
806
                )
×
UNCOV
807
                if err != nil {
×
UNCOV
808
                        return nil, fmt.Errorf("unable to parse script: %w",
×
UNCOV
809
                                err)
×
UNCOV
810
                }
×
UNCOV
811
                if err := b.chainConn.NotifyReceived(addrs); err != nil {
×
UNCOV
812
                        return nil, err
×
UNCOV
813
                }
×
814
        } else {
1✔
815
                ops := []*wire.OutPoint{outpoint}
1✔
816
                if err := b.chainConn.NotifySpent(ops); err != nil {
1✔
UNCOV
817
                        return nil, err
×
818
                }
×
819
        }
820

821
        // If the txNotifier didn't return any details to perform a historical
822
        // scan of the chain, then we can return early as there's nothing left
823
        // for us to do.
824
        if ntfn.HistoricalDispatch == nil {
2✔
825
                return ntfn.Event, nil
1✔
826
        }
1✔
827

828
        // Otherwise, we'll need to dispatch a historical rescan to determine if
829
        // the outpoint was already spent at a previous height.
830
        //
831
        // We'll short-circuit the path when dispatching the spend of a script,
832
        // rather than an outpoint, as there aren't any additional checks we can
833
        // make for scripts.
834
        if outpoint == nil || *outpoint == chainntnfs.ZeroOutPoint {
1✔
UNCOV
835
                startHash, err := b.chainConn.GetBlockHash(
×
UNCOV
836
                        int64(ntfn.HistoricalDispatch.StartHeight),
×
UNCOV
837
                )
×
UNCOV
838
                if err != nil {
×
UNCOV
839
                        return nil, err
×
UNCOV
840
                }
×
841

842
                // TODO(wilmer): add retry logic if rescan fails?
UNCOV
843
                _, addrs, _, err := txscript.ExtractPkScriptAddrs(
×
UNCOV
844
                        pkScript, b.chainParams,
×
845
                )
×
846
                if err != nil {
×
UNCOV
847
                        return nil, fmt.Errorf("unable to parse address: %w",
×
UNCOV
848
                                err)
×
UNCOV
849
                }
×
850

UNCOV
851
                asyncResult := b.chainConn.RescanAsync(startHash, addrs, nil)
×
UNCOV
852
                go func() {
×
853
                        if rescanErr := asyncResult.Receive(); rescanErr != nil {
×
854
                                chainntnfs.Log.Errorf("Rescan to determine "+
×
855
                                        "the spend details of %v failed: %v",
×
UNCOV
856
                                        ntfn.HistoricalDispatch.SpendRequest,
×
UNCOV
857
                                        rescanErr)
×
UNCOV
858
                        }
×
859
                }()
860

861
                return ntfn.Event, nil
×
862
        }
863

864
        // When dispatching spends of outpoints, there are a number of checks we
865
        // can make to start our rescan from a better height or completely avoid
866
        // it.
867
        //
868
        // We'll start by checking the backend's UTXO set to determine whether
869
        // the outpoint has been spent. If it hasn't, we can return to the
870
        // caller as well.
871
        txOut, err := b.chainConn.GetTxOut(&outpoint.Hash, outpoint.Index, true)
1✔
872
        if err != nil {
1✔
UNCOV
873
                return nil, err
×
UNCOV
874
        }
×
875
        if txOut != nil {
2✔
876
                // We'll let the txNotifier know the outpoint is still unspent
1✔
877
                // in order to begin updating its spend hint.
1✔
878
                err := b.txNotifier.UpdateSpendDetails(
1✔
879
                        ntfn.HistoricalDispatch.SpendRequest, nil,
1✔
880
                )
1✔
881
                if err != nil {
1✔
UNCOV
882
                        return nil, err
×
UNCOV
883
                }
×
884

885
                return ntfn.Event, nil
1✔
886
        }
887

888
        // Since the outpoint was spent, as it no longer exists within the UTXO
889
        // set, we'll determine when it happened by scanning the chain. We'll
890
        // begin by fetching the block hash of our starting height.
891
        startHash, err := b.chainConn.GetBlockHash(
1✔
892
                int64(ntfn.HistoricalDispatch.StartHeight),
1✔
893
        )
1✔
894
        if err != nil {
1✔
UNCOV
895
                return nil, fmt.Errorf("unable to get block hash for height "+
×
UNCOV
896
                        "%d: %v", ntfn.HistoricalDispatch.StartHeight, err)
×
UNCOV
897
        }
×
898

899
        // As a minimal optimization, we'll query the backend's transaction
900
        // index (if enabled) to determine if we have a better rescan starting
901
        // height. We can do this as the GetRawTransaction call will return the
902
        // hash of the block it was included in within the chain.
903
        tx, err := b.chainConn.GetRawTransactionVerbose(&outpoint.Hash)
1✔
904
        if err != nil {
2✔
905
                // Avoid returning an error if the transaction was not found to
1✔
906
                // proceed with fallback methods.
1✔
907
                jsonErr, ok := err.(*btcjson.RPCError)
1✔
908
                if !ok || jsonErr.Code != btcjson.ErrRPCNoTxInfo {
1✔
UNCOV
909
                        return nil, fmt.Errorf("unable to query for txid %v: "+
×
UNCOV
910
                                "%w", outpoint.Hash, err)
×
UNCOV
911
                }
×
912
        }
913

914
        // If the transaction index was enabled, we'll use the block's hash to
915
        // retrieve its height and check whether it provides a better starting
916
        // point for our rescan.
917
        if tx != nil {
2✔
918
                // If the transaction containing the outpoint hasn't confirmed
1✔
919
                // on-chain, then there's no need to perform a rescan.
1✔
920
                if tx.BlockHash == "" {
2✔
921
                        return ntfn.Event, nil
1✔
922
                }
1✔
923

924
                blockHash, err := chainhash.NewHashFromStr(tx.BlockHash)
1✔
925
                if err != nil {
1✔
UNCOV
926
                        return nil, err
×
UNCOV
927
                }
×
928
                blockHeader, err := b.chainConn.GetBlockHeaderVerbose(blockHash)
1✔
929
                if err != nil {
1✔
UNCOV
930
                        return nil, fmt.Errorf("unable to get header for "+
×
UNCOV
931
                                "block %v: %v", blockHash, err)
×
932
                }
×
933

934
                if uint32(blockHeader.Height) > ntfn.HistoricalDispatch.StartHeight {
2✔
935
                        startHash, err = b.chainConn.GetBlockHash(
1✔
936
                                int64(blockHeader.Height),
1✔
937
                        )
1✔
938
                        if err != nil {
1✔
UNCOV
939
                                return nil, fmt.Errorf("unable to get block "+
×
UNCOV
940
                                        "hash for height %d: %v",
×
UNCOV
941
                                        blockHeader.Height, err)
×
UNCOV
942
                        }
×
943
                }
944
        }
945

946
        // Now that we've determined the best starting point for our rescan,
947
        // we can go ahead and dispatch it.
948
        //
949
        // In order to ensure that we don't block the caller on what may be a
950
        // long rescan, we'll launch a new goroutine to handle the async result
951
        // of the rescan. We purposefully prevent from adding this goroutine to
952
        // the WaitGroup as we cannot wait for a quit signal due to the
953
        // asyncResult channel not being exposed.
954
        //
955
        // TODO(wilmer): add retry logic if rescan fails?
956
        asyncResult := b.chainConn.RescanAsync(
1✔
957
                startHash, nil, []*wire.OutPoint{outpoint},
1✔
958
        )
1✔
959
        go func() {
2✔
960
                if rescanErr := asyncResult.Receive(); rescanErr != nil {
1✔
UNCOV
961
                        chainntnfs.Log.Errorf("Rescan to determine the spend "+
×
UNCOV
962
                                "details of %v failed: %v", outpoint, rescanErr)
×
UNCOV
963
                }
×
964
        }()
965

966
        return ntfn.Event, nil
1✔
967
}
968

969
// RegisterConfirmationsNtfn registers an intent to be notified once the target
970
// txid/output script has reached numConfs confirmations on-chain. When
971
// intending to be notified of the confirmation of an output script, a nil txid
972
// must be used. The heightHint should represent the earliest height at which
973
// the txid/output script could have been included in the chain.
974
//
975
// Progress on the number of confirmations left can be read from the 'Updates'
976
// channel. Once it has reached all of its confirmations, a notification will be
977
// sent across the 'Confirmed' channel.
978
func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
979
        pkScript []byte, numConfs, heightHint uint32,
980
        opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent, error) {
1✔
981

1✔
982
        // Register the conf notification with the TxNotifier. A non-nil value
1✔
983
        // for `dispatch` will be returned if we are required to perform a
1✔
984
        // manual scan for the confirmation. Otherwise the notifier will begin
1✔
985
        // watching at tip for the transaction to confirm.
1✔
986
        ntfn, err := b.txNotifier.RegisterConf(
1✔
987
                txid, pkScript, numConfs, heightHint, opts...,
1✔
988
        )
1✔
989
        if err != nil {
1✔
UNCOV
990
                return nil, err
×
UNCOV
991
        }
×
992

993
        if ntfn.HistoricalDispatch == nil {
2✔
994
                return ntfn.Event, nil
1✔
995
        }
1✔
996

997
        select {
1✔
998
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
1✔
999
                return ntfn.Event, nil
1✔
UNCOV
1000
        case <-b.quit:
×
UNCOV
1001
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
1002
        }
1003
}
1004

1005
// blockEpochRegistration represents a client's intent to receive a
1006
// notification with each newly connected block.
1007
type blockEpochRegistration struct {
1008
        epochID uint64
1009

1010
        epochChan chan *chainntnfs.BlockEpoch
1011

1012
        epochQueue *queue.ConcurrentQueue
1013

1014
        bestBlock *chainntnfs.BlockEpoch
1015

1016
        errorChan chan error
1017

1018
        cancelChan chan struct{}
1019

1020
        wg sync.WaitGroup
1021
}
1022

1023
// epochCancel is a message sent to the BtcdNotifier when a client wishes to
1024
// cancel an outstanding epoch notification that has yet to be dispatched.
1025
type epochCancel struct {
1026
        epochID uint64
1027
}
1028

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

1✔
1038
        reg := &blockEpochRegistration{
1✔
1039
                epochQueue: queue.NewConcurrentQueue(20),
1✔
1040
                epochChan:  make(chan *chainntnfs.BlockEpoch, 20),
1✔
1041
                cancelChan: make(chan struct{}),
1✔
1042
                epochID:    atomic.AddUint64(&b.epochClientCounter, 1),
1✔
1043
                bestBlock:  bestBlock,
1✔
1044
                errorChan:  make(chan error, 1),
1✔
1045
        }
1✔
1046

1✔
1047
        reg.epochQueue.Start()
1✔
1048

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

1✔
1056
                for {
2✔
1057
                        select {
1✔
1058
                        case ntfn := <-reg.epochQueue.ChanOut():
1✔
1059
                                blockNtfn := ntfn.(*chainntnfs.BlockEpoch)
1✔
1060
                                select {
1✔
1061
                                case reg.epochChan <- blockNtfn:
1✔
1062

1063
                                case <-reg.cancelChan:
1✔
1064
                                        return
1✔
1065

UNCOV
1066
                                case <-b.quit:
×
UNCOV
1067
                                        return
×
1068
                                }
1069

1070
                        case <-reg.cancelChan:
1✔
1071
                                return
1✔
1072

1073
                        case <-b.quit:
1✔
1074
                                return
1✔
1075
                        }
1076
                }
1077
        }()
1078

1079
        select {
1✔
UNCOV
1080
        case <-b.quit:
×
UNCOV
1081
                // As we're exiting before the registration could be sent,
×
UNCOV
1082
                // we'll stop the queue now ourselves.
×
UNCOV
1083
                reg.epochQueue.Stop()
×
UNCOV
1084

×
UNCOV
1085
                return nil, errors.New("chainntnfs: system interrupt while " +
×
1086
                        "attempting to register for block epoch notification.")
×
1087
        case b.notificationRegistry <- reg:
1✔
1088
                return &chainntnfs.BlockEpochEvent{
1✔
1089
                        Epochs: reg.epochChan,
1✔
1090
                        Cancel: func() {
2✔
1091
                                cancel := &epochCancel{
1✔
1092
                                        epochID: reg.epochID,
1✔
1093
                                }
1✔
1094

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

1118
// GetBlock is used to retrieve the block with the given hash. This function
1119
// wraps the blockCache's GetBlock function.
1120
func (b *BtcdNotifier) GetBlock(hash *chainhash.Hash) (*wire.MsgBlock,
1121
        error) {
1✔
1122

1✔
1123
        return b.blockCache.GetBlock(hash, b.chainConn.GetBlock)
1✔
1124
}
1✔
1125

1126
// SubscribeMempoolSpent allows the caller to register a subscription to watch
1127
// for a spend of an outpoint in the mempool.The event will be dispatched once
1128
// the outpoint is spent in the mempool.
1129
//
1130
// NOTE: part of the MempoolWatcher interface.
1131
func (b *BtcdNotifier) SubscribeMempoolSpent(
1132
        outpoint wire.OutPoint) (*chainntnfs.MempoolSpendEvent, error) {
1✔
1133

1✔
1134
        event := b.memNotifier.SubscribeInput(outpoint)
1✔
1135

1✔
1136
        ops := []*wire.OutPoint{&outpoint}
1✔
1137

1✔
1138
        return event, b.chainConn.NotifySpent(ops)
1✔
1139
}
1✔
1140

1141
// CancelMempoolSpendEvent allows the caller to cancel a subscription to watch
1142
// for a spend of an outpoint in the mempool.
1143
//
1144
// NOTE: part of the MempoolWatcher interface.
1145
func (b *BtcdNotifier) CancelMempoolSpendEvent(
1146
        sub *chainntnfs.MempoolSpendEvent) {
1✔
1147

1✔
1148
        b.memNotifier.UnsubscribeEvent(sub)
1✔
1149
}
1✔
1150

1151
// LookupInputMempoolSpend takes an outpoint and queries the mempool to find
1152
// its spending tx. Returns the tx if found, otherwise fn.None.
1153
//
1154
// NOTE: part of the MempoolWatcher interface.
1155
func (b *BtcdNotifier) LookupInputMempoolSpend(
1156
        op wire.OutPoint) fn.Option[wire.MsgTx] {
1✔
1157

1✔
1158
        // Find the spending txid.
1✔
1159
        txid, found := b.chainConn.LookupInputMempoolSpend(op)
1✔
1160
        if !found {
2✔
1161
                return fn.None[wire.MsgTx]()
1✔
1162
        }
1✔
1163

1164
        // Query the spending tx using the id.
1165
        tx, err := b.chainConn.GetRawTransaction(&txid)
1✔
1166
        if err != nil {
1✔
UNCOV
1167
                // TODO(yy): enable logging errors in this package.
×
UNCOV
1168
                return fn.None[wire.MsgTx]()
×
UNCOV
1169
        }
×
1170

1171
        return fn.Some(*tx.MsgTx().Copy())
1✔
1172
}
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