-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
117 lines (100 loc) · 2.47 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
package main
import (
"fmt"
"github.com/polynetwork/neo3-voter/voter"
"github.com/polynetwork/poly/core/types"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/polynetwork/neo3-voter/cmd"
"github.com/polynetwork/neo3-voter/common"
"github.com/polynetwork/neo3-voter/config"
"github.com/polynetwork/neo3-voter/log"
sdk "github.com/polynetwork/poly-go-sdk"
"github.com/urfave/cli"
)
var Log = log.Log
func setupApp() *cli.App {
app := cli.NewApp()
app.Usage = "NEO3 Voter"
app.Action = start
app.Copyright = "Copyright in 2022 The NEO Project"
app.Flags = []cli.Flag{
cmd.ConfigPathFlag,
cmd.PolyPwd,
}
app.Commands = []cli.Command{}
app.Before = func(context *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
return nil
}
return app
}
func main() {
if err := setupApp().Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func start(ctx *cli.Context) {
configPath := ctx.String(cmd.GetFlagName(cmd.ConfigPathFlag))
err := config.DefConfig.Init(configPath)
if err != nil {
fmt.Println("DefConfig.Init error: ", err)
return
}
polyPwd := ctx.GlobalString(cmd.GetFlagName(cmd.PolyPwd))
//create poly RPC Client
polySdk := sdk.NewPolySdk()
err = SetUpPoly(polySdk, config.DefConfig.PolyConfig.RpcUrl)
if err != nil {
panic(fmt.Errorf("failed to set up poly: %v", err))
}
// Get wallet account for poly
signer, ok := common.GetAccountByPassword(polySdk, config.DefConfig.PolyConfig.WalletFile, polyPwd)
if !ok {
Log.Errorf("[NEO Relayer] common.GetAccountByPassword error")
return
}
Log.Infof("voter %s", signer.Address.ToBase58())
v := voter.New(polySdk, signer, config.DefConfig)
v.Start()
waitToExit()
}
func waitToExit() {
exit := make(chan bool, 0)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
for sig := range sc {
Log.Infof("Neo Relayer received exit signal: %v.", sig.String())
close(exit)
break
}
}()
<-exit
}
func SetUpPoly(poly *sdk.PolySdk, rpcAddr string) error {
poly.NewRpcClient().SetAddress(rpcAddr)
c1 := make(chan *types.Header, 1)
c2 := make(chan error, 1)
// use another routine to check time out and error
go func() {
hdr, err := poly.GetHeaderByHeight(0)
if err != nil {
c2 <- err
}
c1 <- hdr
}()
select {
case hdr := <- c1:
poly.SetChainId(hdr.ChainID)
case err := <- c2:
return err
case <- time.After(time.Second * 5):
return fmt.Errorf("poly rpc port timeout")
}
return nil
}