-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
371 lines (311 loc) · 11.3 KB
/
listener.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
// Copyright 2020 EMIT Foundation.
// This file is part of E.M.I.T. .
// E.M.I.T. is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// E.M.I.T. is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with E.M.I.T. . If not, see <http://www.gnu.org/licenses/>.
package sero
import (
"context"
"errors"
"fmt"
"github.com/emit-technology/emit-cross/common"
"github.com/emit-technology/emit-cross/core"
log "github.com/emit-technology/emit-cross/log"
metrics "github.com/emit-technology/emit-cross/metrics/types"
"github.com/emit-technology/emit-cross/types"
"math/big"
"time"
"github.com/sero-cash/go-sero"
seroCommon "github.com/sero-cash/go-sero/common"
"github.com/emit-technology/emit-cross/chains"
utils "github.com/emit-technology/emit-cross/shared/sero"
)
var BlockRetryInterval = time.Second * 10
var WatchDuration = 60 * 30 // 30 Minute
type listener struct {
cfg Config
chainDB *chains.ChainDB
conn Connection
writer *writer
log log.Logger
stop <-chan int
sysErr chan<- error // Reports fatal error to core
latestBlock metrics.LatestBlock
metrics *metrics.ChainMetrics
blockConfirmations *big.Int
state map[types.ChainId]core.ProposalState
}
// NewListener creates and returns a listener
func NewListener(conn Connection, chainDB *chains.ChainDB, cfg *Config, log log.Logger, stop <-chan int, sysErr chan<- error, m *metrics.ChainMetrics) *listener {
return &listener{
cfg: *cfg,
chainDB: chainDB,
conn: conn,
log: log,
stop: stop,
sysErr: sysErr,
latestBlock: metrics.LatestBlock{LastUpdated: time.Now()},
metrics: m,
blockConfirmations: cfg.blockConfirmations,
}
}
func (l *listener) setWriter(w *writer) {
l.writer = w
}
// start registers all subscriptions provided by the config
func (l *listener) start() error {
l.log.Debug("Starting listener...")
go func() {
err := l.pollBlocks()
if err != nil {
l.log.Error("Polling blocks failed", "err", err)
}
}()
go func() {
err := l.signDestProposal()
if err != nil {
l.log.Error("start signDestProposal failed", "err", err)
}
}()
go func() {
err := l.voteProposal()
if err != nil {
l.log.Error("start voteProposal failed", "err", err)
}
}()
if l.cfg.commitNode {
go func() {
err := l.excueteProposal()
if err != nil {
l.log.Error("start excueteProposal failed", "err", err)
}
}()
}
return nil
}
// pollBlocks will poll for the latest block and proceed to parse the associated events as it sees new blocks.
// Polling begins at the block defined in `l.cfg.startBlock`. Failed attempts to fetch the latest block or parse
// a block will be retried up to BlockRetryLimit times before continuing to the next block.
func (l *listener) pollBlocks() error {
var currentBlock = l.cfg.startBlock
l.log.Info("Polling Blocks...", "startBlock", currentBlock.String())
for {
select {
case <-l.stop:
return errors.New("polling terminated")
default:
latestBlock, err := l.conn.LatestBlock()
if err != nil {
l.log.Error("Unable to get latest block", "block", currentBlock, "err", err)
time.Sleep(BlockRetryInterval)
continue
}
if l.metrics != nil {
l.metrics.LatestKnownBlock.Set(float64(latestBlock.Int64()))
}
// Sleep if the difference is less than BlockDelay; (latest - current) < BlockDelay
if big.NewInt(0).Sub(latestBlock, currentBlock).Cmp(l.blockConfirmations) == -1 {
l.log.Debug("Block not ready, will retry", "target", currentBlock, "latest", latestBlock)
time.Sleep(BlockRetryInterval)
continue
}
if currentBlock.Uint64()%100 == 10 {
l.log.Info("Polling Blocks...", "currentBlock", currentBlock.String())
}
err = l.getConcernedEventsForBlock(currentBlock)
if err != nil {
l.log.Error("Failed to get events for block", "block", currentBlock, "err", err)
continue
}
if l.metrics != nil {
l.metrics.BlocksProcessed.Inc()
l.metrics.LatestProcessedBlock.Set(float64(latestBlock.Int64()))
}
l.latestBlock.Height = big.NewInt(0).Set(latestBlock)
l.latestBlock.LastUpdated = time.Now()
// Goto next block and reset retry counter
currentBlock.Add(currentBlock, big.NewInt(1))
err = l.chainDB.UpdateNextPollBlockNum(uint8(l.cfg.id), common.GenCommonAddress(l.conn.Keypair()).String(), currentBlock.Uint64())
if err != nil {
l.log.Error("Failed to write latest block to blockstore", "block", currentBlock, "err", err)
}
}
}
}
// getConcernedEventsForBlock looks for the deposit event and signature envent in the latest block
func (l *listener) getConcernedEventsForBlock(latestBlock *big.Int) error {
l.log.Debug("Querying block for deposit events", "block", latestBlock)
query := buildQuery([]seroCommon.Address{l.cfg.bridgeContract, l.cfg.nftBridgeContract, l.cfg.signatureColletorContact},
latestBlock, latestBlock, utils.Deposit, utils.ProposalEvent, utils.SignProposalEvent)
// querying for logs
logs, err := l.conn.Client().FilterLogs(context.Background(), query)
if err != nil {
return fmt.Errorf("unable to Filter Logs: %w", err)
}
signProposalReqMsgs := []types.TransferMsg{}
voteProposalReqMsgs := []types.TransferMsg{}
proposalExcuteMsgs := []types.TransferMsg{}
commitSingaturesMsgs := []types.ProposalSignatures{}
// read through the log events and handle their deposit event if Handler is recognized
for _, log := range logs {
if log.Address == l.cfg.bridgeContract || log.Address == l.cfg.nftBridgeContract {
if log.Topics[0] == utils.Deposit.GetTopic() {
destId := types.ChainId(log.Topics[1].Big().Uint64())
nonce := types.Nonce(log.Topics[3].Big().Uint64())
var m types.TransferMsg
var err error
if log.Address == l.cfg.bridgeContract {
m, err = l.handleSrc20DepositedEvent(latestBlock.Uint64(), destId, nonce)
} else {
m, err = l.handleSrc721DepositedEvent(latestBlock.Uint64(), destId, nonce)
}
if err != nil {
return err
}
if l.state[m.DestinationId].IsWithCollector() {
signProposalReqMsgs = append(signProposalReqMsgs, m)
} else {
voteProposalReqMsgs = append(voteProposalReqMsgs, m)
}
}
if log.Topics[0] == utils.ProposalEvent.GetTopic() {
sourceId := log.Topics[1].Big().Uint64()
depositNonce := log.Topics[2].Big().Uint64()
status := log.Topics[3].Big().Uint64()
if utils.IsPassed(uint8(status)) {
var m types.TransferMsg
var err error
if log.Address == l.cfg.bridgeContract {
m, err = l.handleSrc20ProposalEvent(latestBlock.Uint64(), types.ChainId(sourceId), types.Nonce(depositNonce))
} else {
m, err = l.handleSrc721ProposalEvent(latestBlock.Uint64(), types.ChainId(sourceId), types.Nonce(depositNonce))
}
if err != nil {
return err
}
proposalExcuteMsgs = append(proposalExcuteMsgs, m)
}
}
}
if log.Address == l.cfg.signatureColletorContact {
sourceId := log.Topics[1].Big().Uint64()
destId := log.Topics[2].Big().Uint64()
depositNonce := log.Topics[3].Big().Uint64()
transferType := new(big.Int).SetBytes(log.Data[:32]).Uint64()
status := new(big.Int).SetBytes(log.Data[32:]).Uint64()
if utils.IsPassed(uint8(status)) {
var m types.ProposalSignatures
var err error
if transferType == 1 {
_, m, err = l.handleDestFungibleProposalSignEvent(latestBlock.Uint64(), types.ChainId(sourceId), types.ChainId(destId), types.Nonce(depositNonce))
} else {
_, m, err = l.handleDestNonFungibleProposalSignEvent(latestBlock.Uint64(), types.ChainId(sourceId), types.ChainId(destId), types.Nonce(depositNonce))
}
if err != nil {
return err
}
commitSingaturesMsgs = append(commitSingaturesMsgs, m)
}
}
if len(signProposalReqMsgs) > 0 {
for _, m := range signProposalReqMsgs {
err = l.chainDB.AddSignReq(m)
if err != nil {
l.log.Error("chainDB: failed to add sign req", "err", err)
return err
}
}
}
if len(voteProposalReqMsgs) > 0 {
for _, m := range voteProposalReqMsgs {
err = l.chainDB.AddProposalMsg(m)
if err != nil {
l.log.Error("chainDB: failed to add vote req", "err", err)
return err
}
}
}
if len(proposalExcuteMsgs) > 0 {
for _, m := range proposalExcuteMsgs {
err = l.chainDB.AddExecuteMsg(m)
if err != nil {
l.log.Error("chainDB: failed to add ExecuteMsg", "err", err)
return err
}
}
}
if len(commitSingaturesMsgs) > 0 {
for _, m := range commitSingaturesMsgs {
err = l.chainDB.AddBatchVotesMsg(m)
if err != nil {
l.log.Error("chainDB: failed to add batchVotesMsg", "err", err)
return err
}
}
}
}
return nil
}
// buildQuery constructs a query for the bridgeContract by hashing sig to get the event topic
func buildQuery(contracts []seroCommon.Address, startBlock *big.Int, endBlock *big.Int, sigs ...utils.EventSig) sero.FilterQuery {
topics := []seroCommon.Hash{}
for _, sig := range sigs {
topics = append(topics, sig.GetTopic())
}
query := sero.FilterQuery{
FromBlock: startBlock,
ToBlock: endBlock,
Addresses: contracts,
Topics: [][]seroCommon.Hash{
topics,
},
}
return query
}
// proposalIsComplete returns true if the proposal state is either Passed, Transferred or Cancelled
func (l *listener) DestProposalIsComplete(srcId types.ChainId, dest types.ChainId, nonce types.Nonce, typ types.TransferType) bool {
var dataHash [32]byte
var prop uint8
var err error
if typ == types.FungibleTransfer {
_, r, a, e := l.state[srcId].GetDepositFTRecord(uint64(nonce), uint8(dest))
if e != nil {
l.log.Error("Failed to check deposit existence", "err", e)
return false
}
dataHash = l.state[dest].FTPropsalDataHash(r, a)
prop, err = l.state[dest].GetFTProposalStatus(uint8(srcId), uint64(nonce), dataHash)
} else {
_, r, a, metadata, feeAmount, e := l.state[srcId].GetDepositNFTRecord(uint64(nonce), uint8(dest))
if e != nil {
l.log.Error("Failed to check deposit existence", "err", e)
return false
}
dataHash = l.state[dest].NFTPropsalDataHash(r, a, metadata, feeAmount)
prop, err = l.state[dest].GetNFTProposalStatus(uint8(srcId), uint64(nonce), dataHash)
}
if err != nil {
l.log.Error("Failed to check proposal existence", "err", err)
return false
}
return prop == PassedStatus || prop == TransferredStatus || prop == CancelledStatus
}
func (l *listener) shouldSign(m types.TransferMsg) bool {
if l.DestProposalIsComplete(m.SourceId, m.DestinationId, m.DepositNonce, m.Type) {
l.log.Info("Proposal On dest chain complete", "typ", m.Type, "src", m.SourceId, "dst", m.DestinationId, "nonce", m.DepositNonce)
return false
}
// Check if relayer has previously voted
if l.writer.hasSigned(m.SourceId, m.DestinationId, m.DepositNonce) {
l.log.Info("Relayer has already sign", "typ", m.Type, "src", m.SourceId, "dst", m.DestinationId, "nonce", m.DepositNonce)
return false
}
return true
}