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

lightningnetwork / lnd / 12281843618

11 Dec 2024 05:38PM UTC coverage: 49.477% (-0.06%) from 49.54%
12281843618

Pull #9242

github

aakselrod
docs: update release-notes for 0.19.0
Pull Request #9242: Reapply #8644

6 of 27 new or added lines in 2 files covered. (22.22%)

170 existing lines in 20 files now uncovered.

100257 of 202632 relevant lines covered (49.48%)

1.54 hits per line

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

71.91
/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 {
1✔
95

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

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

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

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

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

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

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

1✔
115
        return notifier
1✔
116
}
1✔
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 {
1✔
121
        var startErr error
1✔
122
        b.start.Do(func() {
2✔
123
                startErr = b.startNotifier()
1✔
124
        })
1✔
125

126
        return startErr
1✔
127
}
128

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

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

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

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

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

1✔
153
                close(epochClient.epochChan)
1✔
154
        }
1✔
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 {
2✔
159
                b.txNotifier.TearDown()
1✔
160
        }
1✔
161

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

1✔
165
        return nil
1✔
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 {
1✔
174
        // Connect to bitcoind, and register for notifications on connected,
1✔
175
        // and disconnected blocks.
1✔
176
        if err := b.chainConn.Start(); err != nil {
1✔
177
                return err
×
178
        }
×
179
        if err := b.chainConn.NotifyBlocks(); err != nil {
1✔
180
                return err
×
181
        }
×
182

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

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

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

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

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

1✔
210
        return nil
1✔
211
}
212

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

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

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

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

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

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

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

1✔
263
                                        confDetails, _, err := b.historicalConfDetails(
1✔
264
                                                msg.ConfRequest,
1✔
265
                                                msg.StartHeight, msg.EndHeight,
1✔
266
                                        )
1✔
267
                                        if err != nil {
1✔
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.
285
                                        err = b.txNotifier.UpdateConfDetails(
1✔
286
                                                msg.ConfRequest, confDetails,
1✔
287
                                        )
1✔
288
                                        if err != nil {
1✔
289
                                                chainntnfs.Log.Errorf("Unable "+
×
290
                                                        "to update conf "+
×
291
                                                        "details of %v: %v",
×
292
                                                        msg.ConfRequest, err)
×
293
                                        }
×
294
                                }(msg)
295

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

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

1✔
308
                                        spendDetails, err := b.historicalSpendDetails(
1✔
309
                                                msg.SpendRequest,
1✔
310
                                                msg.StartHeight, msg.EndHeight,
1✔
311
                                        )
1✔
312
                                        if err != nil {
1✔
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

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

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

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

1✔
352
                                b.blockEpochClients[msg.epochID] = msg
1✔
353

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

1✔
364
                                        msg.errorChan <- nil
1✔
365
                                        continue
1✔
366
                                }
367

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

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

387
                                msg.errorChan <- nil
1✔
388
                        }
389

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

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

×
UNCOV
415
                                        if err != nil {
×
UNCOV
416
                                                // Set the bestBlock here in case
×
UNCOV
417
                                                // a catch up partially completed.
×
UNCOV
418
                                                b.bestBlock = newBestBlock
×
UNCOV
419
                                                chainntnfs.Log.Error(err)
×
UNCOV
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

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

441
                                continue
1✔
442

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

449
                                newBestBlock, err := chainntnfs.RewindChain(
1✔
450
                                        b.chainConn, b.txNotifier,
1✔
451
                                        b.bestBlock, item.Height-1,
1✔
452
                                )
1✔
453
                                if err != nil {
1✔
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.
461
                                b.bestBlock = newBestBlock
1✔
462

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

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

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

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

481
                case <-b.quit:
1✔
482
                        break out
1✔
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,
492
        mempool bool, height uint32) {
1✔
493

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

503
                return
1✔
504
        }
505

506
        // Otherwise this is a confirmed spend, and we'll ask the tx notifier
507
        // to handle it.
508
        err := b.txNotifier.ProcessRelevantSpendTx(tx, height)
1✔
509
        if err != nil {
1✔
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.
522
        b.memNotifier.UnsubsribeConfirmedSpentTx(tx)
1✔
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,
530
        chainntnfs.TxConfStatus, error) {
1✔
531

1✔
532
        // If a txid was not provided, then we should dispatch upon seeing the
1✔
533
        // script on-chain, so we'll short-circuit straight to scanning manually
1✔
534
        // as there doesn't exist a script index to query.
1✔
535
        if confRequest.TxID == chainntnfs.ZeroHash {
1✔
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.
546
        txNotFoundErr := "No such mempool or blockchain transaction"
1✔
547
        txConf, txStatus, err := chainntnfs.ConfDetailsFromTxIndex(
1✔
548
                b.chainConn, confRequest, txNotFoundErr,
1✔
549
        )
1✔
550

1✔
551
        // We'll then check the status of the transaction lookup returned to
1✔
552
        // determine whether we should proceed with any fallback methods.
1✔
553
        switch {
1✔
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.
563
        case txStatus == chainntnfs.TxFoundMempool:
1✔
564

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

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

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

577
        return txConf, txStatus, nil
1✔
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.
640
func (b *BitcoindNotifier) handleBlockConnected(block chainntnfs.BlockEpoch) error {
1✔
641
        // First, we'll fetch the raw block as we'll need to gather all the
1✔
642
        // transactions to determine whether any are relevant to our registered
1✔
643
        // clients.
1✔
644
        rawBlock, err := b.GetBlock(block.Hash)
1✔
645
        if err != nil {
1✔
646
                return fmt.Errorf("unable to get block: %w", err)
×
647
        }
×
648
        utilBlock := btcutil.NewBlock(rawBlock)
1✔
649

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

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

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

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

673
        b.notifyBlockEpochs(block.Height, block.Hash, block.BlockHeader)
1✔
674

1✔
675
        return nil
1✔
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,
681
        blockHeader *wire.BlockHeader) {
1✔
682

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

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

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

1✔
699
        select {
1✔
700
        case epochClient.epochQueue.ChanIn() <- epoch:
1✔
701
        case <-epochClient.cancelChan:
×
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,
715
        pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
1✔
716

1✔
717
        // Register the conf notification with the TxNotifier. A non-nil value
1✔
718
        // for `dispatch` will be returned if we are required to perform a
1✔
719
        // manual scan for the confirmation. Otherwise the notifier will begin
1✔
720
        // watching at tip for the transaction to confirm.
1✔
721
        ntfn, err := b.txNotifier.RegisterSpend(outpoint, pkScript, heightHint)
1✔
722
        if err != nil {
2✔
723
                return nil, err
1✔
724
        }
1✔
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.
730
        if outpoint == nil || *outpoint == chainntnfs.ZeroOutPoint {
1✔
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
                }
×
741
        } else {
1✔
742
                ops := []*wire.OutPoint{outpoint}
1✔
743
                if err := b.chainConn.NotifySpent(ops); err != nil {
1✔
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.
751
        if ntfn.HistoricalDispatch == nil {
2✔
752
                return ntfn.Event, nil
1✔
753
        }
1✔
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.
761
        if ntfn.HistoricalDispatch.OutPoint == chainntnfs.ZeroOutPoint {
1✔
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.
778
        txOut, err := b.chainConn.GetTxOut(&outpoint.Hash, outpoint.Index, true)
1✔
779
        if err != nil {
1✔
780
                return nil, err
×
781
        }
×
782
        if txOut != nil {
2✔
783
                // We'll let the txNotifier know the outpoint is still unspent
1✔
784
                // in order to begin updating its spend hint.
1✔
785
                err := b.txNotifier.UpdateSpendDetails(
1✔
786
                        ntfn.HistoricalDispatch.SpendRequest, nil,
1✔
787
                )
1✔
788
                if err != nil {
1✔
789
                        return nil, err
×
790
                }
×
791

792
                return ntfn.Event, nil
1✔
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.
802
        tx, err := b.chainConn.GetRawTransactionVerbose(&outpoint.Hash)
1✔
803
        if err != nil {
2✔
804
                // Avoid returning an error if the transaction was not found to
1✔
805
                // proceed with fallback methods.
1✔
806
                jsonErr, ok := err.(*btcjson.RPCError)
1✔
807
                if !ok || jsonErr.Code != btcjson.ErrRPCNoTxInfo {
1✔
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.
816
        if tx != nil {
2✔
817
                // If the transaction containing the outpoint hasn't confirmed
1✔
818
                // on-chain, then there's no need to perform a rescan.
1✔
819
                if tx.BlockHash == "" {
2✔
820
                        return ntfn.Event, nil
1✔
821
                }
1✔
822

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

832
                if uint32(blockHeight) > ntfn.HistoricalDispatch.StartHeight {
2✔
833
                        ntfn.HistoricalDispatch.StartHeight = uint32(blockHeight)
1✔
834
                }
1✔
835
        }
836

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

845
        return ntfn.Event, nil
1✔
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) (
854
        *chainntnfs.SpendDetail, error) {
1✔
855

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

867
                // First, we'll fetch the block for the current height.
868
                blockHash, err := b.chainConn.GetBlockHash(int64(height))
1✔
869
                if err != nil {
1✔
870
                        return nil, fmt.Errorf("unable to retrieve hash for "+
×
871
                                "block with height %d: %v", height, err)
×
872
                }
×
873
                block, err := b.GetBlock(blockHash)
1✔
874
                if err != nil {
1✔
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.
882
                for _, tx := range block.Transactions {
2✔
883
                        matches, inputIdx, err := spendRequest.MatchesTx(tx)
1✔
884
                        if err != nil {
1✔
885
                                return nil, err
×
886
                        }
×
887
                        if !matches {
2✔
888
                                continue
1✔
889
                        }
890

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

904
        return nil, nil
1✔
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,
918
        opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent, error) {
1✔
919

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

931
        if ntfn.HistoricalDispatch == nil {
2✔
932
                return ntfn.Event, nil
1✔
933
        }
1✔
934

935
        select {
1✔
936
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
1✔
937
                return ntfn.Event, nil
1✔
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(
974
        bestBlock *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error) {
1✔
975

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

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

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

1000
                                case <-reg.cancelChan:
1✔
1001
                                        return
1✔
1002

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

1007
                        case <-reg.cancelChan:
1✔
1008
                                return
1✔
1009

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

1016
        select {
1✔
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.")
×
1024
        case b.notificationRegistry <- reg:
1✔
1025
                return &chainntnfs.BlockEpochEvent{
1✔
1026
                        Epochs: reg.epochChan,
1✔
1027
                        Cancel: func() {
2✔
1028
                                cancel := &epochCancel{
1✔
1029
                                        epochID: reg.epochID,
1✔
1030
                                }
1✔
1031

1✔
1032
                                // Submit epoch cancellation to notification dispatcher.
1✔
1033
                                select {
1✔
1034
                                case b.notificationCancels <- cancel:
1✔
1035
                                        // Cancellation is being handled, drain the epoch channel until it is
1✔
1036
                                        // closed before yielding to caller.
1✔
1037
                                        for {
2✔
1038
                                                select {
1✔
1039
                                                case _, ok := <-reg.epochChan:
1✔
1040
                                                        if !ok {
2✔
1041
                                                                return
1✔
1042
                                                        }
1✔
1043
                                                case <-b.quit:
×
1044
                                                        return
×
1045
                                                }
1046
                                        }
1047
                                case <-b.quit:
1✔
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,
1057
        error) {
1✔
1058

1✔
1059
        return b.blockCache.GetBlock(hash, b.chainConn.GetBlock)
1✔
1060
}
1✔
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(
1068
        outpoint wire.OutPoint) (*chainntnfs.MempoolSpendEvent, error) {
1✔
1069

1✔
1070
        event := b.memNotifier.SubscribeInput(outpoint)
1✔
1071

1✔
1072
        ops := []*wire.OutPoint{&outpoint}
1✔
1073

1✔
1074
        return event, b.chainConn.NotifySpent(ops)
1✔
1075
}
1✔
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(
1082
        sub *chainntnfs.MempoolSpendEvent) {
1✔
1083

1✔
1084
        b.memNotifier.UnsubscribeEvent(sub)
1✔
1085
}
1✔
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(
1092
        op wire.OutPoint) fn.Option[wire.MsgTx] {
1✔
1093

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

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

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