-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
155 lines (135 loc) · 3.58 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
package main
import (
"fmt"
"github.com/joeqian10/neo3-gogogo/helper"
"github.com/joeqian10/neo3-gogogo/rpc"
"github.com/joeqian10/neo3-gogogo/wallet"
"github.com/polynetwork/poly/core/types"
"golang.org/x/crypto/ssh/terminal"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/polynetwork/neo3-relayer/cmd"
"github.com/polynetwork/neo3-relayer/common"
"github.com/polynetwork/neo3-relayer/config"
"github.com/polynetwork/neo3-relayer/log"
"github.com/polynetwork/neo3-relayer/service"
relaySdk "github.com/polynetwork/poly-go-sdk"
"github.com/urfave/cli"
)
var Log = log.Log
func setupApp() *cli.App {
app := cli.NewApp()
app.Usage = "NEO Relayer"
app.Action = startSync
app.Copyright = "Copyright in 2021 The NEO Project"
app.Flags = []cli.Flag{
cmd.LogLevelFlag,
cmd.ConfigPathFlag,
cmd.NeoPwd,
cmd.RelayPwd,
}
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 startSync(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
}
neoPwd := ctx.GlobalString(cmd.GetFlagName(cmd.NeoPwd))
relayPwd := ctx.GlobalString(cmd.GetFlagName(cmd.RelayPwd))
//create Relay Chain RPC Client
relaySdk := relaySdk.NewPolySdk()
err = SetUpPoly(relaySdk, config.DefConfig.RelayJsonRpcUrl)
if err != nil {
panic(fmt.Errorf("failed to set up poly: %v", err))
}
// Get wallet account from Relay Chain
account, ok := common.GetAccountByPassword(relaySdk, config.DefConfig.WalletFile, relayPwd)
if !ok {
Log.Errorf("[NEO Relayer] common.GetAccountByPassword error")
return
}
// create a NEO RPC client
neoRpcClient := rpc.NewClient(config.DefConfig.NeoJsonRpcUrl)
// open the NEO wallet
ps := helper.ProtocolSettings{
Magic: config.DefConfig.NeoMagic,
AddressVersion: helper.DefaultAddressVersion,
}
w, err := wallet.NewNEP6Wallet(config.DefConfig.NeoWalletFile, &ps, nil, nil)
if err != nil {
Log.Errorf("[NEO Relayer] Failed to open NEO wallet: %s", err)
return
}
if neoPwd == "" {
fmt.Println()
fmt.Printf("Neo Wallet Password:")
pwd, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
Log.Errorf("[NEO Relayer] Invalid password entered")
}
neoPwd = string(pwd)
fmt.Println()
}
err = w.Unlock(neoPwd)
if err != nil {
Log.Errorf("[NEO Relayer] Failed to decrypt NEO account")
return
}
wh := wallet.NewWalletHelperFromWallet(neoRpcClient, w)
//Start syncing
syncService := service.NewSyncService(account, relaySdk, wh, neoRpcClient)
syncService.Run()
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 *relaySdk.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
}