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

lightningnetwork / lnd / 16181619122

09 Jul 2025 10:33PM UTC coverage: 55.326% (-2.3%) from 57.611%
16181619122

Pull #10060

github

web-flow
Merge d15e8671f into 0e830da9d
Pull Request #10060: sweep: fix expected spending events being missed

9 of 26 new or added lines in 2 files covered. (34.62%)

23695 existing lines in 280 files now uncovered.

108518 of 196143 relevant lines covered (55.33%)

22354.81 hits per line

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

0.0
/lnrpc/routerrpc/subscribe_events.go
1
package routerrpc
2

3
import (
4
        "fmt"
5
        "time"
6

7
        "github.com/lightningnetwork/lnd/htlcswitch"
8
        "github.com/lightningnetwork/lnd/invoices"
9
        "github.com/lightningnetwork/lnd/lnrpc"
10
)
11

12
// rpcHtlcEvent returns a rpc htlc event from a htlcswitch event.
UNCOV
13
func rpcHtlcEvent(htlcEvent interface{}) (*HtlcEvent, error) {
×
UNCOV
14
        var (
×
UNCOV
15
                key       htlcswitch.HtlcKey
×
UNCOV
16
                timestamp time.Time
×
UNCOV
17
                eventType *htlcswitch.HtlcEventType
×
UNCOV
18
                event     isHtlcEvent_Event
×
UNCOV
19
        )
×
UNCOV
20

×
UNCOV
21
        switch e := htlcEvent.(type) {
×
UNCOV
22
        case *htlcswitch.ForwardingEvent:
×
UNCOV
23
                event = &HtlcEvent_ForwardEvent{
×
UNCOV
24
                        ForwardEvent: &ForwardEvent{
×
UNCOV
25
                                Info: rpcInfo(e.HtlcInfo),
×
UNCOV
26
                        },
×
UNCOV
27
                }
×
UNCOV
28

×
UNCOV
29
                key = e.HtlcKey
×
UNCOV
30
                eventType = &e.HtlcEventType
×
UNCOV
31
                timestamp = e.Timestamp
×
32

UNCOV
33
        case *htlcswitch.ForwardingFailEvent:
×
UNCOV
34
                event = &HtlcEvent_ForwardFailEvent{
×
UNCOV
35
                        ForwardFailEvent: &ForwardFailEvent{},
×
UNCOV
36
                }
×
UNCOV
37

×
UNCOV
38
                key = e.HtlcKey
×
UNCOV
39
                eventType = &e.HtlcEventType
×
UNCOV
40
                timestamp = e.Timestamp
×
41

UNCOV
42
        case *htlcswitch.LinkFailEvent:
×
UNCOV
43
                failureCode, failReason, err := rpcFailReason(
×
UNCOV
44
                        e.LinkError,
×
UNCOV
45
                )
×
UNCOV
46
                if err != nil {
×
47
                        return nil, err
×
48
                }
×
49

UNCOV
50
                event = &HtlcEvent_LinkFailEvent{
×
UNCOV
51
                        LinkFailEvent: &LinkFailEvent{
×
UNCOV
52
                                Info:          rpcInfo(e.HtlcInfo),
×
UNCOV
53
                                WireFailure:   failureCode,
×
UNCOV
54
                                FailureDetail: failReason,
×
UNCOV
55
                                FailureString: e.LinkError.Error(),
×
UNCOV
56
                        },
×
UNCOV
57
                }
×
UNCOV
58

×
UNCOV
59
                key = e.HtlcKey
×
UNCOV
60
                eventType = &e.HtlcEventType
×
UNCOV
61
                timestamp = e.Timestamp
×
62

UNCOV
63
        case *htlcswitch.SettleEvent:
×
UNCOV
64
                event = &HtlcEvent_SettleEvent{
×
UNCOV
65
                        SettleEvent: &SettleEvent{
×
UNCOV
66
                                Preimage: e.Preimage[:],
×
UNCOV
67
                        },
×
UNCOV
68
                }
×
UNCOV
69

×
UNCOV
70
                key = e.HtlcKey
×
UNCOV
71
                eventType = &e.HtlcEventType
×
UNCOV
72
                timestamp = e.Timestamp
×
73

UNCOV
74
        case *htlcswitch.FinalHtlcEvent:
×
UNCOV
75
                event = &HtlcEvent_FinalHtlcEvent{
×
UNCOV
76
                        FinalHtlcEvent: &FinalHtlcEvent{
×
UNCOV
77
                                Settled:  e.Settled,
×
UNCOV
78
                                Offchain: e.Offchain,
×
UNCOV
79
                        },
×
UNCOV
80
                }
×
UNCOV
81

×
UNCOV
82
                key = htlcswitch.HtlcKey{
×
UNCOV
83
                        IncomingCircuit: e.CircuitKey,
×
UNCOV
84
                }
×
UNCOV
85
                timestamp = e.Timestamp
×
86

87
        default:
×
88
                return nil, fmt.Errorf("unknown event type: %T", e)
×
89
        }
90

UNCOV
91
        rpcEvent := &HtlcEvent{
×
UNCOV
92
                IncomingChannelId: key.IncomingCircuit.ChanID.ToUint64(),
×
UNCOV
93
                OutgoingChannelId: key.OutgoingCircuit.ChanID.ToUint64(),
×
UNCOV
94
                IncomingHtlcId:    key.IncomingCircuit.HtlcID,
×
UNCOV
95
                OutgoingHtlcId:    key.OutgoingCircuit.HtlcID,
×
UNCOV
96
                TimestampNs:       uint64(timestamp.UnixNano()),
×
UNCOV
97
                Event:             event,
×
UNCOV
98
        }
×
UNCOV
99

×
UNCOV
100
        // Convert the htlc event type to a rpc event.
×
UNCOV
101
        if eventType != nil {
×
UNCOV
102
                switch *eventType {
×
UNCOV
103
                case htlcswitch.HtlcEventTypeSend:
×
UNCOV
104
                        rpcEvent.EventType = HtlcEvent_SEND
×
105

UNCOV
106
                case htlcswitch.HtlcEventTypeReceive:
×
UNCOV
107
                        rpcEvent.EventType = HtlcEvent_RECEIVE
×
108

UNCOV
109
                case htlcswitch.HtlcEventTypeForward:
×
UNCOV
110
                        rpcEvent.EventType = HtlcEvent_FORWARD
×
111

112
                default:
×
113
                        return nil, fmt.Errorf("unknown event type: %v",
×
114
                                eventType)
×
115
                }
116
        }
117

UNCOV
118
        return rpcEvent, nil
×
119
}
120

121
// rpcInfo returns a rpc struct containing the htlc information from the
122
// switch's htlc info struct.
UNCOV
123
func rpcInfo(info htlcswitch.HtlcInfo) *HtlcInfo {
×
UNCOV
124
        return &HtlcInfo{
×
UNCOV
125
                IncomingTimelock: info.IncomingTimeLock,
×
UNCOV
126
                OutgoingTimelock: info.OutgoingTimeLock,
×
UNCOV
127
                IncomingAmtMsat:  uint64(info.IncomingAmt),
×
UNCOV
128
                OutgoingAmtMsat:  uint64(info.OutgoingAmt),
×
UNCOV
129
        }
×
UNCOV
130
}
×
131

132
// rpcFailReason maps a lnwire failure message and failure detail to a rpc
133
// failure code and detail.
134
func rpcFailReason(linkErr *htlcswitch.LinkError) (lnrpc.Failure_FailureCode,
UNCOV
135
        FailureDetail, error) {
×
UNCOV
136

×
UNCOV
137
        wireErr, err := marshallError(linkErr)
×
UNCOV
138
        if err != nil {
×
139
                return 0, 0, err
×
140
        }
×
UNCOV
141
        wireCode := wireErr.GetCode()
×
UNCOV
142

×
UNCOV
143
        // If the link has no failure detail, return with failure detail none.
×
UNCOV
144
        if linkErr.FailureDetail == nil {
×
UNCOV
145
                return wireCode, FailureDetail_NO_DETAIL, nil
×
UNCOV
146
        }
×
147

UNCOV
148
        switch failureDetail := linkErr.FailureDetail.(type) {
×
UNCOV
149
        case invoices.FailResolutionResult:
×
UNCOV
150
                fd, err := rpcFailureResolution(failureDetail)
×
UNCOV
151
                return wireCode, fd, err
×
152

UNCOV
153
        case htlcswitch.OutgoingFailure:
×
UNCOV
154
                fd, err := rpcOutgoingFailure(failureDetail)
×
UNCOV
155
                return wireCode, fd, err
×
156

157
        default:
×
158
                return 0, 0, fmt.Errorf("unknown failure "+
×
159
                        "detail type: %T", linkErr.FailureDetail)
×
160
        }
161
}
162

163
// rpcFailureResolution maps an invoice failure resolution to a rpc failure
164
// detail. Invoice failures have no zero resolution results (every failure
165
// is accompanied with a result), so we error if we fail to match the result
166
// type.
167
func rpcFailureResolution(invoiceFailure invoices.FailResolutionResult) (
UNCOV
168
        FailureDetail, error) {
×
UNCOV
169

×
UNCOV
170
        switch invoiceFailure {
×
171
        case invoices.ResultReplayToCanceled:
×
172
                return FailureDetail_INVOICE_CANCELED, nil
×
173

174
        case invoices.ResultInvoiceAlreadyCanceled:
×
175
                return FailureDetail_INVOICE_CANCELED, nil
×
176

UNCOV
177
        case invoices.ResultAmountTooLow:
×
UNCOV
178
                return FailureDetail_INVOICE_UNDERPAID, nil
×
179

180
        case invoices.ResultExpiryTooSoon:
×
181
                return FailureDetail_INVOICE_EXPIRY_TOO_SOON, nil
×
182

183
        case invoices.ResultCanceled:
×
184
                return FailureDetail_INVOICE_CANCELED, nil
×
185

186
        case invoices.ResultInvoiceNotOpen:
×
187
                return FailureDetail_INVOICE_NOT_OPEN, nil
×
188

189
        case invoices.ResultMppTimeout:
×
190
                return FailureDetail_MPP_INVOICE_TIMEOUT, nil
×
191

192
        case invoices.ResultAddressMismatch:
×
193
                return FailureDetail_ADDRESS_MISMATCH, nil
×
194

195
        case invoices.ResultHtlcSetTotalMismatch:
×
196
                return FailureDetail_SET_TOTAL_MISMATCH, nil
×
197

198
        case invoices.ResultHtlcSetTotalTooLow:
×
199
                return FailureDetail_SET_TOTAL_TOO_LOW, nil
×
200

201
        case invoices.ResultHtlcSetOverpayment:
×
202
                return FailureDetail_SET_OVERPAID, nil
×
203

UNCOV
204
        case invoices.ResultInvoiceNotFound:
×
UNCOV
205
                return FailureDetail_UNKNOWN_INVOICE, nil
×
206

207
        case invoices.ResultKeySendError:
×
208
                return FailureDetail_INVALID_KEYSEND, nil
×
209

210
        case invoices.ResultMppInProgress:
×
211
                return FailureDetail_MPP_IN_PROGRESS, nil
×
212

213
        default:
×
214
                return 0, fmt.Errorf("unknown fail resolution: %v",
×
215
                        invoiceFailure.FailureString())
×
216
        }
217
}
218

219
// rpcOutgoingFailure maps an outgoing failure to a rpc FailureDetail. If the
220
// failure detail is FailureDetailNone, which indicates that the failure was
221
// a wire message which required no further failure detail, we return a no
222
// detail failure detail to indicate that there was no additional information.
223
func rpcOutgoingFailure(failureDetail htlcswitch.OutgoingFailure) (
UNCOV
224
        FailureDetail, error) {
×
UNCOV
225

×
UNCOV
226
        switch failureDetail {
×
227
        case htlcswitch.OutgoingFailureNone:
×
228
                return FailureDetail_NO_DETAIL, nil
×
229

230
        case htlcswitch.OutgoingFailureDecodeError:
×
231
                return FailureDetail_ONION_DECODE, nil
×
232

233
        case htlcswitch.OutgoingFailureLinkNotEligible:
×
234
                return FailureDetail_LINK_NOT_ELIGIBLE, nil
×
235

236
        case htlcswitch.OutgoingFailureOnChainTimeout:
×
237
                return FailureDetail_ON_CHAIN_TIMEOUT, nil
×
238

239
        case htlcswitch.OutgoingFailureHTLCExceedsMax:
×
240
                return FailureDetail_HTLC_EXCEEDS_MAX, nil
×
241

UNCOV
242
        case htlcswitch.OutgoingFailureInsufficientBalance:
×
UNCOV
243
                return FailureDetail_INSUFFICIENT_BALANCE, nil
×
244

245
        case htlcswitch.OutgoingFailureCircularRoute:
×
246
                return FailureDetail_CIRCULAR_ROUTE, nil
×
247

248
        case htlcswitch.OutgoingFailureIncompleteForward:
×
249
                return FailureDetail_INCOMPLETE_FORWARD, nil
×
250

251
        case htlcswitch.OutgoingFailureDownstreamHtlcAdd:
×
252
                return FailureDetail_HTLC_ADD_FAILED, nil
×
253

254
        case htlcswitch.OutgoingFailureForwardsDisabled:
×
255
                return FailureDetail_FORWARDS_DISABLED, nil
×
256

257
        default:
×
258
                return 0, fmt.Errorf("unknown outgoing failure "+
×
259
                        "detail: %v", failureDetail.FailureString())
×
260
        }
261
}
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