-
Notifications
You must be signed in to change notification settings - Fork 119
/
routing.go
596 lines (480 loc) · 15.1 KB
/
routing.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package qln
import (
"bytes"
"container/heap"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"strconv"
"time"
"github.com/awalterschulze/gographviz"
"github.com/mit-dci/lit/bech32"
"github.com/mit-dci/lit/coinparam"
"github.com/mit-dci/lit/consts"
"github.com/mit-dci/lit/crypto/fastsha256"
"github.com/mit-dci/lit/lnutil"
"github.com/mit-dci/lit/logging"
)
func (nd *LitNode) InitRouting() {
nd.ChannelMapMtx.Lock()
defer nd.ChannelMapMtx.Unlock()
nd.ChannelMap = make(map[[20]byte][]LinkDesc)
nd.ExchangeRates = make(map[uint32][]lnutil.RateDesc)
err := nd.PopulateRates()
if err != nil {
if os.IsNotExist(err) {
logging.Infof("Rates file not found.")
}
logging.Warnf("failure loading exchange rates: %s", err.Error())
}
nd.AdvTimeout = time.NewTicker(15 * time.Second)
go func() {
seq := uint32(0)
for {
nd.cleanStaleChannels()
nd.advertiseLinks(seq)
seq++
<-nd.AdvTimeout.C
}
}()
}
func (nd *LitNode) VisualiseGraph() string {
graph := gographviz.NewGraph()
graph.SetName("Lit")
nd.ChannelMapMtx.Lock()
defer nd.ChannelMapMtx.Unlock()
for pkh, node := range nd.ChannelMap {
lnAdr := bech32.Encode("ln", pkh[:])
if !graph.IsNode(lnAdr) {
graph.AddNode("Lit", lnAdr, nil)
}
for _, channel := range node {
theirLnAdr := bech32.Encode("ln", channel.Link.BPKH[:])
if !graph.IsNode(theirLnAdr) {
graph.AddNode("Lit", theirLnAdr, nil)
}
attrs := make(map[string]string)
switch channel.Link.CoinType {
case 0:
attrs["color"] = "orange"
case 28:
attrs["color"] = "green"
}
attrs["label"] = strconv.FormatUint(uint64(channel.Link.CoinType), 10)
graph.AddEdge(lnAdr, theirLnAdr, true, attrs)
}
}
return "di" + graph.String()
}
// FindPath uses Bellman-Ford and Dijkstra to find the path with the best price that has enough capacity to route the payment
func (nd *LitNode) FindPath(targetPkh [20]byte, destCoinType uint32, originCoinType uint32, amount int64) ([]lnutil.RouteHop, error) {
var myIdPkh [20]byte
idHash := fastsha256.Sum256(nd.IdKey().PubKey().SerializeCompressed())
copy(myIdPkh[:], idHash[:20])
type routeHop struct {
Node [20]byte
CoinType uint32
Terminus bool
}
type channelEdge struct {
W float64
U routeHop
V routeHop
Rate lnutil.RateDesc
Capacity int64
}
type channelEdgeLight struct {
W float64
U int
V int
Rate lnutil.RateDesc
Capacity int64
}
// set up initial graph
var edges []channelEdge
var vertices []routeHop
var edgesLight []channelEdgeLight
verticesMap := make(map[routeHop]int)
nd.ChannelMapMtx.Lock()
// for each node visit the nodes connected via its channels and add a
// BPKH:channel_cointype vertex to the graph for each of its channels.
// Then for the current node (APKH), for each channel add an edge from
// APKH:channel_cointype to each BPKH:cointype pair in the graph.
for pkh, channels := range nd.ChannelMap {
logging.Debugf("processing channels from %s", bech32.Encode("ln", pkh[:]))
for _, channel := range channels {
logging.Debugf("...processing channel %s:%d", bech32.Encode("ln", channel.Link.BPKH[:]), channel.Link.CoinType)
var newEdges []channelEdge
origin := routeHop{
channel.Link.APKH,
channel.Link.CoinType,
false,
}
verticesMap[origin] = -1
coinTypes := map[uint32]bool{}
for _, theirChannel := range nd.ChannelMap[channel.Link.BPKH] {
logging.Debugf("......checking outbound connection %s:%d", bech32.Encode("ln", theirChannel.Link.BPKH[:]), theirChannel.Link.CoinType)
if _, ok := coinTypes[theirChannel.Link.CoinType]; !ok {
var rd *lnutil.RateDesc
for _, rate := range theirChannel.Link.Rates {
if rate.CoinType == channel.Link.CoinType {
rd = &rate
break
}
}
if rd == nil {
// this trade is not possible
logging.Debugf(".........ignoring channel because trade %d->%d is not possible", channel.Link.CoinType, theirChannel.Link.CoinType)
continue
}
vertex := routeHop{
channel.Link.BPKH,
theirChannel.Link.CoinType,
false,
}
verticesMap[vertex] = -1
var price float64
if rd.Reciprocal {
price = 1.0 / float64(rd.Rate)
} else {
price = float64(rd.Rate)
}
weight := -math.Log(price)
edge := channelEdge{
weight,
origin,
vertex,
*rd,
channel.Link.ACapacity,
}
logging.Debugf(".........adding edge: %s:%d->%s:%d", bech32.Encode("ln", edge.U.Node[:]), edge.U.CoinType, bech32.Encode("ln", edge.V.Node[:]), edge.V.CoinType)
newEdges = append(newEdges, edge)
coinTypes[theirChannel.Link.CoinType] = true
} else {
logging.Debugf(".........ignoring channel because its cointype has already been covered")
}
}
vertex := routeHop{
channel.Link.BPKH,
channel.Link.CoinType,
true,
}
verticesMap[vertex] = -1
edge := channelEdge{
0,
origin,
vertex,
lnutil.RateDesc{
channel.Link.CoinType,
1,
false,
},
channel.Link.ACapacity,
}
logging.Debugf("...adding sink: %s:%d->%s:%d", bech32.Encode("ln", edge.U.Node[:]), edge.U.CoinType, bech32.Encode("ln", edge.V.Node[:]), edge.V.CoinType)
newEdges = append(newEdges, edge)
edges = append(edges, newEdges...)
}
}
nd.ChannelMapMtx.Unlock()
var predecessor []int
var distance []float64
for k, _ := range verticesMap {
vertices = append(vertices, k)
distance = append(distance, math.MaxFloat64)
predecessor = append(predecessor, -1)
verticesMap[k] = len(vertices) - 1
logging.Debugf("vertex %x:%d: %d", k.Node, k.CoinType, len(vertices)-1)
}
for _, edge := range edges {
edgesLight = append(edgesLight, channelEdgeLight{
edge.W,
verticesMap[edge.U],
verticesMap[edge.V],
edge.Rate,
edge.Capacity,
})
U := vertices[edgesLight[len(edgesLight)-1].U]
V := vertices[edgesLight[len(edgesLight)-1].V]
logging.Debugf("adding edgeLight: %x:%d->%x:%d", U.Node, U.CoinType, V.Node, V.CoinType)
}
graph := gographviz.NewGraph()
graph.SetName("\"bf-step\"")
for _, edge := range edgesLight {
U := fmt.Sprintf("\"%s:%d\"", bech32.Encode("ln", vertices[edge.U].Node[:]), vertices[edge.U].CoinType)
V := fmt.Sprintf("\"%s:%d\"", bech32.Encode("ln", vertices[edge.V].Node[:]), vertices[edge.V].CoinType)
nodeAttrs := make(map[string]string)
if vertices[edge.V].Terminus {
nodeAttrs["peripheries"] = "2"
V = fmt.Sprintf("\"%s:%d terminus\"", bech32.Encode("ln", vertices[edge.V].Node[:]), vertices[edge.V].CoinType)
}
if !graph.IsNode(U) {
graph.AddNode("\"bf-step\"", U, nil)
}
if !graph.IsNode(V) {
graph.AddNode("\"bf-step\"", V, nodeAttrs)
}
attrs := make(map[string]string)
attrs["label"] = fmt.Sprintf("\"weight: %f64, trade: %d->%d\"", edge.W, vertices[edge.U].CoinType, vertices[edge.V].CoinType)
graph.AddEdge(U, V, true, attrs)
}
logging.Debugf("bf-step graph: \n %s", "di"+graph.String())
// find my ID in map
myId, ok := verticesMap[routeHop{myIdPkh, originCoinType, false}]
if !ok {
return nil, fmt.Errorf("origin node not found")
}
targetId, ok := verticesMap[routeHop{targetPkh, destCoinType, true}]
if !ok {
return nil, fmt.Errorf("destination node not found")
}
// add dummy vertex q to the map
vertices = append(vertices, routeHop{[20]byte{}, 0, false})
distance = append(distance, 0)
predecessor = append(predecessor, -1)
// connect q to every other vertex
for idx := range vertices {
if idx < len(vertices)-1 {
edgesLight = append(edgesLight, channelEdgeLight{
0,
len(vertices) - 1,
idx,
lnutil.RateDesc{},
0,
})
}
}
// relax the edges from q
for i := 0; i < len(vertices); i++ {
var relaxed bool
for _, edge := range edgesLight {
if distance[edge.U]+edge.W < distance[edge.V] {
distance[edge.V] = distance[edge.U] + edge.W
predecessor[edge.V] = edge.U
relaxed = true
}
}
// we didn't relax any edges in the last round so we can quit early
if !relaxed {
break
}
}
// check for negative-weight cycles
for _, edge := range edgesLight {
if distance[edge.U]+edge.W < distance[edge.V] {
return nil, fmt.Errorf("negative weight cycle in channel graph")
}
}
// reweight original graph
for idx, edge := range edgesLight {
edgesLight[idx].W += distance[edge.U] - distance[edge.V]
}
// remove q and its edges
edgesLight = edgesLight[:len(edges)]
vertices = vertices[:len(vertices)-1]
predecessor = predecessor[:len(predecessor)-1]
distance = distance[:len(distance)-1]
// run dijkstra over the reweighted graph to find the lowest weight route
// with enough capacity to route the amount we want to send
dDistance := make([]*nodeWithDist, len(vertices))
dEdges := make([][]channelEdgeLight, len(vertices))
dDistance[myId] = &nodeWithDist{
0,
myId,
amount,
}
for idx := range predecessor {
predecessor[idx] = -1
}
for _, edge := range edgesLight {
dEdges[edge.U] = append(dEdges[edge.U], edge)
}
var nodeHeap distanceHeap
heap.Push(&nodeHeap, *dDistance[myId])
for nodeHeap.Len() > 0 {
partialPath := heap.Pop(&nodeHeap).(nodeWithDist)
logging.Debugf("popped %s:%d from heap", bech32.Encode("ln", vertices[partialPath.Node].Node[:]), vertices[partialPath.Node].CoinType)
p, ok := coinparam.RegisteredNets[vertices[partialPath.Node].CoinType]
if !ok {
logging.Debugf("ignoring %s:%d because cointype is unknown", bech32.Encode("ln", vertices[partialPath.Node].Node[:]), vertices[partialPath.Node].CoinType)
continue
}
fee := p.FeePerByte * 1000
for _, edge := range dEdges[partialPath.Node] {
logging.Debugf("considering edge %s:%d", bech32.Encode("ln", vertices[edge.V].Node[:]), vertices[edge.V].CoinType)
amtRqd := partialPath.Amt
if amtRqd < consts.MinOutput+fee {
// this amount is too small to route
logging.Debugf("ignoring %x:%d->%x:%d because amount rqd: %d less than minOutput+fee: %d", vertices[edge.U].Node, vertices[edge.U].CoinType, vertices[edge.V].Node, vertices[edge.V].CoinType, amtRqd, consts.MinOutput+fee)
continue
}
if amtRqd > edge.Capacity {
// this channel doesn't have enough capacity
logging.Debugf("ignoring %x:%d->%x:%d because amount rqd: %d less than capacity: %d", vertices[edge.U].Node, vertices[edge.U].CoinType, vertices[edge.V].Node, vertices[edge.V].CoinType, amtRqd, edge.Capacity)
continue
}
nextAmt := amtRqd
// required capacity for next hop is last hop amt * rate
if edge.Rate.Reciprocal {
// prior hop coin type is worth less than this one
nextAmt /= edge.Rate.Rate
} else {
// prior hop coin type is worth more than this one
nextAmt *= edge.Rate.Rate
}
alt := dDistance[partialPath.Node].Dist + edge.W
if dDistance[edge.V] == nil {
dDistance[edge.V] = &nodeWithDist{
alt,
edge.V,
nextAmt,
}
} else if alt < dDistance[edge.V].Dist {
dDistance[edge.V].Dist = alt
dDistance[edge.V].Amt = nextAmt
} else {
continue
}
logging.Debugf("could use edge %s:%d->%s:%d amt: %d capacity: %d", bech32.Encode("ln", vertices[edge.U].Node[:]), vertices[edge.U].CoinType, bech32.Encode("ln", vertices[edge.V].Node[:]), vertices[edge.V].CoinType, amtRqd, edge.Capacity)
predecessor[edge.V] = edge.U
heap.Push(&nodeHeap, *dDistance[edge.V])
}
}
for target, dist := range dDistance {
if dist != nil && vertices[target].Terminus {
price := math.Exp(-dist.Dist)
logging.Debugf("%s:%d: cap (recv): %d, price: %f64, cap (send): %d", bech32.Encode("ln", vertices[target].Node[:]), vertices[target].CoinType, dist.Amt, price, int(float64(dist.Amt)/price))
}
}
if dDistance[targetId] == nil {
return nil, fmt.Errorf("no route from origin to destination could be found")
}
routeIds := []int{predecessor[targetId], targetId}
for predecessor[routeIds[0]] != -1 {
routeIds = append([]int{predecessor[routeIds[0]]}, routeIds...)
}
var route []lnutil.RouteHop
for _, id := range routeIds {
route = append(route, lnutil.RouteHop{vertices[id].Node, vertices[id].CoinType})
}
return route, nil
}
func (nd *LitNode) cleanStaleChannels() {
nd.ChannelMapMtx.Lock()
defer nd.ChannelMapMtx.Unlock()
newChannelMap := make(map[[20]byte][]LinkDesc)
now := time.Now().Unix()
for pkh, node := range nd.ChannelMap {
for _, channel := range node {
if channel.Link.Timestamp+consts.ChannelAdvTimeout >= now {
newChannelMap[pkh] = append(newChannelMap[pkh], channel)
}
}
}
nd.ChannelMap = newChannelMap
}
func (nd *LitNode) advertiseLinks(seq uint32) {
caps := make(map[[20]byte]map[uint32]int64)
nd.RemoteMtx.Lock()
for _, peer := range nd.RemoteCons {
for _, q := range peer.QCs {
if !q.CloseData.Closed && q.State.MyAmt >= 2*(consts.MinOutput+q.State.Fee) && !q.State.Failed {
outHash := fastsha256.Sum256(peer.Con.RemotePub().SerializeCompressed())
var BPKH [20]byte
copy(BPKH[:], outHash[:20])
if _, ok := caps[BPKH]; !ok {
caps[BPKH] = make(map[uint32]int64)
}
amt := q.State.MyAmt - consts.MinOutput - q.State.Fee
// Since we don't yet perform multi-path routing, the capacity
// is the maximum available single channel capacity
if caps[BPKH][q.Coin()] < amt {
caps[BPKH][q.Coin()] = amt
}
}
}
}
nd.RemoteMtx.Unlock()
var msgs []lnutil.LinkMsg
outHash := fastsha256.Sum256(nd.IdKey().PubKey().SerializeCompressed())
var APKH [20]byte
copy(APKH[:], outHash[:20])
for BPKH, node := range caps {
for coin, capacity := range node {
var outmsg lnutil.LinkMsg
outmsg.CoinType = coin
outmsg.Seq = seq
outmsg.APKH = APKH
outmsg.BPKH = BPKH
outmsg.ACapacity = capacity
if rates, ok := nd.ExchangeRates[coin]; ok {
outmsg.Rates = rates
}
outmsg.PeerIdx = math.MaxUint32
msgs = append(msgs, outmsg)
}
}
for _, msg := range msgs {
nd.LinkMsgHandler(msg)
}
}
func (nd *LitNode) LinkMsgHandler(msg lnutil.LinkMsg) {
nd.ChannelMapMtx.Lock()
defer nd.ChannelMapMtx.Unlock()
nd.RemoteMtx.Lock()
defer nd.RemoteMtx.Unlock()
msg.Timestamp = time.Now().Unix()
newChan := true
// Check if node exists as a router
if _, ok := nd.ChannelMap[msg.APKH]; ok {
// Check if link state is most recent (seq)
for i, v := range nd.ChannelMap[msg.APKH] {
if bytes.Equal(v.Link.BPKH[:], msg.BPKH[:]) && v.Link.CoinType == msg.CoinType {
// This is the link we've been looking for
if msg.Seq <= v.Link.Seq {
// Old advert
return
}
// Update channel map
nd.ChannelMap[msg.APKH][i].Link = msg
nd.ChannelMap[msg.APKH][i].Dirty = false
newChan = false
break
}
}
}
if newChan {
// New peer or new channel
nd.ChannelMap[msg.APKH] = append(nd.ChannelMap[msg.APKH], LinkDesc{msg, false})
}
// Rebroadcast
origIdx := msg.PeerIdx
for peerIdx := range nd.RemoteCons {
if peerIdx != origIdx {
msg.PeerIdx = peerIdx
go func(omsg lnutil.LinkMsg) {
nd.tmpSendLitMsg(omsg)
}(msg)
}
}
}
func (nd *LitNode) PopulateRates() error {
ratesPath := filepath.Join(nd.LitFolder, "rates.json")
jsonFile, err := os.Open(ratesPath)
if err != nil {
return err
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return err
}
err = json.Unmarshal(byteValue, &nd.ExchangeRates)
if err != nil {
return err
}
return nil
}