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

lightningnetwork / lnd / 14358372723

09 Apr 2025 01:26PM UTC coverage: 56.696% (-12.3%) from 69.037%
14358372723

Pull #9696

github

web-flow
Merge e2837e400 into 867d27d68
Pull Request #9696: Add `development_guidelines.md` for both human and machine

107055 of 188823 relevant lines covered (56.7%)

22721.56 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.
13
func rpcHtlcEvent(htlcEvent interface{}) (*HtlcEvent, error) {
×
14
        var (
×
15
                key       htlcswitch.HtlcKey
×
16
                timestamp time.Time
×
17
                eventType *htlcswitch.HtlcEventType
×
18
                event     isHtlcEvent_Event
×
19
        )
×
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

118
        return rpcEvent, nil
×
119
}
120

121
// rpcInfo returns a rpc struct containing the htlc information from the
122
// switch's htlc info struct.
123
func rpcInfo(info htlcswitch.HtlcInfo) *HtlcInfo {
×
124
        return &HtlcInfo{
×
125
                IncomingTimelock: info.IncomingTimeLock,
×
126
                OutgoingTimelock: info.OutgoingTimeLock,
×
127
                IncomingAmtMsat:  uint64(info.IncomingAmt),
×
128
                OutgoingAmtMsat:  uint64(info.OutgoingAmt),
×
129
        }
×
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,
135
        FailureDetail, error) {
×
136

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

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

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

153
        case htlcswitch.OutgoingFailure:
×
154
                fd, err := rpcOutgoingFailure(failureDetail)
×
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) (
168
        FailureDetail, error) {
×
169

×
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

177
        case invoices.ResultAmountTooLow:
×
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

204
        case invoices.ResultInvoiceNotFound:
×
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) (
224
        FailureDetail, error) {
×
225

×
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

242
        case htlcswitch.OutgoingFailureInsufficientBalance:
×
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