-
Notifications
You must be signed in to change notification settings - Fork 2
/
raiju.go
464 lines (393 loc) · 13.4 KB
/
raiju.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package raiju
import (
"context"
"errors"
"fmt"
"math/rand"
"sort"
"time"
"github.com/nyonson/raiju/lightning"
)
const (
// larger step percentages are more efficient because they avoid base fees, but might not get routed
maxStepPercent = 5.0
minStepPercent = 0.5
changeStepPercent = 0.5
)
//go:generate gotests -w -exported raiju.go
//go:generate moq -stub -skip-ensure -out raiju_mock_test.go . lightninger
type lightninger interface {
AddInvoice(ctx context.Context, amount lightning.Satoshi) (lightning.Invoice, error)
DescribeGraph(ctx context.Context) (*lightning.Graph, error)
ForwardingHistory(ctx context.Context, since time.Time) ([]lightning.Forward, error)
GetInfo(ctx context.Context) (*lightning.Info, error)
GetChannel(ctx context.Context, channelID lightning.ChannelID) (lightning.Channel, error)
ListChannels(ctx context.Context) (lightning.Channels, error)
SendPayment(ctx context.Context, invoice lightning.Invoice, outChannelID lightning.ChannelID, lastHopPubKey lightning.PubKey, maxFee lightning.FeePPM) (lightning.Satoshi, error)
SetFees(ctx context.Context, channelID lightning.ChannelID, fee lightning.FeePPM, maxHTLC lightning.MilliSatoshi) error
SubscribeChannelUpdates(ctx context.Context) (<-chan lightning.Channels, <-chan error, error)
}
// Raiju app.
type Raiju struct {
l lightninger
f LiquidityFees
}
// New instance of raiju.
func New(l lightninger, r LiquidityFees) Raiju {
return Raiju{
l: l,
f: r,
}
}
// RelativeNode has information on a node's graph characteristics relative to other nodes.
type RelativeNode struct {
lightning.Node
Distance int64
DistantNeigbors int64
Channels int64
Capacity lightning.Satoshi
Neighbors []lightning.PubKey
}
// sortDistance sorts nodes by distance, distant neighbors, capacity, and channels
type sortDistance []RelativeNode
// Less is true if i is closer than j.
func (s sortDistance) Less(i, j int) bool {
if s[i].Distance != s[j].Distance {
return s[i].Distance < s[j].Distance
}
if s[i].DistantNeigbors != s[j].DistantNeigbors {
return s[i].DistantNeigbors < s[j].DistantNeigbors
}
if s[i].Capacity != s[j].Capacity {
return s[i].Capacity < s[j].Capacity
}
return s[i].Channels < s[j].Channels
}
// Swap nodes in slice.
func (s sortDistance) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Len of the relative node slice.
func (s sortDistance) Len() int {
return len(s)
}
// CandidatesRequest contains necessary info to perform sorting across the network
type CandidatesRequest struct {
// PubKey is the key of the root node to perform crawl from
PubKey lightning.PubKey
// MinCapcity filters nodes with a minimum satoshi capacity (sum of channels)
MinCapacity lightning.Satoshi
// MinChannels filters nodes with a minimum number of channels
MinChannels int64
// MinDistance filters nodes with a minimum distance (number of hops) from the root node
MinDistance int64
// MinDistantNeighbors filters nodes with a minimum number of distant neighbors
MinDistantNeighbors int64
// MinUpdated filters nodes which have not been updated since time
MinUpdated time.Time
// Assume channels to these pubkeys
Assume []lightning.PubKey
// Number of results
Limit int64
// Filter tor nodes
Clearnet bool
}
// Candidates walks the lightning network from a specific node keeping track of distance (hops).
func (r Raiju) Candidates(ctx context.Context, request CandidatesRequest) ([]RelativeNode, error) {
// default root node to local if no key supplied
if request.PubKey == "" {
info, err := r.l.GetInfo(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get root node info: %w", err)
}
request.PubKey = info.PubKey
}
// pull entire network graph from lnd
channelGraph, err := r.l.DescribeGraph(ctx)
if err != nil {
return nil, err
}
// initialize nodes map with static info
nodes := make(map[lightning.PubKey]*RelativeNode, len(channelGraph.Nodes))
for _, n := range channelGraph.Nodes {
nodes[n.PubKey] = &RelativeNode{
Node: n,
}
}
// calculate node properties based on channels: neighbors, capacity, channels
for _, e := range channelGraph.Edges {
if nodes[e.Node1].Neighbors != nil {
nodes[e.Node1].Neighbors = append(nodes[e.Node1].Neighbors, e.Node2)
} else {
nodes[e.Node1].Neighbors = []lightning.PubKey{e.Node2}
}
if nodes[e.Node2].Neighbors != nil {
nodes[e.Node2].Neighbors = append(nodes[e.Node2].Neighbors, e.Node1)
} else {
nodes[e.Node2].Neighbors = []lightning.PubKey{e.Node1}
}
nodes[e.Node1].Capacity += e.Capacity
nodes[e.Node2].Capacity += e.Capacity
nodes[e.Node1].Channels++
nodes[e.Node2].Channels++
}
// Add assumes to root node
for _, c := range request.Assume {
if _, ok := nodes[c]; !ok {
return []RelativeNode{}, errors.New("candidate node does not exist")
}
if nodes[request.PubKey].Neighbors != nil {
nodes[request.PubKey].Neighbors = append(nodes[request.PubKey].Neighbors, c)
} else {
nodes[request.PubKey].Neighbors = []lightning.PubKey{c}
}
if nodes[c].Neighbors != nil {
nodes[c].Neighbors = append(nodes[c].Neighbors, request.PubKey)
} else {
nodes[c].Neighbors = []lightning.PubKey{request.PubKey}
}
}
// BFS node graph to calculate distance from root node
var distance int64 = 1
visited := make(map[lightning.PubKey]bool)
// handle strange case where root node doesn't exist for some reason...
neighbors := make([]lightning.PubKey, 0)
// initialize search from root node's neighbors
if n, ok := nodes[request.PubKey]; ok {
// root node has no distance to self
n.Distance = 0
// mark root as visited
visited[n.PubKey] = true
neighbors = n.Neighbors
}
for len(neighbors) > 0 {
next := make([]lightning.PubKey, 0)
for _, n := range neighbors {
if !visited[n] {
nodes[n].Distance = distance
visited[n] = true
for _, neighbor := range nodes[n].Neighbors {
if !visited[neighbor] {
next = append(next, neighbor)
}
}
}
}
distance++
neighbors = next
}
unfilteredSpan := make([]RelativeNode, 0)
for _, v := range nodes {
unfilteredSpan = append(unfilteredSpan, *v)
}
// hardcode what distance is considered "distant" for a neighbor
const distantNeighborLimit int64 = 2
// calculate number of distant neighbors per node
for node := range unfilteredSpan {
var count int64
for _, neighbor := range unfilteredSpan[node].Neighbors {
if nodes[neighbor].Distance > distantNeighborLimit {
count++
}
}
unfilteredSpan[node].DistantNeigbors = count
}
// filter nodes by request conditions
allCandidates := make([]RelativeNode, 0)
for _, v := range unfilteredSpan {
if v.Capacity >= request.MinCapacity &&
v.Channels >= request.MinChannels &&
v.Distance >= request.MinDistance &&
v.DistantNeigbors >= request.MinDistantNeighbors &&
v.Updated.After(request.MinUpdated) {
if request.Clearnet {
if v.Clearnet() {
allCandidates = append(allCandidates, v)
}
} else {
allCandidates = append(allCandidates, v)
}
}
}
sort.Sort(sort.Reverse(sortDistance(allCandidates)))
candidates := allCandidates
if int64(len(allCandidates)) >= request.Limit {
candidates = allCandidates[:request.Limit]
}
return candidates, nil
}
// Fees to encourage a balanced channel.
//
// Fees are initially set across all channels and then continuously updated as channel liquidity changes.
func (r Raiju) Fees(ctx context.Context) (chan map[lightning.ChannelID]lightning.FeePPM, chan error, error) {
channels, err := r.l.ListChannels(ctx)
if err != nil {
return nil, nil, err
}
// buffer the channel for the first update
updates := make(chan map[lightning.ChannelID]lightning.FeePPM, 1)
errors := make(chan error)
// make sure updated at least once
u, err := r.setFees(ctx, channels)
if err != nil {
return nil, nil, err
}
updates <- u
// listen for channel updates to keep fees in sync
cc, ce, err := r.l.SubscribeChannelUpdates(ctx)
if err != nil {
return nil, nil, err
}
go func() {
for {
select {
case channels = <-cc:
u, err = r.setFees(ctx, channels)
if err != nil {
errors <- fmt.Errorf("error setting fees: %w", err)
} else {
updates <- u
}
case err := <-ce:
errors <- fmt.Errorf("error listening to channel updates: %w", err)
}
}
}()
return updates, errors, nil
}
// setFees on channels who's liquidity has changed, return updated channels and their new liquidity level.
func (r Raiju) setFees(ctx context.Context, channels lightning.Channels) (map[lightning.ChannelID]lightning.FeePPM, error) {
updates := map[lightning.ChannelID]lightning.FeePPM{}
// update channel fees based on liquidity, but only change if necessary
for _, c := range channels {
fee := r.f.Fee(c)
// flow control, broadcast to the network the max payment size to forward through this channel.
maxPayment := c.LocalBalance.Millis() / 2
if c.LocalFee != fee && !c.Private {
err := r.l.SetFees(ctx, c.ChannelID, fee, maxPayment)
if err != nil {
return map[lightning.ChannelID]lightning.FeePPM{}, err
}
updates[c.ChannelID] = fee
}
}
return updates, nil
}
// Rebalance liquidity out of outChannelID and in through lastHopPubkey and returns the percent of capacity rebalanced.
//
// The amount of sats rebalanced is based on the capacity of the out channel. Each rebalance attempt will try to move
// stepPercent worth of sats. A maximum of maxPercent of sats will be moved. The maxFee in ppm controls the amount
// willing to pay for rebalance.
func (r Raiju) rebalanceChannel(ctx context.Context, outChannelID lightning.ChannelID, lastHopPubKey lightning.PubKey, stepPercent float64, maxPercent float64, maxFee lightning.FeePPM) (float64, lightning.Satoshi, error) {
// calculate invoice value
c, err := r.l.GetChannel(ctx, outChannelID)
if err != nil {
return 0, 0, err
}
amount := int64(float64(c.Capacity) * stepPercent / 100)
var currentStepPercent = stepPercent
var percentRebalanced float64
var totalFeePaid lightning.Satoshi
for percentRebalanced < maxPercent && currentStepPercent >= minStepPercent {
// create and pay invoice
invoice, err := r.l.AddInvoice(ctx, lightning.Satoshi(amount))
if err != nil {
return 0, 0, fmt.Errorf("error creating circular rebalance invoice: %w", err)
}
feePaid, err := r.l.SendPayment(ctx, invoice, outChannelID, lastHopPubKey, maxFee)
// assume payment failures might work if we lower the step percentage
// less efficient rebalances, but could still work
if err != nil {
currentStepPercent = currentStepPercent - changeStepPercent
continue
}
percentRebalanced += currentStepPercent
totalFeePaid += feePaid
}
return percentRebalanced, totalFeePaid, nil
}
// Rebalance high local liquidity channels into low liquidity channels, return percent rebalanced per channel attempted.
func (r Raiju) Rebalance(ctx context.Context, maxPercent float64, maxFee lightning.FeePPM) (map[lightning.ChannelID]float64, error) {
local, err := r.l.GetInfo(ctx)
if err != nil {
return map[lightning.ChannelID]float64{}, err
}
channels, err := r.l.ListChannels(ctx)
if err != nil {
return map[lightning.ChannelID]float64{}, err
}
hlcs, llcs := r.f.RebalanceChannels(channels)
// Shuffle arrays so different combos are tried
rand.Shuffle(len(hlcs), func(i, j int) {
hlcs[i], hlcs[j] = hlcs[j], hlcs[i]
})
var totalFeePaid lightning.Satoshi
rebalanced := map[lightning.ChannelID]float64{}
// Roll through high liquidity channels and try to push things through the low liquidity ones.
for _, h := range hlcs {
percentRebalanced := float64(0)
// reshuffle low liquidity channels each time
rand.Shuffle(len(llcs), func(i, j int) {
llcs[i], llcs[j] = llcs[j], llcs[i]
})
for _, l := range llcs {
// the largest step amount needs to be less than the remaining percent to rebalance
pl := (maxPercent - percentRebalanced)
ms := maxStepPercent
if pl < ms {
ms = pl
}
// get the non-local node of the channel
lastHopPubkey := l.Node1
if lastHopPubkey == local.PubKey {
lastHopPubkey = l.Node2
}
// Check that the channel would still be low liquidity to avoid the risk of paying a high fee
// to rebalance and then a standard payment cancels out the liquidity
ul, err := r.l.GetChannel(ctx, l.ChannelID)
if err != nil {
return map[lightning.ChannelID]float64{}, err
}
potentialLocal := lightning.Satoshi(float64(h.Capacity) * maxPercent)
// only shift liquidity if the fees won't change
if r.f.PotentialFee(ul, potentialLocal) != r.f.Fee(ul) {
p, f, err := r.rebalanceChannel(ctx, h.ChannelID, lastHopPubkey, ms, pl, r.f.RebalanceFee())
if err != nil {
return map[lightning.ChannelID]float64{}, err
}
percentRebalanced += p
totalFeePaid += f
}
}
rebalanced[h.ChannelID] = percentRebalanced
}
return rebalanced, nil
}
// Reaper calculates inefficient channels which should be closed.
func (r Raiju) Reaper(ctx context.Context) (lightning.Channels, error) {
// pull the last month of forwards
forwards, err := r.l.ForwardingHistory(ctx, time.Now().AddDate(0, -1, 0))
if err != nil {
return lightning.Channels{}, err
}
channels, err := r.l.ListChannels(ctx)
if err != nil {
return lightning.Channels{}, err
}
// initialize tracker
m := make(map[lightning.ChannelID]bool)
for _, c := range channels {
m[c.ChannelID] = false
}
for _, f := range forwards {
m[f.ChannelIn] = true
m[f.ChannelOut] = true
}
inefficient := make(lightning.Channels, 0)
for _, c := range channels {
if !m[c.ChannelID] {
inefficient = append(inefficient, c)
}
}
return inefficient, nil
}