-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinfra_check.go
232 lines (207 loc) · 5.96 KB
/
infra_check.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
package main
import (
"github.com/cwntr/go-dex-client/pkg/common"
"strconv"
"fmt"
"github.com/cwntr/go-dex-client/lncli"
)
type InfraChecks []Check
type Check struct {
Name string `json:"name"`
Currency string `json:"currency"`
Result bool `json:"result"`
Details string `json:"details"`
}
type Channel struct {
Currency string `json:"currency"`
Active bool `json:"active"`
ChanID string `json:"chan_id"`
Capacity string `json:"capacity"`
LocalBalance string `json:"local_balance"`
RemoteBalance string `json:"remote_balance"`
UnsettledBalance string `json:"unsettled_balance"`
ChanStatusFlags string `json:"chan_status_flags"`
}
func getMinimumSatoshis(currency string) int64 {
switch currency {
case common.CurrencyXSN:
return 60000
case common.CurrencyLTC:
return 275000
case common.CurrencyBTC:
return 20000
}
return 0
}
func getHubPeers(currency string) []HubPeer {
switch currency {
case common.CurrencyXSN:
return cfg.XSN.HubPeers
case common.CurrencyLTC:
return cfg.LTC.HubPeers
case common.CurrencyBTC:
return cfg.BTC.HubPeers
}
return []HubPeer{}
}
func getChannelCapacities(currency string) (min int64, max int64) {
switch currency {
case common.CurrencyXSN:
min = 60000
max = 100000000000
return
case common.CurrencyLTC:
min = 275000
max = 1000000000
return
case common.CurrencyBTC:
min = 20000
max = 1600000
return
}
return
}
const (
InfraCheckNamePeers = "HubPeers"
InfraCheckNameChannels = "HubChannels"
)
func getChannels(currency string) []Channel {
var lndCfg LNDConfig
switch currency {
case common.CurrencyXSN:
lndCfg = cfg.XSN
case common.CurrencyLTC:
lndCfg = cfg.LTC
case common.CurrencyBTC:
lndCfg = cfg.BTC
default:
return []Channel{}
}
// Hub Channels
lc, err := lncli.GetListChannels(cfg.Bot.LNCLIPath, lndCfg.Directory, lndCfg.Host, lndCfg.Port)
if err != nil {
logger.Errorf("unable to get listChannels, err: %v", err)
return []Channel{}
}
var channels []Channel
for _, c := range lc.Channels {
channels = append(channels, Channel{
Currency: currency,
Active: c.Active,
ChanID: c.ChanID,
Capacity: c.Capacity,
LocalBalance: c.LocalBalance,
RemoteBalance: c.RemoteBalance,
UnsettledBalance: c.UnsettledBalance,
ChanStatusFlags: c.ChanStatusFlags,
})
}
return channels
}
func checkInfra(currency string, printCLI bool) (InfraChecks, error) {
var infra InfraChecks
var lndCfg LNDConfig
switch currency {
case common.CurrencyXSN:
lndCfg = cfg.XSN
case common.CurrencyLTC:
lndCfg = cfg.LTC
case common.CurrencyBTC:
lndCfg = cfg.BTC
default:
return infra, fmt.Errorf("unable to resolve trading pair")
}
hubPeersCheck := Check{Name: InfraCheckNamePeers, Currency: currency}
hubChannelsCheck := Check{Name: InfraCheckNameChannels, Currency: currency}
// HubPeers
lp, err := lncli.GetPeers(cfg.Bot.LNCLIPath, lndCfg.Directory, lndCfg.Host, lndCfg.Port)
if err != nil {
logger.Errorf("unable to list peers, err: %v", err)
hubPeersCheck.Details = "unable to list peers"
return append(infra, hubPeersCheck), err
}
peerCheck := false
hubPeers := getHubPeers(currency)
for _, p := range lp.Peers {
for _, hp := range hubPeers {
if p.PubKey == hp.RemoteKey && p.Address == hp.Address {
if printCLI {
logger.Debugf("hub peer found: %s@%s", p.PubKey, p.Address)
}
hubPeersCheck.Details = fmt.Sprintf("hub peer found: %s@%s", p.PubKey, p.Address)
peerCheck = true
}
}
}
if peerCheck {
if printCLI {
logger.Debugf("Infra check: %s - peers (1/2) OK ", currency)
}
hubPeersCheck.Result = true
} else {
logger.Errorf("Infra check: %s - peers (1/2) FAILED", currency)
return append(infra, hubPeersCheck), err
}
infra = append(infra, hubPeersCheck)
// Hub Channels
lc, err := lncli.GetListChannels(cfg.Bot.LNCLIPath, lndCfg.Directory, lndCfg.Host, lndCfg.Port)
if err != nil {
logger.Errorf("unable to get listChannels, err: %v", err)
return infra, err
}
capacityMin, capacityMax := getChannelCapacities(currency)
channelCheck := false
cntHubChannels := 0
for _, c := range lc.Channels {
for _, hp := range hubPeers {
if hp.RemoteKey == c.RemotePubkey {
capacity, _ := strconv.ParseInt(c.Capacity, 10, 64)
if capacity >= capacityMin && capacity <= capacityMax {
localBalance, _ := strconv.ParseInt(c.LocalBalance, 10, 64)
lbf := float64(localBalance)
remoteBalance, _ := strconv.ParseInt(c.RemoteBalance, 10, 64)
rbf := float64(remoteBalance)
if printCLI {
logger.Debugf("[%s] channel capacity to hub is OK (chanID: %s) local_balance: %d (%.8f %s), remote_balance: %d (%.8f %s) ",
currency,
c.ChanID,
localBalance,
lbf/1e8,
currency,
remoteBalance,
rbf/1e8,
currency,
)
}
channelCheck = true
cntHubChannels++
} else {
logger.Errorf("[%s] channel capacity NOT OK -> your capacity %d must be between %d and %d ", currency, capacity, capacityMin, capacityMax)
}
}
}
}
if cntHubChannels > 0 && cntHubChannels%2 != 0 {
logger.Errorf("Infra check: %s - channels (2/2) FAILED", currency)
hubChannelsCheck.Result = false
hubChannelsCheck.Details = fmt.Sprintf("[%s] channels NOT OK: missing back channel(s)", currency)
return append(infra, hubChannelsCheck), err
}
if channelCheck {
if printCLI {
logger.Debugf("Infra check: %s - channels (2/2) OK ", currency)
}
hubChannelsCheck.Result = true
hubChannelsCheck.Details = "hub channel balances are ok"
} else {
logger.Errorf("Infra check: %s - channels (2/2) FAILED", currency)
hubChannelsCheck.Details = fmt.Sprintf("channels capacity NOT OK: capacity must be between %d and %d ", capacityMin, capacityMax)
return append(infra, hubChannelsCheck), err
}
if printCLI {
logger.Debugf("Infra check: %s complete", currency)
logger.Debugln("-------------------------------------")
}
infra = append(infra, hubChannelsCheck)
return infra, nil
}