-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
105 lines (91 loc) · 2.5 KB
/
utils.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
package main
import (
"log"
"math/big"
"sync"
"git.ngx.fi/c0mm4nd/tronetl/tron"
"github.com/jszwec/csvutil"
)
// locateStartBlock is a util for locating the start block by start timestamp
func locateStartBlock(cli *tron.TronClient, startTimestamp uint64) uint64 {
latestBlock := cli.GetJSONBlockByNumberWithTxIDs(nil)
top := latestBlock.Number
half := uint64(*top) / 2
estimateStartNumber := half
for {
block := cli.GetJSONBlockByNumberWithTxIDs(new(big.Int).SetUint64(estimateStartNumber))
if block == nil {
break
}
log.Println(half, block.Timestamp)
var timestamp uint64
timestamp = uint64(*block.Timestamp)
if timestamp < startTimestamp && startTimestamp-timestamp < 60 {
break
}
//
if timestamp < startTimestamp {
log.Printf("%d is too small: %d", estimateStartNumber, timestamp)
half = half / 2
estimateStartNumber = estimateStartNumber + half
} else {
log.Printf("%d is too large: %d", estimateStartNumber, timestamp)
half = half / 2
estimateStartNumber = estimateStartNumber - half
}
if half == 0 || estimateStartNumber >= uint64(*top) {
panic("failed to find the block on that timestamp")
}
}
return estimateStartNumber
}
// locateEndBlock is a util for locating the end block by end timestamp
func locateEndBlock(cli *tron.TronClient, endTimestamp uint64) uint64 {
latestBlock := cli.GetJSONBlockByNumberWithTxIDs(nil)
top := latestBlock.Number
half := uint64(*top) / 2
estimateEndNumber := half
for {
block := cli.GetJSONBlockByNumberWithTxIDs(new(big.Int).SetUint64(estimateEndNumber))
if block == nil {
break
}
log.Println(half, block.Timestamp)
var timestamp uint64
timestamp = uint64(*block.Timestamp)
if timestamp > endTimestamp && timestamp-endTimestamp < 60 {
break
}
//
if timestamp < endTimestamp {
log.Printf("%d is too small: %d", estimateEndNumber, timestamp)
half = half / 2
estimateEndNumber = estimateEndNumber + half
} else {
log.Printf("%d is too large: %d", estimateEndNumber, timestamp)
half = half / 2
estimateEndNumber = estimateEndNumber - half
}
if half == 0 || estimateEndNumber >= uint64(*top) {
panic("failed to find the block on that timestamp")
}
}
return estimateEndNumber
}
func createCSVEncodeCh(wg *sync.WaitGroup, enc *csvutil.Encoder, maxWorker uint) chan any {
wg.Add(1)
ch := make(chan any, maxWorker)
writeFn := func() {
for {
obj, ok := <-ch
if !ok {
wg.Done()
return
}
err := enc.Encode(obj)
chk(err)
}
}
go writeFn()
return ch
}