/
batch_relaying.go
105 lines (94 loc) · 2.8 KB
/
batch_relaying.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
package relayer
import (
"crypto/ecdsa"
"fmt"
"github.com/ci123chain/ci123chain/gravity-bridge/orchestrator/cosmos_gravity"
"github.com/ci123chain/ci123chain/gravity-bridge/orchestrator/ethereum_gravity"
"github.com/ci123chain/ci123chain/gravity-bridge/orchestrator/gravity_utils"
"github.com/ci123chain/ci123chain/gravity-bridge/orchestrator/gravity_utils/types"
"github.com/ci123chain/ci123chain/pkg/logger"
"github.com/ethereum/go-ethereum/crypto"
"github.com/umbracle/go-web3/jsonrpc"
"time"
)
func relayBatches(
currentValSet types.ValSet,
ethKey *ecdsa.PrivateKey,
client *jsonrpc.Client,
contact cosmos_gravity.Contact,
contractAddr, gravityId string,
timeout time.Duration,
) {
lg := logger.GetLogger()
ourEthereumAddress := crypto.PubkeyToAddress(ethKey.PublicKey)
getLatestBatches := gravity_utils.Exec(func() interface{} {
batches, err := cosmos_gravity.GetLatestTransactionBatches(contact)
if err != nil {
return err
}
return batches
}).Await()
latestBatches, ok := getLatestBatches.([]types.TransactionBatch)
if !ok {
return
}
for _, batch := range latestBatches {
getSigs := gravity_utils.Exec(func() interface{} {
sigs, err := cosmos_gravity.GetTransactionBatchSignatures(contact, batch.Nonce, batch.TokenContract)
if err != nil {
return err
}
return sigs
}).Await()
sigs, ok := getSigs.([]types.BatchConfirmResponse)
if !ok {
lg.Error(fmt.Sprintf("could not get signatures for %v:%d with %v", batch.TokenContract, batch.Nonce, sigs))
return
}
var confirm []types.Confirm
for _, v := range sigs {
confirm = append(confirm, v)
}
hash := types.EncodeTxBatchConfirmHashed(gravityId, batch)
if _, err := currentValSet.OrderSigs(hash, confirm); err != nil{
lg.Info("Batch can not be submitted yet, waiting for more signatures")
return
}
oldestSignedBatch := batch
oldestSignatures := sigs
erc20Contract := oldestSignedBatch.TokenContract
getLatestEthereumBatch := gravity_utils.Exec(func() interface{} {
latestEthereumBatch, err := ethereum_gravity.GetTxBatchNonce(contractAddr, erc20Contract, ourEthereumAddress, client)
if err != nil {
return err
}
return latestEthereumBatch
}).Await()
latestEthereumBatch, ok := getLatestEthereumBatch.(uint64)
if !ok {
lg.Error("Failed to get latest Ethereum batch")
return
}
latestCosmosBatchNonce := oldestSignedBatch.Nonce
if latestCosmosBatchNonce > latestEthereumBatch {
res := gravity_utils.Exec(func() interface{} {
err := ethereum_gravity.SendEthTransactionBatch(
currentValSet,
oldestSignedBatch,
oldestSignatures,
client,
timeout,
contractAddr,
gravityId,
ethKey)
if err != nil {
return err
}
return nil
}).Await()
if _, ok := res.(error); ok {
lg.Error("Batch submission failed")
}
}
}
}