-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
73 lines (65 loc) · 1.62 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
package main
import (
"fmt"
"github.com/St3ffn/chia-bouncer/bouncer"
"os"
)
func main() {
if err := run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
// run will initialise the cli, get all connected full nodes, filter them by given location and
// finally remove filtered full nodes
func run() error {
ctx, err := bouncer.RunCli()
if err != nil {
return err
}
if ctx.Done {
return nil
}
chiaCli := bouncer.ChiaCli{ChiaExecutable: ctx.ChiaExecutable}
nodes, err := chiaCli.ListNodes()
if err != nil {
return err
}
var filter bouncer.Filter
var filteredDownThreshold []bouncer.FullNode
if ctx.IsDownThreshold {
filter = bouncer.FilterByDown{
Nodes: nodes,
Threshold: ctx.DownThreshold,
}
filteredDownThreshold, err = filter.Filter()
if err != nil {
return err
}
}
filter = bouncer.FilterByLocation{
Nodes: nodes,
LocationToFilter: ctx.Location,
}
filteredByLocation, err := filter.Filter()
if err != nil {
return err
}
filtered := append(filteredDownThreshold, filteredByLocation...)
filterSet := make(map[string]bouncer.FullNode)
for _, node := range filtered {
filterSet[node.NodeId] = node
}
if len(filtered) == 0 {
_, _ = fmt.Fprintf(os.Stdout, "nothing from %s\n", ctx.Location)
}
for _, node := range filterSet {
if err := chiaCli.RemoveNode(node.NodeId); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Can not filter %s:%s from %s...stopping\n",
node.Ip, node.NodeId, ctx.Location)
return err
}
}
_, _ = fmt.Fprintf(os.Stdout, "found %d - filtered %d from %s\n", len(nodes), len(filterSet), ctx.Location)
return nil
}