-
Notifications
You must be signed in to change notification settings - Fork 61
/
receipt_log_handler.go
78 lines (66 loc) · 1.79 KB
/
receipt_log_handler.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
package ethereum_watcher
import (
"context"
"github.com/HydroProtocol/ethereum-watcher/rpc"
"github.com/HydroProtocol/ethereum-watcher/structs"
"github.com/sirupsen/logrus"
"time"
)
const DefaultStepSizeForBigLag = 10
//deprecated, please use receipt_log_watcher instead.
func ListenForReceiptLogTillExit(
ctx context.Context,
api string,
startBlock int,
contract string,
interestedTopics []string,
handler func(receiptLog structs.RemovableReceiptLog),
steps ...int,
) int {
var stepSizeForBigLag int
if len(steps) > 0 && steps[0] > 0 {
stepSizeForBigLag = steps[0]
} else {
stepSizeForBigLag = DefaultStepSizeForBigLag
}
rpc := rpc.NewEthRPCWithRetry(api, 5)
var blockNumToBeProcessedNext = startBlock
for {
select {
case <-ctx.Done():
return blockNumToBeProcessedNext - 1
default:
highestBlock, err := rpc.GetCurrentBlockNum()
if err != nil {
return blockNumToBeProcessedNext - 1
}
if blockNumToBeProcessedNext < 0 {
blockNumToBeProcessedNext = int(highestBlock)
}
numOfBlocksToProcess := int(highestBlock) - blockNumToBeProcessedNext + 1
if numOfBlocksToProcess <= 0 {
logrus.Debugf("no ready block after %d, sleep 3 seconds", highestBlock)
time.Sleep(3 * time.Second)
continue
}
var to int
if numOfBlocksToProcess > stepSizeForBigLag {
// quick mode
to = blockNumToBeProcessedNext + stepSizeForBigLag - 1
} else {
// normal mode, 1block each time
to = blockNumToBeProcessedNext
}
logs, err := rpc.GetLogs(uint64(blockNumToBeProcessedNext), uint64(to), contract, interestedTopics)
if err != nil {
return blockNumToBeProcessedNext - 1
}
for i := 0; i < len(logs); i++ {
handler(structs.RemovableReceiptLog{
IReceiptLog: logs[i],
})
}
blockNumToBeProcessedNext = to + 1
}
}
}