-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
273 lines (217 loc) · 7.48 KB
/
types.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
package bridge_core
import (
"context"
"math/big"
"time"
"github.com/axieinfinity/bridge-core/models"
"github.com/axieinfinity/bridge-core/stores"
"github.com/axieinfinity/bridge-core/utils"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
const (
ListenHandler = iota
CallbackHandler
)
const (
TxEvent = iota
LogEvent
)
type Listener interface {
GetName() string
GetStore() stores.MainStore
Config() *LsConfig
Period() time.Duration
GetSafeBlockRange() uint64
GetCurrentBlock() Block
GetLatestBlock() (Block, error)
GetLatestBlockHeight() (uint64, error)
GetBlock(height uint64) (Block, error)
GetBlockWithLogs(height uint64) (Block, error)
GetChainID() (*big.Int, error)
GetReceipt(common.Hash) (*types.Receipt, error)
Context() context.Context
GetSubscriptions() map[string]*Subscribe
UpdateCurrentBlock(block Block) error
SaveCurrentBlockToDB() error
SaveTransactionsToDB(txs []Transaction) error
GetListenHandleJob(subscriptionName string, tx Transaction, eventId string, data []byte) JobHandler
SendCallbackJobs(listeners map[string]Listener, subscriptionName string, tx Transaction, inputData []byte)
NewJobFromDB(job *models.Job) (JobHandler, error)
Start()
Close()
IsDisabled() bool
SetInitHeight(uint64)
GetInitHeight() uint64
GetEthClient() utils.EthClient
GetTasks() []TaskHandler
GetTask(index int) TaskHandler
AddTask(handler TaskHandler)
IsUpTodate() bool
GetBridgeOperatorSign() utils.ISign
GetVoterSign() utils.ISign
GetRelayerSign() utils.ISign
GetLegacyBridgeOperatorSign() utils.ISign
AddListeners(map[string]Listener)
// GetListener returns listener by name
GetListener(string) Listener
CacheBlocks(blockNumbers map[uint64]struct{})
}
type Transaction interface {
GetHash() common.Hash
GetFromAddress() string
GetToAddress() string
GetData() []byte
GetValue() *big.Int
}
type Log interface {
GetContractAddress() string
GetTopics() []string
GetData() []byte
GetIndex() uint
GetTxIndex() uint
GetTransactionHash() string
}
type Receipt interface {
GetTransaction() Transaction
GetStatus() bool
GetLogs() []Log
}
type Block interface {
GetHash() common.Hash
GetHeight() uint64
GetTransactions() []Transaction
GetLogs() []Log
GetTimestamp() uint64
}
type JobHandler interface {
GetID() int32
GetType() int
GetRetryCount() int
GetNextTry() int64
GetMaxTry() int
GetData() []byte
GetValue() *big.Int
GetBackOff() int
Process() ([]byte, error)
Hash() common.Hash
IncreaseRetryCount()
UpdateNextTry(int64)
GetListener() Listener
GetSubscriptionName() string
GetTransaction() Transaction
FromChainID() *big.Int
Save(string) error
CreatedAt() time.Time
String() string
}
type TaskHandler interface {
Start()
Close()
GetListener() Listener
SetLimitQuery(limit int)
}
type Config struct {
Listeners map[string]*LsConfig `json:"listeners" mapstructure:"listeners"`
NumberOfWorkers int `json:"numberOfWorkers" mapstructure:"numberOfWorkers"`
MaxQueueSize int `json:"maxQueueSize" mapstructure:"maxQueueSize"`
MaxRetry int32 `json:"maxRetry" mapstructure:"maxRetry"`
BackOff int32 `json:"backoff" mapstructure:"backoff"`
DB *stores.Database `json:"database" mapstructure:"database"`
// this field is used for testing purpose
Testing bool
}
type LsConfig struct {
ChainId string `json:"chainId" mapstructure:"chainId"`
Name string `json:"-"`
RpcUrl string `json:"rpcUrl" mapstructure:"rpcUrl"`
LoadInterval time.Duration `json:"blockTime" mapstructure:"blockTime"`
SafeBlockRange uint64 `json:"safeBlockRange" mapstructure:"safeBlockRange"`
FromHeight uint64 `json:"fromHeight" mapstructure:"fromHeight"`
TaskInterval time.Duration `json:"taskInterval" mapstructure:"taskInterval"`
Disabled bool `json:"disabled" mapstructure:"disabled"`
// TODO: apply more ways to get privatekey. such as: PLAINTEXT, KMS, etc.
Secret *Secret `json:"secret" mapstructure:"secret"`
Subscriptions map[string]*Subscribe `json:"subscriptions" mapstructure:"subscriptions"`
TransactionCheckPeriod time.Duration `json:"transactionCheckPeriod" mapstructure:"transactionCheckPeriod"`
Contracts map[string]string `json:"contracts" mapstructure:"contracts"`
ProcessWithinBlocks uint64 `json:"processWithinBlocks" mapstructure:"processWithinBlocks"`
MaxTasksQuery int `json:"maxTasksQuery" mapstructure:"maxTasksQuery"`
MinTasksQuery int `json:"minTasksQuery" mapstructure:"minTasksQuery"`
// GetLogsBatchSize is used at batch size when calling processBatchLogs
GetLogsBatchSize int `json:"getLogsBatchSize" mapstructure:"getLogsBatchSize"`
// MaxProcessingTasks is used to specify max processing tasks allowed while processing tasks
// if number of tasks reaches this number, it waits until this number decrease
MaxProcessingTasks int `json:"maxProcessingTasks" mapstructure:"maxProcessingTasks"`
GasLimitBumpRatio uint64 `json:"gasLimitBumpRatio" mapstructure:"gasLimitBumpRatio"`
Stats *ListenerStats `json:"stats" mapstructure:"stats"`
}
type ListenerStats struct {
Node string `json:"node" mapstructure:"node"`
Host string `json:"host" mapstructure:"host"`
Secret string `json:"secret" mapstructure:"secret"`
}
type Secret struct {
BridgeOperator *utils.SignMethodConfig `json:"bridgeOperator" mapstructure:"bridgeOperator"`
Voter *utils.SignMethodConfig `json:"voter" mapstructure:"voter"`
Relayer *utils.SignMethodConfig `json:"relayer" mapstructure:"relayer"`
LegacyBridgeOperator *utils.SignMethodConfig `json:"legacyBridgeOperator" mapstructure:"legacyBridgeOperator"`
}
type Subscribe struct {
From string `json:"from" mapstructure:"from"`
To string `json:"to" mapstructure:"to"`
// Type can be either TxEvent or LogEvent
Type int `json:"type" mapstructure:"type"`
Handler *Handler `json:"handler" mapstructure:"handler"`
CallBacks map[string]string `json:"callbacks" mapstructure:"callbacks"`
}
type Handler struct {
// Contract Name that will be used to get ABI
Contract string `json:"contract" mapstructure:"contract"`
// Name is method/event name
Name string `json:"name" mapstructure:"name"`
// ContractAddress is used in callback case
ContractAddress string `json:"contractAddress" mapstructure:"contractAddress"`
// Listener who triggers callback event
Listener string `json:"listener" mapstructure:"listener"`
ABI *abi.ABI `json:"-"`
// HandleMethod is used when processing listened job, do nothing if it is empty
HandleMethod string `json:"handleMethod" mapstructure:"handleMethod"`
}
type EmptyTransaction struct {
chainId *big.Int
hash common.Hash
from, to *common.Address
data []byte
}
func NewEmptyTransaction(chainId *big.Int, tx common.Hash, data []byte, from, to *common.Address) *EmptyTransaction {
return &EmptyTransaction{
chainId: chainId,
hash: tx,
from: from,
to: to,
data: data,
}
}
func (b *EmptyTransaction) GetHash() common.Hash {
return b.hash
}
func (b *EmptyTransaction) GetFromAddress() string {
if b.from != nil {
return b.from.Hex()
}
return ""
}
func (b *EmptyTransaction) GetToAddress() string {
if b.to != nil {
return b.to.Hex()
}
return ""
}
func (b *EmptyTransaction) GetData() []byte {
return b.data
}
func (b *EmptyTransaction) GetValue() *big.Int {
return nil
}