/
listener.go
378 lines (329 loc) · 9.21 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
372
373
374
375
376
377
378
// Copyright 2021 The Penguin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package listener
import (
"context"
"errors"
"fmt"
"math/big"
"strings"
"sync"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
//"github.com/ethersphere/go-storage-incentives-abi/postageabi"
"github.com/penguintop/penguin_bsc/pkg/logging"
"github.com/penguintop/penguin_bsc/pkg/postage"
"github.com/penguintop/penguin_bsc/pkg/postageabi"
"github.com/penguintop/penguin_bsc/pkg/transaction"
"github.com/prometheus/client_golang/prometheus"
)
const (
blockPage = 5000 // how many blocks to sync every time we page
tailSize = 4 // how many blocks to tail from the tip of the chain
)
var (
//postageStampABI = parseABI(postageabi.PostageStampABIv0_2_0)
postageStampABI = parseABI(postageabi.PostageStampABIv0_1_0)
// batchCreatedTopic is the postage contract's batch created event topic
batchCreatedTopic = postageStampABI.Events["BatchCreated"].ID
// batchTopupTopic is the postage contract's batch topup event topic
batchTopupTopic = postageStampABI.Events["BatchTopUp"].ID
// batchDepthIncreaseTopic is the postage contract's batch dilution event topic
batchDepthIncreaseTopic = postageStampABI.Events["BatchDepthIncrease"].ID
// priceUpdateTopic is the postage contract's price update event topic
priceUpdateTopic = postageStampABI.Events["PriceUpdate"].ID
)
type BlockHeightContractFilterer interface {
bind.ContractFilterer
BlockNumber(context.Context) (uint64, error)
}
// Shutdowner interface is passed to the listener to shutdown the node if we hit
// error while listening for blockchain events.
type Shutdowner interface {
Shutdown(context.Context) error
}
type listener struct {
logger logging.Logger
ev BlockHeightContractFilterer
blockTime uint64
postageStampAddress common.Address
quit chan struct{}
wg sync.WaitGroup
metrics metrics
shutdowner Shutdowner
}
func New(
logger logging.Logger,
ev BlockHeightContractFilterer,
postageStampAddress common.Address,
blockTime uint64,
shutdowner Shutdowner,
) postage.Listener {
return &listener{
logger: logger,
ev: ev,
blockTime: blockTime,
postageStampAddress: postageStampAddress,
quit: make(chan struct{}),
metrics: newMetrics(),
shutdowner: shutdowner,
}
}
func (l *listener) filterQuery(from, to *big.Int) ethereum.FilterQuery {
return ethereum.FilterQuery{
FromBlock: from,
ToBlock: to,
Addresses: []common.Address{
l.postageStampAddress,
},
Topics: [][]common.Hash{
{
batchCreatedTopic,
batchTopupTopic,
batchDepthIncreaseTopic,
priceUpdateTopic,
},
},
}
}
func (l *listener) processEvent(e types.Log, updater postage.EventUpdater) error {
defer l.metrics.EventsProcessed.Inc()
switch e.Topics[0] {
case batchCreatedTopic:
c := &batchCreatedEvent{}
err := transaction.ParseEvent(&postageStampABI, "BatchCreated", c, e)
if err != nil {
return err
}
l.metrics.CreatedCounter.Inc()
return updater.Create(
c.BatchId[:],
c.Owner.Bytes(),
c.NormalisedBalance,
c.Depth,
)
case batchTopupTopic:
c := &batchTopUpEvent{}
err := transaction.ParseEvent(&postageStampABI, "BatchTopUp", c, e)
if err != nil {
return err
}
l.metrics.TopupCounter.Inc()
return updater.TopUp(
c.BatchId[:],
c.NormalisedBalance,
)
case batchDepthIncreaseTopic:
c := &batchDepthIncreaseEvent{}
err := transaction.ParseEvent(&postageStampABI, "BatchDepthIncrease", c, e)
if err != nil {
return err
}
l.metrics.DepthCounter.Inc()
return updater.UpdateDepth(
c.BatchId[:],
c.NewDepth,
c.NormalisedBalance,
)
case priceUpdateTopic:
c := &priceUpdateEvent{}
err := transaction.ParseEvent(&postageStampABI, "PriceUpdate", c, e)
if err != nil {
return err
}
l.metrics.PriceCounter.Inc()
return updater.UpdatePrice(
c.Price,
)
default:
l.metrics.EventErrors.Inc()
return errors.New("unknown event")
}
}
func (l *listener) Listen(from uint64, updater postage.EventUpdater) <-chan struct{} {
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-l.quit
cancel()
}()
chainUpdateInterval := (time.Duration(l.blockTime) * time.Second) / 2
synced := make(chan struct{})
closeOnce := new(sync.Once)
paged := make(chan struct{}, 1)
paged <- struct{}{}
l.wg.Add(1)
listenf := func() error {
defer l.wg.Done()
evRpcRetryTimes := 0
fastRetryTimesLimit := 60
retryTimesMaxAllow := 60000
for {
select {
case <-paged:
// if we paged then it means there's more things to sync on
case <-time.After(chainUpdateInterval):
case <-l.quit:
return nil
}
start := time.Now()
l.metrics.BackendCalls.Inc()
to, err := l.ev.BlockNumber(ctx)
if err != nil {
l.metrics.BackendErrors.Inc()
evRpcRetryTimes += 1
if evRpcRetryTimes > retryTimesMaxAllow {
return err
} else {
l.logger.Warningf("l.ev.BlockNumber, retry [%d/%d]", evRpcRetryTimes, retryTimesMaxAllow)
if evRpcRetryTimes < fastRetryTimesLimit {
time.Sleep(1 * time.Second)
} else {
time.Sleep(10 * time.Second)
}
continue
}
}
if to < tailSize {
// in a test blockchain there might be not be enough blocks yet
continue
}
// consider to-tailSize as the "latest" block we need to sync to
to = to - tailSize
if to < from {
// if the blockNumber is actually less than what we already, it might mean the backend is not synced or some reorg scenario
continue
}
// do some paging (sub-optimal)
if to-from > blockPage {
paged <- struct{}{}
to = from + blockPage
} else {
closeOnce.Do(func() { close(synced) })
}
l.metrics.BackendCalls.Inc()
events, err := l.ev.FilterLogs(ctx, l.filterQuery(big.NewInt(int64(from)), big.NewInt(int64(to))))
if err != nil {
l.metrics.BackendErrors.Inc()
evRpcRetryTimes += 1
if evRpcRetryTimes > retryTimesMaxAllow {
return err
} else {
l.logger.Warningf("l.ev.FilterLogs, retry [%d/%d]", evRpcRetryTimes, retryTimesMaxAllow)
if evRpcRetryTimes < fastRetryTimesLimit {
time.Sleep(1 * time.Second)
} else {
time.Sleep(10 * time.Second)
}
continue
}
}
// recover evRpcRetryTimes
evRpcRetryTimes = 0
if err := updater.TransactionStart(); err != nil {
return err
}
for _, e := range events {
startEv := time.Now()
err = updater.UpdateBlockNumber(e.BlockNumber)
if err != nil {
return err
}
if err = l.processEvent(e, updater); err != nil {
return err
}
totalTimeMetric(l.metrics.EventProcessDuration, startEv)
}
err = updater.UpdateBlockNumber(to)
if err != nil {
return err
}
if err := updater.TransactionEnd(); err != nil {
return err
}
from = to + 1
totalTimeMetric(l.metrics.PageProcessDuration, start)
l.metrics.PagesProcessed.Inc()
}
}
go func() {
err := listenf()
if err != nil {
if errors.Is(err, context.Canceled) {
// context cancelled is returned on shutdown,
// therefore we do nothing here
return
}
l.logger.Errorf("failed syncing event listener, shutting down node err: %v", err)
if l.shutdowner != nil {
err = l.shutdowner.Shutdown(context.Background())
if err != nil {
l.logger.Errorf("failed shutting down node: %v", err)
}
}
}
}()
return synced
}
func (l *listener) Close() error {
close(l.quit)
done := make(chan struct{})
go func() {
defer close(done)
l.wg.Wait()
}()
select {
case <-done:
case <-time.After(5 * time.Second):
return errors.New("postage listener closed with running goroutines")
}
return nil
}
func parseABI(json string) abi.ABI {
cabi, err := abi.JSON(strings.NewReader(json))
if err != nil {
panic(fmt.Sprintf("error creating ABI for postage contract: %v", err))
}
return cabi
}
type batchCreatedEvent struct {
BatchId [32]byte
TotalAmount *big.Int
NormalisedBalance *big.Int
Owner common.Address
Depth uint8
}
type batchTopUpEvent struct {
BatchId [32]byte
TopupAmount *big.Int
NormalisedBalance *big.Int
}
type batchDepthIncreaseEvent struct {
BatchId [32]byte
NewDepth uint8
NormalisedBalance *big.Int
}
type priceUpdateEvent struct {
Price *big.Int
}
var (
GoerliPostageStampContractAddress = common.HexToAddress("0x9b2A60d9263D1b2FE9Fe8C160ab14BcbdeFd5dcC")
GoerliStartBlock = uint64(0)
)
// DiscoverAddresses returns the canonical contracts for this chainID
func DiscoverAddresses(chainID int64) (postageStamp common.Address, startBlock uint64, found bool) {
/*if chainID == 377 {
// goerli
return GoerliPostageStampContractAddress, GoerliStartBlock, true
} //*/
// modify postagestamp contract address before deploy
return GoerliPostageStampContractAddress, GoerliStartBlock, true
}
func totalTimeMetric(metric prometheus.Counter, start time.Time) {
totalTime := time.Since(start)
metric.Add(float64(totalTime))
}