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

lightningnetwork / lnd / 14053082453

25 Mar 2025 06:25AM UTC coverage: 68.983% (-0.04%) from 69.02%
14053082453

Pull #9626

github

web-flow
Merge 0356f0e0a into 3351a1745
Pull Request #9626: payment lifecycle small fix

15 of 15 new or added lines in 3 files covered. (100.0%)

105 existing lines in 22 files now uncovered.

132982 of 192774 relevant lines covered (68.98%)

22234.66 hits per line

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

88.24
/lntest/mock/spendnotifier.go
1
package mock
2

3
import (
4
        "sync"
5

6
        "github.com/btcsuite/btcd/wire"
7
        "github.com/lightningnetwork/lnd/chainntnfs"
8
)
9

10
// SpendNotifier extends the mock.ChainNotifier so that spend
11
// notifications can be triggered and delivered to subscribers.
12
type SpendNotifier struct {
13
        *ChainNotifier
14
        spendMap map[wire.OutPoint][]chan *chainntnfs.SpendDetail
15
        spends   map[wire.OutPoint]*chainntnfs.SpendDetail
16
        mtx      sync.Mutex
17
}
18

19
// MakeMockSpendNotifier creates a SpendNotifier.
20
func MakeMockSpendNotifier() *SpendNotifier {
8✔
21
        return &SpendNotifier{
8✔
22
                ChainNotifier: &ChainNotifier{
8✔
23
                        SpendChan: make(chan *chainntnfs.SpendDetail),
8✔
24
                        EpochChan: make(chan *chainntnfs.BlockEpoch),
8✔
25
                        ConfChan:  make(chan *chainntnfs.TxConfirmation),
8✔
26
                },
8✔
27
                spendMap: make(map[wire.OutPoint][]chan *chainntnfs.SpendDetail),
8✔
28
                spends:   make(map[wire.OutPoint]*chainntnfs.SpendDetail),
8✔
29
        }
8✔
30
}
8✔
31

32
// RegisterSpendNtfn registers a spend notification for a specified outpoint.
33
func (s *SpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
34
        _ []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
14✔
35

14✔
36
        s.mtx.Lock()
14✔
37
        defer s.mtx.Unlock()
14✔
38

14✔
39
        spendChan := make(chan *chainntnfs.SpendDetail, 1)
14✔
40
        if detail, ok := s.spends[*outpoint]; ok {
14✔
UNCOV
41
                // Deliver spend immediately if details are already known.
×
UNCOV
42
                spendChan <- &chainntnfs.SpendDetail{
×
UNCOV
43
                        SpentOutPoint:     detail.SpentOutPoint,
×
UNCOV
44
                        SpendingHeight:    detail.SpendingHeight,
×
UNCOV
45
                        SpendingTx:        detail.SpendingTx,
×
UNCOV
46
                        SpenderTxHash:     detail.SpenderTxHash,
×
UNCOV
47
                        SpenderInputIndex: detail.SpenderInputIndex,
×
UNCOV
48
                }
×
49
        } else {
14✔
50
                // Otherwise, queue the notification for delivery if the spend
14✔
51
                // is ever received.
14✔
52
                s.spendMap[*outpoint] = append(s.spendMap[*outpoint], spendChan)
14✔
53
        }
14✔
54

55
        return &chainntnfs.SpendEvent{
14✔
56
                Spend:  spendChan,
14✔
57
                Cancel: func() {},
14✔
58
        }, nil
59
}
60

61
// Spend dispatches SpendDetails to all subscribers of the outpoint. The details
62
// will includethe transaction and height provided by the caller.
63
func (s *SpendNotifier) Spend(outpoint *wire.OutPoint, height int32,
64
        txn *wire.MsgTx) {
14✔
65

14✔
66
        s.mtx.Lock()
14✔
67
        defer s.mtx.Unlock()
14✔
68

14✔
69
        var inputIndex uint32
14✔
70
        for i, in := range txn.TxIn {
36✔
71
                if in.PreviousOutPoint == *outpoint {
36✔
72
                        inputIndex = uint32(i)
14✔
73
                }
14✔
74
        }
75

76
        txnHash := txn.TxHash()
14✔
77
        details := &chainntnfs.SpendDetail{
14✔
78
                SpentOutPoint:     outpoint,
14✔
79
                SpendingHeight:    height,
14✔
80
                SpendingTx:        txn,
14✔
81
                SpenderTxHash:     &txnHash,
14✔
82
                SpenderInputIndex: inputIndex,
14✔
83
        }
14✔
84

14✔
85
        // Cache details in case of late registration.
14✔
86
        if _, ok := s.spends[*outpoint]; !ok {
28✔
87
                s.spends[*outpoint] = details
14✔
88
        }
14✔
89

90
        // Deliver any backlogged spend notifications.
91
        if spendChans, ok := s.spendMap[*outpoint]; ok {
28✔
92
                delete(s.spendMap, *outpoint)
14✔
93
                for _, spendChan := range spendChans {
28✔
94
                        spendChan <- &chainntnfs.SpendDetail{
14✔
95
                                SpentOutPoint:     details.SpentOutPoint,
14✔
96
                                SpendingHeight:    details.SpendingHeight,
14✔
97
                                SpendingTx:        details.SpendingTx,
14✔
98
                                SpenderTxHash:     details.SpenderTxHash,
14✔
99
                                SpenderInputIndex: details.SpenderInputIndex,
14✔
100
                        }
14✔
101
                }
14✔
102
        }
103
}
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