aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: 6f3f29982c65799e4745a7855b74223c9f69af75 (plain) (blame)
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
package main

import (
    "bufio"
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "path/filepath"
    "strconv"

    "github.com/tangerine-network/tan-monitor/monitor"
    "github.com/urfave/cli"
)

var app *cli.App

func init() {
    app = cli.NewApp()
    app.Name = filepath.Base(os.Args[0])
    app.Usage = "Tangerine Newtwork Monitor"
    app.Commands = []cli.Command{
        commandStart,
        commandGetNodeStatus,
    }
}
func getLines(path string) ([]string, error) {
    file, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer file.Close()

    var lines []string
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        lines = append(lines, scanner.Text())
    }
    return lines, scanner.Err()
}

var commandGetNodeStatus = cli.Command{
    Name:        "get_nodes_status",
    Usage:       "Get nodes' status",
    ArgsUsage:   "",
    Description: `Print out current nodes's status`,
    Flags:       []cli.Flag{},
    Action: func(ctx *cli.Context) error {
        networkID := 411
        backend := monitor.NewBlockchainBackend(networkID)
        nodes := backend.NodeSet()
        for _, node := range nodes {
            node.Print()
        }
        return nil
    },
}
var commandStart = cli.Command{
    Name:      "start",
    Usage:     "Start monitor job",
    ArgsUsage: "<networkID> <ethThreshold>",
    Description: `Start network monitor with <networkID> and <ethThreshold>
   <ethThreshold>'s unit is ETH`,
    Flags: []cli.Flag{
        cli.StringFlag{
            Name:  "emailPassword",
            Value: "./email.password",
            Usage: "Path to the email sender password",
        },
        cli.StringFlag{
            Name:  "ccList",
            Value: "./cc.txt",
            Usage: "Path to the cc list",
        },
        cli.StringFlag{
            Name:  "skipList",
            Value: "./skip-list.txt",
            Usage: "Path to the skip list",
        },
    },
    Action: func(ctx *cli.Context) error {
        if len(ctx.Args()) != 2 {
            log.Fatalf("invalid argument count")
        }
        networkID, err := strconv.Atoi(ctx.Args()[0])
        if err != nil {
            panic(err)
        }
        threshold := ctx.Args()[1]
        emailPassword, err := ioutil.ReadFile(ctx.String("emailPassword"))
        if err != nil {
            panic(err)
        }
        ccList, _ := ioutil.ReadFile(ctx.String("ccList"))
        skipList, _ := getLines(ctx.String("skipList"))
        fmt.Println("skip list:")
        for _, skip := range skipList {
            fmt.Println(skip)
        }
        backend := monitor.NewBlockchainBackend(networkID)
        m := monitor.NewMonitor(networkID, backend, threshold)
        email := monitor.NewEmail(
            "no-reply@byzantine-lab.io",
            string(emailPassword),
            "smtp-relay.gmail.com",
            string(ccList),
            skipList,
        )
        m.Register(email)
        m.Run()
        return nil
    },
}

func main() {
    if err := app.Run(os.Args); err != nil {
        fmt.Println("app.Run error:", err)
        os.Exit(1)
    }
}