-
Notifications
You must be signed in to change notification settings - Fork 134
/
tracker.go
915 lines (797 loc) · 19.1 KB
/
tracker.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
package tracker
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/big"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/blocktracker"
"github.com/umbracle/ethgo/etherscan"
"github.com/umbracle/ethgo/jsonrpc/codec"
"github.com/umbracle/ethgo/tracker/store"
"github.com/umbracle/ethgo/tracker/store/inmem"
)
var (
dbGenesis = "genesis"
dbChainID = "chainID"
dbLastBlock = "lastBlock"
dbFilter = "filter"
)
const (
defaultBatchSize = 100
)
// BlockTracking defines interface for block tracker implementations
type BlockTracking interface {
BlocksBlocked() []*ethgo.Block
AddBlockLocked(block *ethgo.Block) error
MaxBlockBacklog() uint64
Init() error
Start() error
Close() error
Subscribe() chan *blocktracker.BlockEvent
AcquireLock() blocktracker.Lock
LastBlocked() *ethgo.Block
HandleBlockEvent(block *ethgo.Block) (*blocktracker.BlockEvent, error)
Len() int
}
// FilterConfig is a tracker filter configuration
type FilterConfig struct {
Address []ethgo.Address `json:"address"`
Topics [][]*ethgo.Hash `json:"topics"`
Start uint64
Hash string
Async bool
}
func (f *FilterConfig) buildHash() {
h := sha256.New()
for _, i := range f.Address {
h.Write([]byte(i.String()))
}
for _, topics := range f.Topics {
if topics == nil {
h.Write([]byte("empty"))
continue
}
for _, innerTopic := range topics {
if innerTopic == nil {
h.Write([]byte("empty"))
continue
}
h.Write([]byte(innerTopic.String()))
}
}
f.Hash = hex.EncodeToString(h.Sum(nil))
}
func (f *FilterConfig) getFilterSearch() *ethgo.LogFilter {
filter := ðgo.LogFilter{}
if len(f.Address) != 0 {
filter.Address = f.Address
}
if len(f.Topics) != 0 {
filter.Topics = f.Topics
}
return filter
}
// Config is the configuration of the tracker
type Config struct {
BatchSize uint64
BlockTracker BlockTracking
EtherscanAPIKey string
Filter *FilterConfig
Store store.Store
}
type ConfigOption func(*Config)
func WithBatchSize(b uint64) ConfigOption {
return func(c *Config) {
c.BatchSize = b
}
}
func WithBlockTracker(b BlockTracking) ConfigOption {
return func(c *Config) {
c.BlockTracker = b
}
}
func WithStore(s store.Store) ConfigOption {
return func(c *Config) {
c.Store = s
}
}
func WithFilter(f *FilterConfig) ConfigOption {
return func(c *Config) {
c.Filter = f
}
}
func WithEtherscan(k string) ConfigOption {
return func(c *Config) {
c.EtherscanAPIKey = k
}
}
// DefaultConfig returns the default tracker config
func DefaultConfig() *Config {
return &Config{
BatchSize: defaultBatchSize,
Store: inmem.NewInmemStore(),
Filter: &FilterConfig{},
EtherscanAPIKey: "",
}
}
// Provider are the eth1x methods required by the tracker
type Provider interface {
BlockNumber() (uint64, error)
GetBlockByHash(hash ethgo.Hash, full bool) (*ethgo.Block, error)
GetBlockByNumber(i ethgo.BlockNumber, full bool) (*ethgo.Block, error)
GetLogs(filter *ethgo.LogFilter) ([]*ethgo.Log, error)
ChainID() (*big.Int, error)
}
// Tracker is a contract event tracker
type Tracker struct {
logger *log.Logger
provider Provider
config *Config
store store.Store
entry store.Entry
preSyncOnce sync.Once
blockTracker BlockTracking
synced int32
BlockCh chan *blocktracker.BlockEvent
ReadyCh chan struct{}
SyncCh chan uint64
EventCh chan *Event
DoneCh chan struct{}
}
// NewTracker creates a new tracker
func NewTracker(provider Provider, opts ...ConfigOption) (*Tracker, error) {
config := DefaultConfig()
for _, opt := range opts {
opt(config)
}
t := &Tracker{
provider: provider,
config: config,
BlockCh: make(chan *blocktracker.BlockEvent, 1),
logger: log.New(ioutil.Discard, "", log.LstdFlags),
ReadyCh: make(chan struct{}),
store: config.Store,
blockTracker: config.BlockTracker,
DoneCh: make(chan struct{}, 1),
EventCh: make(chan *Event),
SyncCh: make(chan uint64, 1),
synced: 0,
}
if err := t.setupFilter(); err != nil {
return nil, err
}
return t, nil
}
// NewFilter creates a new log filter
func (t *Tracker) setupFilter() error {
if t.config.Filter == nil {
// generic config
t.config.Filter = &FilterConfig{}
}
// generate a random hash if not provided
if t.config.Filter.Hash == "" {
t.config.Filter.buildHash()
}
entry, err := t.store.GetEntry(t.config.Filter.Hash)
if err != nil {
return err
}
t.entry = entry
// insert the filter config in the db
filterKey := dbFilter + "_" + t.config.Filter.Hash
data, err := t.store.Get(filterKey)
if err != nil {
return err
}
if data == "" {
raw, err := json.Marshal(t.config.Filter)
if err != nil {
return err
}
rawStr := hex.EncodeToString(raw)
if err := t.store.Set(filterKey, rawStr); err != nil {
return err
}
}
return nil
}
func (t *Tracker) Entry() store.Entry {
return t.entry
}
// GetLastBlock returns the last block processed for this filter
func (t *Tracker) GetLastBlock() (*ethgo.Block, error) {
buf, err := t.store.Get(dbLastBlock + "_" + t.config.Filter.Hash)
if err != nil {
return nil, err
}
if len(buf) == 0 {
return nil, nil
}
raw, err := hex.DecodeString(buf)
if err != nil {
return nil, err
}
b := ðgo.Block{}
if err := b.UnmarshalJSON(raw); err != nil {
return nil, err
}
return b, nil
}
func (t *Tracker) storeLastBlock(b *ethgo.Block) error {
if b.Difficulty == nil {
b.Difficulty = big.NewInt(0)
}
buf, err := b.MarshalJSON()
if err != nil {
return err
}
raw := hex.EncodeToString(buf)
return t.store.Set(dbLastBlock+"_"+t.config.Filter.Hash, raw)
}
func (t *Tracker) emitEvent(evnt *Event) {
if evnt == nil {
return
}
if t.config.Filter.Async {
select {
case t.EventCh <- evnt:
default:
}
} else {
t.EventCh <- evnt
}
}
// IsSynced returns true if the filter is synced to head
func (t *Tracker) IsSynced() bool {
return atomic.LoadInt32(&t.synced) != 0
}
// Wait waits the filter to finish
func (t *Tracker) Wait() {
t.WaitDuration(0)
}
// WaitDuration waits for the filter to finish up to duration
func (t *Tracker) WaitDuration(dur time.Duration) error {
if t.IsSynced() {
return nil
}
var waitCh <-chan time.Time
if dur == 0 {
waitCh = time.After(dur)
}
select {
case <-waitCh:
return fmt.Errorf("timeout")
case <-t.DoneCh:
}
return nil
}
func (t *Tracker) findAncestor(block, pivot *ethgo.Block) (uint64, error) {
// block is part of a fork that is not the current head, find a common ancestor
// both block and pivot are at the same height
var err error
for i := uint64(0); i < t.blockTracker.MaxBlockBacklog(); i++ {
if block.Number != pivot.Number {
return 0, fmt.Errorf("block numbers do not match")
}
if block.Hash == pivot.Hash {
// this is the common ancestor in both
return block.Number, nil
}
block, err = t.provider.GetBlockByHash(block.ParentHash, false)
if err != nil {
return 0, err
} else if block == nil {
// if block does not exist (for example reorg happened) GetBlockByNumber will return nil, nil
return 0, fmt.Errorf("block with hash %s not found", block.ParentHash)
}
pivot, err = t.provider.GetBlockByHash(pivot.ParentHash, false)
if err != nil {
return 0, err
} else if pivot == nil {
// if block does not exist (for example reorg happened) GetBlockByNumber will return nil, nil
return 0, fmt.Errorf("block/pivot with hash %s not found", pivot.ParentHash)
}
}
return 0, fmt.Errorf("the reorg is bigger than maxBlockBacklog %d", t.blockTracker.MaxBlockBacklog())
}
func (t *Tracker) emitLogs(typ EventType, logs []*ethgo.Log) {
evnt := &Event{}
if typ == EventAdd {
evnt.Added = logs
}
if typ == EventDel {
evnt.Removed = logs
}
t.emitEvent(evnt)
}
func tooMuchDataRequestedError(err error) bool {
obj, ok := err.(*codec.ErrorObject)
if !ok {
return false
}
if obj.Message == "query returned more than 10000 results" {
return true
}
return false
}
func (t *Tracker) syncBatch(ctx context.Context, from, to uint64) error {
query := t.config.Filter.getFilterSearch()
batchSize := t.config.BatchSize
additiveFactor := uint64(float64(batchSize) * 0.10)
i := from
START:
dst := min(to, i+batchSize)
query.SetFromUint64(i)
query.SetToUint64(dst)
logs, err := t.provider.GetLogs(query)
if err != nil {
if tooMuchDataRequestedError(err) {
// multiplicative decrease
batchSize = batchSize / 2
goto START
}
return err
}
if t.SyncCh != nil {
select {
case t.SyncCh <- dst:
default:
}
}
// add logs to the store
if err := t.entry.StoreLogs(logs); err != nil {
return err
}
t.emitLogs(EventAdd, logs)
// update the last block entry
block, err := t.getBlockByNumber(dst)
if err != nil {
return err
}
if err := t.storeLastBlock(block); err != nil {
return err
}
// check if the execution is over after each query batch
if err := ctx.Err(); err != nil {
return err
}
i += batchSize + 1
// update the batchSize with additive increase
if batchSize < t.config.BatchSize {
batchSize = min(t.config.BatchSize, batchSize+additiveFactor)
}
if i <= to {
goto START
}
return nil
}
func (t *Tracker) preSyncCheck() error {
var err error
t.preSyncOnce.Do(func() {
err = t.preSyncCheckImpl()
})
return err
}
func (t *Tracker) preSyncCheckImpl() error {
rGenesis, err := t.getBlockByNumber(0)
if err != nil {
return err
}
rChainID, err := t.provider.ChainID()
if err != nil {
return err
}
genesis, err := t.store.Get(dbGenesis)
if err != nil {
return err
}
chainID, err := t.store.Get(dbChainID)
if err != nil {
return err
}
if len(genesis) != 0 {
if genesis != rGenesis.Hash.String() {
return fmt.Errorf("bad genesis")
}
if chainID != rChainID.String() {
return fmt.Errorf("bad genesis")
}
} else {
if err := t.store.Set(dbGenesis, rGenesis.Hash.String()); err != nil {
return err
}
if err := t.store.Set(dbChainID, rChainID.String()); err != nil {
return err
}
}
return nil
}
func (t *Tracker) fastTrack(filterConfig *FilterConfig) (*ethgo.Block, error) {
// Try to use first the user provided block if any
if filterConfig.Start != 0 {
bb, err := t.getBlockByNumber(filterConfig.Start)
if err != nil {
return nil, err
}
return bb, nil
}
// Only possible if we filter addresses
if len(filterConfig.Address) == 0 {
return nil, nil
}
if t.config.EtherscanAPIKey != "" {
chainID, err := t.provider.ChainID()
if err != nil {
return nil, err
}
// get the etherscan instance for this chainID
e, err := etherscan.NewEtherscanFromNetwork(ethgo.Network(chainID.Uint64()), t.config.EtherscanAPIKey)
if err != nil {
// there is no etherscan api for this specific chainid
return nil, nil
}
getAddress := func(addr ethgo.Address) (uint64, error) {
params := map[string]string{
"address": addr.String(),
"fromBlock": "0",
"toBlock": "latest",
}
var out []map[string]interface{}
if err := e.Query("logs", "getLogs", &out, params); err != nil {
return 0, err
}
if len(out) == 0 {
return 0, nil
}
cc, ok := out[0]["blockNumber"].(string)
if !ok {
return 0, fmt.Errorf("failed to cast blocknumber")
}
num, err := parseUint64orHex(cc)
if err != nil {
return 0, err
}
return num, nil
}
minBlock := ^uint64(0) // max uint64
for _, addr := range filterConfig.Address {
num, err := getAddress(addr)
if err != nil {
return nil, err
}
if num < minBlock {
minBlock = num
}
}
bb, err := t.getBlockByNumber(minBlock - 1)
if err != nil {
return nil, err
}
return bb, nil
}
return nil, nil
}
func (t *Tracker) BatchSync(ctx context.Context) error {
if err := t.preSyncCheck(); err != nil {
return err
}
if t.blockTracker == nil {
// run a specfic block tracker
t.blockTracker = blocktracker.NewBlockTracker(t.provider)
if err := t.blockTracker.Init(); err != nil {
return err
}
go t.blockTracker.Start()
go func() {
// track our stop
<-ctx.Done()
t.blockTracker.Close()
}()
} else {
// just try to init
if err := t.blockTracker.Init(); err != nil {
return err
}
}
close(t.ReadyCh)
if err := t.syncImpl(ctx); err != nil {
return err
}
select {
case t.DoneCh <- struct{}{}:
default:
}
atomic.StoreInt32(&t.synced, 1)
return nil
}
// Sync syncs a specific filter.
// This can take a long time so should be run concurrently.
func (t *Tracker) Sync(ctx context.Context) error {
if err := t.BatchSync(ctx); err != nil {
return err
}
// subscribe and sync
sub := t.blockTracker.Subscribe()
for {
select {
case evnt := <-sub:
if err := t.handleBlockEvnt(evnt); err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
func (t *Tracker) syncImpl(ctx context.Context) error {
if err := t.preSyncCheck(); err != nil {
return err
}
lock := t.blockTracker.AcquireLock()
defer func() {
if lock.Locked {
lock.Unlock()
}
}()
// We only hold the lock when we sync the head (last MaxBackLogs)
// because we want to avoid changes in the head while we sync.
// We will only release the lock if we do a bulk sync since it can
// move the target block for the sync.
lock.Lock()
if t.blockTracker.Len() == 0 {
return nil
}
// get the current target
target := t.blockTracker.LastBlocked()
if target == nil {
return nil
}
targetNum := target.Number
last, err := t.GetLastBlock()
if err != nil {
return err
}
if last == nil {
// Try to fast track to the valid block (if possible)
last, err = t.fastTrack(t.config.Filter)
if err != nil {
return fmt.Errorf("failed to fasttrack: %v", err)
}
if last != nil {
if err := t.storeLastBlock(last); err != nil {
return err
}
}
} else {
if last.Hash == target.Hash {
return nil
}
}
// There might been a reorg when we stopped syncing last time,
// check that our 'beacon' block matches the one in the chain.
// If that is not the case, we consider beacon-maxBackLog our
// real origin point and remove any logs ahead of that point.
var origin uint64
if last != nil {
if last.Number > targetNum {
return fmt.Errorf("store is more advanced than the chain")
}
pivot, err := t.getBlockByNumber(last.Number)
if err != nil {
return err
}
if last.Number == targetNum {
origin = last.Number
} else {
origin = last.Number + 1
}
if pivot.Hash != last.Hash {
ancestor, err := t.findAncestor(last, pivot)
if err != nil {
return err
}
origin = ancestor + 1
logs, err := t.removeLogs(ancestor+1, nil)
if err != nil {
return err
}
t.emitLogs(EventDel, logs)
last, err = t.getBlockByNumber(ancestor)
if err != nil {
return err
}
}
}
step := targetNum - origin + 1
if step > t.blockTracker.MaxBlockBacklog() {
// we are far (more than maxBackLog) from the target block
// Do a bulk sync with the eth_getLogs endpoint and get closer
// to the target block.
for {
if origin > targetNum {
return fmt.Errorf("from (%d) higher than to (%d)", origin, targetNum)
}
if targetNum-origin+1 <= t.blockTracker.MaxBlockBacklog() {
break
}
// release the lock
lock.Unlock()
limit := targetNum - t.blockTracker.MaxBlockBacklog()
if err := t.syncBatch(ctx, origin, limit); err != nil {
return err
}
origin = limit + 1
// lock again to reset the target block
lock.Lock()
targetNum = t.blockTracker.LastBlocked().Number
}
}
// we are still holding the lock on the blocksLock so that we are sure
// that the targetNum has not changed
trackerBlocks := t.blockTracker.BlocksBlocked()
added := trackerBlocks[uint64(len(trackerBlocks))-1-(targetNum-origin):]
evnt, err := t.doFilter(added, nil)
if err != nil {
return err
}
if evnt != nil {
t.emitEvent(evnt)
}
// release the lock on the blocks
lock.Unlock()
return nil
}
func (t *Tracker) removeLogs(number uint64, hash *ethgo.Hash) ([]*ethgo.Log, error) {
index, err := t.entry.LastIndex()
if err != nil {
return nil, err
}
if index == 0 {
return nil, nil
}
var remove []*ethgo.Log
for {
elemIndex := index - 1
var log ethgo.Log
if err := t.entry.GetLog(elemIndex, &log); err != nil {
return nil, err
}
if log.BlockNumber == number {
if hash != nil && log.BlockHash != *hash {
break
}
}
if log.BlockNumber < number {
break
}
remove = append(remove, &log)
if elemIndex == 0 {
index = 0
break
}
index = elemIndex
}
if err := t.entry.RemoveLogs(index); err != nil {
return nil, err
}
return remove, nil
}
func revertLogs(in []*ethgo.Log) (out []*ethgo.Log) {
for i := len(in) - 1; i >= 0; i-- {
out = append(out, in[i])
}
return
}
func (t *Tracker) handleBlockEvnt(blockEvnt *blocktracker.BlockEvent) error {
if blockEvnt == nil {
return nil
}
// emit the block event
select {
case t.BlockCh <- blockEvnt:
default:
}
if t.IsSynced() {
evnt, err := t.doFilter(blockEvnt.Added, blockEvnt.Removed)
if err != nil {
return err
}
if evnt != nil {
t.emitEvent(evnt)
}
}
return nil
}
func (t *Tracker) doFilter(added []*ethgo.Block, removed []*ethgo.Block) (*Event, error) {
evnt := &Event{}
if len(removed) != 0 {
pivot := removed[0]
logs, err := t.removeLogs(pivot.Number, &pivot.Hash)
if err != nil {
return nil, err
}
evnt.Removed = append(evnt.Removed, revertLogs(logs)...)
}
for _, block := range added {
// check logs for this blocks
query := t.config.Filter.getFilterSearch()
query.BlockHash = &block.Hash
// We check the hash, we need to do a retry to let unsynced nodes get the block
var logs []*ethgo.Log
var err error
for i := 0; i < 5; i++ {
logs, err = t.provider.GetLogs(query)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
if err != nil {
return nil, err
}
// add logs to the store
if err := t.entry.StoreLogs(logs); err != nil {
return nil, err
}
evnt.Added = append(evnt.Added, logs...)
}
// store the last block as the new index
if err := t.storeLastBlock(added[len(added)-1]); err != nil {
return nil, err
}
return evnt, nil
}
func (t *Tracker) getBlockByNumber(blockNumber uint64) (*ethgo.Block, error) {
block, err := t.provider.GetBlockByNumber(ethgo.BlockNumber(blockNumber), false)
if err != nil {
return nil, err
} else if block == nil {
// if block does not exist (for example reorg happened) GetBlockByNumber will return nil, nil
return nil, fmt.Errorf("block with number %d not found", blockNumber)
}
return block, nil
}
// EventType is the type of the event
type EventType int
const (
// EventAdd happens when a new event is included in the chain
EventAdd EventType = iota
// EventDel may happen when there is a reorg and a past event is deleted
EventDel
)
// Event is an event emitted when a new log is included
type Event struct {
Type EventType
Added []*ethgo.Log
Removed []*ethgo.Log
}
// BlockEvent is an event emitted when a new block is included
type BlockEvent struct {
Type EventType
Added []*ethgo.Block
Removed []*ethgo.Block
}
func min(i, j uint64) uint64 {
if i < j {
return i
}
return j
}
func parseUint64orHex(str string) (uint64, error) {
base := 10
if strings.HasPrefix(str, "0x") {
str = str[2:]
base = 16
}
return strconv.ParseUint(str, base, 64)
}