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

lightningnetwork / lnd / 9935147745

15 Jul 2024 07:07AM UTC coverage: 49.819% (+0.6%) from 49.268%
9935147745

Pull #8900

github

guggero
Makefile: add GOCC variable
Pull Request #8900: Makefile: add GOCC variable

93876 of 188433 relevant lines covered (49.82%)

2.07 hits per line

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

74.89
/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"
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 {
2✔
95

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

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

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

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

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

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

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

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

126
        return startErr
2✔
127
}
128

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

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

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

2✔
143
        close(b.quit)
2✔
144
        b.wg.Wait()
2✔
145

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

2✔
152
                close(epochClient.epochChan)
2✔
153
        }
2✔
154
        b.txNotifier.TearDown()
2✔
155

2✔
156
        // Stop the mempool notifier.
2✔
157
        b.memNotifier.TearDown()
2✔
158

2✔
159
        return nil
2✔
160
}
161

162
// Started returns true if this instance has been started, and false otherwise.
163
func (b *BitcoindNotifier) Started() bool {
2✔
164
        return atomic.LoadInt32(&b.active) != 0
2✔
165
}
2✔
166

167
func (b *BitcoindNotifier) startNotifier() error {
2✔
168
        // Connect to bitcoind, and register for notifications on connected,
2✔
169
        // and disconnected blocks.
2✔
170
        if err := b.chainConn.Start(); err != nil {
2✔
171
                return err
×
172
        }
×
173
        if err := b.chainConn.NotifyBlocks(); err != nil {
2✔
174
                return err
×
175
        }
×
176

177
        currentHash, currentHeight, err := b.chainConn.GetBestBlock()
2✔
178
        if err != nil {
2✔
179
                return err
×
180
        }
×
181
        blockHeader, err := b.chainConn.GetBlockHeader(currentHash)
2✔
182
        if err != nil {
2✔
183
                return err
×
184
        }
×
185

186
        b.txNotifier = chainntnfs.NewTxNotifier(
2✔
187
                uint32(currentHeight), chainntnfs.ReorgSafetyLimit,
2✔
188
                b.confirmHintCache, b.spendHintCache,
2✔
189
        )
2✔
190

2✔
191
        b.bestBlock = chainntnfs.BlockEpoch{
2✔
192
                Height:      currentHeight,
2✔
193
                Hash:        currentHash,
2✔
194
                BlockHeader: blockHeader,
2✔
195
        }
2✔
196

2✔
197
        b.wg.Add(1)
2✔
198
        go b.notificationDispatcher()
2✔
199

2✔
200
        // Set the active flag now that we've completed the full
2✔
201
        // startup.
2✔
202
        atomic.StoreInt32(&b.active, 1)
2✔
203

2✔
204
        return nil
2✔
205
}
206

207
// notificationDispatcher is the primary goroutine which handles client
208
// notification registrations, as well as notification dispatches.
209
func (b *BitcoindNotifier) notificationDispatcher() {
2✔
210
        defer b.wg.Done()
2✔
211

2✔
212
out:
2✔
213
        for {
4✔
214
                select {
2✔
215
                case cancelMsg := <-b.notificationCancels:
2✔
216
                        switch msg := cancelMsg.(type) {
2✔
217
                        case *epochCancel:
2✔
218
                                chainntnfs.Log.Infof("Cancelling epoch "+
2✔
219
                                        "notification, epoch_id=%v", msg.epochID)
2✔
220

2✔
221
                                // First, we'll lookup the original
2✔
222
                                // registration in order to stop the active
2✔
223
                                // queue goroutine.
2✔
224
                                reg := b.blockEpochClients[msg.epochID]
2✔
225
                                reg.epochQueue.Stop()
2✔
226

2✔
227
                                // Next, close the cancel channel for this
2✔
228
                                // specific client, and wait for the client to
2✔
229
                                // exit.
2✔
230
                                close(b.blockEpochClients[msg.epochID].cancelChan)
2✔
231
                                b.blockEpochClients[msg.epochID].wg.Wait()
2✔
232

2✔
233
                                // Once the client has exited, we can then
2✔
234
                                // safely close the channel used to send epoch
2✔
235
                                // notifications, in order to notify any
2✔
236
                                // listeners that the intent has been
2✔
237
                                // canceled.
2✔
238
                                close(b.blockEpochClients[msg.epochID].epochChan)
2✔
239
                                delete(b.blockEpochClients, msg.epochID)
2✔
240

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

2✔
253
                                //nolint:lll
2✔
254
                                go func(msg *chainntnfs.HistoricalConfDispatch) {
4✔
255
                                        defer b.wg.Done()
2✔
256

2✔
257
                                        confDetails, _, err := b.historicalConfDetails(
2✔
258
                                                msg.ConfRequest,
2✔
259
                                                msg.StartHeight, msg.EndHeight,
2✔
260
                                        )
2✔
261
                                        if err != nil {
2✔
262
                                                chainntnfs.Log.Errorf("Rescan to "+
×
263
                                                        "determine the conf "+
×
264
                                                        "details of %v within "+
×
265
                                                        "range %d-%d failed: %v",
×
266
                                                        msg.ConfRequest,
×
267
                                                        msg.StartHeight,
×
268
                                                        msg.EndHeight, err)
×
269
                                                return
×
270
                                        }
×
271

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

290
                        case *chainntnfs.HistoricalSpendDispatch:
2✔
291
                                // In order to ensure we don't block the caller
2✔
292
                                // on what may be a long rescan, we'll launch a
2✔
293
                                // goroutine to do so in the background.
2✔
294
                                //
2✔
295
                                // TODO(wilmer): add retry logic if rescan fails?
2✔
296
                                b.wg.Add(1)
2✔
297

2✔
298
                                //nolint:lll
2✔
299
                                go func(msg *chainntnfs.HistoricalSpendDispatch) {
4✔
300
                                        defer b.wg.Done()
2✔
301

2✔
302
                                        spendDetails, err := b.historicalSpendDetails(
2✔
303
                                                msg.SpendRequest,
2✔
304
                                                msg.StartHeight, msg.EndHeight,
2✔
305
                                        )
2✔
306
                                        if err != nil {
2✔
307
                                                chainntnfs.Log.Errorf("Rescan to "+
×
308
                                                        "determine the spend "+
×
309
                                                        "details of %v within "+
×
310
                                                        "range %d-%d failed: %v",
×
311
                                                        msg.SpendRequest,
×
312
                                                        msg.StartHeight,
×
313
                                                        msg.EndHeight, err)
×
314
                                                return
×
315
                                        }
×
316

317
                                        chainntnfs.Log.Infof("Historical "+
2✔
318
                                                "spend dispatch finished "+
2✔
319
                                                "for request %v (start=%v "+
2✔
320
                                                "end=%v) with details: %v",
2✔
321
                                                msg.SpendRequest,
2✔
322
                                                msg.StartHeight, msg.EndHeight,
2✔
323
                                                spendDetails)
2✔
324

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

343
                        case *blockEpochRegistration:
2✔
344
                                chainntnfs.Log.Infof("New block epoch subscription")
2✔
345

2✔
346
                                b.blockEpochClients[msg.epochID] = msg
2✔
347

2✔
348
                                // If the client did not provide their best
2✔
349
                                // known block, then we'll immediately dispatch
2✔
350
                                // a notification for the current tip.
2✔
351
                                if msg.bestBlock == nil {
4✔
352
                                        b.notifyBlockEpochClient(
2✔
353
                                                msg, b.bestBlock.Height,
2✔
354
                                                b.bestBlock.Hash,
2✔
355
                                                b.bestBlock.BlockHeader,
2✔
356
                                        )
2✔
357

2✔
358
                                        msg.errorChan <- nil
2✔
359
                                        continue
2✔
360
                                }
361

362
                                // Otherwise, we'll attempt to deliver the
363
                                // backlog of notifications from their best
364
                                // known block.
365
                                missedBlocks, err := chainntnfs.GetClientMissedBlocks(
2✔
366
                                        b.chainConn, msg.bestBlock,
2✔
367
                                        b.bestBlock.Height, true,
2✔
368
                                )
2✔
369
                                if err != nil {
3✔
370
                                        msg.errorChan <- err
1✔
371
                                        continue
1✔
372
                                }
373

374
                                for _, block := range missedBlocks {
4✔
375
                                        b.notifyBlockEpochClient(
2✔
376
                                                msg, block.Height, block.Hash,
2✔
377
                                                block.BlockHeader,
2✔
378
                                        )
2✔
379
                                }
2✔
380

381
                                msg.errorChan <- nil
2✔
382
                        }
383

384
                case ntfn := <-b.chainConn.Notifications():
2✔
385
                        switch item := ntfn.(type) {
2✔
386
                        case chain.BlockConnected:
2✔
387
                                blockHeader, err :=
2✔
388
                                        b.chainConn.GetBlockHeader(&item.Hash)
2✔
389
                                if err != nil {
2✔
390
                                        chainntnfs.Log.Errorf("Unable to fetch "+
×
391
                                                "block header: %v", err)
×
392
                                        continue
×
393
                                }
394

395
                                if blockHeader.PrevBlock != *b.bestBlock.Hash {
3✔
396
                                        // Handle the case where the notifier
1✔
397
                                        // missed some blocks from its chain
1✔
398
                                        // backend.
1✔
399
                                        chainntnfs.Log.Infof("Missed blocks, " +
1✔
400
                                                "attempting to catch up")
1✔
401
                                        newBestBlock, missedBlocks, err :=
1✔
402
                                                chainntnfs.HandleMissedBlocks(
1✔
403
                                                        b.chainConn,
1✔
404
                                                        b.txNotifier,
1✔
405
                                                        b.bestBlock, item.Height,
1✔
406
                                                        true,
1✔
407
                                                )
1✔
408

1✔
409
                                        if err != nil {
2✔
410
                                                // Set the bestBlock here in case
1✔
411
                                                // a catch up partially completed.
1✔
412
                                                b.bestBlock = newBestBlock
1✔
413
                                                chainntnfs.Log.Error(err)
1✔
414
                                                continue
1✔
415
                                        }
416

417
                                        for _, block := range missedBlocks {
×
418
                                                err := b.handleBlockConnected(block)
×
419
                                                if err != nil {
×
420
                                                        chainntnfs.Log.Error(err)
×
421
                                                        continue out
×
422
                                                }
423
                                        }
424
                                }
425

426
                                newBlock := chainntnfs.BlockEpoch{
2✔
427
                                        Height:      item.Height,
2✔
428
                                        Hash:        &item.Hash,
2✔
429
                                        BlockHeader: blockHeader,
2✔
430
                                }
2✔
431
                                if err := b.handleBlockConnected(newBlock); err != nil {
2✔
432
                                        chainntnfs.Log.Error(err)
×
433
                                }
×
434

435
                                continue
2✔
436

437
                        case chain.BlockDisconnected:
2✔
438
                                if item.Height != b.bestBlock.Height {
2✔
439
                                        chainntnfs.Log.Infof("Missed disconnected" +
×
440
                                                "blocks, attempting to catch up")
×
441
                                }
×
442

443
                                newBestBlock, err := chainntnfs.RewindChain(
2✔
444
                                        b.chainConn, b.txNotifier,
2✔
445
                                        b.bestBlock, item.Height-1,
2✔
446
                                )
2✔
447
                                if err != nil {
2✔
448
                                        chainntnfs.Log.Errorf("Unable to rewind chain "+
×
449
                                                "from height %d to height %d: %v",
×
450
                                                b.bestBlock.Height, item.Height-1, err)
×
451
                                }
×
452

453
                                // Set the bestBlock here in case a chain
454
                                // rewind partially completed.
455
                                b.bestBlock = newBestBlock
2✔
456

457
                        case chain.RelevantTx:
2✔
458
                                tx := btcutil.NewTx(&item.TxRecord.MsgTx)
2✔
459

2✔
460
                                // Init values.
2✔
461
                                isMempool := false
2✔
462
                                height := uint32(0)
2✔
463

2✔
464
                                // Unwrap values.
2✔
465
                                if item.Block == nil {
4✔
466
                                        isMempool = true
2✔
467
                                } else {
4✔
468
                                        height = uint32(item.Block.Height)
2✔
469
                                }
2✔
470

471
                                // Handle the transaction.
472
                                b.handleRelevantTx(tx, isMempool, height)
2✔
473
                        }
474

475
                case <-b.quit:
2✔
476
                        break out
2✔
477
                }
478
        }
479
}
480

481
// handleRelevantTx handles a new transaction that has been seen either in a
482
// block or in the mempool. If in mempool, it will ask the mempool notifier to
483
// handle it. If in a block, it will ask the txNotifier to handle it, and
484
// cancel any relevant subscriptions made in the mempool.
485
func (b *BitcoindNotifier) handleRelevantTx(tx *btcutil.Tx,
486
        mempool bool, height uint32) {
2✔
487

2✔
488
        // If this is a mempool spend, we'll ask the mempool notifier to hanlde
2✔
489
        // it.
2✔
490
        if mempool {
4✔
491
                err := b.memNotifier.ProcessRelevantSpendTx(tx)
2✔
492
                if err != nil {
2✔
493
                        chainntnfs.Log.Errorf("Unable to process transaction "+
×
494
                                "%v: %v", tx.Hash(), err)
×
495
                }
×
496

497
                return
2✔
498
        }
499

500
        // Otherwise this is a confirmed spend, and we'll ask the tx notifier
501
        // to handle it.
502
        err := b.txNotifier.ProcessRelevantSpendTx(tx, height)
2✔
503
        if err != nil {
2✔
504
                chainntnfs.Log.Errorf("Unable to process transaction %v: %v",
×
505
                        tx.Hash(), err)
×
506

×
507
                return
×
508
        }
×
509

510
        // Once the tx is processed, we will ask the memNotifier to unsubscribe
511
        // the input.
512
        //
513
        // NOTE(yy): we could build it into txNotifier.ProcessRelevantSpendTx,
514
        // but choose to implement it here so we can easily decouple the two
515
        // notifiers in the future.
516
        b.memNotifier.UnsubsribeConfirmedSpentTx(tx)
2✔
517
}
518

519
// historicalConfDetails looks up whether a confirmation request (txid/output
520
// script) has already been included in a block in the active chain and, if so,
521
// returns details about said block.
522
func (b *BitcoindNotifier) historicalConfDetails(confRequest chainntnfs.ConfRequest,
523
        startHeight, endHeight uint32) (*chainntnfs.TxConfirmation,
524
        chainntnfs.TxConfStatus, error) {
2✔
525

2✔
526
        // If a txid was not provided, then we should dispatch upon seeing the
2✔
527
        // script on-chain, so we'll short-circuit straight to scanning manually
2✔
528
        // as there doesn't exist a script index to query.
2✔
529
        if confRequest.TxID == chainntnfs.ZeroHash {
2✔
530
                return b.confDetailsManually(
×
531
                        confRequest, startHeight, endHeight,
×
532
                )
×
533
        }
×
534

535
        // Otherwise, we'll dispatch upon seeing a transaction on-chain with the
536
        // given hash.
537
        //
538
        // We'll first attempt to retrieve the transaction using the node's
539
        // txindex.
540
        txNotFoundErr := "No such mempool or blockchain transaction"
2✔
541
        txConf, txStatus, err := chainntnfs.ConfDetailsFromTxIndex(
2✔
542
                b.chainConn, confRequest, txNotFoundErr,
2✔
543
        )
2✔
544

2✔
545
        // We'll then check the status of the transaction lookup returned to
2✔
546
        // determine whether we should proceed with any fallback methods.
2✔
547
        switch {
2✔
548

549
        // We failed querying the index for the transaction, fall back to
550
        // scanning manually.
551
        case err != nil:
×
552
                chainntnfs.Log.Debugf("Failed getting conf details from "+
×
553
                        "index (%v), scanning manually", err)
×
554
                return b.confDetailsManually(confRequest, startHeight, endHeight)
×
555

556
        // The transaction was found within the node's mempool.
557
        case txStatus == chainntnfs.TxFoundMempool:
2✔
558

559
        // The transaction was found within the node's txindex.
560
        case txStatus == chainntnfs.TxFoundIndex:
2✔
561

562
        // The transaction was not found within the node's mempool or txindex.
563
        case txStatus == chainntnfs.TxNotFoundIndex:
2✔
564

565
        // Unexpected txStatus returned.
566
        default:
×
567
                return nil, txStatus,
×
568
                        fmt.Errorf("Got unexpected txConfStatus: %v", txStatus)
×
569
        }
570

571
        return txConf, txStatus, nil
2✔
572
}
573

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

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

594
                blockHash, err := b.chainConn.GetBlockHash(int64(height))
×
595
                if err != nil {
×
596
                        return nil, chainntnfs.TxNotFoundManually,
×
597
                                fmt.Errorf("unable to get hash from block "+
×
598
                                        "with height %d", height)
×
599
                }
×
600

601
                block, err := b.GetBlock(blockHash)
×
602
                if err != nil {
×
603
                        return nil, chainntnfs.TxNotFoundManually,
×
604
                                fmt.Errorf("unable to get block with hash "+
×
605
                                        "%v: %v", blockHash, err)
×
606
                }
×
607

608
                // For every transaction in the block, check which one matches
609
                // our request. If we find one that does, we can dispatch its
610
                // confirmation details.
611
                for txIndex, tx := range block.Transactions {
×
612
                        if !confRequest.MatchesTx(tx) {
×
613
                                continue
×
614
                        }
615

616
                        return &chainntnfs.TxConfirmation{
×
617
                                Tx:          tx.Copy(),
×
618
                                BlockHash:   blockHash,
×
619
                                BlockHeight: height,
×
620
                                TxIndex:     uint32(txIndex),
×
621
                                Block:       block,
×
622
                        }, chainntnfs.TxFoundManually, nil
×
623
                }
624
        }
625

626
        // If we reach here, then we were not able to find the transaction
627
        // within a block, so we avoid returning an error.
628
        return nil, chainntnfs.TxNotFoundManually, nil
×
629
}
630

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

2✔
644
        // We'll then extend the txNotifier's height with the information of
2✔
645
        // this new block, which will handle all of the notification logic for
2✔
646
        // us.
2✔
647
        err = b.txNotifier.ConnectTip(utilBlock, uint32(block.Height))
2✔
648
        if err != nil {
2✔
649
                return fmt.Errorf("unable to connect tip: %w", err)
×
650
        }
×
651

652
        chainntnfs.Log.Infof("New block: height=%v, sha=%v", block.Height,
2✔
653
                block.Hash)
2✔
654

2✔
655
        // Now that we've guaranteed the new block extends the txNotifier's
2✔
656
        // current tip, we'll proceed to dispatch notifications to all of our
2✔
657
        // registered clients whom have had notifications fulfilled. Before
2✔
658
        // doing so, we'll make sure update our in memory state in order to
2✔
659
        // satisfy any client requests based upon the new block.
2✔
660
        b.bestBlock = block
2✔
661

2✔
662
        b.notifyBlockEpochs(block.Height, block.Hash, block.BlockHeader)
2✔
663
        return b.txNotifier.NotifyHeight(uint32(block.Height))
2✔
664
}
665

666
// notifyBlockEpochs notifies all registered block epoch clients of the newly
667
// connected block to the main chain.
668
func (b *BitcoindNotifier) notifyBlockEpochs(newHeight int32, newSha *chainhash.Hash,
669
        blockHeader *wire.BlockHeader) {
2✔
670

2✔
671
        for _, client := range b.blockEpochClients {
4✔
672
                b.notifyBlockEpochClient(client, newHeight, newSha, blockHeader)
2✔
673
        }
2✔
674
}
675

676
// notifyBlockEpochClient sends a registered block epoch client a notification
677
// about a specific block.
678
func (b *BitcoindNotifier) notifyBlockEpochClient(epochClient *blockEpochRegistration,
679
        height int32, sha *chainhash.Hash, header *wire.BlockHeader) {
2✔
680

2✔
681
        epoch := &chainntnfs.BlockEpoch{
2✔
682
                Height:      height,
2✔
683
                Hash:        sha,
2✔
684
                BlockHeader: header,
2✔
685
        }
2✔
686

2✔
687
        select {
2✔
688
        case epochClient.epochQueue.ChanIn() <- epoch:
2✔
689
        case <-epochClient.cancelChan:
×
690
        case <-b.quit:
×
691
        }
692
}
693

694
// RegisterSpendNtfn registers an intent to be notified once the target
695
// outpoint/output script has been spent by a transaction on-chain. When
696
// intending to be notified of the spend of an output script, a nil outpoint
697
// must be used. The heightHint should represent the earliest height in the
698
// chain of the transaction that spent the outpoint/output script.
699
//
700
// Once a spend of has been detected, the details of the spending event will be
701
// sent across the 'Spend' channel.
702
func (b *BitcoindNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
703
        pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
2✔
704

2✔
705
        // Register the conf notification with the TxNotifier. A non-nil value
2✔
706
        // for `dispatch` will be returned if we are required to perform a
2✔
707
        // manual scan for the confirmation. Otherwise the notifier will begin
2✔
708
        // watching at tip for the transaction to confirm.
2✔
709
        ntfn, err := b.txNotifier.RegisterSpend(outpoint, pkScript, heightHint)
2✔
710
        if err != nil {
4✔
711
                return nil, err
2✔
712
        }
2✔
713

714
        // We'll then request the backend to notify us when it has detected the
715
        // outpoint/output script as spent.
716
        //
717
        // TODO(wilmer): use LoadFilter API instead.
718
        if outpoint == nil || *outpoint == chainntnfs.ZeroOutPoint {
2✔
719
                _, addrs, _, err := txscript.ExtractPkScriptAddrs(
×
720
                        pkScript, b.chainParams,
×
721
                )
×
722
                if err != nil {
×
723
                        return nil, fmt.Errorf("unable to parse script: %w",
×
724
                                err)
×
725
                }
×
726
                if err := b.chainConn.NotifyReceived(addrs); err != nil {
×
727
                        return nil, err
×
728
                }
×
729
        } else {
2✔
730
                ops := []*wire.OutPoint{outpoint}
2✔
731
                if err := b.chainConn.NotifySpent(ops); err != nil {
2✔
732
                        return nil, err
×
733
                }
×
734
        }
735

736
        // If the txNotifier didn't return any details to perform a historical
737
        // scan of the chain, then we can return early as there's nothing left
738
        // for us to do.
739
        if ntfn.HistoricalDispatch == nil {
4✔
740
                return ntfn.Event, nil
2✔
741
        }
2✔
742

743
        // Otherwise, we'll need to dispatch a historical rescan to determine if
744
        // the outpoint was already spent at a previous height.
745
        //
746
        // We'll short-circuit the path when dispatching the spend of a script,
747
        // rather than an outpoint, as there aren't any additional checks we can
748
        // make for scripts.
749
        if ntfn.HistoricalDispatch.OutPoint == chainntnfs.ZeroOutPoint {
2✔
750
                select {
×
751
                case b.notificationRegistry <- ntfn.HistoricalDispatch:
×
752
                case <-b.quit:
×
753
                        return nil, chainntnfs.ErrChainNotifierShuttingDown
×
754
                }
755

756
                return ntfn.Event, nil
×
757
        }
758

759
        // When dispatching spends of outpoints, there are a number of checks we
760
        // can make to start our rescan from a better height or completely avoid
761
        // it.
762
        //
763
        // We'll start by checking the backend's UTXO set to determine whether
764
        // the outpoint has been spent. If it hasn't, we can return to the
765
        // caller as well.
766
        txOut, err := b.chainConn.GetTxOut(&outpoint.Hash, outpoint.Index, true)
2✔
767
        if err != nil {
2✔
768
                return nil, err
×
769
        }
×
770
        if txOut != nil {
4✔
771
                // We'll let the txNotifier know the outpoint is still unspent
2✔
772
                // in order to begin updating its spend hint.
2✔
773
                err := b.txNotifier.UpdateSpendDetails(
2✔
774
                        ntfn.HistoricalDispatch.SpendRequest, nil,
2✔
775
                )
2✔
776
                if err != nil {
2✔
777
                        return nil, err
×
778
                }
×
779

780
                return ntfn.Event, nil
2✔
781
        }
782

783
        // Since the outpoint was spent, as it no longer exists within the UTXO
784
        // set, we'll determine when it happened by scanning the chain.
785
        //
786
        // As a minimal optimization, we'll query the backend's transaction
787
        // index (if enabled) to determine if we have a better rescan starting
788
        // height. We can do this as the GetRawTransaction call will return the
789
        // hash of the block it was included in within the chain.
790
        tx, err := b.chainConn.GetRawTransactionVerbose(&outpoint.Hash)
2✔
791
        if err != nil {
4✔
792
                // Avoid returning an error if the transaction was not found to
2✔
793
                // proceed with fallback methods.
2✔
794
                jsonErr, ok := err.(*btcjson.RPCError)
2✔
795
                if !ok || jsonErr.Code != btcjson.ErrRPCNoTxInfo {
2✔
796
                        return nil, fmt.Errorf("unable to query for txid "+
×
797
                                "%v: %w", outpoint.Hash, err)
×
798
                }
×
799
        }
800

801
        // If the transaction index was enabled, we'll use the block's hash to
802
        // retrieve its height and check whether it provides a better starting
803
        // point for our rescan.
804
        if tx != nil {
4✔
805
                // If the transaction containing the outpoint hasn't confirmed
2✔
806
                // on-chain, then there's no need to perform a rescan.
2✔
807
                if tx.BlockHash == "" {
4✔
808
                        return ntfn.Event, nil
2✔
809
                }
2✔
810

811
                blockHash, err := chainhash.NewHashFromStr(tx.BlockHash)
2✔
812
                if err != nil {
2✔
813
                        return nil, err
×
814
                }
×
815
                blockHeight, err := b.chainConn.GetBlockHeight(blockHash)
2✔
816
                if err != nil {
2✔
817
                        return nil, err
×
818
                }
×
819

820
                if uint32(blockHeight) > ntfn.HistoricalDispatch.StartHeight {
4✔
821
                        ntfn.HistoricalDispatch.StartHeight = uint32(blockHeight)
2✔
822
                }
2✔
823
        }
824

825
        // Now that we've determined the starting point of our rescan, we can
826
        // dispatch it and return.
827
        select {
2✔
828
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
2✔
829
        case <-b.quit:
×
830
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
831
        }
832

833
        return ntfn.Event, nil
2✔
834
}
835

836
// historicalSpendDetails attempts to manually scan the chain within the given
837
// height range for a transaction that spends the given outpoint/output script.
838
// If one is found, the spend details are assembled and returned to the caller.
839
// If the spend is not found, a nil spend detail will be returned.
840
func (b *BitcoindNotifier) historicalSpendDetails(
841
        spendRequest chainntnfs.SpendRequest, startHeight, endHeight uint32) (
842
        *chainntnfs.SpendDetail, error) {
2✔
843

2✔
844
        // Begin scanning blocks at every height to determine if the outpoint
2✔
845
        // was spent.
2✔
846
        for height := endHeight; height >= startHeight && height > 0; height-- {
4✔
847
                // Ensure we haven't been requested to shut down before
2✔
848
                // processing the next height.
2✔
849
                select {
2✔
850
                case <-b.quit:
×
851
                        return nil, chainntnfs.ErrChainNotifierShuttingDown
×
852
                default:
2✔
853
                }
854

855
                // First, we'll fetch the block for the current height.
856
                blockHash, err := b.chainConn.GetBlockHash(int64(height))
2✔
857
                if err != nil {
2✔
858
                        return nil, fmt.Errorf("unable to retrieve hash for "+
×
859
                                "block with height %d: %v", height, err)
×
860
                }
×
861
                block, err := b.GetBlock(blockHash)
2✔
862
                if err != nil {
2✔
863
                        return nil, fmt.Errorf("unable to retrieve block "+
×
864
                                "with hash %v: %v", blockHash, err)
×
865
                }
×
866

867
                // Then, we'll manually go over every input in every transaction
868
                // in it and determine whether it spends the request in
869
                // question. If we find one, we'll dispatch the spend details.
870
                for _, tx := range block.Transactions {
4✔
871
                        matches, inputIdx, err := spendRequest.MatchesTx(tx)
2✔
872
                        if err != nil {
2✔
873
                                return nil, err
×
874
                        }
×
875
                        if !matches {
4✔
876
                                continue
2✔
877
                        }
878

879
                        txCopy := tx.Copy()
2✔
880
                        txHash := txCopy.TxHash()
2✔
881
                        spendOutPoint := &txCopy.TxIn[inputIdx].PreviousOutPoint
2✔
882
                        return &chainntnfs.SpendDetail{
2✔
883
                                SpentOutPoint:     spendOutPoint,
2✔
884
                                SpenderTxHash:     &txHash,
2✔
885
                                SpendingTx:        txCopy,
2✔
886
                                SpenderInputIndex: inputIdx,
2✔
887
                                SpendingHeight:    int32(height),
2✔
888
                        }, nil
2✔
889
                }
890
        }
891

892
        return nil, nil
2✔
893
}
894

895
// RegisterConfirmationsNtfn registers an intent to be notified once the target
896
// txid/output script has reached numConfs confirmations on-chain. When
897
// intending to be notified of the confirmation of an output script, a nil txid
898
// must be used. The heightHint should represent the earliest height at which
899
// the txid/output script could have been included in the chain.
900
//
901
// Progress on the number of confirmations left can be read from the 'Updates'
902
// channel. Once it has reached all of its confirmations, a notification will be
903
// sent across the 'Confirmed' channel.
904
func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
905
        pkScript []byte, numConfs, heightHint uint32,
906
        opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent, error) {
2✔
907

2✔
908
        // Register the conf notification with the TxNotifier. A non-nil value
2✔
909
        // for `dispatch` will be returned if we are required to perform a
2✔
910
        // manual scan for the confirmation. Otherwise the notifier will begin
2✔
911
        // watching at tip for the transaction to confirm.
2✔
912
        ntfn, err := b.txNotifier.RegisterConf(
2✔
913
                txid, pkScript, numConfs, heightHint, opts...,
2✔
914
        )
2✔
915
        if err != nil {
2✔
916
                return nil, err
×
917
        }
×
918

919
        if ntfn.HistoricalDispatch == nil {
4✔
920
                return ntfn.Event, nil
2✔
921
        }
2✔
922

923
        select {
2✔
924
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
2✔
925
                return ntfn.Event, nil
2✔
926
        case <-b.quit:
×
927
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
928
        }
929
}
930

931
// blockEpochRegistration represents a client's intent to receive a
932
// notification with each newly connected block.
933
type blockEpochRegistration struct {
934
        epochID uint64
935

936
        epochChan chan *chainntnfs.BlockEpoch
937

938
        epochQueue *queue.ConcurrentQueue
939

940
        bestBlock *chainntnfs.BlockEpoch
941

942
        errorChan chan error
943

944
        cancelChan chan struct{}
945

946
        wg sync.WaitGroup
947
}
948

949
// epochCancel is a message sent to the BitcoindNotifier when a client wishes
950
// to cancel an outstanding epoch notification that has yet to be dispatched.
951
type epochCancel struct {
952
        epochID uint64
953
}
954

955
// RegisterBlockEpochNtfn returns a BlockEpochEvent which subscribes the
956
// caller to receive notifications, of each new block connected to the main
957
// chain. Clients have the option of passing in their best known block, which
958
// the notifier uses to check if they are behind on blocks and catch them up. If
959
// they do not provide one, then a notification will be dispatched immediately
960
// for the current tip of the chain upon a successful registration.
961
func (b *BitcoindNotifier) RegisterBlockEpochNtfn(
962
        bestBlock *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error) {
2✔
963

2✔
964
        reg := &blockEpochRegistration{
2✔
965
                epochQueue: queue.NewConcurrentQueue(20),
2✔
966
                epochChan:  make(chan *chainntnfs.BlockEpoch, 20),
2✔
967
                cancelChan: make(chan struct{}),
2✔
968
                epochID:    atomic.AddUint64(&b.epochClientCounter, 1),
2✔
969
                bestBlock:  bestBlock,
2✔
970
                errorChan:  make(chan error, 1),
2✔
971
        }
2✔
972
        reg.epochQueue.Start()
2✔
973

2✔
974
        // Before we send the request to the main goroutine, we'll launch a new
2✔
975
        // goroutine to proxy items added to our queue to the client itself.
2✔
976
        // This ensures that all notifications are received *in order*.
2✔
977
        reg.wg.Add(1)
2✔
978
        go func() {
4✔
979
                defer reg.wg.Done()
2✔
980

2✔
981
                for {
4✔
982
                        select {
2✔
983
                        case ntfn := <-reg.epochQueue.ChanOut():
2✔
984
                                blockNtfn := ntfn.(*chainntnfs.BlockEpoch)
2✔
985
                                select {
2✔
986
                                case reg.epochChan <- blockNtfn:
2✔
987

988
                                case <-reg.cancelChan:
2✔
989
                                        return
2✔
990

991
                                case <-b.quit:
×
992
                                        return
×
993
                                }
994

995
                        case <-reg.cancelChan:
2✔
996
                                return
2✔
997

998
                        case <-b.quit:
2✔
999
                                return
2✔
1000
                        }
1001
                }
1002
        }()
1003

1004
        select {
2✔
1005
        case <-b.quit:
×
1006
                // As we're exiting before the registration could be sent,
×
1007
                // we'll stop the queue now ourselves.
×
1008
                reg.epochQueue.Stop()
×
1009

×
1010
                return nil, errors.New("chainntnfs: system interrupt while " +
×
1011
                        "attempting to register for block epoch notification.")
×
1012
        case b.notificationRegistry <- reg:
2✔
1013
                return &chainntnfs.BlockEpochEvent{
2✔
1014
                        Epochs: reg.epochChan,
2✔
1015
                        Cancel: func() {
4✔
1016
                                cancel := &epochCancel{
2✔
1017
                                        epochID: reg.epochID,
2✔
1018
                                }
2✔
1019

2✔
1020
                                // Submit epoch cancellation to notification dispatcher.
2✔
1021
                                select {
2✔
1022
                                case b.notificationCancels <- cancel:
2✔
1023
                                        // Cancellation is being handled, drain the epoch channel until it is
2✔
1024
                                        // closed before yielding to caller.
2✔
1025
                                        for {
4✔
1026
                                                select {
2✔
1027
                                                case _, ok := <-reg.epochChan:
2✔
1028
                                                        if !ok {
4✔
1029
                                                                return
2✔
1030
                                                        }
2✔
1031
                                                case <-b.quit:
1✔
1032
                                                        return
1✔
1033
                                                }
1034
                                        }
1035
                                case <-b.quit:
2✔
1036
                                }
1037
                        },
1038
                }, nil
1039
        }
1040
}
1041

1042
// GetBlock is used to retrieve the block with the given hash. This function
1043
// wraps the blockCache's GetBlock function.
1044
func (b *BitcoindNotifier) GetBlock(hash *chainhash.Hash) (*wire.MsgBlock,
1045
        error) {
2✔
1046

2✔
1047
        return b.blockCache.GetBlock(hash, b.chainConn.GetBlock)
2✔
1048
}
2✔
1049

1050
// SubscribeMempoolSpent allows the caller to register a subscription to watch
1051
// for a spend of an outpoint in the mempool.The event will be dispatched once
1052
// the outpoint is spent in the mempool.
1053
//
1054
// NOTE: part of the MempoolWatcher interface.
1055
func (b *BitcoindNotifier) SubscribeMempoolSpent(
1056
        outpoint wire.OutPoint) (*chainntnfs.MempoolSpendEvent, error) {
2✔
1057

2✔
1058
        event := b.memNotifier.SubscribeInput(outpoint)
2✔
1059

2✔
1060
        ops := []*wire.OutPoint{&outpoint}
2✔
1061

2✔
1062
        return event, b.chainConn.NotifySpent(ops)
2✔
1063
}
2✔
1064

1065
// CancelMempoolSpendEvent allows the caller to cancel a subscription to watch
1066
// for a spend of an outpoint in the mempool.
1067
//
1068
// NOTE: part of the MempoolWatcher interface.
1069
func (b *BitcoindNotifier) CancelMempoolSpendEvent(
1070
        sub *chainntnfs.MempoolSpendEvent) {
2✔
1071

2✔
1072
        b.memNotifier.UnsubscribeEvent(sub)
2✔
1073
}
2✔
1074

1075
// LookupInputMempoolSpend takes an outpoint and queries the mempool to find
1076
// its spending tx. Returns the tx if found, otherwise fn.None.
1077
//
1078
// NOTE: part of the MempoolWatcher interface.
1079
func (b *BitcoindNotifier) LookupInputMempoolSpend(
1080
        op wire.OutPoint) fn.Option[wire.MsgTx] {
2✔
1081

2✔
1082
        // Find the spending txid.
2✔
1083
        txid, found := b.chainConn.LookupInputMempoolSpend(op)
2✔
1084
        if !found {
4✔
1085
                return fn.None[wire.MsgTx]()
2✔
1086
        }
2✔
1087

1088
        // Query the spending tx using the id.
1089
        tx, err := b.chainConn.GetRawTransaction(&txid)
2✔
1090
        if err != nil {
2✔
1091
                // TODO(yy): enable logging errors in this package.
×
1092
                return fn.None[wire.MsgTx]()
×
1093
        }
×
1094

1095
        return fn.Some(*tx.MsgTx().Copy())
2✔
1096
}
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