/
main.go
185 lines (171 loc) · 4.28 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"context"
"crypto/ecdsa"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/mapprotocol/tools/ethclient"
"gopkg.in/urfave/cli.v1"
"log"
"os"
"path/filepath"
"sort"
"time"
)
var (
// The app that holds all commands and flags.
app *cli.App
// Flags needed by abigen
KeyFlag = cli.StringFlag{
Name: "key",
Usage: "Private key file path",
Value: "",
}
KeyStoreFlag = cli.StringFlag{
Name: "keystore",
Usage: "Keystore file path",
}
RPCListenAddrFlag = cli.StringFlag{
Name: "rpcaddr",
Usage: "HTTP-RPC server listening interface",
Value: "localhost",
}
RPCPortFlag = cli.IntFlag{
Name: "rpcport",
Usage: "HTTP-RPC server listening port",
Value: 7445,
}
FeeFlag = cli.Uint64Flag{
Name: "fee",
Usage: "work fee",
Value: 0,
}
PublicAdressFlag = cli.StringFlag{
Name: "PkAdress",
Usage: "Relayer bft key for BFT (no 0x prefix)",
Value: "",
}
relayerflags = []cli.Flag{
KeyFlag,
KeyStoreFlag,
RPCListenAddrFlag,
RPCPortFlag,
FeeFlag,
}
withdrawMockCommand = cli.Command{
Name: "withdrawMock",
Usage: "relayer to withdraw ",
Action: MigrateFlags(withdrawMock),
Flags: relayerflags,
}
appendMockCommand = cli.Command{
Name: "appendMock",
Usage: "relayer to append ",
Action: MigrateFlags(appendMock),
Flags: relayerflags,
}
registerMockCommand = cli.Command{
Name: "registerMock",
Usage: "relayer to register ",
Action: MigrateFlags(registerMock),
Flags: relayerflags,
}
saveMockCommand = cli.Command{
Name: "saveMock",
Usage: "relayer to save ",
Action: MigrateFlags(saveMock),
Flags: relayerflags,
}
)
func init() {
app = cli.NewApp()
app.Usage = "Atlas Register Tool"
app.Name = filepath.Base(os.Args[0])
app.Version = "1.0.0"
app.Copyright = "Copyright 2020-2021 The Atlas Authors"
app.Flags = []cli.Flag{
KeyFlag,
KeyStoreFlag,
RPCListenAddrFlag,
RPCPortFlag,
FeeFlag,
PublicAdressFlag,
}
app.Action = MigrateFlags(start)
app.CommandNotFound = func(ctx *cli.Context, cmd string) {
fmt.Fprintf(os.Stderr, "No such command: %s\n", cmd)
os.Exit(1)
}
// Add subcommands.
app.Commands = []cli.Command{
withdrawMockCommand,
appendMockCommand,
registerMockCommand,
saveMockCommand,
}
cli.CommandHelpTemplate = OriginCommandHelpTemplate
sort.Sort(cli.CommandsByName(app.Commands))
}
func main() {
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
var OriginCommandHelpTemplate string = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} [arguments...] {{if .Description}}{{.Description}} {{end}}{{if .Subcommands}} SUBCOMMANDS: {{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}} {{end}}{{end}}{{if .Flags}} OPTIONS: {{range $.Flags}}{{"\t"}}{{.}} {{end}} {{end}}`
func MigrateFlags(action func(ctx *cli.Context) error) func(*cli.Context) error {
return func(ctx *cli.Context) error {
for _, name := range ctx.FlagNames() {
if ctx.IsSet(name) {
ctx.GlobalSet(name, ctx.String(name))
}
}
return action(ctx)
}
}
func start(ctx *cli.Context) error {
conn := getConn11(ctx)
from, priKey := register(ctx, conn)
syncloop(ctx, conn, from, priKey)
return nil
}
//single node start
func syncloop(ctx *cli.Context, conn *ethclient.Client, from common.Address, priKey *ecdsa.PrivateKey) {
chains := getEthChains()
for {
for {
time.Sleep(time.Second * 1)
//1.now Number
num, err := conn.BlockNumber(context.Background())
if err != nil {
log.Fatal("BlockNumber err")
}
//2. isrelayers
isrelayers := queryIsRegister(conn, from)
if !isrelayers {
log.Fatal("not Relayers")
time.Sleep(time.Second)
continue
}
//3.judge number at range
if !queryRelayerEpoch(conn, num, from) {
log.Fatal("wrong range !")
continue
}
break
}
// 1.get current num
chainNum, _ := getCurrentNumberAbi(conn, ChainTypeETH, from)
// 2.store
marshal, err2 := json.Marshal(chains[chainNum : chainNum+10])
if err2 != nil {
log.Fatal("marshal err")
}
//ret, _ := rlp.EncodeToBytes(chains)
input := packInputStore("save", "ETH", "ETH", marshal)
sendContractTransaction(conn, from, HeaderStoreAddress, nil, priKey, input)
Append(conn, from, priKey)
withdraw(conn, from, priKey)
}
}