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

lightningnetwork / lnd / 14446551845

14 Apr 2025 01:12PM UTC coverage: 57.404% (-1.2%) from 58.624%
14446551845

push

github

web-flow
Merge pull request #9703 from yyforyongyu/fix-attempt-hash

Patch htlc attempt hash for legacy payments

12 of 26 new or added lines in 1 file covered. (46.15%)

2039 existing lines in 44 files now uncovered.

95138 of 165734 relevant lines covered (57.4%)

0.61 hits per line

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

0.0
/chainntnfs/bitcoindnotify/bitcoind.go
1
package bitcoindnotify
2

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

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

22
const (
23
        // notifierType uniquely identifies a concrete implementation of the
24
        // ChainNotifier interface that makes use of the bitcoind ZMQ interface.
25
        notifierTypeZMQ = "bitcoind"
26

27
        // notifierTypeRPCPolling uniquely identifies a concrete implementation
28
        // of the ChainNotifier interface that makes use of the bitcoind RPC
29
        // interface.
30
        notifierTypeRPCPolling = "bitcoind-rpc-polling"
31
)
32

33
// TODO(roasbeef): generalize struct below:
34
//  * move chans to config
35
//  * extract common code
36
//  * allow outside callers to handle send conditions
37

38
// BitcoindNotifier implements the ChainNotifier interface using a bitcoind
39
// chain client. Multiple concurrent clients are supported. All notifications
40
// are achieved via non-blocking sends on client channels.
41
type BitcoindNotifier 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
        chainConn   *chain.BitcoindClient
49
        chainParams *chaincfg.Params
50

51
        notificationCancels  chan interface{}
52
        notificationRegistry chan interface{}
53

54
        txNotifier *chainntnfs.TxNotifier
55

56
        blockEpochClients map[uint64]*blockEpochRegistration
57

58
        bestBlock chainntnfs.BlockEpoch
59

60
        // blockCache is a LRU block cache.
61
        blockCache *blockcache.BlockCache
62

63
        // spendHintCache is a cache used to query and update the latest height
64
        // hints for an outpoint. Each height hint represents the earliest
65
        // height at which the outpoint could have been spent within the chain.
66
        spendHintCache chainntnfs.SpendHintCache
67

68
        // confirmHintCache is a cache used to query the latest height hints for
69
        // a transaction. Each height hint represents the earliest height at
70
        // which the transaction could have confirmed within the chain.
71
        confirmHintCache chainntnfs.ConfirmHintCache
72

73
        // memNotifier notifies clients of events related to the mempool.
74
        memNotifier *chainntnfs.MempoolNotifier
75

76
        wg   sync.WaitGroup
77
        quit chan struct{}
78
}
79

80
// Ensure BitcoindNotifier implements the ChainNotifier interface at compile
81
// time.
82
var _ chainntnfs.ChainNotifier = (*BitcoindNotifier)(nil)
83

84
// Ensure BitcoindNotifier implements the MempoolWatcher interface at compile
85
// time.
86
var _ chainntnfs.MempoolWatcher = (*BitcoindNotifier)(nil)
87

88
// New returns a new BitcoindNotifier instance. This function assumes the
89
// bitcoind node detailed in the passed configuration is already running, and
90
// willing to accept RPC requests and new zmq clients.
91
func New(chainConn *chain.BitcoindConn, chainParams *chaincfg.Params,
92
        spendHintCache chainntnfs.SpendHintCache,
93
        confirmHintCache chainntnfs.ConfirmHintCache,
UNCOV
94
        blockCache *blockcache.BlockCache) *BitcoindNotifier {
×
UNCOV
95

×
UNCOV
96
        notifier := &BitcoindNotifier{
×
UNCOV
97
                chainParams: chainParams,
×
UNCOV
98

×
UNCOV
99
                notificationCancels:  make(chan interface{}),
×
UNCOV
100
                notificationRegistry: make(chan interface{}),
×
UNCOV
101

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

×
UNCOV
104
                spendHintCache:   spendHintCache,
×
UNCOV
105
                confirmHintCache: confirmHintCache,
×
UNCOV
106

×
UNCOV
107
                blockCache:  blockCache,
×
UNCOV
108
                memNotifier: chainntnfs.NewMempoolNotifier(),
×
UNCOV
109

×
UNCOV
110
                quit: make(chan struct{}),
×
UNCOV
111
        }
×
UNCOV
112

×
UNCOV
113
        notifier.chainConn = chainConn.NewBitcoindClient()
×
UNCOV
114

×
UNCOV
115
        return notifier
×
UNCOV
116
}
×
117

118
// Start connects to the running bitcoind node over websockets, registers for
119
// block notifications, and finally launches all related helper goroutines.
UNCOV
120
func (b *BitcoindNotifier) Start() error {
×
UNCOV
121
        var startErr error
×
UNCOV
122
        b.start.Do(func() {
×
UNCOV
123
                startErr = b.startNotifier()
×
UNCOV
124
        })
×
125

UNCOV
126
        return startErr
×
127
}
128

129
// Stop shutsdown the BitcoindNotifier.
UNCOV
130
func (b *BitcoindNotifier) Stop() error {
×
UNCOV
131
        // Already shutting down?
×
UNCOV
132
        if atomic.AddInt32(&b.stopped, 1) != 1 {
×
133
                return nil
×
134
        }
×
135

UNCOV
136
        chainntnfs.Log.Info("bitcoind notifier shutting down...")
×
UNCOV
137
        defer chainntnfs.Log.Debug("bitcoind notifier shutdown complete")
×
UNCOV
138

×
UNCOV
139
        // Shutdown the rpc client, this gracefully disconnects from bitcoind,
×
UNCOV
140
        // and cleans up all related resources.
×
UNCOV
141
        b.chainConn.Stop()
×
UNCOV
142
        b.chainConn.WaitForShutdown()
×
UNCOV
143

×
UNCOV
144
        close(b.quit)
×
UNCOV
145
        b.wg.Wait()
×
UNCOV
146

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

×
UNCOV
153
                close(epochClient.epochChan)
×
UNCOV
154
        }
×
155

156
        // The txNotifier is only initialized in the start method therefore we
157
        // need to make sure we don't access a nil pointer here.
UNCOV
158
        if b.txNotifier != nil {
×
UNCOV
159
                b.txNotifier.TearDown()
×
UNCOV
160
        }
×
161

162
        // Stop the mempool notifier.
UNCOV
163
        b.memNotifier.TearDown()
×
UNCOV
164

×
UNCOV
165
        return nil
×
166
}
167

168
// Started returns true if this instance has been started, and false otherwise.
UNCOV
169
func (b *BitcoindNotifier) Started() bool {
×
UNCOV
170
        return atomic.LoadInt32(&b.active) != 0
×
UNCOV
171
}
×
172

UNCOV
173
func (b *BitcoindNotifier) startNotifier() error {
×
UNCOV
174
        // Connect to bitcoind, and register for notifications on connected,
×
UNCOV
175
        // and disconnected blocks.
×
UNCOV
176
        if err := b.chainConn.Start(); err != nil {
×
177
                return err
×
178
        }
×
UNCOV
179
        if err := b.chainConn.NotifyBlocks(); err != nil {
×
180
                return err
×
181
        }
×
182

UNCOV
183
        currentHash, currentHeight, err := b.chainConn.GetBestBlock()
×
UNCOV
184
        if err != nil {
×
185
                return err
×
186
        }
×
UNCOV
187
        blockHeader, err := b.chainConn.GetBlockHeader(currentHash)
×
UNCOV
188
        if err != nil {
×
189
                return err
×
190
        }
×
191

UNCOV
192
        b.txNotifier = chainntnfs.NewTxNotifier(
×
UNCOV
193
                uint32(currentHeight), chainntnfs.ReorgSafetyLimit,
×
UNCOV
194
                b.confirmHintCache, b.spendHintCache,
×
UNCOV
195
        )
×
UNCOV
196

×
UNCOV
197
        b.bestBlock = chainntnfs.BlockEpoch{
×
UNCOV
198
                Height:      currentHeight,
×
UNCOV
199
                Hash:        currentHash,
×
UNCOV
200
                BlockHeader: blockHeader,
×
UNCOV
201
        }
×
UNCOV
202

×
UNCOV
203
        b.wg.Add(1)
×
UNCOV
204
        go b.notificationDispatcher()
×
UNCOV
205

×
UNCOV
206
        // Set the active flag now that we've completed the full
×
UNCOV
207
        // startup.
×
UNCOV
208
        atomic.StoreInt32(&b.active, 1)
×
UNCOV
209

×
UNCOV
210
        return nil
×
211
}
212

213
// notificationDispatcher is the primary goroutine which handles client
214
// notification registrations, as well as notification dispatches.
UNCOV
215
func (b *BitcoindNotifier) notificationDispatcher() {
×
UNCOV
216
        defer b.wg.Done()
×
UNCOV
217

×
UNCOV
218
out:
×
UNCOV
219
        for {
×
UNCOV
220
                select {
×
UNCOV
221
                case cancelMsg := <-b.notificationCancels:
×
UNCOV
222
                        switch msg := cancelMsg.(type) {
×
UNCOV
223
                        case *epochCancel:
×
UNCOV
224
                                chainntnfs.Log.Infof("Cancelling epoch "+
×
UNCOV
225
                                        "notification, epoch_id=%v", msg.epochID)
×
UNCOV
226

×
UNCOV
227
                                // First, we'll lookup the original
×
UNCOV
228
                                // registration in order to stop the active
×
UNCOV
229
                                // queue goroutine.
×
UNCOV
230
                                reg := b.blockEpochClients[msg.epochID]
×
UNCOV
231
                                reg.epochQueue.Stop()
×
UNCOV
232

×
UNCOV
233
                                // Next, close the cancel channel for this
×
UNCOV
234
                                // specific client, and wait for the client to
×
UNCOV
235
                                // exit.
×
UNCOV
236
                                close(b.blockEpochClients[msg.epochID].cancelChan)
×
UNCOV
237
                                b.blockEpochClients[msg.epochID].wg.Wait()
×
UNCOV
238

×
UNCOV
239
                                // Once the client has exited, we can then
×
UNCOV
240
                                // safely close the channel used to send epoch
×
UNCOV
241
                                // notifications, in order to notify any
×
UNCOV
242
                                // listeners that the intent has been
×
UNCOV
243
                                // canceled.
×
UNCOV
244
                                close(b.blockEpochClients[msg.epochID].epochChan)
×
UNCOV
245
                                delete(b.blockEpochClients, msg.epochID)
×
246

247
                        }
UNCOV
248
                case registerMsg := <-b.notificationRegistry:
×
UNCOV
249
                        switch msg := registerMsg.(type) {
×
UNCOV
250
                        case *chainntnfs.HistoricalConfDispatch:
×
UNCOV
251
                                // Look up whether the transaction is already
×
UNCOV
252
                                // included in the active chain. We'll do this
×
UNCOV
253
                                // in a goroutine to prevent blocking
×
UNCOV
254
                                // potentially long rescans.
×
UNCOV
255
                                //
×
UNCOV
256
                                // TODO(wilmer): add retry logic if rescan fails?
×
UNCOV
257
                                b.wg.Add(1)
×
UNCOV
258

×
UNCOV
259
                                //nolint:ll
×
UNCOV
260
                                go func(msg *chainntnfs.HistoricalConfDispatch) {
×
UNCOV
261
                                        defer b.wg.Done()
×
UNCOV
262

×
UNCOV
263
                                        confDetails, _, err := b.historicalConfDetails(
×
UNCOV
264
                                                msg.ConfRequest,
×
UNCOV
265
                                                msg.StartHeight, msg.EndHeight,
×
UNCOV
266
                                        )
×
UNCOV
267
                                        if err != nil {
×
268
                                                chainntnfs.Log.Errorf("Rescan to "+
×
269
                                                        "determine the conf "+
×
270
                                                        "details of %v within "+
×
271
                                                        "range %d-%d failed: %v",
×
272
                                                        msg.ConfRequest,
×
273
                                                        msg.StartHeight,
×
274
                                                        msg.EndHeight, err)
×
275
                                                return
×
276
                                        }
×
277

278
                                        // If the historical dispatch finished
279
                                        // without error, we will invoke
280
                                        // UpdateConfDetails even if none were
281
                                        // found. This allows the notifier to
282
                                        // begin safely updating the height hint
283
                                        // cache at tip, since any pending
284
                                        // rescans have now completed.
UNCOV
285
                                        err = b.txNotifier.UpdateConfDetails(
×
UNCOV
286
                                                msg.ConfRequest, confDetails,
×
UNCOV
287
                                        )
×
UNCOV
288
                                        if err != nil {
×
289
                                                chainntnfs.Log.Errorf("Unable "+
×
290
                                                        "to update conf "+
×
291
                                                        "details of %v: %v",
×
292
                                                        msg.ConfRequest, err)
×
293
                                        }
×
294
                                }(msg)
295

UNCOV
296
                        case *chainntnfs.HistoricalSpendDispatch:
×
UNCOV
297
                                // In order to ensure we don't block the caller
×
UNCOV
298
                                // on what may be a long rescan, we'll launch a
×
UNCOV
299
                                // goroutine to do so in the background.
×
UNCOV
300
                                //
×
UNCOV
301
                                // TODO(wilmer): add retry logic if rescan fails?
×
UNCOV
302
                                b.wg.Add(1)
×
UNCOV
303

×
UNCOV
304
                                //nolint:ll
×
UNCOV
305
                                go func(msg *chainntnfs.HistoricalSpendDispatch) {
×
UNCOV
306
                                        defer b.wg.Done()
×
UNCOV
307

×
UNCOV
308
                                        spendDetails, err := b.historicalSpendDetails(
×
UNCOV
309
                                                msg.SpendRequest,
×
UNCOV
310
                                                msg.StartHeight, msg.EndHeight,
×
UNCOV
311
                                        )
×
UNCOV
312
                                        if err != nil {
×
313
                                                chainntnfs.Log.Errorf("Rescan to "+
×
314
                                                        "determine the spend "+
×
315
                                                        "details of %v within "+
×
316
                                                        "range %d-%d failed: %v",
×
317
                                                        msg.SpendRequest,
×
318
                                                        msg.StartHeight,
×
319
                                                        msg.EndHeight, err)
×
320
                                                return
×
321
                                        }
×
322

UNCOV
323
                                        chainntnfs.Log.Infof("Historical "+
×
UNCOV
324
                                                "spend dispatch finished "+
×
UNCOV
325
                                                "for request %v (start=%v "+
×
UNCOV
326
                                                "end=%v) with details: %v",
×
UNCOV
327
                                                msg.SpendRequest,
×
UNCOV
328
                                                msg.StartHeight, msg.EndHeight,
×
UNCOV
329
                                                spendDetails)
×
UNCOV
330

×
UNCOV
331
                                        // If the historical dispatch finished
×
UNCOV
332
                                        // without error, we will invoke
×
UNCOV
333
                                        // UpdateSpendDetails even if none were
×
UNCOV
334
                                        // found. This allows the notifier to
×
UNCOV
335
                                        // begin safely updating the height hint
×
UNCOV
336
                                        // cache at tip, since any pending
×
UNCOV
337
                                        // rescans have now completed.
×
UNCOV
338
                                        err = b.txNotifier.UpdateSpendDetails(
×
UNCOV
339
                                                msg.SpendRequest, spendDetails,
×
UNCOV
340
                                        )
×
UNCOV
341
                                        if err != nil {
×
342
                                                chainntnfs.Log.Errorf("Unable "+
×
343
                                                        "to update spend "+
×
344
                                                        "details of %v: %v",
×
345
                                                        msg.SpendRequest, err)
×
346
                                        }
×
347
                                }(msg)
348

UNCOV
349
                        case *blockEpochRegistration:
×
UNCOV
350
                                chainntnfs.Log.Infof("New block epoch subscription")
×
UNCOV
351

×
UNCOV
352
                                b.blockEpochClients[msg.epochID] = msg
×
UNCOV
353

×
UNCOV
354
                                // If the client did not provide their best
×
UNCOV
355
                                // known block, then we'll immediately dispatch
×
UNCOV
356
                                // a notification for the current tip.
×
UNCOV
357
                                if msg.bestBlock == nil {
×
UNCOV
358
                                        b.notifyBlockEpochClient(
×
UNCOV
359
                                                msg, b.bestBlock.Height,
×
UNCOV
360
                                                b.bestBlock.Hash,
×
UNCOV
361
                                                b.bestBlock.BlockHeader,
×
UNCOV
362
                                        )
×
UNCOV
363

×
UNCOV
364
                                        msg.errorChan <- nil
×
UNCOV
365
                                        continue
×
366
                                }
367

368
                                // Otherwise, we'll attempt to deliver the
369
                                // backlog of notifications from their best
370
                                // known block.
UNCOV
371
                                missedBlocks, err := chainntnfs.GetClientMissedBlocks(
×
UNCOV
372
                                        b.chainConn, msg.bestBlock,
×
UNCOV
373
                                        b.bestBlock.Height, true,
×
UNCOV
374
                                )
×
UNCOV
375
                                if err != nil {
×
UNCOV
376
                                        msg.errorChan <- err
×
UNCOV
377
                                        continue
×
378
                                }
379

UNCOV
380
                                for _, block := range missedBlocks {
×
UNCOV
381
                                        b.notifyBlockEpochClient(
×
UNCOV
382
                                                msg, block.Height, block.Hash,
×
UNCOV
383
                                                block.BlockHeader,
×
UNCOV
384
                                        )
×
UNCOV
385
                                }
×
386

UNCOV
387
                                msg.errorChan <- nil
×
388
                        }
389

UNCOV
390
                case ntfn := <-b.chainConn.Notifications():
×
UNCOV
391
                        switch item := ntfn.(type) {
×
UNCOV
392
                        case chain.BlockConnected:
×
UNCOV
393
                                blockHeader, err :=
×
UNCOV
394
                                        b.chainConn.GetBlockHeader(&item.Hash)
×
UNCOV
395
                                if err != nil {
×
396
                                        chainntnfs.Log.Errorf("Unable to fetch "+
×
397
                                                "block header: %v", err)
×
398
                                        continue
×
399
                                }
400

UNCOV
401
                                if blockHeader.PrevBlock != *b.bestBlock.Hash {
×
402
                                        // Handle the case where the notifier
×
403
                                        // missed some blocks from its chain
×
404
                                        // backend.
×
405
                                        chainntnfs.Log.Infof("Missed blocks, " +
×
406
                                                "attempting to catch up")
×
407
                                        newBestBlock, missedBlocks, err :=
×
408
                                                chainntnfs.HandleMissedBlocks(
×
409
                                                        b.chainConn,
×
410
                                                        b.txNotifier,
×
411
                                                        b.bestBlock, item.Height,
×
412
                                                        true,
×
413
                                                )
×
414

×
415
                                        if err != nil {
×
416
                                                // Set the bestBlock here in case
×
417
                                                // a catch up partially completed.
×
418
                                                b.bestBlock = newBestBlock
×
419
                                                chainntnfs.Log.Error(err)
×
420
                                                continue
×
421
                                        }
422

423
                                        for _, block := range missedBlocks {
×
424
                                                err := b.handleBlockConnected(block)
×
425
                                                if err != nil {
×
426
                                                        chainntnfs.Log.Error(err)
×
427
                                                        continue out
×
428
                                                }
429
                                        }
430
                                }
431

UNCOV
432
                                newBlock := chainntnfs.BlockEpoch{
×
UNCOV
433
                                        Height:      item.Height,
×
UNCOV
434
                                        Hash:        &item.Hash,
×
UNCOV
435
                                        BlockHeader: blockHeader,
×
UNCOV
436
                                }
×
UNCOV
437
                                if err := b.handleBlockConnected(newBlock); err != nil {
×
438
                                        chainntnfs.Log.Error(err)
×
439
                                }
×
440

UNCOV
441
                                continue
×
442

UNCOV
443
                        case chain.BlockDisconnected:
×
UNCOV
444
                                if item.Height != b.bestBlock.Height {
×
445
                                        chainntnfs.Log.Infof("Missed disconnected" +
×
446
                                                "blocks, attempting to catch up")
×
447
                                }
×
448

UNCOV
449
                                newBestBlock, err := chainntnfs.RewindChain(
×
UNCOV
450
                                        b.chainConn, b.txNotifier,
×
UNCOV
451
                                        b.bestBlock, item.Height-1,
×
UNCOV
452
                                )
×
UNCOV
453
                                if err != nil {
×
454
                                        chainntnfs.Log.Errorf("Unable to rewind chain "+
×
455
                                                "from height %d to height %d: %v",
×
456
                                                b.bestBlock.Height, item.Height-1, err)
×
457
                                }
×
458

459
                                // Set the bestBlock here in case a chain
460
                                // rewind partially completed.
UNCOV
461
                                b.bestBlock = newBestBlock
×
462

UNCOV
463
                        case chain.RelevantTx:
×
UNCOV
464
                                tx := btcutil.NewTx(&item.TxRecord.MsgTx)
×
UNCOV
465

×
UNCOV
466
                                // Init values.
×
UNCOV
467
                                isMempool := false
×
UNCOV
468
                                height := uint32(0)
×
UNCOV
469

×
UNCOV
470
                                // Unwrap values.
×
UNCOV
471
                                if item.Block == nil {
×
UNCOV
472
                                        isMempool = true
×
UNCOV
473
                                } else {
×
UNCOV
474
                                        height = uint32(item.Block.Height)
×
UNCOV
475
                                }
×
476

477
                                // Handle the transaction.
UNCOV
478
                                b.handleRelevantTx(tx, isMempool, height)
×
479
                        }
480

UNCOV
481
                case <-b.quit:
×
UNCOV
482
                        break out
×
483
                }
484
        }
485
}
486

487
// handleRelevantTx handles a new transaction that has been seen either in a
488
// block or in the mempool. If in mempool, it will ask the mempool notifier to
489
// handle it. If in a block, it will ask the txNotifier to handle it, and
490
// cancel any relevant subscriptions made in the mempool.
491
func (b *BitcoindNotifier) handleRelevantTx(tx *btcutil.Tx,
UNCOV
492
        mempool bool, height uint32) {
×
UNCOV
493

×
UNCOV
494
        // If this is a mempool spend, we'll ask the mempool notifier to handle
×
UNCOV
495
        // it.
×
UNCOV
496
        if mempool {
×
UNCOV
497
                err := b.memNotifier.ProcessRelevantSpendTx(tx)
×
UNCOV
498
                if err != nil {
×
499
                        chainntnfs.Log.Errorf("Unable to process transaction "+
×
500
                                "%v: %v", tx.Hash(), err)
×
501
                }
×
502

UNCOV
503
                return
×
504
        }
505

506
        // Otherwise this is a confirmed spend, and we'll ask the tx notifier
507
        // to handle it.
UNCOV
508
        err := b.txNotifier.ProcessRelevantSpendTx(tx, height)
×
UNCOV
509
        if err != nil {
×
510
                chainntnfs.Log.Errorf("Unable to process transaction %v: %v",
×
511
                        tx.Hash(), err)
×
512

×
513
                return
×
514
        }
×
515

516
        // Once the tx is processed, we will ask the memNotifier to unsubscribe
517
        // the input.
518
        //
519
        // NOTE(yy): we could build it into txNotifier.ProcessRelevantSpendTx,
520
        // but choose to implement it here so we can easily decouple the two
521
        // notifiers in the future.
UNCOV
522
        b.memNotifier.UnsubsribeConfirmedSpentTx(tx)
×
523
}
524

525
// historicalConfDetails looks up whether a confirmation request (txid/output
526
// script) has already been included in a block in the active chain and, if so,
527
// returns details about said block.
528
func (b *BitcoindNotifier) historicalConfDetails(confRequest chainntnfs.ConfRequest,
529
        startHeight, endHeight uint32) (*chainntnfs.TxConfirmation,
UNCOV
530
        chainntnfs.TxConfStatus, error) {
×
UNCOV
531

×
UNCOV
532
        // If a txid was not provided, then we should dispatch upon seeing the
×
UNCOV
533
        // script on-chain, so we'll short-circuit straight to scanning manually
×
UNCOV
534
        // as there doesn't exist a script index to query.
×
UNCOV
535
        if confRequest.TxID == chainntnfs.ZeroHash {
×
536
                return b.confDetailsManually(
×
537
                        confRequest, startHeight, endHeight,
×
538
                )
×
539
        }
×
540

541
        // Otherwise, we'll dispatch upon seeing a transaction on-chain with the
542
        // given hash.
543
        //
544
        // We'll first attempt to retrieve the transaction using the node's
545
        // txindex.
UNCOV
546
        txNotFoundErr := "No such mempool or blockchain transaction"
×
UNCOV
547
        txConf, txStatus, err := chainntnfs.ConfDetailsFromTxIndex(
×
UNCOV
548
                b.chainConn, confRequest, txNotFoundErr,
×
UNCOV
549
        )
×
UNCOV
550

×
UNCOV
551
        // We'll then check the status of the transaction lookup returned to
×
UNCOV
552
        // determine whether we should proceed with any fallback methods.
×
UNCOV
553
        switch {
×
554

555
        // We failed querying the index for the transaction, fall back to
556
        // scanning manually.
557
        case err != nil:
×
558
                chainntnfs.Log.Debugf("Failed getting conf details from "+
×
559
                        "index (%v), scanning manually", err)
×
560
                return b.confDetailsManually(confRequest, startHeight, endHeight)
×
561

562
        // The transaction was found within the node's mempool.
UNCOV
563
        case txStatus == chainntnfs.TxFoundMempool:
×
564

565
        // The transaction was found within the node's txindex.
UNCOV
566
        case txStatus == chainntnfs.TxFoundIndex:
×
567

568
        // The transaction was not found within the node's mempool or txindex.
UNCOV
569
        case txStatus == chainntnfs.TxNotFoundIndex:
×
570

571
        // Unexpected txStatus returned.
572
        default:
×
573
                return nil, txStatus,
×
574
                        fmt.Errorf("Got unexpected txConfStatus: %v", txStatus)
×
575
        }
576

UNCOV
577
        return txConf, txStatus, nil
×
578
}
579

580
// confDetailsManually looks up whether a transaction/output script has already
581
// been included in a block in the active chain by scanning the chain's blocks
582
// within the given range. If the transaction/output script is found, its
583
// confirmation details are returned. Otherwise, nil is returned.
584
func (b *BitcoindNotifier) confDetailsManually(confRequest chainntnfs.ConfRequest,
585
        heightHint, currentHeight uint32) (*chainntnfs.TxConfirmation,
586
        chainntnfs.TxConfStatus, error) {
×
587

×
588
        // Begin scanning blocks at every height to determine where the
×
589
        // transaction was included in.
×
590
        for height := currentHeight; height >= heightHint && height > 0; height-- {
×
591
                // Ensure we haven't been requested to shut down before
×
592
                // processing the next height.
×
593
                select {
×
594
                case <-b.quit:
×
595
                        return nil, chainntnfs.TxNotFoundManually,
×
596
                                chainntnfs.ErrChainNotifierShuttingDown
×
597
                default:
×
598
                }
599

600
                blockHash, err := b.chainConn.GetBlockHash(int64(height))
×
601
                if err != nil {
×
602
                        return nil, chainntnfs.TxNotFoundManually,
×
603
                                fmt.Errorf("unable to get hash from block "+
×
604
                                        "with height %d", height)
×
605
                }
×
606

607
                block, err := b.GetBlock(blockHash)
×
608
                if err != nil {
×
609
                        return nil, chainntnfs.TxNotFoundManually,
×
610
                                fmt.Errorf("unable to get block with hash "+
×
611
                                        "%v: %v", blockHash, err)
×
612
                }
×
613

614
                // For every transaction in the block, check which one matches
615
                // our request. If we find one that does, we can dispatch its
616
                // confirmation details.
617
                for txIndex, tx := range block.Transactions {
×
618
                        if !confRequest.MatchesTx(tx) {
×
619
                                continue
×
620
                        }
621

622
                        return &chainntnfs.TxConfirmation{
×
623
                                Tx:          tx.Copy(),
×
624
                                BlockHash:   blockHash,
×
625
                                BlockHeight: height,
×
626
                                TxIndex:     uint32(txIndex),
×
627
                                Block:       block,
×
628
                        }, chainntnfs.TxFoundManually, nil
×
629
                }
630
        }
631

632
        // If we reach here, then we were not able to find the transaction
633
        // within a block, so we avoid returning an error.
634
        return nil, chainntnfs.TxNotFoundManually, nil
×
635
}
636

637
// handleBlockConnected applies a chain update for a new block. Any watched
638
// transactions included this block will processed to either send notifications
639
// now or after numConfirmations confs.
UNCOV
640
func (b *BitcoindNotifier) handleBlockConnected(block chainntnfs.BlockEpoch) error {
×
UNCOV
641
        // First, we'll fetch the raw block as we'll need to gather all the
×
UNCOV
642
        // transactions to determine whether any are relevant to our registered
×
UNCOV
643
        // clients.
×
UNCOV
644
        rawBlock, err := b.GetBlock(block.Hash)
×
UNCOV
645
        if err != nil {
×
646
                return fmt.Errorf("unable to get block: %w", err)
×
647
        }
×
UNCOV
648
        utilBlock := btcutil.NewBlock(rawBlock)
×
UNCOV
649

×
UNCOV
650
        // We'll then extend the txNotifier's height with the information of
×
UNCOV
651
        // this new block, which will handle all of the notification logic for
×
UNCOV
652
        // us.
×
UNCOV
653
        err = b.txNotifier.ConnectTip(utilBlock, uint32(block.Height))
×
UNCOV
654
        if err != nil {
×
655
                return fmt.Errorf("unable to connect tip: %w", err)
×
656
        }
×
657

UNCOV
658
        chainntnfs.Log.Infof("New block: height=%v, sha=%v", block.Height,
×
UNCOV
659
                block.Hash)
×
UNCOV
660

×
UNCOV
661
        // Now that we've guaranteed the new block extends the txNotifier's
×
UNCOV
662
        // current tip, we'll proceed to dispatch notifications to all of our
×
UNCOV
663
        // registered clients whom have had notifications fulfilled. Before
×
UNCOV
664
        // doing so, we'll make sure update our in memory state in order to
×
UNCOV
665
        // satisfy any client requests based upon the new block.
×
UNCOV
666
        b.bestBlock = block
×
UNCOV
667

×
UNCOV
668
        err = b.txNotifier.NotifyHeight(uint32(block.Height))
×
UNCOV
669
        if err != nil {
×
670
                return fmt.Errorf("unable to notify height: %w", err)
×
671
        }
×
672

UNCOV
673
        b.notifyBlockEpochs(block.Height, block.Hash, block.BlockHeader)
×
UNCOV
674

×
UNCOV
675
        return nil
×
676
}
677

678
// notifyBlockEpochs notifies all registered block epoch clients of the newly
679
// connected block to the main chain.
680
func (b *BitcoindNotifier) notifyBlockEpochs(newHeight int32, newSha *chainhash.Hash,
UNCOV
681
        blockHeader *wire.BlockHeader) {
×
UNCOV
682

×
UNCOV
683
        for _, client := range b.blockEpochClients {
×
UNCOV
684
                b.notifyBlockEpochClient(client, newHeight, newSha, blockHeader)
×
UNCOV
685
        }
×
686
}
687

688
// notifyBlockEpochClient sends a registered block epoch client a notification
689
// about a specific block.
690
func (b *BitcoindNotifier) notifyBlockEpochClient(epochClient *blockEpochRegistration,
UNCOV
691
        height int32, sha *chainhash.Hash, header *wire.BlockHeader) {
×
UNCOV
692

×
UNCOV
693
        epoch := &chainntnfs.BlockEpoch{
×
UNCOV
694
                Height:      height,
×
UNCOV
695
                Hash:        sha,
×
UNCOV
696
                BlockHeader: header,
×
UNCOV
697
        }
×
UNCOV
698

×
UNCOV
699
        select {
×
UNCOV
700
        case epochClient.epochQueue.ChanIn() <- epoch:
×
701
        case <-epochClient.cancelChan:
×
UNCOV
702
        case <-b.quit:
×
703
        }
704
}
705

706
// RegisterSpendNtfn registers an intent to be notified once the target
707
// outpoint/output script has been spent by a transaction on-chain. When
708
// intending to be notified of the spend of an output script, a nil outpoint
709
// must be used. The heightHint should represent the earliest height in the
710
// chain of the transaction that spent the outpoint/output script.
711
//
712
// Once a spend of has been detected, the details of the spending event will be
713
// sent across the 'Spend' channel.
714
func (b *BitcoindNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
UNCOV
715
        pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
×
UNCOV
716

×
UNCOV
717
        // Register the conf notification with the TxNotifier. A non-nil value
×
UNCOV
718
        // for `dispatch` will be returned if we are required to perform a
×
UNCOV
719
        // manual scan for the confirmation. Otherwise the notifier will begin
×
UNCOV
720
        // watching at tip for the transaction to confirm.
×
UNCOV
721
        ntfn, err := b.txNotifier.RegisterSpend(outpoint, pkScript, heightHint)
×
UNCOV
722
        if err != nil {
×
UNCOV
723
                return nil, err
×
UNCOV
724
        }
×
725

726
        // We'll then request the backend to notify us when it has detected the
727
        // outpoint/output script as spent.
728
        //
729
        // TODO(wilmer): use LoadFilter API instead.
UNCOV
730
        if outpoint == nil || *outpoint == chainntnfs.ZeroOutPoint {
×
731
                _, addrs, _, err := txscript.ExtractPkScriptAddrs(
×
732
                        pkScript, b.chainParams,
×
733
                )
×
734
                if err != nil {
×
735
                        return nil, fmt.Errorf("unable to parse script: %w",
×
736
                                err)
×
737
                }
×
738
                if err := b.chainConn.NotifyReceived(addrs); err != nil {
×
739
                        return nil, err
×
740
                }
×
UNCOV
741
        } else {
×
UNCOV
742
                ops := []*wire.OutPoint{outpoint}
×
UNCOV
743
                if err := b.chainConn.NotifySpent(ops); err != nil {
×
744
                        return nil, err
×
745
                }
×
746
        }
747

748
        // If the txNotifier didn't return any details to perform a historical
749
        // scan of the chain, then we can return early as there's nothing left
750
        // for us to do.
UNCOV
751
        if ntfn.HistoricalDispatch == nil {
×
UNCOV
752
                return ntfn.Event, nil
×
UNCOV
753
        }
×
754

755
        // Otherwise, we'll need to dispatch a historical rescan to determine if
756
        // the outpoint was already spent at a previous height.
757
        //
758
        // We'll short-circuit the path when dispatching the spend of a script,
759
        // rather than an outpoint, as there aren't any additional checks we can
760
        // make for scripts.
UNCOV
761
        if ntfn.HistoricalDispatch.OutPoint == chainntnfs.ZeroOutPoint {
×
762
                select {
×
763
                case b.notificationRegistry <- ntfn.HistoricalDispatch:
×
764
                case <-b.quit:
×
765
                        return nil, chainntnfs.ErrChainNotifierShuttingDown
×
766
                }
767

768
                return ntfn.Event, nil
×
769
        }
770

771
        // When dispatching spends of outpoints, there are a number of checks we
772
        // can make to start our rescan from a better height or completely avoid
773
        // it.
774
        //
775
        // We'll start by checking the backend's UTXO set to determine whether
776
        // the outpoint has been spent. If it hasn't, we can return to the
777
        // caller as well.
UNCOV
778
        txOut, err := b.chainConn.GetTxOut(&outpoint.Hash, outpoint.Index, true)
×
UNCOV
779
        if err != nil {
×
780
                return nil, err
×
781
        }
×
UNCOV
782
        if txOut != nil {
×
UNCOV
783
                // We'll let the txNotifier know the outpoint is still unspent
×
UNCOV
784
                // in order to begin updating its spend hint.
×
UNCOV
785
                err := b.txNotifier.UpdateSpendDetails(
×
UNCOV
786
                        ntfn.HistoricalDispatch.SpendRequest, nil,
×
UNCOV
787
                )
×
UNCOV
788
                if err != nil {
×
789
                        return nil, err
×
790
                }
×
791

UNCOV
792
                return ntfn.Event, nil
×
793
        }
794

795
        // Since the outpoint was spent, as it no longer exists within the UTXO
796
        // set, we'll determine when it happened by scanning the chain.
797
        //
798
        // As a minimal optimization, we'll query the backend's transaction
799
        // index (if enabled) to determine if we have a better rescan starting
800
        // height. We can do this as the GetRawTransaction call will return the
801
        // hash of the block it was included in within the chain.
UNCOV
802
        tx, err := b.chainConn.GetRawTransactionVerbose(&outpoint.Hash)
×
UNCOV
803
        if err != nil {
×
UNCOV
804
                // Avoid returning an error if the transaction was not found to
×
UNCOV
805
                // proceed with fallback methods.
×
UNCOV
806
                jsonErr, ok := err.(*btcjson.RPCError)
×
UNCOV
807
                if !ok || jsonErr.Code != btcjson.ErrRPCNoTxInfo {
×
808
                        return nil, fmt.Errorf("unable to query for txid "+
×
809
                                "%v: %w", outpoint.Hash, err)
×
810
                }
×
811
        }
812

813
        // If the transaction index was enabled, we'll use the block's hash to
814
        // retrieve its height and check whether it provides a better starting
815
        // point for our rescan.
UNCOV
816
        if tx != nil {
×
UNCOV
817
                // If the transaction containing the outpoint hasn't confirmed
×
UNCOV
818
                // on-chain, then there's no need to perform a rescan.
×
UNCOV
819
                if tx.BlockHash == "" {
×
UNCOV
820
                        return ntfn.Event, nil
×
UNCOV
821
                }
×
822

UNCOV
823
                blockHash, err := chainhash.NewHashFromStr(tx.BlockHash)
×
UNCOV
824
                if err != nil {
×
825
                        return nil, err
×
826
                }
×
UNCOV
827
                blockHeight, err := b.chainConn.GetBlockHeight(blockHash)
×
UNCOV
828
                if err != nil {
×
829
                        return nil, err
×
830
                }
×
831

UNCOV
832
                if uint32(blockHeight) > ntfn.HistoricalDispatch.StartHeight {
×
UNCOV
833
                        ntfn.HistoricalDispatch.StartHeight = uint32(blockHeight)
×
UNCOV
834
                }
×
835
        }
836

837
        // Now that we've determined the starting point of our rescan, we can
838
        // dispatch it and return.
UNCOV
839
        select {
×
UNCOV
840
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
×
841
        case <-b.quit:
×
842
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
843
        }
844

UNCOV
845
        return ntfn.Event, nil
×
846
}
847

848
// historicalSpendDetails attempts to manually scan the chain within the given
849
// height range for a transaction that spends the given outpoint/output script.
850
// If one is found, the spend details are assembled and returned to the caller.
851
// If the spend is not found, a nil spend detail will be returned.
852
func (b *BitcoindNotifier) historicalSpendDetails(
853
        spendRequest chainntnfs.SpendRequest, startHeight, endHeight uint32) (
UNCOV
854
        *chainntnfs.SpendDetail, error) {
×
UNCOV
855

×
UNCOV
856
        // Begin scanning blocks at every height to determine if the outpoint
×
UNCOV
857
        // was spent.
×
UNCOV
858
        for height := endHeight; height >= startHeight && height > 0; height-- {
×
UNCOV
859
                // Ensure we haven't been requested to shut down before
×
UNCOV
860
                // processing the next height.
×
UNCOV
861
                select {
×
862
                case <-b.quit:
×
863
                        return nil, chainntnfs.ErrChainNotifierShuttingDown
×
UNCOV
864
                default:
×
865
                }
866

867
                // First, we'll fetch the block for the current height.
UNCOV
868
                blockHash, err := b.chainConn.GetBlockHash(int64(height))
×
UNCOV
869
                if err != nil {
×
870
                        return nil, fmt.Errorf("unable to retrieve hash for "+
×
871
                                "block with height %d: %v", height, err)
×
872
                }
×
UNCOV
873
                block, err := b.GetBlock(blockHash)
×
UNCOV
874
                if err != nil {
×
875
                        return nil, fmt.Errorf("unable to retrieve block "+
×
876
                                "with hash %v: %v", blockHash, err)
×
877
                }
×
878

879
                // Then, we'll manually go over every input in every transaction
880
                // in it and determine whether it spends the request in
881
                // question. If we find one, we'll dispatch the spend details.
UNCOV
882
                for _, tx := range block.Transactions {
×
UNCOV
883
                        matches, inputIdx, err := spendRequest.MatchesTx(tx)
×
UNCOV
884
                        if err != nil {
×
885
                                return nil, err
×
886
                        }
×
UNCOV
887
                        if !matches {
×
UNCOV
888
                                continue
×
889
                        }
890

UNCOV
891
                        txCopy := tx.Copy()
×
UNCOV
892
                        txHash := txCopy.TxHash()
×
UNCOV
893
                        spendOutPoint := &txCopy.TxIn[inputIdx].PreviousOutPoint
×
UNCOV
894
                        return &chainntnfs.SpendDetail{
×
UNCOV
895
                                SpentOutPoint:     spendOutPoint,
×
UNCOV
896
                                SpenderTxHash:     &txHash,
×
UNCOV
897
                                SpendingTx:        txCopy,
×
UNCOV
898
                                SpenderInputIndex: inputIdx,
×
UNCOV
899
                                SpendingHeight:    int32(height),
×
UNCOV
900
                        }, nil
×
901
                }
902
        }
903

UNCOV
904
        return nil, nil
×
905
}
906

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

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

UNCOV
931
        if ntfn.HistoricalDispatch == nil {
×
UNCOV
932
                return ntfn.Event, nil
×
UNCOV
933
        }
×
934

UNCOV
935
        select {
×
UNCOV
936
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
×
UNCOV
937
                return ntfn.Event, nil
×
938
        case <-b.quit:
×
939
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
940
        }
941
}
942

943
// blockEpochRegistration represents a client's intent to receive a
944
// notification with each newly connected block.
945
type blockEpochRegistration struct {
946
        epochID uint64
947

948
        epochChan chan *chainntnfs.BlockEpoch
949

950
        epochQueue *queue.ConcurrentQueue
951

952
        bestBlock *chainntnfs.BlockEpoch
953

954
        errorChan chan error
955

956
        cancelChan chan struct{}
957

958
        wg sync.WaitGroup
959
}
960

961
// epochCancel is a message sent to the BitcoindNotifier when a client wishes
962
// to cancel an outstanding epoch notification that has yet to be dispatched.
963
type epochCancel struct {
964
        epochID uint64
965
}
966

967
// RegisterBlockEpochNtfn returns a BlockEpochEvent which subscribes the
968
// caller to receive notifications, of each new block connected to the main
969
// chain. Clients have the option of passing in their best known block, which
970
// the notifier uses to check if they are behind on blocks and catch them up. If
971
// they do not provide one, then a notification will be dispatched immediately
972
// for the current tip of the chain upon a successful registration.
973
func (b *BitcoindNotifier) RegisterBlockEpochNtfn(
UNCOV
974
        bestBlock *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error) {
×
UNCOV
975

×
UNCOV
976
        reg := &blockEpochRegistration{
×
UNCOV
977
                epochQueue: queue.NewConcurrentQueue(20),
×
UNCOV
978
                epochChan:  make(chan *chainntnfs.BlockEpoch, 20),
×
UNCOV
979
                cancelChan: make(chan struct{}),
×
UNCOV
980
                epochID:    atomic.AddUint64(&b.epochClientCounter, 1),
×
UNCOV
981
                bestBlock:  bestBlock,
×
UNCOV
982
                errorChan:  make(chan error, 1),
×
UNCOV
983
        }
×
UNCOV
984
        reg.epochQueue.Start()
×
UNCOV
985

×
UNCOV
986
        // Before we send the request to the main goroutine, we'll launch a new
×
UNCOV
987
        // goroutine to proxy items added to our queue to the client itself.
×
UNCOV
988
        // This ensures that all notifications are received *in order*.
×
UNCOV
989
        reg.wg.Add(1)
×
UNCOV
990
        go func() {
×
UNCOV
991
                defer reg.wg.Done()
×
UNCOV
992

×
UNCOV
993
                for {
×
UNCOV
994
                        select {
×
UNCOV
995
                        case ntfn := <-reg.epochQueue.ChanOut():
×
UNCOV
996
                                blockNtfn := ntfn.(*chainntnfs.BlockEpoch)
×
UNCOV
997
                                select {
×
UNCOV
998
                                case reg.epochChan <- blockNtfn:
×
999

UNCOV
1000
                                case <-reg.cancelChan:
×
UNCOV
1001
                                        return
×
1002

1003
                                case <-b.quit:
×
1004
                                        return
×
1005
                                }
1006

UNCOV
1007
                        case <-reg.cancelChan:
×
UNCOV
1008
                                return
×
1009

UNCOV
1010
                        case <-b.quit:
×
UNCOV
1011
                                return
×
1012
                        }
1013
                }
1014
        }()
1015

UNCOV
1016
        select {
×
1017
        case <-b.quit:
×
1018
                // As we're exiting before the registration could be sent,
×
1019
                // we'll stop the queue now ourselves.
×
1020
                reg.epochQueue.Stop()
×
1021

×
1022
                return nil, errors.New("chainntnfs: system interrupt while " +
×
1023
                        "attempting to register for block epoch notification.")
×
UNCOV
1024
        case b.notificationRegistry <- reg:
×
UNCOV
1025
                return &chainntnfs.BlockEpochEvent{
×
UNCOV
1026
                        Epochs: reg.epochChan,
×
UNCOV
1027
                        Cancel: func() {
×
UNCOV
1028
                                cancel := &epochCancel{
×
UNCOV
1029
                                        epochID: reg.epochID,
×
UNCOV
1030
                                }
×
UNCOV
1031

×
UNCOV
1032
                                // Submit epoch cancellation to notification dispatcher.
×
UNCOV
1033
                                select {
×
UNCOV
1034
                                case b.notificationCancels <- cancel:
×
UNCOV
1035
                                        // Cancellation is being handled, drain the epoch channel until it is
×
UNCOV
1036
                                        // closed before yielding to caller.
×
UNCOV
1037
                                        for {
×
UNCOV
1038
                                                select {
×
UNCOV
1039
                                                case _, ok := <-reg.epochChan:
×
UNCOV
1040
                                                        if !ok {
×
UNCOV
1041
                                                                return
×
UNCOV
1042
                                                        }
×
1043
                                                case <-b.quit:
×
1044
                                                        return
×
1045
                                                }
1046
                                        }
UNCOV
1047
                                case <-b.quit:
×
1048
                                }
1049
                        },
1050
                }, nil
1051
        }
1052
}
1053

1054
// GetBlock is used to retrieve the block with the given hash. This function
1055
// wraps the blockCache's GetBlock function.
1056
func (b *BitcoindNotifier) GetBlock(hash *chainhash.Hash) (*wire.MsgBlock,
UNCOV
1057
        error) {
×
UNCOV
1058

×
UNCOV
1059
        return b.blockCache.GetBlock(hash, b.chainConn.GetBlock)
×
UNCOV
1060
}
×
1061

1062
// SubscribeMempoolSpent allows the caller to register a subscription to watch
1063
// for a spend of an outpoint in the mempool.The event will be dispatched once
1064
// the outpoint is spent in the mempool.
1065
//
1066
// NOTE: part of the MempoolWatcher interface.
1067
func (b *BitcoindNotifier) SubscribeMempoolSpent(
UNCOV
1068
        outpoint wire.OutPoint) (*chainntnfs.MempoolSpendEvent, error) {
×
UNCOV
1069

×
UNCOV
1070
        event := b.memNotifier.SubscribeInput(outpoint)
×
UNCOV
1071

×
UNCOV
1072
        ops := []*wire.OutPoint{&outpoint}
×
UNCOV
1073

×
UNCOV
1074
        return event, b.chainConn.NotifySpent(ops)
×
UNCOV
1075
}
×
1076

1077
// CancelMempoolSpendEvent allows the caller to cancel a subscription to watch
1078
// for a spend of an outpoint in the mempool.
1079
//
1080
// NOTE: part of the MempoolWatcher interface.
1081
func (b *BitcoindNotifier) CancelMempoolSpendEvent(
UNCOV
1082
        sub *chainntnfs.MempoolSpendEvent) {
×
UNCOV
1083

×
UNCOV
1084
        b.memNotifier.UnsubscribeEvent(sub)
×
UNCOV
1085
}
×
1086

1087
// LookupInputMempoolSpend takes an outpoint and queries the mempool to find
1088
// its spending tx. Returns the tx if found, otherwise fn.None.
1089
//
1090
// NOTE: part of the MempoolWatcher interface.
1091
func (b *BitcoindNotifier) LookupInputMempoolSpend(
UNCOV
1092
        op wire.OutPoint) fn.Option[wire.MsgTx] {
×
UNCOV
1093

×
UNCOV
1094
        // Find the spending txid.
×
UNCOV
1095
        txid, found := b.chainConn.LookupInputMempoolSpend(op)
×
UNCOV
1096
        if !found {
×
UNCOV
1097
                return fn.None[wire.MsgTx]()
×
UNCOV
1098
        }
×
1099

1100
        // Query the spending tx using the id.
UNCOV
1101
        tx, err := b.chainConn.GetRawTransaction(&txid)
×
UNCOV
1102
        if err != nil {
×
1103
                // TODO(yy): enable logging errors in this package.
×
1104
                return fn.None[wire.MsgTx]()
×
1105
        }
×
1106

UNCOV
1107
        return fn.Some(*tx.MsgTx().Copy())
×
1108
}
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