forked from VolumeFi/pigeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
438 lines (368 loc) · 10.7 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"math/big"
"os"
"sort"
"strconv"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/palomachain/concord/config"
"github.com/palomachain/concord/types"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const cMaxPower = 1 << 32
func main() {
log.SetOutput(os.Stdout)
if printVersion() {
return
}
msgId, cfgPath, url, err := parseArgs()
if err != nil {
log.Fatalf("😿 %v", err)
}
cfg := getConfig(cfgPath)
ethCfg := getEthConfig(cfg)
signer := newSigner(ethCfg)
client, err := ethclient.Dial(url)
if err != nil {
log.Fatalf("Failed to dial to eth RPC: %v", err)
}
abi, err := loadABI()
if err != nil {
log.Fatalf("Failed to load ABI: %v", err)
}
valset, err := parseValset()
if err != nil {
log.Fatalf("Failed to load valset: %v", err)
}
msg, err := getMsg(msgId)
if err != nil {
log.Fatalf("failed to read message: %v", err)
}
tx, err := submitLogicCall(msg, valset, client, signer, abi)
if err != nil {
log.Fatalf("failed to relay message: %v", err)
}
log.Printf("🎉 Message relayed, tx: %v\n", tx.Hash())
}
func getStore(id uint64) (*leveldb.DB, error) {
path := fmt.Sprintf("./data/%d.db", id)
if stat, err := os.Stat(path); err != nil || !stat.IsDir() {
return nil, err
}
return leveldb.OpenFile(path, &opt.Options{
NoSync: true,
ReadOnly: true,
})
}
func parseValset() (*types.Valset, error) {
fi, err := os.Open("./data/valset.json")
if err != nil {
return nil, fmt.Errorf("failed to open valset file: %w", err)
}
defer fi.Close()
bz, err := io.ReadAll(fi)
if err != nil {
return nil, fmt.Errorf("failed to read valset file: %w", err)
}
var v types.Valset
if err := json.Unmarshal(bz, &v); err != nil {
return nil, fmt.Errorf("failed to parse valset: %w", err)
}
sort.SliceStable(v.Snapshot.Validators, func(i, j int) bool {
// doing GTE because we want a reverse sort
return v.Snapshot.Validators[i].ShareCount >= v.Snapshot.Validators[j].ShareCount
})
return &v, nil
}
func getMsg(id uint64) (types.MessageWithSignatures, error) {
store, err := getStore(id)
if err != nil {
return types.MessageWithSignatures{}, fmt.Errorf("failed to open store: %w", err)
}
defer store.Close()
msgBz, err := store.Get([]byte("msg"), nil)
if err != nil {
return types.MessageWithSignatures{}, fmt.Errorf("failed to get message from store: %w", err)
}
var msg types.QueuedMessage
if err := bson.Unmarshal(msgBz, &msg); err != nil {
return types.MessageWithSignatures{}, fmt.Errorf("failed to unmarshal serialized message: %w", err)
}
mws := types.MessageWithSignatures{QueuedMessage: msg, Signatures: make([]types.ValidatorSignature, 0, 64)}
iter := store.NewIterator(nil, nil)
for iter.Next() {
if string(iter.Key()) == "msg" {
continue
}
addr := ethcommon.BytesToAddress(iter.Key())
mws.Signatures = append(mws.Signatures, types.ValidatorSignature{
Signature: iter.Value(),
SignedByAddress: addr.Hex(),
})
}
return mws, nil
}
func submitLogicCall(
msg types.MessageWithSignatures,
valset *types.Valset,
client *ethclient.Client,
signer *Signer,
abi abi.ABI,
) (*ethtypes.Transaction, error) {
d := msg.Msg.(primitive.D)
compass := (d.Map()["compassaddr"]).(string)
con := buildCompassConsensus(valset, msg.Signatures)
compassArgs := types.CompassLogicCallArgs{
LogicContractAddress: ethcommon.HexToAddress(compass),
Payload: msg.BytesToSign,
}
args := []any{
con,
compassArgs,
new(big.Int).SetInt64(int64(msg.ID)),
new(big.Int).SetInt64(time.Now().UTC().Add(time.Minute * 10).Unix()),
}
compassAddr := ethcommon.HexToAddress(compass)
tx, err := callSmartContract("submit_logic_call", args, abi, client, signer.addr, compassAddr, signer.keystore)
if err != nil {
return nil, fmt.Errorf("failed to call compass: %w", err)
}
return tx, nil
}
func buildCompassConsensus(v *types.Valset, signatures []types.ValidatorSignature) types.CompassConsensus {
signatureMap := make(map[string]types.ValidatorSignature)
for _, v := range signatures {
signatureMap[v.SignedByAddress] = v
}
con := types.CompassConsensus{
Valset: transformValsetToCompassValset(v),
}
for i := range v.Snapshot.Validators {
sig, ok := signatureMap[v.Snapshot.Validators[i].Address]
if !ok {
con.Signatures = append(con.Signatures,
types.Signature{
V: big.NewInt(0),
R: big.NewInt(0),
S: big.NewInt(0),
})
} else {
con.Signatures = append(con.Signatures,
types.Signature{
V: new(big.Int).SetInt64(int64(sig.Signature[64]) + 27),
R: new(big.Int).SetBytes(sig.Signature[:32]),
S: new(big.Int).SetBytes(sig.Signature[32:64]),
},
)
}
}
return con
}
func transformValsetToCompassValset(val *types.Valset) types.CompassValset {
var cs types.CompassValset
var totalPower int64 = 0
for _, val := range val.Snapshot.Validators {
totalPower += int64(val.ShareCount)
}
for _, v := range val.Snapshot.Validators {
if v.State != "ACTIVE" {
continue
}
for _, c := range v.ExternalChainInfos {
if c.ChainReferenceID != "eth-main" {
continue
}
cs.Validators = append(cs.Validators, ethcommon.HexToAddress(c.Address))
break
}
power := cMaxPower * (float64(v.ShareCount) / float64(totalPower))
cs.Powers = append(cs.Powers, big.NewInt(int64(power)))
}
cs.ValsetId = big.NewInt(int64(val.Snapshot.Id))
if len(cs.Validators) != len(cs.Powers) {
panic("validator power mismatch")
}
return cs
}
func printVersion() bool {
if len(os.Args) < 2 || os.Args[1] != "version" {
return false
}
fmt.Printf("Relay\nVersion: %s\nCommit: %s\n", config.Version(), config.Commit())
return true
}
func parseArgs() (uint64, string, string, error) {
if len(os.Args) != 4 {
return 0, "", "", fmt.Errorf("expected exactly 3 argument [msgId] [path-to-pigeon-config] [eth-rpc-url]")
}
id, err := strconv.ParseUint(os.Args[1], 10, 64)
return id, os.Args[2], os.Args[3], err
}
func callSmartContract(
method string,
args []any,
abi abi.ABI,
client *ethclient.Client,
sender ethcommon.Address,
contract ethcommon.Address,
keystore *keystore.KeyStore,
) (*ethtypes.Transaction, error) {
packedBytes, err := abi.Pack(
method,
args...,
)
if err != nil {
return nil, fmt.Errorf("failed to pack ABI: %w", err)
}
nonce, err := client.PendingNonceAt(context.Background(), sender)
if err != nil {
return nil, fmt.Errorf("failed to get pending nonce: %w", err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to suggest gas price: %w", err)
}
// adapt for eth-main
gasAdj := big.NewFloat(2)
gasAdj = gasAdj.Mul(gasAdj, new(big.Float).SetInt(gasPrice))
gasPrice, _ = gasAdj.Int(big.NewInt(0))
var gasTipCap *big.Int
gasPrice = gasPrice.Mul(gasPrice, big.NewInt(2)) // double gas price for EIP-1559 tx
gasTipCap, err = client.SuggestGasTipCap(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to suggest gas tip cap: %w", err)
}
gasPrice = gasPrice.Add(gasPrice, gasTipCap)
boundContract := bind.NewBoundContract(
contract,
abi,
client,
client,
client,
)
txOpts, err := bind.NewKeyStoreTransactorWithChainID(
keystore,
accounts.Account{Address: sender},
big.NewInt(1),
)
if err != nil {
return nil, fmt.Errorf("failed to setup keystore transactor: %w", err)
}
txOpts.Nonce = big.NewInt(int64(nonce))
txOpts.From = sender
value := new(big.Int)
gasFeeCap := new(big.Int)
var gasLimit uint64
gasFeeCap = gasPrice
gasLimit, err = estimateGasLimit(context.Background(), client, txOpts, &contract, packedBytes, nil, gasTipCap, gasFeeCap, value)
if err != nil {
return nil, fmt.Errorf("failed to estimate gas limit: %w", err)
}
txOpts.GasLimit = uint64(float64(gasLimit) * 1.2)
txOpts.GasFeeCap = gasPrice
txOpts.GasTipCap = gasTipCap
txOpts.GasPrice = nil
tx, err := boundContract.RawTransact(txOpts, packedBytes)
if err != nil {
return nil, fmt.Errorf("failed to raw transact: %w", err)
}
return tx, nil
}
func loadABI() (abi.ABI, error) {
fi, err := os.Open("./data/abi.json")
if err != nil {
return abi.ABI{}, fmt.Errorf("failed to open ABI: %v", err)
}
aabi, err := abi.JSON(fi)
if err != nil {
return abi.ABI{}, fmt.Errorf("failed to parse ABI: %v", err)
}
return aabi, err
}
func estimateGasLimit(ctx context.Context, c *ethclient.Client, opts *bind.TransactOpts, contract *ethcommon.Address, input []byte, gasPrice, gasTipCap, gasFeeCap, value *big.Int) (uint64, error) {
if contract != nil {
// Gas estimation cannot succeed without code for method invocations.
if code, err := c.PendingCodeAt(ctx, *contract); err != nil {
return 0, err
} else if len(code) == 0 {
return 0, bind.ErrNoCode
}
}
msg := ethereum.CallMsg{
From: opts.From,
To: contract,
GasPrice: gasPrice,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Value: value,
Data: input,
}
return c.EstimateGas(ctx, msg)
}
type Signer struct {
addr ethcommon.Address
keystore *keystore.KeyStore
}
func newSigner(cfg *config.EVM) *Signer {
s := &Signer{}
if !ethcommon.IsHexAddress(cfg.SigningKey) {
panic("invalid signing address")
}
s.addr = ethcommon.HexToAddress(cfg.SigningKey)
s.keystore = keystore.NewKeyStore(cfg.KeyringDirectory.Path(), keystore.StandardScryptN, keystore.StandardScryptP)
if !s.keystore.HasAddress(s.addr) {
panic("address not found in keystore")
}
acc := accounts.Account{Address: s.addr}
if err := s.keystore.Unlock(acc, config.KeyringPassword(cfg.KeyringPassEnvName)); err != nil {
panic(fmt.Sprintf("failed to unlock account with keystore: %v", err))
}
return s
}
func getConfig(path string) *config.Config {
path = config.Filepath(path).Path()
fi, err := os.Stat(path)
if err != nil {
panic(fmt.Sprintf("couldn't find config file: %v", err))
}
if fi.IsDir() {
panic("path must be a file, not a directory")
}
file, err := os.Open(path)
if err != nil {
panic(fmt.Sprintf("failed to open config file: %v", err))
}
defer file.Close()
cnf, err := config.FromReader(file)
if err != nil {
panic(fmt.Sprintf("couldn't read config file: %v", err))
}
if len(cnf.Paloma.ValidatorKey) < 1 {
cnf.Paloma.ValidatorKey = cnf.Paloma.SigningKey
}
return cnf
}
func getEthConfig(cfg *config.Config) *config.EVM {
for k, v := range cfg.EVM {
if k == "eth-main" {
return &v
}
}
panic("no configuration for eth-main found.")
}