-
Notifications
You must be signed in to change notification settings - Fork 9
/
routes.go
331 lines (310 loc) · 9.34 KB
/
routes.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
package main
import (
"context"
"encoding/hex"
"fmt"
"log"
"math/rand"
"time"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
)
const (
COIN = 1e8
)
func (r *regolancer) getChanInfo(ctx context.Context, chanId uint64) (*lnrpc.ChannelEdge, error) {
if c, ok := r.chanCache[chanId]; ok {
return c, nil
}
c, err := r.lnClient.GetChanInfo(ctx, &lnrpc.ChanInfoRequest{ChanId: chanId})
if err != nil {
return nil, err
}
r.chanCache[chanId] = c
return c, nil
}
func (r *regolancer) calcFeeLimitMsat(ctx context.Context, to uint64,
amtMsat int64, ppm int64) (feeMsat int64, lastPKstr string, err error) {
cTo, err := r.getChanInfo(ctx, to)
if err != nil {
return 0, "", err
}
lastPKstr = cTo.Node1Pub
if lastPKstr == r.myPK {
lastPKstr = cTo.Node2Pub
}
feeMsat = amtMsat * ppm / 1e6
return
}
func (r *regolancer) calcEconFeeMsat(ctx context.Context, from, to uint64, amtMsat int64, ratio float64) (feeMsat int64,
lastPKstr string, err error) {
cTo, err := r.getChanInfo(ctx, to)
if err != nil {
return 0, "", err
}
lastPKstr = cTo.Node1Pub
policyTo := cTo.Node2Policy
if lastPKstr == r.myPK {
lastPKstr = cTo.Node2Pub
policyTo = cTo.Node1Policy
}
lostProfitMsat := int64(0)
if params.LostProfit {
cFrom, err := r.getChanInfo(ctx, from)
if err != nil {
return 0, "", err
}
policyFrom := cFrom.Node1Policy
if cFrom.Node2Pub == r.myPK {
policyFrom = cFrom.Node2Policy
}
lostProfitMsat = int64(float64(policyFrom.FeeBaseMsat+
amtMsat*policyFrom.FeeRateMilliMsat) / 1e6)
}
feeMsat = int64(float64(policyTo.FeeBaseMsat+amtMsat*
policyTo.FeeRateMilliMsat)*ratio/1e6) - lostProfitMsat
if params.EconRatioMaxPPM != 0 && int64(float64(feeMsat)/float64(amtMsat)*1e6) > params.EconRatioMaxPPM {
feeMsat = params.EconRatioMaxPPM * amtMsat / 1e6
}
if feeMsat < 0 {
return 0, "", fmt.Errorf("max fee less than zero")
}
return
}
func (r *regolancer) calcFeeMsat(ctx context.Context, from, to uint64,
amtMsat int64) (feeMsat int64, lastPKstr string, err error) {
if params.FeeLimitPPM > 0 {
return r.calcFeeLimitMsat(ctx, to, amtMsat, params.FeeLimitPPM)
} else {
return r.calcEconFeeMsat(ctx, from, to, amtMsat, params.EconRatio)
}
}
func (r *regolancer) getRoutes(ctx context.Context, from, to uint64, amtMsat int64) ([]*lnrpc.Route, int64, error) {
routeCtx, cancel := context.WithTimeout(ctx, time.Second*time.Duration(params.TimeoutRoute))
defer cancel()
feeMsat, lastPKstr, err := r.calcFeeMsat(routeCtx, from, to, amtMsat)
if err != nil {
return nil, 0, err
}
lastPK, err := hex.DecodeString(lastPKstr)
if err != nil {
return nil, 0, err
}
routes, err := r.lnClient.QueryRoutes(routeCtx, &lnrpc.QueryRoutesRequest{
PubKey: r.myPK,
OutgoingChanId: from,
LastHopPubkey: lastPK,
AmtMsat: amtMsat,
UseMissionControl: true,
FeeLimit: &lnrpc.FeeLimit{Limit: &lnrpc.FeeLimit_FixedMsat{FixedMsat: feeMsat}},
IgnoredNodes: r.excludeNodes,
IgnoredPairs: r.failedPairs,
})
if err != nil {
return nil, 0, err
}
result := []*lnrpc.Route{}
for i := range routes.Routes { // lnd always returns 1 route for now but just in case it changes
if err := r.validateRoute(routes.Routes[i]); err == nil {
result = append(result, routes.Routes[i])
} else {
log.Print(err)
}
}
if len(result) == 0 {
return r.getRoutes(ctx, from, to, amtMsat)
}
r.routeFound = true
return result, feeMsat, nil
}
func (r *regolancer) getNodeInfo(ctx context.Context, pk string) (*lnrpc.NodeInfo, error) {
if nodeInfo, ok := r.nodeCache[pk]; ok {
return nodeInfo.NodeInfo, nil
}
nodeInfo, err := r.lnClient.GetNodeInfo(ctx, &lnrpc.NodeInfoRequest{PubKey: pk})
if err == nil {
r.nodeCache[pk] = cachedNodeInfo{
NodeInfo: nodeInfo,
Timestamp: time.Now(),
}
}
return nodeInfo, err
}
func (r *regolancer) printRoute(ctx context.Context, route *lnrpc.Route) {
if len(route.Hops) == 0 {
return
}
errs := ""
fmt.Printf("%s %s sat | %s ppm\n", faintWhiteColor("Total fee:"),
formatFee(route.TotalFeesMsat), formatFeePPM(route.TotalAmtMsat-route.TotalFeesMsat, route.TotalFeesMsat))
for i, hop := range route.Hops {
cached := ""
if params.NodeCacheInfo {
cached = errColor("x")
if _, ok := r.nodeCache[hop.PubKey]; ok {
cached = cyanColor("x")
}
cached += "|"
}
nodeInfo, err := r.getNodeInfo(ctx, hop.PubKey)
if err != nil {
errs = errs + err.Error() + "\n"
continue
}
fee := hiWhiteColorF("%-6s", "")
if i > 0 {
fee = hiWhiteColorF("%-6d", route.Hops[i-1].FeeMsat)
}
fmt.Printf("%s %s [%s%s|%sch|%ssat|%s]\n", faintWhiteColor(hop.ChanId), fee, cached, cyanColor(nodeInfo.Node.Alias),
infoColor(nodeInfo.NumChannels), formatAmt(nodeInfo.TotalCapacity), infoColor(nodeInfo.Node.PubKey))
}
if errs != "" {
fmt.Println(errColor(errs))
}
}
func (r *regolancer) rebuildRoute(ctx context.Context, route *lnrpc.Route, amount int64) (*lnrpc.Route, error) {
pks := [][]byte{}
for _, h := range route.Hops {
pk, _ := hex.DecodeString(h.PubKey)
pks = append(pks, pk)
}
resultRoute, err := r.routerClient.BuildRoute(ctx, &routerrpc.BuildRouteRequest{
AmtMsat: amount * 1000,
OutgoingChanId: route.Hops[0].ChanId,
HopPubkeys: pks,
FinalCltvDelta: 144,
})
if err != nil {
return nil, err
}
return resultRoute.Route, err
}
func (r *regolancer) probeRoute(ctx context.Context, route *lnrpc.Route,
goodAmount, badAmount, amount int64, steps int) (maxAmount int64, err error) {
defer func() {
if ctx.Err() == context.DeadlineExceeded && goodAmount > 0 {
maxAmount = goodAmount
log.Printf("Probing timed out with value %s", hiWhiteColor(maxAmount))
}
}()
if absoluteDeltaPPM(badAmount, amount) <= params.FailTolerance || absoluteDeltaPPM(amount, goodAmount) <= params.FailTolerance || amount == -goodAmount {
bestAmount := hiWhiteColor(goodAmount)
maxAmount = goodAmount
if goodAmount <= 0 {
bestAmount = hiWhiteColor("unknown")
maxAmount = 0
}
log.Printf("Best amount is %s", bestAmount)
return
}
probedRoute, err := r.rebuildRoute(ctx, route, amount)
if err != nil {
return
}
maxFeeMsat, _, err := r.calcFeeMsat(ctx, probedRoute.Hops[0].ChanId,
probedRoute.Hops[len(probedRoute.Hops)-1].ChanId, amount*1000)
if err != nil {
return
}
if probedRoute.TotalFeesMsat > maxFeeMsat {
nextAmount := amount + (badAmount-amount)/2
log.Printf("%s requires too high fee %s (max allowed is %s), increasing amount to %s",
hiWhiteColor(amount), formatFee(probedRoute.TotalFeesMsat),
formatFee(maxFeeMsat), hiWhiteColor(nextAmount))
// returning negative amount as "good", it's a special case which means
// this is rather the lower bound and the actual good amount is still
// unknown
return r.probeRoute(ctx, route, -amount, badAmount, nextAmount, steps)
}
fakeHash := make([]byte, 32)
rand.Read(fakeHash)
result, err := r.routerClient.SendToRouteV2(ctx,
&routerrpc.SendToRouteRequest{
PaymentHash: fakeHash,
Route: probedRoute,
})
if err != nil {
return
}
if result.Status == lnrpc.HTLCAttempt_SUCCEEDED {
return 0, fmt.Errorf("this should never happen")
}
if result.Status == lnrpc.HTLCAttempt_FAILED {
if result.Failure.Code == lnrpc.Failure_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS { // payment can succeed
if steps == 1 {
log.Printf("best amount is %s", hiWhiteColor(amount))
maxAmount = amount
return
}
nextAmount := amount + (badAmount-amount)/2
log.Printf("%s is good enough, trying amount %s, %s steps left",
hiWhiteColor(amount), hiWhiteColor(nextAmount),
hiWhiteColor(steps-1))
return r.probeRoute(ctx, route, amount, badAmount, nextAmount,
steps-1)
}
if result.Failure.Code == lnrpc.Failure_TEMPORARY_CHANNEL_FAILURE {
if steps == 1 {
maxAmount = goodAmount
bestAmount := hiWhiteColor(goodAmount)
if goodAmount <= 0 {
bestAmount = hiWhiteColor("unknown")
maxAmount = 0
}
log.Printf("%s is too much, best amount is %s",
hiWhiteColor(amount), bestAmount)
return
}
var nextAmount int64
if goodAmount >= 0 {
nextAmount = amount + (goodAmount-amount)/2
} else {
nextAmount = amount - (goodAmount+amount)/2
}
log.Printf("%s is too much, lowering amount to %s, %s steps left",
hiWhiteColor(amount), hiWhiteColor(nextAmount),
hiWhiteColor(steps-1))
return r.probeRoute(ctx, route, goodAmount, amount, nextAmount,
steps-1)
}
if result.Failure.Code == lnrpc.Failure_FEE_INSUFFICIENT {
log.Printf("Fee insufficient, retrying...")
return r.probeRoute(ctx, route, goodAmount, badAmount, amount,
steps)
}
}
return 0, fmt.Errorf("unknown error: %+v", result)
}
func (r *regolancer) makeNodeList(nodes []string) error {
for _, nid := range nodes {
if len(nid) != 66 {
return fmt.Errorf("invalid node id (%s) length, expected 66 characters, got %d", nid, len(nid))
}
pk, err := hex.DecodeString(nid)
if err != nil {
return err
}
r.excludeNodes = append(r.excludeNodes, pk)
}
return nil
}
func getSource(route *lnrpc.Route) uint64 {
if len(route.Hops) > 0 {
return route.Hops[0].ChanId
}
return 0
}
func getTarget(route *lnrpc.Route) uint64 {
if len(route.Hops) > 0 {
return route.Hops[len(route.Hops)-1].ChanId
}
return 0
}
func compareHops(hop1 *lnrpc.Hop, hop2 *lnrpc.Hop) bool {
if hop1 == nil || hop2 == nil {
return false
}
return hop1.ChanId == hop2.ChanId &&
hop1.FeeMsat == hop2.FeeMsat &&
hop1.Expiry == hop2.Expiry
}