-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmux.go
397 lines (318 loc) · 9.58 KB
/
mux.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package lnmux
import (
"bytes"
"context"
"fmt"
"sync"
"github.com/bottlepay/lnmux/lnd"
"github.com/bottlepay/lnmux/persistence"
"github.com/bottlepay/lnmux/types"
"github.com/btcsuite/btcd/chaincfg"
sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"go.uber.org/zap"
)
type Mux struct {
registry *InvoiceRegistry
sphinx *hop.OnionProcessor
lnd lnd.LndClient
logger *zap.SugaredLogger
settledHandler *NodeSettledHandler
routingPolicy RoutingPolicy
virtualChannel uint64
}
type MuxConfig struct {
KeyRing keychain.SecretKeyRing
ActiveNetParams *chaincfg.Params
FinalCallback func(lntypes.Hash, bool)
Persister *persistence.PostgresPersister
Lnd lnd.LndClient
Logger *zap.SugaredLogger
Registry *InvoiceRegistry
// RoutingPolicy is the policy that is enforced for the hop towards the
// virtual channel.
RoutingPolicy RoutingPolicy
}
type RoutingPolicy struct {
CltvDelta int64
FeeBaseMsat int64
FeeRatePpm int64
}
func New(cfg *MuxConfig) (*Mux,
error) {
idKeyDesc, err := cfg.KeyRing.DeriveKey(
keychain.KeyLocator{
Family: keychain.KeyFamilyNodeKey,
Index: 0,
},
)
if err != nil {
return nil, err
}
nodeKeyECDH := keychain.NewPubKeyECDH(idKeyDesc, cfg.KeyRing)
replayLog := &replayLog{}
sphinxRouter := sphinx.NewRouter(
nodeKeyECDH, cfg.ActiveNetParams, replayLog,
)
sphinx := hop.NewOnionProcessor(sphinxRouter)
connectedNode := cfg.Lnd.PubKey()
logger := cfg.Logger.With("node", connectedNode)
virtualChannel := virtualChannelFromNode(connectedNode)
settledHandlerCfg := &NodeSettledHandlerConfig{
Persister: cfg.Persister,
Logger: cfg.Logger,
Lnd: cfg.Lnd,
FinalCallback: cfg.FinalCallback,
}
settledHandler := NewNodeSettledHandler(settledHandlerCfg)
return &Mux{
registry: cfg.Registry,
sphinx: sphinx,
lnd: cfg.Lnd,
logger: logger,
settledHandler: settledHandler,
routingPolicy: cfg.RoutingPolicy,
virtualChannel: virtualChannel,
}, nil
}
type interceptedHtlc struct {
circuitKey types.CircuitKey
hash lntypes.Hash
onionBlob []byte
incomingAmountMsat uint64
outgoingAmountMsat uint64
incomingExpiry uint32
outgoingExpiry uint32
outgoingChanID uint64
reply func(*interceptedHtlcResponse) error
}
type interceptedHtlcResponse struct {
action routerrpc.ResolveHoldForwardAction
preimage lntypes.Preimage
failureMessage []byte
failureCode lnrpc.Failure_FailureCode
}
func (p *Mux) Run(mainCtx context.Context) error {
p.logger.Infow("Routing policy",
"cltvDelta", p.routingPolicy.CltvDelta,
"feeBaseMsat", p.routingPolicy.FeeBaseMsat,
"feeRatePpm", p.routingPolicy.FeeRatePpm)
var wg sync.WaitGroup
defer wg.Wait()
ctx, cancel := context.WithCancel(mainCtx)
defer cancel()
// Register for htlc interception and block events.
htlcChan := make(chan *interceptedHtlc)
heightChan := make(chan int)
interceptor := newInterceptor(
p.lnd, p.logger, htlcChan, heightChan,
)
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
interceptor.run(ctx)
}(ctx)
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
p.settledHandler.Run(ctx)
}(ctx)
// All connected lnd nodes will immediately send the current block height.
// Pick up the first height received to initialize our local height.
var height int
select {
case height = <-heightChan:
case <-ctx.Done():
return nil
}
p.logger.Debugw("Starting main event loop")
for {
select {
case receivedHeight := <-heightChan:
// Keep track of the highest height only. Perhaps this can be made
// more sophisticated in the future.
if receivedHeight > height {
height = receivedHeight
}
case htlc := <-htlcChan:
// Only intercept htlcs for the virtual channel.
if htlc.outgoingChanID != p.virtualChannel {
err := htlc.reply(&interceptedHtlcResponse{
action: routerrpc.ResolveHoldForwardAction_RESUME,
})
if err != nil {
p.logger.Errorw("htlc reply error", "err", err)
}
break
}
err := p.ProcessHtlc(htlc, height)
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}
func marshallFailureCode(code lnwire.FailCode) (
lnrpc.Failure_FailureCode, error) {
switch code {
case lnwire.CodeInvalidOnionHmac:
return lnrpc.Failure_INVALID_ONION_HMAC, nil
case lnwire.CodeInvalidOnionVersion:
return lnrpc.Failure_INVALID_ONION_VERSION, nil
case lnwire.CodeInvalidOnionKey:
return lnrpc.Failure_INVALID_ONION_KEY, nil
// Unfortunately these codes are not supported by lnd. Return 0, which is
// mapped to TemporaryChannelFailure.
//
// See https://github.com/lightningnetwork/lnd/pull/7067
case lnwire.CodeFeeInsufficient, lnwire.CodeIncorrectCltvExpiry:
return 0, nil
default:
return 0, fmt.Errorf("unsupported code %v", code)
}
}
func (p *Mux) ProcessHtlc(
htlc *interceptedHtlc, height int) error {
logger := p.logger.With(
"hash", htlc.hash,
"circuitKey", htlc.circuitKey,
)
logger.Infow("Htlc received")
fail := func(code lnwire.FailCode) error {
logger.Debugw("Failing htlc", "code", code)
rpcCode, err := marshallFailureCode(code)
if err != nil {
return err
}
return htlc.reply(&interceptedHtlcResponse{
action: routerrpc.ResolveHoldForwardAction_FAIL,
failureCode: rpcCode,
})
}
// Verify that the amount of the incoming htlc is at least what is forwarded
// over the virtual channel plus fee.
//
// TODO: Fee accounting for successful payments.
expectedFee := uint64(p.routingPolicy.FeeBaseMsat) +
(uint64(p.routingPolicy.FeeRatePpm)*htlc.outgoingAmountMsat)/1e6
if htlc.incomingAmountMsat < htlc.outgoingAmountMsat+expectedFee {
logger.Debugw("Insufficient incoming htlc amount",
"expectedFee", expectedFee)
return fail(lnwire.CodeFeeInsufficient)
}
// Verify that the cltv delta is sufficiently large.
if htlc.incomingExpiry < htlc.outgoingExpiry+uint32(p.routingPolicy.CltvDelta) {
logger.Debugw("Cltv delta insufficient")
return fail(lnwire.CodeIncorrectCltvExpiry)
}
// Try decode final hop onion. Expiry can be set to zero, because the
// replay log is disabled.
onionReader := bytes.NewReader(htlc.onionBlob)
iterator, failCode := p.sphinx.DecodeHopIterator(
onionReader, htlc.hash[:], uint32(height),
)
if failCode != lnwire.CodeNone {
logger.Debugw("Cannot decode hop iterator")
return fail(failCode)
}
payload, err := iterator.HopPayload()
if err != nil {
return err
}
obfuscator, failCode := iterator.ExtractErrorEncrypter(
p.sphinx.ExtractErrorEncrypter,
)
if failCode != lnwire.CodeNone {
logger.Debugw("Cannot extract error encryptor")
return fail(failCode)
}
failLocal := func(failureMessage lnwire.FailureMessage) error {
reason, err := obfuscator.EncryptFirstHop(failureMessage)
if err != nil {
return err
}
// Here we need more control over htlc
// interception so that we can send back an
// encrypted failure message to the sender.
return htlc.reply(&interceptedHtlcResponse{
action: routerrpc.ResolveHoldForwardAction_FAIL,
failureMessage: reason,
})
}
// Verify that the amount going out over the virtual channel matches what
// the sender intended. See BOLT 04.
if uint64(payload.ForwardingInfo().AmountToForward) !=
htlc.outgoingAmountMsat {
logger.Debugw("Payload amount mismatch")
return failLocal(&lnwire.FailFinalIncorrectHtlcAmount{
IncomingHTLCAmount: lnwire.MilliSatoshi(htlc.outgoingAmountMsat),
})
}
// Verify that the expiry going out over the virtual channel matches what
// the sender intended. See BOLT 04.
if uint64(payload.ForwardingInfo().OutgoingCTLV) !=
uint64(htlc.outgoingExpiry) {
logger.Debugw("Final expiry mismatch")
return failLocal(&lnwire.FailFinalExpiryTooSoon{})
}
resolve := func(resolution HtlcResolution) error {
// Determine required action for the resolution based on the type of
// resolution we have received.
switch res := resolution.(type) {
case *HtlcSettleResolution:
logger.Debugw("Sending settle resolution",
"outcome", res.Outcome)
return htlc.reply(&interceptedHtlcResponse{
action: routerrpc.ResolveHoldForwardAction_SETTLE,
preimage: res.Preimage,
})
case *HtlcFailResolution:
logger.Debugw("Sending failed resolution",
"outcome", res.Outcome)
var failureMessage lnwire.FailureMessage
if res.Outcome == ResultMppTimeout {
failureMessage = &lnwire.FailMPPTimeout{}
} else {
failureMessage = lnwire.NewFailIncorrectDetails(
lnwire.MilliSatoshi(htlc.outgoingAmountMsat), 0,
)
}
return failLocal(failureMessage)
// Fail if we do not get a settle of fail resolution, since we
// are only expecting to handle settles and fails.
default:
return fmt.Errorf("unknown htlc resolution type: %T",
resolution)
}
}
// Notify the invoice registry of the intercepted htlc.
htlcKey := types.HtlcKey{
ChanID: htlc.circuitKey.ChanID,
HtlcID: htlc.circuitKey.HtlcID,
Node: p.lnd.PubKey(),
}
p.registry.NotifyExitHopHtlc(
®istryHtlc{
rHash: htlc.hash,
amtPaid: lnwire.MilliSatoshi(htlc.outgoingAmountMsat),
expiry: htlc.outgoingExpiry,
currentHeight: int32(height),
circuitKey: htlcKey,
payload: payload,
resolve: func(res HtlcResolution) {
err := resolve(res)
if err != nil {
logger.Errorf("resolve error", "err", err)
}
},
},
)
return nil
}