-
Notifications
You must be signed in to change notification settings - Fork 2
/
toEth.go
176 lines (162 loc) · 4.97 KB
/
toEth.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
/*
Copyright [2019] - [2021], PERSISTENCE TECHNOLOGIES PTE. LTD. and the persistenceBridge contributors
SPDX-License-Identifier: Apache-2.0
*/
package handler
import (
"encoding/json"
"errors"
"github.com/Shopify/sarama"
"github.com/persistenceOne/persistenceBridge/application/configuration"
"github.com/persistenceOne/persistenceBridge/application/db"
"github.com/persistenceOne/persistenceBridge/application/outgoingTx"
"github.com/persistenceOne/persistenceBridge/kafka/utils"
"github.com/persistenceOne/persistenceBridge/utilities/logging"
"time"
)
func (m MsgHandler) HandleToEth(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
var kafkaMsgs []sarama.ConsumerMessage
claimMsgChan := claim.Messages()
ticker := time.NewTicker(configuration.GetAppConfig().Kafka.ToEth.Ticker)
defer ticker.Stop()
var kafkaMsg *sarama.ConsumerMessage
var ok bool
ConsumerLoop:
for {
select {
case <-ticker.C:
if len(kafkaMsgs) >= configuration.GetAppConfig().Kafka.ToEth.MinBatchSize {
break ConsumerLoop
} else {
return nil
}
case kafkaMsg, ok = <-claimMsgChan:
if ok {
kafkaMsgs = append(kafkaMsgs, *kafkaMsg)
if len(kafkaMsgs) == configuration.GetAppConfig().Kafka.ToEth.MaxBatchSize {
break ConsumerLoop
}
} else {
break ConsumerLoop
}
}
}
if len(kafkaMsgs) == 0 {
return nil
}
if kafkaMsg == nil {
return errors.New("kafka returned nil message")
}
// 1.add to database
var msgBytes [][]byte
for _, msg := range kafkaMsgs {
msgBytes = append(msgBytes, msg.Value)
}
index, err := db.AddKafkaEthereumConsume(kafkaMsg.Offset, msgBytes)
if err != nil {
return err
}
// 2.set kafka offset so all next steps are independent of kafka consumer queue.
session.MarkMessage(kafkaMsg, "")
err = SendBatchToEth(index, m)
if err != nil {
return err
}
return nil
}
func convertMsgBytesToEthMsg(msgBytes [][]byte) ([]db.WrapTokenMsg, error) {
msgs := make([]db.WrapTokenMsg, len(msgBytes))
for i, msgByte := range msgBytes {
var msg db.WrapTokenMsg
err := json.Unmarshal(msgByte, &msg)
if err != nil {
return nil, err
}
msgs[i] = msg
}
return msgs, nil
}
// SendBatchToEth : Handling of msgSend
func SendBatchToEth(index uint64, handler MsgHandler) error {
kafkaConsume, err := db.GetKafkaEthereumConsume(index)
if err != nil {
logging.Fatal(err)
}
msgs, err := convertMsgBytesToEthMsg(kafkaConsume.MsgBytes)
if err != nil {
logging.Fatal(err)
}
logging.Info("batched messages to send to ETH:", msgs)
hash, err := outgoingTx.EthereumWrapAndStakeToken(handler.EthClient, msgs)
if err != nil {
logging.Error("Unable to do ethereum tx (adding messages again to kafka), messages:", msgs, "error:", err)
config := utils.SaramaConfig()
producer := utils.NewProducer(configuration.GetAppConfig().Kafka.Brokers, config)
defer func() {
err := producer.Close()
if err != nil {
logging.Error("failed to close producer in topic: SendBatchToEth, err:", err)
}
}()
err = db.DeleteKafkaEthereumConsume(index)
if err != nil {
logging.Error("Failed to delete Ethereum msg at index: ", index, " Error: ", err)
}
for i, msg := range msgs {
msgBytes, err := json.Marshal(msg)
if err != nil {
logging.Error("Failed to Marshal an unmarshalled msg")
}
err = utils.ProducerDeliverMessage(msgBytes, utils.ToEth, producer)
if err != nil {
logging.Error("Failed to add msg to kafka ToEth queue (need to do manually), message:", msgs[i], "error:", err)
}
}
return err
}
err = db.UpdateKafkaEthereumConsumeTxHash(index, hash)
if err != nil {
logging.Fatal(err)
}
err = db.SetOutgoingEthereumTx(db.NewOutgoingETHTransaction(hash, msgs))
if err != nil {
logging.Fatal(err)
}
logging.Info("Broadcast Eth Tx hash:", hash.String())
checkKafkaEthereumConsumeDBAndAddToRetry()
return nil
}
func checkKafkaEthereumConsumeDBAndAddToRetry() {
//all logging, no return
kafkaEthereumConsumes, err := db.GetEmptyTxHashesETH()
if err != nil {
logging.Error(err)
}
if len(kafkaEthereumConsumes) > 0 {
config := utils.SaramaConfig()
producer := utils.NewProducer(configuration.GetAppConfig().Kafka.Brokers, config)
defer func() {
err := producer.Close()
if err != nil {
logging.Error("failed to close producer in topic: SendBatchToEth, err:", err)
}
}()
for _, kafkaEthereumConsume := range kafkaEthereumConsumes {
err = db.DeleteKafkaEthereumConsume(kafkaEthereumConsume.Index)
if err != nil {
logging.Error("Failed to delete Ethereum msg at index: ", kafkaEthereumConsume.Index, " Error: ", err)
}
for _, msgByte := range kafkaEthereumConsume.MsgBytes {
err = utils.ProducerDeliverMessage(msgByte, utils.ToEth, producer)
if err != nil {
var msg db.WrapTokenMsg
err2 := json.Unmarshal(msgByte, &msg)
if err2 != nil {
logging.Error("Failed to Unmarshal Retry ToEth queue msg", "error:", err)
}
logging.Error("Failed to add msg to kafka ToEth queue (need to do manually), message:", msg, "error:", err)
}
}
}
}
}