-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe_events.go
247 lines (191 loc) · 6.5 KB
/
subscribe_events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package routerrpc
import (
"fmt"
"time"
"github.com/brsuite/lnd/htlcswitch"
"github.com/brsuite/lnd/invoices"
"github.com/brsuite/lnd/lnrpc"
)
// rpcHtlcEvent returns a rpc htlc event from a htlcswitch event.
func rpcHtlcEvent(htlcEvent interface{}) (*HtlcEvent, error) {
var (
key htlcswitch.HtlcKey
timestamp time.Time
eventType htlcswitch.HtlcEventType
event isHtlcEvent_Event
)
switch e := htlcEvent.(type) {
case *htlcswitch.ForwardingEvent:
event = &HtlcEvent_ForwardEvent{
ForwardEvent: &ForwardEvent{
Info: rpcInfo(e.HtlcInfo),
},
}
key = e.HtlcKey
eventType = e.HtlcEventType
timestamp = e.Timestamp
case *htlcswitch.ForwardingFailEvent:
event = &HtlcEvent_ForwardFailEvent{
ForwardFailEvent: &ForwardFailEvent{},
}
key = e.HtlcKey
eventType = e.HtlcEventType
timestamp = e.Timestamp
case *htlcswitch.LinkFailEvent:
failureCode, failReason, err := rpcFailReason(
e.LinkError,
)
if err != nil {
return nil, err
}
event = &HtlcEvent_LinkFailEvent{
LinkFailEvent: &LinkFailEvent{
Info: rpcInfo(e.HtlcInfo),
WireFailure: failureCode,
FailureDetail: failReason,
FailureString: e.LinkError.Error(),
},
}
key = e.HtlcKey
eventType = e.HtlcEventType
timestamp = e.Timestamp
case *htlcswitch.SettleEvent:
event = &HtlcEvent_SettleEvent{
SettleEvent: &SettleEvent{
Preimage: e.Preimage[:],
},
}
key = e.HtlcKey
eventType = e.HtlcEventType
timestamp = e.Timestamp
default:
return nil, fmt.Errorf("unknown event type: %T", e)
}
rpcEvent := &HtlcEvent{
IncomingChannelId: key.IncomingCircuit.ChanID.ToUint64(),
OutgoingChannelId: key.OutgoingCircuit.ChanID.ToUint64(),
IncomingHtlcId: key.IncomingCircuit.HtlcID,
OutgoingHtlcId: key.OutgoingCircuit.HtlcID,
TimestampNs: uint64(timestamp.UnixNano()),
Event: event,
}
// Convert the htlc event type to a rpc event.
switch eventType {
case htlcswitch.HtlcEventTypeSend:
rpcEvent.EventType = HtlcEvent_SEND
case htlcswitch.HtlcEventTypeReceive:
rpcEvent.EventType = HtlcEvent_RECEIVE
case htlcswitch.HtlcEventTypeForward:
rpcEvent.EventType = HtlcEvent_FORWARD
default:
return nil, fmt.Errorf("unknown event type: %v", eventType)
}
return rpcEvent, nil
}
// rpcInfo returns a rpc struct containing the htlc information from the
// switch's htlc info struct.
func rpcInfo(info htlcswitch.HtlcInfo) *HtlcInfo {
return &HtlcInfo{
IncomingTimelock: info.IncomingTimeLock,
OutgoingTimelock: info.OutgoingTimeLock,
IncomingAmtMsat: uint64(info.IncomingAmt),
OutgoingAmtMsat: uint64(info.OutgoingAmt),
}
}
// rpcFailReason maps a lnwire failure message and failure detail to a rpc
// failure code and detail.
func rpcFailReason(linkErr *htlcswitch.LinkError) (lnrpc.Failure_FailureCode,
FailureDetail, error) {
wireErr, err := marshallError(linkErr)
if err != nil {
return 0, 0, err
}
wireCode := wireErr.GetCode()
// If the link has no failure detail, return with failure detail none.
if linkErr.FailureDetail == nil {
return wireCode, FailureDetail_NO_DETAIL, nil
}
switch failureDetail := linkErr.FailureDetail.(type) {
case invoices.FailResolutionResult:
fd, err := rpcFailureResolution(failureDetail)
return wireCode, fd, err
case htlcswitch.OutgoingFailure:
fd, err := rpcOutgoingFailure(failureDetail)
return wireCode, fd, err
default:
return 0, 0, fmt.Errorf("unknown failure "+
"detail type: %T", linkErr.FailureDetail)
}
}
// rpcFailureResolution maps an invoice failure resolution to a rpc failure
// detail. Invoice failures have no zero resolution results (every failure
// is accompanied with a result), so we error if we fail to match the result
// type.
func rpcFailureResolution(invoiceFailure invoices.FailResolutionResult) (
FailureDetail, error) {
switch invoiceFailure {
case invoices.ResultReplayToCanceled:
return FailureDetail_INVOICE_CANCELED, nil
case invoices.ResultInvoiceAlreadyCanceled:
return FailureDetail_INVOICE_CANCELED, nil
case invoices.ResultAmountTooLow:
return FailureDetail_INVOICE_UNDERPAID, nil
case invoices.ResultExpiryTooSoon:
return FailureDetail_INVOICE_EXPIRY_TOO_SOON, nil
case invoices.ResultCanceled:
return FailureDetail_INVOICE_CANCELED, nil
case invoices.ResultInvoiceNotOpen:
return FailureDetail_INVOICE_NOT_OPEN, nil
case invoices.ResultMppTimeout:
return FailureDetail_MPP_INVOICE_TIMEOUT, nil
case invoices.ResultAddressMismatch:
return FailureDetail_ADDRESS_MISMATCH, nil
case invoices.ResultHtlcSetTotalMismatch:
return FailureDetail_SET_TOTAL_MISMATCH, nil
case invoices.ResultHtlcSetTotalTooLow:
return FailureDetail_SET_TOTAL_TOO_LOW, nil
case invoices.ResultHtlcSetOverpayment:
return FailureDetail_SET_OVERPAID, nil
case invoices.ResultInvoiceNotFound:
return FailureDetail_UNKNOWN_INVOICE, nil
case invoices.ResultKeySendError:
return FailureDetail_INVALID_KEYSEND, nil
case invoices.ResultMppInProgress:
return FailureDetail_MPP_IN_PROGRESS, nil
default:
return 0, fmt.Errorf("unknown fail resolution: %v",
invoiceFailure.FailureString())
}
}
// rpcOutgoingFailure maps an outgoing failure to a rpc FailureDetail. If the
// failure detail is FailureDetailNone, which indicates that the failure was
// a wire message which required no further failure detail, we return a no
// detail failure detail to indicate that there was no additional information.
func rpcOutgoingFailure(failureDetail htlcswitch.OutgoingFailure) (
FailureDetail, error) {
switch failureDetail {
case htlcswitch.OutgoingFailureNone:
return FailureDetail_NO_DETAIL, nil
case htlcswitch.OutgoingFailureDecodeError:
return FailureDetail_ONION_DECODE, nil
case htlcswitch.OutgoingFailureLinkNotEligible:
return FailureDetail_LINK_NOT_ELIGIBLE, nil
case htlcswitch.OutgoingFailureOnChainTimeout:
return FailureDetail_ON_CHAIN_TIMEOUT, nil
case htlcswitch.OutgoingFailureHTLCExceedsMax:
return FailureDetail_HTLC_EXCEEDS_MAX, nil
case htlcswitch.OutgoingFailureInsufficientBalance:
return FailureDetail_INSUFFICIENT_BALANCE, nil
case htlcswitch.OutgoingFailureCircularRoute:
return FailureDetail_CIRCULAR_ROUTE, nil
case htlcswitch.OutgoingFailureIncompleteForward:
return FailureDetail_INCOMPLETE_FORWARD, nil
case htlcswitch.OutgoingFailureDownstreamHtlcAdd:
return FailureDetail_HTLC_ADD_FAILED, nil
case htlcswitch.OutgoingFailureForwardsDisabled:
return FailureDetail_FORWARDS_DISABLED, nil
default:
return 0, fmt.Errorf("unknown outgoing failure "+
"detail: %v", failureDetail.FailureString())
}
}