-
Notifications
You must be signed in to change notification settings - Fork 6
/
sitzprobe.go
254 lines (221 loc) · 6.29 KB
/
sitzprobe.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
package main
import (
"fmt"
"github.com/niftynei/glightning/glightning"
"github.com/niftynei/glightning/jrpc2"
"log"
"os"
"strconv"
"math/rand"
"time"
"encoding/hex"
"regexp"
)
const (
Frequency string = "sitzprobe-freq"
Amount string = "sitzprobe-amt"
DefaultFreq int = 60
DefaultAmt uint64 = uint64(1)
)
// Note: if you add a new type that's not of type 'failure', you should
// update countFailures to account for this.
const (
Success string = "success" // should never happen
NoActiveChannelFound string = "no_active_channel_found"
ChannelsUnavailable string = "channels_unavailable"
NoRouteFound string = "no_route_found"
SendPayFailed string = "sendpay_call_failed"
Run string = "runs_started"
UnknownError string = "unknown_error"
)
// The goal of sitzprobe is to provide a utility
// maintain a healthy node graph. The goal is to pick a
// node to pay, and then send a payment.
//
// I'd like to keep a log of what payments are successful
// and which are not successful.
var ln *glightning.Lightning
var plugin *glightning.Plugin
var reportSet map[string]uint64
var start string
func main() {
plugin = glightning.NewPlugin(onInit)
ln = glightning.NewLightning()
reportSet = make(map[string]uint64)
registerOptions()
registerMethods()
err := plugin.Start(os.Stdin, os.Stdout)
if err != nil {
log.Fatal(err)
}
}
type Report struct {
}
type ReportResult struct {
Frequency string `json:"frequency"`
StartedAt string `json:"started_at"`
Runs uint64 `json:"runs"`
Successes uint64 `json:"successes"`
Failures uint64 `json:"failures"`
Stats map[string]uint64 `json:"stats"`
}
func (r *Report) New() interface{} {
return &Report{}
}
func (r *Report) Name() string {
return "sitzprobe-report"
}
func (r *Report) Call() (jrpc2.Result, error) {
return &ReportResult{
Frequency: fmt.Sprintf("every %d min", freq),
StartedAt: start,
Runs: reportSet[Run],
Successes: reportSet[Success],
Failures: countFailures(),
Stats: reportSet,
}, nil
}
func countFailures() uint64 {
var result uint64
for k,v := range reportSet {
switch (k) {
case Success, Run:
// do nothing
default:
result += v
}
}
return result
}
// Sitzprobe offers two options:
// - frequency: repetition frequency with which Sitzprobe tries a new payment, in minutes.
// - probe amount: amount to probe with, in millisatoshis.
func registerOptions() {
freqOption := glightning.NewOption(Frequency, "Interval to run sitzprobe on, in minutes", fmt.Sprintf("%d",DefaultFreq))
probeamount := glightning.NewOption(Amount, "Amount to probe with, in millisatoshis", fmt.Sprintf("%d",DefaultAmt))
plugin.RegisterOption(freqOption)
plugin.RegisterOption(probeamount)
}
func registerMethods() {
reportMethod := glightning.NewRpcMethod(&Report{}, "Print a probe report")
reportMethod.LongDesc = "Returns a set of metrics around probes, including failures and successes"
plugin.RegisterMethod(reportMethod)
}
var freq int
var amount uint64
func onInit(plugin *glightning.Plugin, options map[string]string, config *glightning.Config) {
amount = parseAmount(options[Amount])
freq = parseFreq(options[Frequency])
ln.StartUp(config.RpcFile, config.LightningDir)
start = time.Now().UTC().Format("2006-01-02T15:04:05-0700")
reschedule(0)
}
func reschedule(number int) {
if number != 0 {
timer := time.NewTimer(time.Duration(freq) * time.Minute)
// wait for timer to elapse
<-timer.C
}
go run(number+1, amount)
}
func count(errType string) {
reportSet[errType] += 1
}
func run(runNumber int, amount uint64) {
// we wait to reschedule until after this payment
// has successfully been 'finished'
defer reschedule(runNumber)
count(Run)
// list all the available channels
channels, err := ln.ListChannels()
if err != nil {
log.Printf("(RUN%d)Unable to fetch channel list: %s", runNumber, err.Error())
count(ChannelsUnavailable)
return
}
// pick a node at random from the channel list
rand.Seed(time.Now().Unix())
var channel glightning.Channel
ok := false
// try to find an active channel
for i := 0; i < 1000 && !ok; i++ {
n := rand.Int() % len(channels)
// use the destination of the channel
channel = channels[n]
if channel.IsActive {
ok = true
}
}
if !ok {
log.Printf("(RUN%d)Unable to find active channel out of %d channels", runNumber, len(channels))
count(NoActiveChannelFound)
return
}
// find a route to the selected node.
route, err := ln.GetRouteSimple(channel.Destination, amount, 5)
if err != nil {
log.Printf("(RUN%d)Unable to find route to node id %s: %s", runNumber, channel.Destination, err.Error())
count(NoRouteFound)
return
}
// generate a fake payment hash
fakeHash := randomPayHash()
_, err = ln.SendPayLite(route, fakeHash)
if err != nil {
log.Printf("(RUN%d)Unable to send payment along route: %s", runNumber, err.Error())
count(SendPayFailed)
return
}
// block until we get a result (should fail)
payment, err := ln.WaitSendPay(fakeHash, 0)
if err, ok := err.(*glightning.PaymentError); ok {
log.Printf("(RUN%d)Payment successfully failed with failcode %d: %s", runNumber, err.Data.FailCode, err.Message)
logFailure(err.Error())
} else if err != nil {
log.Printf("(RUN%d)Failure: %s", err.Error())
count(UnknownError)
} else {
log.Printf("(RUN%d)Payment somehow miraculously succeeded wtf. Peer %s reached", runNumber, payment.Destination)
count(Success)
}
}
var pattern *regexp.Regexp = regexp.MustCompile("[A-Z_]+")
func logFailure(errMsg string) {
errorType := pattern.FindString(errMsg)
switch errorType {
case "":
count(UnknownError)
case "WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS":
count(Success)
default:
count(errorType)
}
}
func randomPayHash() string {
b := make([]byte, 32)
for i := range b {
// was seeded earlier
b[i] = byte(rand.Intn(127))
}
return hex.EncodeToString(b)
}
func parseAmount(setAmount string) uint64 {
amt, err := strconv.ParseUint(setAmount, 10, 64)
if err != nil {
log.Printf("Invalid amount set (%s), defaulting to %d", setAmount, DefaultAmt)
return DefaultAmt
}
return amt
}
func parseFreq(setFreq string) int {
freq, err := strconv.Atoi(setFreq)
if err != nil {
log.Printf("Invalid frequency set (%s), defaulting to %d", setFreq, DefaultFreq)
return DefaultFreq
}
if freq <= 0 {
log.Printf("Invalid frequency set (%s), defaulting to %d", freq, DefaultFreq)
return DefaultFreq
}
return freq
}