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

lightningnetwork / lnd / 14994910928

13 May 2025 11:00AM UTC coverage: 68.991% (+10.4%) from 58.559%
14994910928

Pull #9752

github

web-flow
Merge ca77283bc into 1db6c31e2
Pull Request #9752: routerrpc: reject payment to invoice that don't have payment secret or blinded paths

6 of 7 new or added lines in 1 file covered. (85.71%)

959 existing lines in 12 files now uncovered.

133922 of 194116 relevant lines covered (68.99%)

22091.33 hits per line

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

80.46
/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,
94
        blockCache *blockcache.BlockCache) *BitcoindNotifier {
13✔
95

13✔
96
        notifier := &BitcoindNotifier{
13✔
97
                chainParams: chainParams,
13✔
98

13✔
99
                notificationCancels:  make(chan interface{}),
13✔
100
                notificationRegistry: make(chan interface{}),
13✔
101

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

13✔
104
                spendHintCache:   spendHintCache,
13✔
105
                confirmHintCache: confirmHintCache,
13✔
106

13✔
107
                blockCache:  blockCache,
13✔
108
                memNotifier: chainntnfs.NewMempoolNotifier(),
13✔
109

13✔
110
                quit: make(chan struct{}),
13✔
111
        }
13✔
112

13✔
113
        notifier.chainConn = chainConn.NewBitcoindClient()
13✔
114

13✔
115
        return notifier
13✔
116
}
13✔
117

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

126
        return startErr
7✔
127
}
128

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

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

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

13✔
144
        close(b.quit)
13✔
145
        b.wg.Wait()
13✔
146

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

47✔
153
                close(epochClient.epochChan)
47✔
154
        }
47✔
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.
158
        if b.txNotifier != nil {
26✔
159
                b.txNotifier.TearDown()
13✔
160
        }
13✔
161

162
        // Stop the mempool notifier.
163
        b.memNotifier.TearDown()
13✔
164

13✔
165
        return nil
13✔
166
}
167

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

173
func (b *BitcoindNotifier) startNotifier() error {
7✔
174
        chainntnfs.Log.Infof("bitcoind notifier starting...")
7✔
175

7✔
176
        // Connect to bitcoind, and register for notifications on connected,
7✔
177
        // and disconnected blocks.
7✔
178
        if err := b.chainConn.Start(); err != nil {
7✔
179
                return err
×
180
        }
×
181
        if err := b.chainConn.NotifyBlocks(); err != nil {
7✔
182
                return err
×
183
        }
×
184

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

194
        b.txNotifier = chainntnfs.NewTxNotifier(
7✔
195
                uint32(currentHeight), chainntnfs.ReorgSafetyLimit,
7✔
196
                b.confirmHintCache, b.spendHintCache,
7✔
197
        )
7✔
198

7✔
199
        b.bestBlock = chainntnfs.BlockEpoch{
7✔
200
                Height:      currentHeight,
7✔
201
                Hash:        currentHash,
7✔
202
                BlockHeader: blockHeader,
7✔
203
        }
7✔
204

7✔
205
        b.wg.Add(1)
7✔
206
        go b.notificationDispatcher()
7✔
207

7✔
208
        // Set the active flag now that we've completed the full
7✔
209
        // startup.
7✔
210
        atomic.StoreInt32(&b.active, 1)
7✔
211

7✔
212
        chainntnfs.Log.Debugf("bitcoind notifier started")
7✔
213

7✔
214
        return nil
7✔
215
}
216

217
// notificationDispatcher is the primary goroutine which handles client
218
// notification registrations, as well as notification dispatches.
219
func (b *BitcoindNotifier) notificationDispatcher() {
13✔
220
        defer b.wg.Done()
13✔
221

13✔
222
out:
13✔
223
        for {
853✔
224
                select {
840✔
225
                case cancelMsg := <-b.notificationCancels:
3✔
226
                        switch msg := cancelMsg.(type) {
3✔
227
                        case *epochCancel:
3✔
228
                                chainntnfs.Log.Infof("Cancelling epoch "+
3✔
229
                                        "notification, epoch_id=%v", msg.epochID)
3✔
230

3✔
231
                                // First, we'll lookup the original
3✔
232
                                // registration in order to stop the active
3✔
233
                                // queue goroutine.
3✔
234
                                reg := b.blockEpochClients[msg.epochID]
3✔
235
                                reg.epochQueue.Stop()
3✔
236

3✔
237
                                // Next, close the cancel channel for this
3✔
238
                                // specific client, and wait for the client to
3✔
239
                                // exit.
3✔
240
                                close(b.blockEpochClients[msg.epochID].cancelChan)
3✔
241
                                b.blockEpochClients[msg.epochID].wg.Wait()
3✔
242

3✔
243
                                // Once the client has exited, we can then
3✔
244
                                // safely close the channel used to send epoch
3✔
245
                                // notifications, in order to notify any
3✔
246
                                // listeners that the intent has been
3✔
247
                                // canceled.
3✔
248
                                close(b.blockEpochClients[msg.epochID].epochChan)
3✔
249
                                delete(b.blockEpochClients, msg.epochID)
3✔
250

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

65✔
263
                                //nolint:ll
65✔
264
                                go func(msg *chainntnfs.HistoricalConfDispatch) {
130✔
265
                                        defer b.wg.Done()
65✔
266

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

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

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

4✔
308
                                //nolint:ll
4✔
309
                                go func(msg *chainntnfs.HistoricalSpendDispatch) {
8✔
310
                                        defer b.wg.Done()
4✔
311

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

327
                                        chainntnfs.Log.Infof("Historical "+
4✔
328
                                                "spend dispatch finished "+
4✔
329
                                                "for request %v (start=%v "+
4✔
330
                                                "end=%v) with details: %v",
4✔
331
                                                msg.SpendRequest,
4✔
332
                                                msg.StartHeight, msg.EndHeight,
4✔
333
                                                spendDetails)
4✔
334

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

353
                        case *blockEpochRegistration:
49✔
354
                                chainntnfs.Log.Infof("New block epoch subscription")
49✔
355

49✔
356
                                b.blockEpochClients[msg.epochID] = msg
49✔
357

49✔
358
                                // If the client did not provide their best
49✔
359
                                // known block, then we'll immediately dispatch
49✔
360
                                // a notification for the current tip.
49✔
361
                                if msg.bestBlock == nil {
88✔
362
                                        b.notifyBlockEpochClient(
39✔
363
                                                msg, b.bestBlock.Height,
39✔
364
                                                b.bestBlock.Hash,
39✔
365
                                                b.bestBlock.BlockHeader,
39✔
366
                                        )
39✔
367

39✔
368
                                        msg.errorChan <- nil
39✔
369
                                        continue
39✔
370
                                }
371

372
                                // Otherwise, we'll attempt to deliver the
373
                                // backlog of notifications from their best
374
                                // known block.
375
                                missedBlocks, err := chainntnfs.GetClientMissedBlocks(
11✔
376
                                        b.chainConn, msg.bestBlock,
11✔
377
                                        b.bestBlock.Height, true,
11✔
378
                                )
11✔
379
                                if err != nil {
12✔
380
                                        msg.errorChan <- err
1✔
381
                                        continue
1✔
382
                                }
383

384
                                for _, block := range missedBlocks {
112✔
385
                                        b.notifyBlockEpochClient(
101✔
386
                                                msg, block.Height, block.Hash,
101✔
387
                                                block.BlockHeader,
101✔
388
                                        )
101✔
389
                                }
101✔
390

391
                                msg.errorChan <- nil
11✔
392
                        }
393

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

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

4✔
419
                                        if err != nil {
4✔
420
                                                // Set the bestBlock here in case
×
421
                                                // a catch up partially completed.
×
422
                                                b.bestBlock = newBestBlock
×
423
                                                chainntnfs.Log.Error(err)
×
424
                                                continue
×
425
                                        }
426

427
                                        for _, block := range missedBlocks {
46✔
428
                                                err := b.handleBlockConnected(block)
42✔
429
                                                if err != nil {
42✔
430
                                                        chainntnfs.Log.Error(err)
×
431
                                                        continue out
×
432
                                                }
433
                                        }
434
                                }
435

436
                                newBlock := chainntnfs.BlockEpoch{
317✔
437
                                        Height:      item.Height,
317✔
438
                                        Hash:        &item.Hash,
317✔
439
                                        BlockHeader: blockHeader,
317✔
440
                                }
317✔
441
                                if err := b.handleBlockConnected(newBlock); err != nil {
317✔
442
                                        chainntnfs.Log.Error(err)
×
443
                                }
×
444

445
                                continue
317✔
446

447
                        case chain.BlockDisconnected:
10✔
448
                                if item.Height != b.bestBlock.Height {
10✔
449
                                        chainntnfs.Log.Infof("Missed disconnected" +
×
450
                                                "blocks, attempting to catch up")
×
451
                                }
×
452

453
                                newBestBlock, err := chainntnfs.RewindChain(
10✔
454
                                        b.chainConn, b.txNotifier,
10✔
455
                                        b.bestBlock, item.Height-1,
10✔
456
                                )
10✔
457
                                if err != nil {
10✔
458
                                        chainntnfs.Log.Errorf("Unable to rewind chain "+
×
459
                                                "from height %d to height %d: %v",
×
460
                                                b.bestBlock.Height, item.Height-1, err)
×
461
                                }
×
462

463
                                // Set the bestBlock here in case a chain
464
                                // rewind partially completed.
465
                                b.bestBlock = newBestBlock
10✔
466

467
                        case chain.RelevantTx:
62✔
468
                                tx := btcutil.NewTx(&item.TxRecord.MsgTx)
62✔
469

62✔
470
                                // Init values.
62✔
471
                                isMempool := false
62✔
472
                                height := uint32(0)
62✔
473

62✔
474
                                // Unwrap values.
62✔
475
                                if item.Block == nil {
98✔
476
                                        isMempool = true
36✔
477
                                } else {
63✔
478
                                        height = uint32(item.Block.Height)
27✔
479
                                }
27✔
480

481
                                // Handle the transaction.
482
                                b.handleRelevantTx(tx, isMempool, height)
62✔
483
                        }
484

485
                case <-b.quit:
13✔
486
                        break out
13✔
487
                }
488
        }
489
}
490

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

62✔
498
        // If this is a mempool spend, we'll ask the mempool notifier to handle
62✔
499
        // it.
62✔
500
        if mempool {
98✔
501
                err := b.memNotifier.ProcessRelevantSpendTx(tx)
36✔
502
                if err != nil {
36✔
503
                        chainntnfs.Log.Errorf("Unable to process transaction "+
×
504
                                "%v: %v", tx.Hash(), err)
×
505
                }
×
506

507
                return
36✔
508
        }
509

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

×
517
                return
×
518
        }
×
519

520
        // Once the tx is processed, we will ask the memNotifier to unsubscribe
521
        // the input.
522
        //
523
        // NOTE(yy): we could build it into txNotifier.ProcessRelevantSpendTx,
524
        // but choose to implement it here so we can easily decouple the two
525
        // notifiers in the future.
526
        b.memNotifier.UnsubsribeConfirmedSpentTx(tx)
27✔
527
}
528

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

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

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

45✔
555
        // We'll then check the status of the transaction lookup returned to
45✔
556
        // determine whether we should proceed with any fallback methods.
45✔
557
        switch {
45✔
558

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

566
        // The transaction was found within the node's mempool.
567
        case txStatus == chainntnfs.TxFoundMempool:
14✔
568

569
        // The transaction was found within the node's txindex.
570
        case txStatus == chainntnfs.TxFoundIndex:
10✔
571

572
        // The transaction was not found within the node's mempool or txindex.
573
        case txStatus == chainntnfs.TxNotFoundIndex:
15✔
574

575
        // Unexpected txStatus returned.
576
        default:
×
577
                return nil, txStatus,
×
578
                        fmt.Errorf("Got unexpected txConfStatus: %v", txStatus)
×
579
        }
580

581
        return txConf, txStatus, nil
37✔
582
}
583

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

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

604
                blockHash, err := b.chainConn.GetBlockHash(int64(height))
62✔
605
                if err != nil {
62✔
606
                        return nil, chainntnfs.TxNotFoundManually,
×
607
                                fmt.Errorf("unable to get hash from block "+
×
608
                                        "with height %d", height)
×
609
                }
×
610

611
                block, err := b.GetBlock(blockHash)
62✔
612
                if err != nil {
62✔
613
                        return nil, chainntnfs.TxNotFoundManually,
×
614
                                fmt.Errorf("unable to get block with hash "+
×
615
                                        "%v: %v", blockHash, err)
×
616
                }
×
617

618
                // For every transaction in the block, check which one matches
619
                // our request. If we find one that does, we can dispatch its
620
                // confirmation details.
621
                for txIndex, tx := range block.Transactions {
159✔
622
                        if !confRequest.MatchesTx(tx) {
188✔
623
                                continue
91✔
624
                        }
625

626
                        return &chainntnfs.TxConfirmation{
6✔
627
                                Tx:          tx.Copy(),
6✔
628
                                BlockHash:   blockHash,
6✔
629
                                BlockHeight: height,
6✔
630
                                TxIndex:     uint32(txIndex),
6✔
631
                                Block:       block,
6✔
632
                        }, chainntnfs.TxFoundManually, nil
6✔
633
                }
634
        }
635

636
        // If we reach here, then we were not able to find the transaction
637
        // within a block, so we avoid returning an error.
638
        return nil, chainntnfs.TxNotFoundManually, nil
32✔
639
}
640

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

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

662
        chainntnfs.Log.Infof("New block: height=%v, sha=%v", block.Height,
359✔
663
                block.Hash)
359✔
664

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

359✔
672
        err = b.txNotifier.NotifyHeight(uint32(block.Height))
359✔
673
        if err != nil {
359✔
674
                return fmt.Errorf("unable to notify height: %w", err)
×
675
        }
×
676

677
        b.notifyBlockEpochs(block.Height, block.Hash, block.BlockHeader)
359✔
678

359✔
679
        return nil
359✔
680
}
681

682
// notifyBlockEpochs notifies all registered block epoch clients of the newly
683
// connected block to the main chain.
684
func (b *BitcoindNotifier) notifyBlockEpochs(newHeight int32, newSha *chainhash.Hash,
685
        blockHeader *wire.BlockHeader) {
359✔
686

359✔
687
        for _, client := range b.blockEpochClients {
903✔
688
                b.notifyBlockEpochClient(client, newHeight, newSha, blockHeader)
544✔
689
        }
544✔
690
}
691

692
// notifyBlockEpochClient sends a registered block epoch client a notification
693
// about a specific block.
694
func (b *BitcoindNotifier) notifyBlockEpochClient(epochClient *blockEpochRegistration,
695
        height int32, sha *chainhash.Hash, header *wire.BlockHeader) {
682✔
696

682✔
697
        epoch := &chainntnfs.BlockEpoch{
682✔
698
                Height:      height,
682✔
699
                Hash:        sha,
682✔
700
                BlockHeader: header,
682✔
701
        }
682✔
702

682✔
703
        select {
682✔
704
        case epochClient.epochQueue.ChanIn() <- epoch:
682✔
705
        case <-epochClient.cancelChan:
×
706
        case <-b.quit:
×
707
        }
708
}
709

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

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

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

752
        // If the txNotifier didn't return any details to perform a historical
753
        // scan of the chain, then we can return early as there's nothing left
754
        // for us to do.
755
        if ntfn.HistoricalDispatch == nil {
103✔
756
                return ntfn.Event, nil
50✔
757
        }
50✔
758

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

772
                return ntfn.Event, nil
2✔
773
        }
774

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

796
                return ntfn.Event, nil
1✔
797
        }
798

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

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

827
                blockHash, err := chainhash.NewHashFromStr(tx.BlockHash)
2✔
828
                if err != nil {
2✔
829
                        return nil, err
×
830
                }
×
831
                blockHeight, err := b.chainConn.GetBlockHeight(blockHash)
2✔
832
                if err != nil {
2✔
833
                        return nil, err
×
834
                }
×
835

836
                if uint32(blockHeight) > ntfn.HistoricalDispatch.StartHeight {
3✔
837
                        ntfn.HistoricalDispatch.StartHeight = uint32(blockHeight)
1✔
838
                }
1✔
839
        }
840

841
        // Now that we've determined the starting point of our rescan, we can
842
        // dispatch it and return.
843
        select {
2✔
844
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
2✔
845
        case <-b.quit:
×
846
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
847
        }
848

849
        return ntfn.Event, nil
2✔
850
}
851

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

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

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

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

895
                        txCopy := tx.Copy()
1✔
896
                        txHash := txCopy.TxHash()
1✔
897
                        spendOutPoint := &txCopy.TxIn[inputIdx].PreviousOutPoint
1✔
898
                        return &chainntnfs.SpendDetail{
1✔
899
                                SpentOutPoint:     spendOutPoint,
1✔
900
                                SpenderTxHash:     &txHash,
1✔
901
                                SpendingTx:        txCopy,
1✔
902
                                SpenderInputIndex: inputIdx,
1✔
903
                                SpendingHeight:    int32(height),
1✔
904
                        }, nil
1✔
905
                }
906
        }
907

908
        return nil, nil
4✔
909
}
910

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

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

935
        if ntfn.HistoricalDispatch == nil {
130✔
936
                return ntfn.Event, nil
33✔
937
        }
33✔
938

939
        select {
65✔
940
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
65✔
941
                return ntfn.Event, nil
65✔
942
        case <-b.quit:
×
943
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
944
        }
945
}
946

947
// blockEpochRegistration represents a client's intent to receive a
948
// notification with each newly connected block.
949
type blockEpochRegistration struct {
950
        epochID uint64
951

952
        epochChan chan *chainntnfs.BlockEpoch
953

954
        epochQueue *queue.ConcurrentQueue
955

956
        bestBlock *chainntnfs.BlockEpoch
957

958
        errorChan chan error
959

960
        cancelChan chan struct{}
961

962
        wg sync.WaitGroup
963
}
964

965
// epochCancel is a message sent to the BitcoindNotifier when a client wishes
966
// to cancel an outstanding epoch notification that has yet to be dispatched.
967
type epochCancel struct {
968
        epochID uint64
969
}
970

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

49✔
980
        reg := &blockEpochRegistration{
49✔
981
                epochQueue: queue.NewConcurrentQueue(20),
49✔
982
                epochChan:  make(chan *chainntnfs.BlockEpoch, 20),
49✔
983
                cancelChan: make(chan struct{}),
49✔
984
                epochID:    atomic.AddUint64(&b.epochClientCounter, 1),
49✔
985
                bestBlock:  bestBlock,
49✔
986
                errorChan:  make(chan error, 1),
49✔
987
        }
49✔
988
        reg.epochQueue.Start()
49✔
989

49✔
990
        // Before we send the request to the main goroutine, we'll launch a new
49✔
991
        // goroutine to proxy items added to our queue to the client itself.
49✔
992
        // This ensures that all notifications are received *in order*.
49✔
993
        reg.wg.Add(1)
49✔
994
        go func() {
98✔
995
                defer reg.wg.Done()
49✔
996

49✔
997
                for {
686✔
998
                        select {
637✔
999
                        case ntfn := <-reg.epochQueue.ChanOut():
593✔
1000
                                blockNtfn := ntfn.(*chainntnfs.BlockEpoch)
593✔
1001
                                select {
593✔
1002
                                case reg.epochChan <- blockNtfn:
589✔
1003

1004
                                case <-reg.cancelChan:
1✔
1005
                                        return
1✔
1006

1007
                                case <-b.quit:
4✔
1008
                                        return
4✔
1009
                                }
1010

1011
                        case <-reg.cancelChan:
3✔
1012
                                return
3✔
1013

1014
                        case <-b.quit:
43✔
1015
                                return
43✔
1016
                        }
1017
                }
1018
        }()
1019

1020
        select {
49✔
1021
        case <-b.quit:
×
1022
                // As we're exiting before the registration could be sent,
×
1023
                // we'll stop the queue now ourselves.
×
1024
                reg.epochQueue.Stop()
×
1025

×
1026
                return nil, errors.New("chainntnfs: system interrupt while " +
×
1027
                        "attempting to register for block epoch notification.")
×
1028
        case b.notificationRegistry <- reg:
49✔
1029
                return &chainntnfs.BlockEpochEvent{
49✔
1030
                        Epochs: reg.epochChan,
49✔
1031
                        Cancel: func() {
52✔
1032
                                cancel := &epochCancel{
3✔
1033
                                        epochID: reg.epochID,
3✔
1034
                                }
3✔
1035

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

1058
// GetBlock is used to retrieve the block with the given hash. This function
1059
// wraps the blockCache's GetBlock function.
1060
func (b *BitcoindNotifier) GetBlock(hash *chainhash.Hash) (*wire.MsgBlock,
1061
        error) {
424✔
1062

424✔
1063
        return b.blockCache.GetBlock(hash, b.chainConn.GetBlock)
424✔
1064
}
424✔
1065

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

1✔
1074
        event := b.memNotifier.SubscribeInput(outpoint)
1✔
1075

1✔
1076
        ops := []*wire.OutPoint{&outpoint}
1✔
1077

1✔
1078
        return event, b.chainConn.NotifySpent(ops)
1✔
1079
}
1✔
1080

1081
// CancelMempoolSpendEvent allows the caller to cancel a subscription to watch
1082
// for a spend of an outpoint in the mempool.
1083
//
1084
// NOTE: part of the MempoolWatcher interface.
1085
func (b *BitcoindNotifier) CancelMempoolSpendEvent(
1086
        sub *chainntnfs.MempoolSpendEvent) {
1✔
1087

1✔
1088
        b.memNotifier.UnsubscribeEvent(sub)
1✔
1089
}
1✔
1090

1091
// LookupInputMempoolSpend takes an outpoint and queries the mempool to find
1092
// its spending tx. Returns the tx if found, otherwise fn.None.
1093
//
1094
// NOTE: part of the MempoolWatcher interface.
1095
func (b *BitcoindNotifier) LookupInputMempoolSpend(
1096
        op wire.OutPoint) fn.Option[wire.MsgTx] {
1✔
1097

1✔
1098
        // Find the spending txid.
1✔
1099
        txid, found := b.chainConn.LookupInputMempoolSpend(op)
1✔
1100
        if !found {
2✔
1101
                return fn.None[wire.MsgTx]()
1✔
1102
        }
1✔
1103

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

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