forked from joostjager/whatsat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_chat_peers.go
145 lines (121 loc) · 2.81 KB
/
cmd_chat_peers.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/urfave/cli"
)
var chatPeersCommand = cli.Command{
Name: "chatpeers",
Category: "Chat",
Usage: "Show recommended peers to connect to for chatting.",
Action: actionDecorator(chatPeers),
}
func chatPeers(ctx *cli.Context) error {
network := strings.ToLower(ctx.GlobalString("network"))
bosNodes, err := getBosNodes(network)
if err != nil {
return err
}
conn := getClientConn(ctx, false)
defer conn.Close()
client := lnrpc.NewLightningClient(conn)
graph, err := client.DescribeGraph(
context.Background(),
&lnrpc.ChannelGraphRequest{},
)
if err != nil {
return err
}
type bestPolicy struct {
fee int64
minHtlc int64
}
lowestFee := make(map[string]bestPolicy)
for _, u := range graph.Edges {
process := func(key string, p *lnrpc.RoutingPolicy) {
if p == nil {
return
}
amt := p.MinHtlc
fee := p.FeeBaseMsat + amt*p.FeeRateMilliMsat/1000000
lowest, ok := lowestFee[key]
if !ok || fee > lowest.fee {
lowest = bestPolicy{
fee: fee,
}
lowestFee[key] = lowest
}
if p.MinHtlc > lowest.minHtlc {
lowest.minHtlc = p.MinHtlc
lowestFee[key] = lowest
}
}
process(u.Node1Pub, u.Node1Policy)
process(u.Node2Pub, u.Node2Policy)
}
type nodeFee struct {
node string
alias string
fee bestPolicy
}
list := make([]nodeFee, 0)
for n, f := range lowestFee {
alias, ok := bosNodes[n]
if !ok {
continue
}
list = append(list, nodeFee{
node: n,
fee: f,
alias: alias,
})
}
sort.Slice(list, func(i, j int) bool {
return list[i].fee.fee < list[j].fee.fee || (list[i].fee.fee == list[j].fee.fee && list[i].fee.minHtlc < list[j].fee.minHtlc)
})
for _, item := range list {
fmt.Printf("%v (%v) %v msat (min_htlc %v msat)\n", item.node, item.alias, item.fee.fee, item.fee.minHtlc)
}
return nil
}
type BosScore struct {
Alias string
PublicKey string `json:"public_key"`
}
type BosList struct {
Scores []*BosScore
}
func getBosNodes(network string) (map[string]string, error) {
var url string
switch network {
case "mainnet":
url = "https://nodes.lightning.computer/availability/v1/btc.json"
case "testnet":
url = "https://nodes.lightning.computer/availability/v1/btctestnet.json"
default:
return nil, fmt.Errorf("no bos list for network %v", network)
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
respByte := buf.Bytes()
var bosList BosList
if err := json.Unmarshal(respByte, &bosList); err != nil {
return nil, err
}
bosNodes := make(map[string]string)
for _, item := range bosList.Scores {
bosNodes[item.PublicKey] = item.Alias
}
return bosNodes, nil
}