/
relayer_cli.go
331 lines (296 loc) · 9.22 KB
/
relayer_cli.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
ethchain "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/mapprotocol/atlas/accounts/keystore"
"github.com/mapprotocol/tools/ethclient"
"gopkg.in/urfave/cli.v1"
"io/ioutil"
"log"
"math/big"
"time"
)
var (
key string
store string
ip string
port int
)
var (
//baseUnit = new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
Base = new(big.Int).SetUint64(10000)
)
var (
contractQueryFailedErr = errors.New("Contract query failed result ")
)
const (
registerValue int64 = 100000
datadirPrivateKey = "key"
datadirDefaultKeyStore = "keystore"
RegisterAmount = 100000
RewardInterval = 14
)
func getConn11(ctx *cli.Context) *ethclient.Client {
conn, _ := dialConn()
return conn
}
func register(ctx *cli.Context, conn *ethclient.Client) (common.Address, *ecdsa.PrivateKey) {
from, priKey := loadPrivate(ctx)
header, err := conn.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
if common.IsHexAddress(from.Hex()) {
fmt.Println(" current number ", header.Number.String(), " address ", from.Hex())
} else {
fmt.Println(" current number ", header.Number.String())
}
fmt.Println("Your wallet balance is ", getBalance(conn, from), "'eth ")
value := ethToWei(registerValue)
fee := ctx.GlobalUint64(FeeFlag.Name)
checkFee(new(big.Int).SetUint64(fee))
pubkey, pk, _ := getPubKey(priKey)
fmt.Println("Fee", fee, " Pubkey ", pubkey, " value ", value)
input := packInput("register", pk, new(big.Int).SetUint64(fee), value)
sendContractTransaction(conn, from, RelayerAddress, nil, priKey, input)
return from, priKey
}
func checkFee(fee *big.Int) {
if fee.Sign() < 0 || fee.Cmp(Base) > 0 {
log.Fatal("Please set correct fee value")
}
}
func getPubKey(priKey *ecdsa.PrivateKey) (string, []byte, error) {
var (
pubkey string
err error
)
pk := crypto.FromECDSAPub(&priKey.PublicKey)
pubkey = crypto.PubkeyToAddress(priKey.PublicKey).String()
if _, err := crypto.UnmarshalPubkey(pk); err != nil {
log.Fatal("ValidPk error", err)
}
return pubkey, pk, err
}
func sendContractTransaction(client *ethclient.Client, from, toAddress common.Address, value *big.Int, privateKey *ecdsa.PrivateKey, input []byte) {
// Ensure a valid value field and resolve the account nonce
nonce, err := client.PendingNonceAt(context.Background(), from)
if err != nil {
log.Fatal(err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
gasLimit := uint64(2100000) // in units
// If the contract surely has code (or code is not needed), estimate the transaction
msg := ethchain.CallMsg{From: from, To: &toAddress, GasPrice: gasPrice, Value: value, Data: input}
gasLimit, err = client.EstimateGas(context.Background(), msg)
if err != nil {
fmt.Println("Contract exec failed", err)
}
if gasLimit < 1 {
gasLimit = 866328
}
// Create the transaction, sign it and schedule it for execution
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, input)
chainID, err := client.ChainID(context.Background())
if err != nil {
log.Fatal(err)
}
//fmt.Println("TX data nonce ", nonce, " transfer value ", value, " gasLimit ", gasLimit, " gasPrice ", gasPrice, " chainID ", chainID)
signer := types.LatestSignerForChainID(chainID)
signedTx, err := types.SignTx(tx, signer, privateKey)
if err != nil {
log.Fatal(err)
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}
txHash := signedTx.Hash()
count := 0
for {
time.Sleep(time.Millisecond * 200)
_, isPending, err := client.TransactionByHash(context.Background(), txHash)
if err != nil {
log.Fatal(err)
}
count++
if !isPending {
break
}
}
receipt, err := client.TransactionReceipt(context.Background(), txHash)
if err != nil {
log.Fatal(err)
}
if receipt.Status == types.ReceiptStatusSuccessful {
block, err := client.BlockByHash(context.Background(), receipt.BlockHash)
if err != nil {
log.Fatal(err)
}
fmt.Println("Transaction Success", " block Number", receipt.BlockNumber.Uint64(), " block txs", len(block.Transactions()), "blockhash", block.Hash().Hex())
} else if receipt.Status == types.ReceiptStatusFailed {
fmt.Println("Transaction Failed ", " Block Number", receipt.BlockNumber.Uint64())
}
}
func getAllFile(path string) (string, error) {
rd, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal("path ", err)
}
for _, fi := range rd {
if fi.IsDir() {
fmt.Printf("[%s]\n", path+"\\"+fi.Name())
getAllFile(path + fi.Name() + "\\")
return "", errors.New("path error")
} else {
fmt.Println(path, "dir has ", fi.Name(), "file")
return fi.Name(), nil
}
}
return "", err
}
func packInput(abiMethod string, params ...interface{}) []byte {
input, err := abiRelayer.Pack(abiMethod, params...)
if err != nil {
log.Fatal(abiMethod, " error ", err)
}
return input
}
func loadPrivate(ctx *cli.Context) (common.Address, *ecdsa.PrivateKey) {
key = ctx.GlobalString(KeyFlag.Name)
store = ctx.GlobalString(KeyStoreFlag.Name)
keyjson, err := ioutil.ReadFile(store)
if err != nil {
log.Fatal(fmt.Errorf("failed to read the keyfile at '%s': %v", store, err))
}
key, err := keystore.DecryptKey(keyjson, password)
if err != nil || key == nil {
log.Fatal(fmt.Errorf("error decrypting key: %v", err))
}
priKey := key.PrivateKey
from := crypto.PubkeyToAddress(priKey.PublicKey)
//fmt.Println("address ", from.Hex(), "key", hex.EncodeToString(crypto.FromECDSA(priKey)))
ctx.GlobalSet(PublicAdressFlag.Name, ctx.String(from.String()))
if priKey == nil {
log.Fatal("load privateKey failed")
}
return from, priKey
}
func dialConn() (*ethclient.Client, string) {
ip = AtlasRPCListenAddr
port = AtlasRPCPortFlag
url := fmt.Sprintf("http://%s", fmt.Sprintf("%s:%d", ip, port))
conn, err := ethclient.Dial(url)
if err != nil {
log.Fatalf("Failed to connect to the atlas chain client: %v", err)
}
return conn, url
}
func queryRegisterInfo(conn *ethclient.Client, from common.Address) (bool, bool, *big.Int, error) {
header, err := conn.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
var input []byte
input = packInput("getRelayer", from)
msg := ethchain.CallMsg{From: from, To: &RelayerAddress, Data: input}
output, err := conn.CallContract(context.Background(), msg, header.Number)
if err != nil {
log.Fatal("method CallContract error", err)
}
method, _ := abiRelayer.Methods["getRelayer"]
ret, err := method.Outputs.Unpack(output)
if len(ret) != 0 {
args := struct {
register bool
relayer bool
epoch *big.Int
}{
ret[0].(bool),
ret[1].(bool),
ret[2].(*big.Int),
}
return args.register, args.relayer, args.epoch, nil
} else {
fmt.Println("Contract query failed result len == 0")
return false, false, nil, contractQueryFailedErr
}
}
func queryIsRegister(conn *ethclient.Client, from common.Address) bool {
header, err := conn.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
var input []byte
input = packInput("getRelayer", from)
msg := ethchain.CallMsg{From: from, To: &RelayerAddress, Data: input}
output, err := conn.CallContract(context.Background(), msg, header.Number)
if err != nil {
log.Fatal("method CallContract error", err)
}
method, _ := abiRelayer.Methods["getRelayer"]
ret, err := method.Outputs.Unpack(output)
if len(ret) != 0 {
return ret[1].(bool)
} else {
return false
}
}
func queryRelayerEpoch(conn *ethclient.Client, currentNum uint64, from common.Address) bool {
header, err := conn.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
input := packInput("getPeriodHeight", from)
msg := ethchain.CallMsg{From: from, To: &RelayerAddress, Data: input}
output, err := conn.CallContract(context.Background(), msg, header.Number)
if err != nil {
log.Fatal("method CallContract error", err)
}
method, _ := abiRelayer.Methods["getPeriodHeight"]
ret, err := method.Outputs.Unpack(output)
start := ret[0].(*big.Int).Uint64()
end := ret[1].(*big.Int).Uint64()
remain := ret[2].(*big.Int).Uint64()
relayer := ret[3].(bool)
if len(ret) != 0 {
if relayer {
fmt.Println("query successfully,your account is relayer")
fmt.Println("start height in epoch: ", start)
fmt.Println("end height in epoch: ", end)
fmt.Println("remain height in epoch:", remain)
if start <= currentNum && currentNum <= end {
return true
} else {
return false
}
} else {
fmt.Println("query successfully,your account is not relayer")
return false
}
} else {
fmt.Println("Contract query failed result len == 0")
return false
}
}
func withdraw(conn *ethclient.Client, from common.Address, priKey *ecdsa.PrivateKey) error {
value := ethToWei(100000)
input := packInput("withdraw", from, value)
sendContractTransaction(conn, from, RelayerAddress, new(big.Int).SetInt64(0), priKey, input)
return nil
}
func Append(conn *ethclient.Client, from common.Address, priKey *ecdsa.PrivateKey) error {
value := ethToWei(100000)
input := packInput("append", from, value)
sendContractTransaction(conn, from, RelayerAddress, nil, priKey, input)
return nil
}