-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
274 lines (247 loc) · 6.32 KB
/
main.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
package main
import (
"os"
"time"
"github.com/labstack/gommon/log"
"github.com/hesusruiz/signers/history"
"github.com/hesusruiz/signers/logfilter"
"github.com/hesusruiz/signers/redt"
"github.com/hesusruiz/signers/serve"
"github.com/urfave/cli/v2"
)
// The default node address used is a local one
var localNodeHTTP = "http://127.0.0.1:22000"
var localNodeWS = "ws://127.0.0.1:22001"
func main() {
// Define commands, parse command line arguments and start execution
app := &cli.App{
Usage: "Monitoring of block signers activity for the Alastria RedT blockchain network",
UsageText: `signers [global options] command [command options]
where 'nodeURL' is the address of the Quorum node.
It supports both HTTP and WebSockets endpoints.
By default it uses WebSockets and for HTTP you have to use the 'poll' subcommand.`,
EnableBashCompletion: true,
UseShortOptionHandling: true,
Version: "v0.2",
Compiled: time.Now(),
Authors: []*cli.Author{
{
Name: "Jesus Ruiz",
Email: "hesus.ruiz@gmail.com",
},
},
Action: func(c *cli.Context) error {
cli.ShowAppHelpAndExit(c, 0)
return nil
},
}
monitorWSCMD := &cli.Command{
Name: "monitor",
Usage: "monitor the signers activity via WebSockets events",
UsageText: "signers monitor [options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Value: localNodeWS,
Usage: "ws url of the endpoint of blockchain node",
Aliases: []string{"u"},
Required: false,
},
&cli.Int64Flag{
Name: "blocks",
Value: 10,
Usage: "number of blocks in the past to process",
Aliases: []string{"b"},
},
},
Action: func(c *cli.Context) error {
url := c.String("url")
numBlocks := c.Int64("blocks")
redt.MonitorSignersWS(url, numBlocks)
return nil
},
}
monitorCMD := &cli.Command{
Name: "poll",
Usage: "monitor the signers activity via HTTP polling",
UsageText: "signers poll [options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Value: localNodeHTTP,
Usage: "url of the endpoint of blockchain node",
Aliases: []string{"u"},
Required: false,
},
&cli.Int64Flag{
Name: "blocks",
Value: 10,
Usage: "number of blocks in the past to process before starting",
Aliases: []string{"b"},
},
&cli.Int64Flag{
Name: "refresh",
Value: 2,
Usage: "refresh interval for presentation. All blocks are processed independent of this value",
Aliases: []string{"r"},
},
},
Action: func(c *cli.Context) error {
url := c.String("url")
numBlocks := c.Int64("blocks")
refresh := c.Int64("refresh")
redt.MonitorSigners(url, numBlocks, refresh)
return nil
},
}
displayPeersCMD := &cli.Command{
Name: "peers",
Usage: "display peers information",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Value: localNodeHTTP,
Usage: "url of the endpoint of blockchain node",
Aliases: []string{"u"},
Required: false,
},
},
Action: func(c *cli.Context) error {
url := c.String("url")
redt.DisplayPeersInfo(url)
return nil
},
}
logfilterCMD := &cli.Command{
Name: "logfilter",
Usage: "display filtered log information",
Action: func(c *cli.Context) error {
logfilter.FilterLogs()
return nil
},
}
serveCMD := &cli.Command{
Name: "serve",
Usage: "run a web server to display signers behaviour in real time",
UsageText: "signers serve [options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Value: localNodeWS,
Usage: "url of the endpoint of blockchain node",
Aliases: []string{"u"},
Required: false,
},
&cli.StringFlag{
Name: "ip",
Value: "0.0.0.0",
Usage: "IP address of the web server",
Aliases: []string{"i"},
},
&cli.Int64Flag{
Name: "port",
Value: 8080,
Usage: "port of the IP address for the web server",
Aliases: []string{"p"},
},
},
Action: func(c *cli.Context) error {
url := c.String("url")
ip := c.String("ip")
port := c.Int64("port")
serve.ServeSigners(url, ip, port)
return nil
},
}
historyCMD := &cli.Command{
Name: "history",
Usage: "download blockchain headers into SQLite database, from current towards genesis",
UsageText: "signers poll [options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Value: localNodeHTTP,
Usage: "url of the endpoint of blockchain node",
Aliases: []string{"u"},
Required: false,
},
&cli.StringFlag{
Name: "dsn",
Value: "./blockchain.sqlite?_journal=WAL",
Usage: "dsn of the SQLite database",
Aliases: []string{"d"},
Required: false,
},
&cli.BoolFlag{
Name: "stats",
Value: false,
Usage: "dsn of the SQLite database",
Aliases: []string{"s"},
Required: false,
},
},
Action: func(c *cli.Context) error {
url := c.String("url")
dsn := c.String("dsn")
stats := c.Bool("stats")
err := history.HistoryBackwards(url, dsn, stats)
if err != nil {
log.Error(err)
}
return err
},
}
historyForwardCMD := &cli.Command{
Name: "historyfw",
Usage: "download blockchain headers into SQLite database, from newest stored towards current",
UsageText: "signers poll [options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Value: localNodeHTTP,
Usage: "url of the endpoint of blockchain node",
Aliases: []string{"u"},
Required: false,
},
&cli.StringFlag{
Name: "dsn",
Value: "./blockchain.sqlite?_journal=WAL",
Usage: "dsn of the SQLite database",
Aliases: []string{"d"},
Required: false,
},
&cli.BoolFlag{
Name: "stats",
Value: false,
Usage: "dsn of the SQLite database",
Aliases: []string{"s"},
Required: false,
},
},
Action: func(c *cli.Context) error {
url := c.String("url")
dsn := c.String("dsn")
stats := c.Bool("stats")
err := history.HistoryForward(url, dsn, stats)
if err != nil {
log.Error(err)
}
return err
},
}
app.Commands = []*cli.Command{
monitorWSCMD,
monitorCMD,
displayPeersCMD,
logfilterCMD,
serveCMD,
historyCMD,
historyForwardCMD,
}
// Run the application
err := app.Run(os.Args)
if err != nil {
log.Error(err)
os.Exit(1)
}
}