-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
93 lines (82 loc) · 1.72 KB
/
block.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
package main
import (
"context"
"log"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
type BlockRangeJob = struct {
client *ethclient.Client
start int64
end int64
toAddress common.Address
txEntries chan<- *TransactionEntry
}
func NewBlockRangeJob(
client *ethclient.Client,
start int64,
end int64,
toAddress common.Address,
txEntries chan<- *TransactionEntry,
) *BlockRangeJob {
return &BlockRangeJob{
client,
start,
end,
toAddress,
txEntries,
}
}
func scanBlockRange(job *BlockRangeJob) error {
q := ethereum.FilterQuery{
Addresses: []common.Address{job.toAddress},
FromBlock: big.NewInt(job.start),
ToBlock: big.NewInt(job.end),
}
logs, err := getFilterLogs(q, job.client)
if err != nil {
log.Fatalln(err)
}
count := 0
for _, l := range logs {
if l.Address != job.toAddress {
continue
}
job.txEntries <- &TransactionEntry{
txHash: l.TxHash,
blockHash: l.BlockHash,
}
count += 1
}
log.Printf("start=%d end=%d count=%d\n", job.start, job.end, count)
return nil
}
func getFilterLogs(q ethereum.FilterQuery, client *ethclient.Client) ([]types.Log, error) {
ctx := context.Background()
for {
logs, err := client.FilterLogs(ctx, q)
if err != nil {
if ok := checkErrorRetry(err); ok {
continue
}
return nil, err
}
return logs, nil
}
}
func getBlock(hash common.Hash, client *ethclient.Client) (*types.Block, error) {
ctx := context.Background()
for {
block, err := client.BlockByHash(ctx, hash)
if err != nil {
if ok := checkErrorRetry(err); ok {
continue
}
return nil, err
}
return block, nil
}
}