-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
121 lines (103 loc) · 2.75 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
package main
import (
"bharvest.io/axelmon/app"
"bharvest.io/axelmon/client/api"
"bharvest.io/axelmon/log"
"bharvest.io/axelmon/server"
"bharvest.io/axelmon/wallet"
"context"
"errors"
"flag"
"fmt"
"github.com/pelletier/go-toml/v2"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
ctx, cancel := context.WithCancel(context.Background())
cfgPath := flag.String("config", "", "Config file")
stateFilePath := flag.String("state", ".axelmon-state.json", "state file")
flag.Parse()
if *cfgPath == "" {
panic("Error: Please input config file path with -config flag.")
}
f, err := os.ReadFile(*cfgPath)
if err != nil {
log.Error(err)
panic(err)
}
cfg := app.Config{}
err = toml.Unmarshal(f, &cfg)
if err != nil {
log.Error(err)
panic(err)
}
if cfg.PollingVote.MissPercentage < 0 || cfg.PollingVote.MissPercentage > 100 {
panic(errors.New("MissPercentage must be between 0 to 100"))
} else if cfg.PollingVote.MissPercentage == 0 {
log.Debug("MissPercentage seems like zero. it'll alert if there is any failed record")
}
cfg.Wallet.Validator, err = wallet.NewWallet(ctx, cfg.General.ValidatorAcc)
if err != nil {
log.Error(err)
panic(err)
}
api.Set(cfg.General.Network, cfg.General.API)
log.Info(fmt.Sprintf("Start Axelmon (for %s)", cfg.General.Network))
proxyAcc, err := api.C.GetProxyByVal(cfg.Wallet.Validator.PrintValoper())
if err != nil {
log.Error(err)
panic(err)
}
if proxyAcc != "" {
cfg.Wallet.Proxy, err = wallet.NewWallet(ctx, proxyAcc)
if err != nil {
log.Error(err)
panic(err)
}
} else {
log.Warn("Cannot fetch proxy acc. it may cause error while retrieving voting infos.")
cfg.Wallet.Proxy = cfg.Wallet.Validator
if err != nil {
log.Error(err)
panic(err)
}
}
cfg.General.ExceptChains = map[string]bool{}
exceptChains := strings.Split(strings.ReplaceAll(cfg.General.ExceptChainsString, " ", ""), ",")
for _, exceptChain := range exceptChains {
cfg.General.ExceptChains[strings.ToLower(exceptChain)] = true
}
cfg.Ctx = ctx
go server.Run(cfg.General.ListenPort, *stateFilePath)
quitting := make(chan os.Signal, 1)
signal.Notify(quitting, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
period := time.Hour
if cfg.General.Period != nil {
customPeriod := time.Duration(*cfg.General.Period)
if customPeriod < time.Minute {
log.Warn(fmt.Sprintf("general.period is too low. %v. it'll set as defaultValue(%v)", customPeriod, period))
}
period = customPeriod
}
ticker := time.NewTicker(period)
go app.Run(ctx, &cfg)
for {
select {
case <-ctx.Done():
app.SaveOnExit(*stateFilePath)
return
case <-quitting:
cancel()
app.SaveOnExit(*stateFilePath)
return
case <-ticker.C:
go app.Run(ctx, &cfg)
}
}
}