-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.go
276 lines (214 loc) · 5.98 KB
/
fetcher.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
package main
import (
"context"
"math/big"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
// will be exposed via config later on
var FETCH_SLEEP_TIME = 1
// Represents either the sender chain, or any chain in the dependency set
type Chain struct {
RPC string
Client *ethclient.Client
ChainId *big.Int
timestampCache map[uint64]*big.Int
}
// Any contract, knows how to fetch events and decode them
type Contract struct {
ABI *abi.ABI
Address common.Address
Chain *Chain
}
type Identifier struct {
Origin common.Address
BlockNumber uint64
LogIndex uint64
Timestamp uint64
ChainId uint64
}
type ContractPair struct {
Sender *Chain
Receiver *Chain
}
func FetcherInit() (err error) {
CrossL2InboxABI, err = crossL2InboxMetaData.GetAbi()
if err != nil {
return
}
L2ToL2CrossDomainMessengerABI, err = l2ToL2CrossDomainMessengerMetaData.GetAbi()
if err != nil {
return
}
return nil
}
func NewChain(RPC string) (c *Chain, err error) {
client, err := ethclient.Dial(RPC)
if err != nil {
return nil, err
}
chainId, err := client.ChainID(context.Background())
if err != nil {
return nil, err
}
c = &Chain{
RPC: RPC,
Client: client,
ChainId: chainId,
timestampCache: make(map[uint64]*big.Int),
}
return
}
func (c *Chain) FetchLogs(address common.Address, from *big.Int) (logs []types.Log, err error) {
logs, err = c.Client.FilterLogs(context.Background(), ethereum.FilterQuery{
FromBlock: from,
Addresses: []common.Address{address},
})
return
}
func (c *Chain) SubscribeLogsNotification(address common.Address, from *big.Int, logsChan chan<- types.Log, errChan chan<- error) (ethereum.Subscription, error) {
// Create a filter query for the subscription
query := ethereum.FilterQuery{
FromBlock: from,
Addresses: []common.Address{address},
}
// Subscribe to the logs
subscription, err := c.Client.SubscribeFilterLogs(context.Background(), query, logsChan)
if err != nil {
return nil, err
}
// Start a goroutine to handle errors
go func() {
for {
select {
case err := <-subscription.Err():
// Send subscription errors to the error channel
errChan <- err
return
}
}
}()
return subscription, nil
}
func (c *Chain) GetBlockTimestamp(blockNumber *big.Int) (timestamp *big.Int, err error) {
time, ok := c.timestampCache[blockNumber.Uint64()]
if ok {
return time, nil
}
header, err := c.Client.HeaderByNumber(context.Background(), blockNumber)
if err != nil {
return nil, err
}
c.timestampCache[blockNumber.Uint64()] = big.NewInt(int64(header.Time))
return big.NewInt(int64(header.Time)), nil
}
func (c *Chain) GetCurrentBlockNumber() (blockNum *big.Int, err error) {
b, err := c.Client.BlockNumber(context.Background())
return big.NewInt(int64(b)), err
}
func (c Contract) FetchLogs(from *big.Int) (logs []types.Log, err error) {
logs, err = c.Chain.FetchLogs(c.Address, from)
return
}
func (c Contract) SubscribeLogsNotification(from *big.Int, logsChan chan<- types.Log, errChan chan<- error) (ethereum.Subscription, error) {
return c.Chain.SubscribeLogsNotification(c.Address, from, logsChan, errChan)
}
func (c Contract) CreateFetchChannel(from *big.Int, errChan chan error) (logsChan chan types.Log) {
logsChan = make(chan types.Log)
lastFetch := from.Uint64()
go func() {
for {
logs, err := c.FetchLogs(big.NewInt(int64(lastFetch)))
if err != nil {
errChan <- err
} else {
for _, l := range logs {
lastFetch = max(lastFetch, l.BlockNumber+1)
logsChan <- l
}
}
time.Sleep(time.Second * time.Duration(FETCH_SLEEP_TIME))
}
}()
return
}
func (c Contract) ParseEventToDic(eventLog types.Log) (eventName string, logData map[string]interface{}, err error) {
event, err := c.ABI.EventByID(eventLog.Topics[0])
if err != nil {
return "", nil, err
}
logData = make(map[string]interface{})
err = event.Inputs.UnpackIntoMap(logData, eventLog.Data)
if err != nil {
return "", nil, err
}
return event.Name, logData, nil
}
func (chain *Chain) GetEventIdentifier(eventLog types.Log) (id Identifier, err error) {
id.BlockNumber = eventLog.BlockNumber
id.ChainId = chain.ChainId.Uint64()
id.LogIndex = uint64(eventLog.Index)
id.Origin = eventLog.Address
ts, err := chain.GetBlockTimestamp(big.NewInt(int64(eventLog.BlockNumber)))
if err != nil {
return Identifier{}, err
}
id.Timestamp = ts.Uint64()
return
}
func (cp ContractPair) GetContracts() (inbox Contract, messenger Contract) {
inbox = Contract{
ABI: CrossL2InboxABI,
Address: common.HexToAddress("0x4200000000000000000000000000000000000022"),
Chain: cp.Receiver,
}
messenger = Contract{
ABI: L2ToL2CrossDomainMessengerABI,
Address: common.HexToAddress("0x4200000000000000000000000000000000000023"),
Chain: cp.Sender,
}
return
}
func (cp ContractPair) FetchAggregateCycle() (agg Aggregator, errChan chan error, err error) {
agg = MakeAggregator(cp.Sender, cp.Receiver)
errChan = make(chan error)
inbox, messenger := agg.inboxContract, agg.messengerContract
inboxCurrentBlock, err := cp.Receiver.GetCurrentBlockNumber()
if err != nil {
return agg, nil, err
}
messengerCurrentBlock, err := cp.Sender.GetCurrentBlockNumber()
if err != nil {
return agg, nil, err
}
inboxChan := inbox.CreateFetchChannel(inboxCurrentBlock, errChan)
if err != nil {
return agg, nil, err
}
messengerChan := messenger.CreateFetchChannel(messengerCurrentBlock, errChan)
if err != nil {
return agg, nil, err
}
// We read from both channels and log the events
go func() {
for {
select {
case inboxLog := <-inboxChan:
err := agg.AddInboxMessage(&inboxLog)
if err != nil {
errChan <- err
}
case messengerLog := <-messengerChan:
err := agg.AddMessengerMessage(&messengerLog)
if err != nil {
errChan <- err
}
}
}
}()
return
}