-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
713 lines (630 loc) · 18.9 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package main
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/cenkalti/backoff"
pb "github.com/commonprefix/xrpl-relayer/axelar"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
amqp "github.com/rabbitmq/amqp091-go"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)
const CHAIN_NAME = "xrpl"
const STORAGE_FILE_PERMISSIONS = 0644
type CCID struct {
Chain string `json:"chain"`
Id string `json:"id"`
}
type Coin struct {
Amount string `json:"amount"` // todo: uint128
Denom string `json:"denom"`
}
func (c *Coin) String() string {
return fmt.Sprintf("%s%s", c.Amount, c.Denom)
}
type XRPLConstructProof struct {
MessageID CCID `json:"message_id"`
Coin Coin `json:"coin"`
}
type XRPLSigner struct {
Account string
SigningPubKey string
}
type XRPLSignerWrapper struct {
Signer XRPLSigner
}
type TXRPCResult struct {
Status string `json:"status"`
Signers []XRPLSigner
}
type TXRPCResponse struct {
Result TXRPCResult `json:"result"`
}
type PublicKey struct {
ECDSA string `json:"ecdsa"`
}
type Memo struct {
Memo MemoDetails `json:"Memo"`
}
type MemoDetails struct {
MemoType string `json:"MemoType"`
MemoData string `json:"MemoData"`
}
type XRPLTransactionInfo struct {
Meta struct {
TransactionResult string
} `json:"meta"`
Tx *XRPLTransaction `json:"tx"`
Validated bool `json:"validated"`
}
type AccountTxResponsePayload struct {
Result struct {
Marker *json.RawMessage
Transactions []XRPLTransactionInfo `json:"transactions"`
} `json:"result"`
}
type Task func(ctx context.Context) error
// forever runs the given task indefinitely until it returns an error or the context is cancelled.
func forever(ctx context.Context, task Task) error {
for {
select {
case <-ctx.Done():
return nil
default:
if err := task(ctx); err != nil {
return err
}
}
}
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errChan := make(chan error, 4)
grpcConn, err := grpc.Dial("your_server_address:port", grpc.WithInsecure())
if err != nil {
log.Fatalf("Failed to dial: %v", err)
}
defer grpcConn.Close()
axelarClient := pb.NewAmplifierClient(grpcConn)
xrplClient := XRPLClient{os.Getenv("XRPL_RPC_URL")}
rmqConn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
fmt.Println("Failed to connect to RabbitMQ")
return
}
defer rmqConn.Close()
sentinelWindowSize, err := strconv.ParseUint(os.Getenv("SENTINEL_WINDOW_SIZE"), 10, 32)
if err != nil {
fmt.Println("Invalid SENTINEL_WINDOW_SIZE", err)
return
}
sentinelSleepTimeSeconds, err := strconv.Atoi(os.Getenv("SENTINEL_SLEEP_TIME_SECONDS"))
if err != nil {
fmt.Println("Invalid SENTINEL_SLEEP_TIME_SECONDS", err)
return
}
verificationTimeoutSeconds, err := strconv.Atoi(os.Getenv("SENTINEL_VERIFICATION_TIME_SECONDS"))
if err != nil {
fmt.Println("Invalid SENTINEL_VERIFICATION_TIMEOUT_SECONDS", err)
return
}
sentinel := Sentinel{
storagePath: os.Getenv("SENTINEL_STORAGE_PATH"),
processingWindowSize: uint32(sentinelWindowSize),
xrplMultisigAddress: os.Getenv("XRPL_MULTISIG_ADDRESS"),
xrplClient: &xrplClient,
axelarClient: axelarClient,
sleepTime: time.Duration(sentinelSleepTimeSeconds) * time.Second,
verificationTimeout: time.Duration(verificationTimeoutSeconds) * time.Second,
}
sentinel.lastProcessedLedgerIndex, err = sentinel.ReadLastProcessedLedgerIndex()
if err != nil {
fmt.Println("Failed reading last processed ledger index", err)
return
}
approverChannel, err := rmqConn.Channel()
if err != nil {
fmt.Println("Failed to open a rabbitmq channel")
return
}
defer approverChannel.Close()
approver := Approver{axelarClient: axelarClient, messageQueue: approverChannel, storagePath: os.Getenv("APPROVER_STORAGE_PATH")}
approver.lastProcessedHeight, err = approver.ReadLastProcessedHeight()
if err != nil {
fmt.Println("Failed to read approver last processed height")
return
}
includerChannel, err := rmqConn.Channel()
if err != nil {
fmt.Println("Failed to open a rabbitmq channel", err)
return
}
defer includerChannel.Close()
includerMaxRetries, err := strconv.ParseUint(os.Getenv("INCLUDER_MAX_RETRIES"))
if err != nil {
fmt.Println("Invalid INCLUDER_MAX_RETRIES", err)
return
}
includer := Includer{messageQueue: includerChannel, xrplClient: &xrplClient, maxRetries: uint32(includerMaxRetries)}
broadcasterChannel, err := rmqConn.Channel()
if err != nil {
fmt.Println("Failed to open a rabbitmq channel", err)
return
}
defer broadcasterChannel.Close()
broadcasterMaxRetries, err := strconv.ParseUint(os.Getenv("BROADCASTER_MAX_RETRIES"))
if err != nil {
fmt.Println("Invalid BROADCASTER_MAX_RETRIES", err)
return
}
broadcaster := Broadcaster{axelarClient: axelarClient, messageQueue: broadcasterChannel, maxRetries: uint32(broadcasterMaxRetries)}
tasks := []Task{sentinel.ProcessEvents, approver.ProcessEvents, includer.SubmitProofs, broadcaster.ConsumeQueueAndBroadcast}
for _, task := range tasks {
go func(t Task) {
errChan <- forever(ctx, t)
}(task)
}
if err := <-errChan; err != nil {
fmt.Println("Received an error:", err)
cancel()
}
}
type Sentinel struct {
storagePath string
lastProcessedLedgerIndex uint32
processingWindowSize uint32
xrplMultisigAddress string
xrplClient *XRPLClient
axelarClient pb.AmplifierClient
sleepTime time.Duration
verificationTimeout time.Duration
}
func (s *Sentinel) ProcessEvents(ctx context.Context) error {
minLedgerIndex := s.lastProcessedLedgerIndex + 1
validatedLedger, err := s.xrplClient.Ledger(ctx, XRPLLedgerRequest{"validated", false, false, false})
if err != nil {
return err
}
validatedLedgerIndex, err := strconv.ParseUint(validatedLedger.LedgerIndex, 10, 32)
if err != nil {
return err
}
maxLedgerIndex := min(minLedgerIndex+s.processingWindowSize, uint32(validatedLedgerIndex))
var marker *json.RawMessage = nil
currentLedger := s.lastProcessedLedgerIndex + 1
processedTransactions := false
for { // for each page
// AccountTx(addr, minIndex, maxIndex) assumptions:
// 1. it will return a non-nil marker if there are more pages to fetch
// 2. it will return a nil marker if there are no more pages to fetch
// 3. across all pages, it will return all transactions between minIndex and maxIndex
// 4. it will not return unvalidated transactions if maxIndex is <= validatedIndex (according to some earlier time)
txs, marker, err := s.xrplClient.AccountTx(ctx, s.xrplMultisigAddress, minLedgerIndex, maxLedgerIndex, marker)
if err != nil {
return err
}
for _, tx := range txs {
if !tx.Tx.Validated {
panic("found unvalidated tx even if requesting only validated transactions")
}
if tx.Tx.LedgerIndex > currentLedger {
s.lastProcessedLedgerIndex = currentLedger
currentLedger = tx.Tx.LedgerIndex
err := s.PersistLastProcessedLedgerIndex()
if err != nil {
return err
}
}
// TODO: can this stream be used for multiple messages??
stream, err := s.axelarClient.Verify(ctx)
if err == nil && stream != nil {
defer stream.CloseSend()
}
if err != nil {
return err
}
err = s.TryVerifyTransaction(ctx, tx.Tx, stream)
if err != nil {
fmt.Println("Failed verifying transaction", tx.Tx.Hash, "after multiple attempts, giving up. Error:", err)
}
processedTransactions = true
}
if marker == nil {
break
}
}
if maxLedgerIndex > s.lastProcessedLedgerIndex {
s.lastProcessedLedgerIndex = maxLedgerIndex
err := s.PersistLastProcessedLedgerIndex()
if err != nil {
return err
}
}
if !processedTransactions {
time.Sleep(s.sleepTime)
}
return nil
}
// errors returned by TryVerifyTransaction are permanent errors and the failing txs will end up being skipped forever
func (s *Sentinel) TryVerifyTransaction(ctx context.Context, tx *XRPLTransaction, stream pb.Amplifier_VerifyClient) error {
message, err := s.TransactionToMessage(tx)
if err != nil {
// there is no way to recover from error in transforming transactions to messages
return err
}
expB := backoff.NewExponentialBackOff()
expB.MaxElapsedTime = s.verificationTimeout
b := backoff.WithContext(expB, ctx)
// TODO: are we supposed to have a timeout or keep trying forever?
err = backoff.Retry(func() error {
if err := stream.Send(&pb.VerifyRequest{Message: message}); err != nil {
return err
}
response, err := stream.Recv()
if err != nil {
return err
}
log.Printf("Received Verify Response: %v", response)
// TODO: permanent errors?
// docs mention an InvalidGMPRequest which is permanent error, but it's not part of proto spec
return errors.New(response.Error.Error)
}, b)
return err
}
func (s *Sentinel) PersistLastProcessedLedgerIndex() error {
return os.WriteFile(s.storagePath, []byte(strconv.Itoa(int(s.lastProcessedLedgerIndex))), STORAGE_FILE_PERMISSIONS)
}
func (s *Sentinel) ReadLastProcessedLedgerIndex() (uint32, error) {
value, err := os.ReadFile(s.storagePath)
if err != nil {
return 0, err
}
height, err := strconv.ParseUint(string(value), 10, 32)
if err != nil {
return 0, err
}
return uint32(height), nil
}
func (s *Sentinel) TransactionToMessage(tx *XRPLTransaction) (*pb.Message, error) {
// TODO: this whole function needs to be rewritten when we know ITS Hub specifics
destinationChain, err := tx.GetMemo("destination_chain")
if err != nil {
return nil, err
}
destinationAddress, err := tx.GetMemo("destination_address")
if err != nil {
return nil, err
}
payloadHash, err := tx.GetMemo("payload_hash")
if err != nil {
return nil, err
}
var amount, tokenId string
switch amt := tx.Amount.(type) {
case string:
// XRP
amount = amt
tokenId = "uxrp"
case map[string]interface{}:
// Token
amount = amt["value"].(string)
tokenId = amt["currency"].(string)
default:
panic("Unexpected amount type")
}
payload, err := ABIEncode([]string{"string", "string", "bytes32"}, []interface{}{amount, tokenId, common.HexToHash(payloadHash)})
if err != nil {
return nil, err
}
msg := pb.Message{
Id: tx.Hash,
SourceChain: CHAIN_NAME,
SourceAddress: tx.Account,
DestinationChain: destinationChain,
DestinationAddress: destinationAddress,
Payload: payload,
}
return &msg, nil
}
func ABIEncode(types []string, values []interface{}) ([]byte, error) {
arguments := abi.Arguments{}
for _, t := range types {
ty, err := abi.NewType(t, "", nil)
if err != nil {
panic(err)
}
arguments = append(arguments, abi.Argument{Type: ty})
}
return arguments.Pack(values...)
}
type Approver struct {
lastProcessedHeight uint64
axelarClient pb.AmplifierClient
messageQueue *amqp.Channel
storagePath string
}
func (a *Approver) ProcessEvents(ctx context.Context) error {
req := &pb.SubscribeToApprovalsRequest{Chains: []string{CHAIN_NAME}, StartHeight: &a.lastProcessedHeight}
stream, err := a.axelarClient.SubscribeToApprovals(ctx, req)
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return nil
default:
// continue
}
resp, err := stream.Recv()
if err != nil {
return err
}
err = a.messageQueue.PublishWithContext(ctx, "", "includer", false, false, amqp.Publishing{
ContentType: "application/octet-stream",
Body: resp.ExecuteData,
})
if err != nil {
return err
}
if resp.BlockHeight > a.lastProcessedHeight {
a.lastProcessedHeight = resp.BlockHeight
err = a.PersistLastProcessedHeight()
if err != nil {
return err
}
}
}
}
func (a *Approver) ReadLastProcessedHeight() (uint64, error) {
value, err := os.ReadFile(a.storagePath)
if err != nil {
return 0, err
}
return strconv.ParseUint(string(value), 10, 64)
}
func (a *Approver) PersistLastProcessedHeight() error {
return os.WriteFile(a.storagePath, []byte(strconv.Itoa(int(a.lastProcessedHeight))), STORAGE_FILE_PERMISSIONS)
}
type Broadcaster struct {
axelarClient pb.AmplifierClient
messageQueue *amqp.Channel
maxRetries uint32
}
func (b *Broadcaster) ConsumeQueueAndBroadcast(ctx context.Context) error {
deliveries, err := b.messageQueue.ConsumeWithContext(ctx, "broadcast", "broadcaster", false, true, false, false, amqp.Table{})
if err != nil {
return err
}
for delivery := range deliveries {
var req pb.BroadcastRequest
err := proto.Unmarshal(delivery.Body, &req)
if err != nil {
return err
}
res, err := b.axelarClient.Broadcast(ctx, &req)
if err == nil && res.Success {
delivery.Ack(false)
} else {
log.Println("Failed to broadcast: ", err, res)
nackWithLimit(b.messageQueue, delivery, b.maxRetries)
}
}
return nil
}
type Includer struct {
messageQueue *amqp.Channel
xrplClient *XRPLClient
maxRetries uint32
}
func (i *Includer) SubmitProofs(ctx context.Context) error {
deliveries, err := i.messageQueue.ConsumeWithContext(ctx, "inclusion", "includer", false, true, false, false, amqp.Table{})
if err != nil {
return err
}
for delivery := range deliveries {
proof := delivery.Body
res, err := i.xrplClient.Submit(ctx, proof)
if err != nil && res.EngineResult == "tesSUCCESS" {
delivery.Ack(false)
} else {
log.Println("Failed to submit transaction", proof, err, res.EngineResult, res.EngineResultCode, res.EngineResultMessage)
nackWithLimit(i.messageQueue, delivery, i.maxRetries) // TODO: requeue how many times?
}
}
return nil
}
type XRPLClient struct {
RPCUrl string
}
func (c *XRPLClient) Tx(ctx context.Context, hash string) (XRPLTransaction, error) {
payload := map[string]interface{}{
"method": "tx",
"params": []map[string]string{
{
"transaction": hash,
},
},
}
var data struct {
Result XRPLTransaction `json:"result"`
}
err := JsonRPCRequest(ctx, c.RPCUrl, payload, &data)
if err == nil && data.Result.Status != "success" {
return data.Result, fmt.Errorf("failed to fetch transaction %s: %s", hash, data.Result.ErrorMessage)
}
return data.Result, err
}
type XRPLSubmitResult struct {
EngineResult string `json:"engine_result"`
// TODO: documentation specifies it as "signed integer" but not how many bits
EngineResultCode int64 `json:"engine_result_code"`
EngineResultMessage string `json:"engine_result_message"`
// TXJson struct {} `json:"tx_json"`
}
type XRPLLedgerRequest struct {
LedgerIndex string `json:"ledger_index"`
Transactions bool `json:"transactions"`
Expand bool `json:"expand"`
OwnerFunds bool `json:"owner_funds"`
}
type XRPLLedger struct {
LedgerHash string `json:"ledger_hash"`
// string type not a mistake, this is what the RPC returns...
LedgerIndex string `json:"ledger_index"`
ParentHash string `json:"parent_hash"`
}
func (c *XRPLClient) Ledger(ctx context.Context, params XRPLLedgerRequest) (XRPLLedger, error) {
payload := map[string]interface{}{
"method": "ledger",
"params": []XRPLLedgerRequest{params},
}
var data struct {
Result struct {
Ledger XRPLLedger `json:"ledger"`
} `json:"result"`
}
err := JsonRPCRequest(ctx, c.RPCUrl, payload, &data)
return data.Result.Ledger, err
}
func (c *XRPLClient) Submit(ctx context.Context, blob []byte) (XRPLSubmitResult, error) {
payload := map[string]interface{}{
"method": "submit",
"params": []map[string]string{
{
"tx_blob": string(blob),
},
},
}
var data struct {
Result XRPLSubmitResult `json:"result"`
}
err := JsonRPCRequest(ctx, c.RPCUrl, payload, &data)
return data.Result, err
}
func (c *XRPLClient) AccountTx(ctx context.Context, account string, ledgerIndexMin uint32, ledgerIndexMax uint32, marker *json.RawMessage) ([]XRPLTransactionInfo, *json.RawMessage, error) {
params := map[string]interface{}{
"account": account,
"ledger_index_min": ledgerIndexMin,
"ledger_index_max": ledgerIndexMax,
}
if marker != nil {
params["marker"] = marker
}
payload := map[string]interface{}{
"method": "account_tx",
"params": []map[string]interface{}{params},
}
var responseData AccountTxResponsePayload
err := JsonRPCRequest(ctx, c.RPCUrl, payload, &responseData)
if err != nil {
return []XRPLTransactionInfo{}, nil, err
}
return responseData.Result.Transactions, responseData.Result.Marker, nil
}
func JsonRPCRequest(ctx context.Context, url string, payload interface{}, responseData interface{}) error {
jsonData, err := json.Marshal(payload)
if err != nil {
panic(err)
}
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return json.Unmarshal(body, responseData)
}
type XRPLTransaction struct {
TransactionType string
Hash string `json:"hash"`
Account string
Amount interface{} `json:"Amount"`
Destination string
Memos []Memo `json:"Memos,omitempty"`
Fee string
Flags int64
Sequence int64
Validated bool `json:"validated"`
Signers []XRPLSignerWrapper
Meta struct{ TransactionResult string } `json:"meta"`
Status string `json:"status"`
ErrorMessage string `json:"error_message,omitempty"`
LedgerIndex uint32 `json:"LedgerIndex"`
}
func (tx *XRPLTransaction) SignerPubKeys() []PublicKey {
pubKeys := make([]PublicKey, len(tx.Signers))
for i, signer := range tx.Signers {
pubKeys[i] = PublicKey{signer.Signer.SigningPubKey}
}
return pubKeys
}
func (tx *XRPLTransaction) GetMemo(byType string) (string, error) {
for _, memo := range tx.Memos {
memoTypeBytes, err := hex.DecodeString(memo.Memo.MemoType)
if err != nil {
fmt.Println("Error decoding memo type:", err)
continue
}
memoType := string(memoTypeBytes)
if memoType != byType {
continue
}
if byType == "destination_address" || byType == "payload_hash" {
return memo.Memo.MemoData, nil
} else {
memoDataBytes, err := hex.DecodeString(memo.Memo.MemoData)
if err != nil {
fmt.Println("Error decoding memo data:", err)
return "", err
}
memoData := string(memoDataBytes)
return memoData, nil
}
}
return "", fmt.Errorf("memo type %s not found", byType)
}
func nackWithLimit(channel *amqp.Channel, msg amqp.Delivery, maxRetries uint32) {
retryCountHeader := msg.Headers["x-retry-count"]
retryCount, ok := retryCountHeader.(uint32)
if !ok {
retryCount = 0
}
if retryCount < maxRetries {
retryCount++
msg.Headers["x-retry-count"] = retryCount
err := msg.Nack(false, true)
if err != nil {
log.Printf("Failed to nack and requeue message: %s", err)
} else {
log.Printf("Message requeued, attempt %d", retryCount)
}
} else {
log.Printf("Max retries exceeded for message, sending to DLX or handling failure")
err := msg.Nack(false, false)
if err != nil {
log.Printf("Failed to nack message: %s", err)
}
}
}