-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
89 lines (69 loc) · 2.22 KB
/
Copy pathutils.go
File metadata and controls
89 lines (69 loc) · 2.22 KB
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
package chainalysis
import (
"context"
"errors"
"log/slog"
"math/big"
"strings"
"time"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/go-ethereum"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/core/types"
"github.com/goware/breaker"
)
func fetchEthereumLogs(ctx context.Context, provider ethrpc.Interface, maxBatchSize, lastBatchSize, from, to uint64, optContractFilter *common.Address, topicID string) ([]types.Log, uint64, error) {
result := []types.Log{}
batchSize := lastBatchSize
additiveFactor := uint64(float64(batchSize) * 0.10)
br := breaker.New(slog.Default(), time.Second*2, 2, 5)
for i := from; i < to; {
dst := min(i+batchSize, to)
query := ethereum.FilterQuery{
FromBlock: big.NewInt(int64(i)),
ToBlock: big.NewInt(int64(dst - 1)),
Topics: [][]common.Hash{{common.HexToHash(topicID)}},
}
// optional contract filter, useful for debugging
if optContractFilter != nil {
query.Addresses = []common.Address{*optContractFilter}
}
var logs []types.Log
err := br.Do(ctx, func() error {
var err error
logs, err = provider.FilterLogs(ctx, query)
if err != nil {
if tooMuchDataRequestedError(err) {
slog.Warn("fetchEthereumLogs hit too-much-data error", "batchSize", batchSize)
batchSize = uint64(float64(batchSize) / 1.5)
return err
}
if !errors.Is(err, context.Canceled) {
slog.Error("fetchEthereumLogs failed", "error", err)
}
slog.Warn("fetchEthereumLogs error", "error", err)
return err
}
return nil
})
if err != nil {
return nil, batchSize, err
}
// append logs to result
result = append(result, logs...)
// check if the execution is over after each query batch
if err := ctx.Err(); err != nil {
return nil, batchSize, err
}
i = dst
// update the batchSize with additive increase
if i < to && batchSize < maxBatchSize {
batchSize = min(maxBatchSize, batchSize+additiveFactor)
}
}
slog.Debug("fetchEthereumLogs completed", "fromBlock", from, "toBlock", to, "logCount", len(result))
return result, batchSize, nil
}
func tooMuchDataRequestedError(err error) bool {
return strings.Contains(err.Error(), "query returned more than")
}