forked from c0mm4nd/tronetl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_blocks_and_transactions.go
188 lines (153 loc) · 5.73 KB
/
export_blocks_and_transactions.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
package main
import (
"encoding/csv"
"encoding/json"
"io"
"log"
"math/big"
"sync"
"git.ngx.fi/c0mm4nd/tronetl/tron"
"github.com/jszwec/csvutil"
)
// ExportBlocksAndTransactionsOptions is the option for ExportBlocksAndTransactions func
type ExportBlocksAndTransactionsOptions struct {
blksOutput io.Writer
txsOutput io.Writer
trc10Output io.Writer
ProviderURI string `json:"provider_uri,omitempty"`
StartBlock uint64 `json:"start_block,omitempty"`
EndBlock uint64 `json:"end_block,omitempty"`
// extension
StartTimestamp uint64 `json:"start_timestamp,omitempty"`
EndTimestamp uint64 `json:"end_timestamp,omitempty"`
}
// ExportBlocksAndTransactions is the main func for handling export_blocks_and_transactions command
func ExportBlocksAndTransactions(options *ExportBlocksAndTransactionsOptions) {
cli := tron.NewTronClient(options.ProviderURI)
var blksCsvEncoder, txsCsvEncoder, trc10CsvEncoder *csvutil.Encoder
if options.blksOutput != nil {
blksCsvWriter := csv.NewWriter(options.blksOutput)
defer blksCsvWriter.Flush()
blksCsvEncoder = csvutil.NewEncoder(blksCsvWriter)
}
if options.txsOutput != nil {
txsCsvWriter := csv.NewWriter(options.txsOutput)
defer txsCsvWriter.Flush()
txsCsvEncoder = csvutil.NewEncoder(txsCsvWriter)
}
if options.trc10Output != nil {
trc10CsvWriter := csv.NewWriter(options.trc10Output)
defer trc10CsvWriter.Flush()
trc10CsvEncoder = csvutil.NewEncoder(trc10CsvWriter)
}
log.Printf("try parsing blocks and transactions from block %d to %d", options.StartBlock, options.EndBlock)
for number := options.StartBlock; number <= options.EndBlock; number++ {
num := new(big.Int).SetUint64(number)
jsonblock := cli.GetJSONBlockByNumberWithTxs(num)
httpblock := cli.GetHTTPBlockByNumber(num)
blockTime := uint64(httpblock.BlockHeader.RawData.Timestamp)
csvBlock := NewCsvBlock(jsonblock, httpblock)
blockHash := csvBlock.Hash
if options.txsOutput != nil || options.trc10Output != nil {
for txIndex, jsontx := range jsonblock.Transactions {
httptx := httpblock.Transactions[txIndex]
if options.txsOutput != nil {
csvTx := NewCsvTransaction(blockTime, txIndex, &jsontx, &httptx)
err := txsCsvEncoder.Encode(csvTx)
chk(err)
}
if options.trc10Output != nil {
for callIndex, contractCall := range httptx.RawData.Contract {
if contractCall.ContractType == "TransferAssetContract" ||
contractCall.ContractType == "TransferContract" {
var tfParams tron.TRC10TransferParams
err := json.Unmarshal(contractCall.Parameter.Value, &tfParams)
chk(err)
csvTf := NewCsvTRC10Transfer(blockHash, number, txIndex, callIndex, &httpblock.Transactions[txIndex], &tfParams)
err = trc10CsvEncoder.Encode(csvTf)
chk(err)
}
}
}
}
}
err := blksCsvEncoder.Encode(csvBlock)
chk(err)
log.Printf("parsed block %d", number)
}
}
// ExportBlocksAndTransactions is the main func for handling export_blocks_and_transactions command
func ExportBlocksAndTransactionsWithWorkers(options *ExportBlocksAndTransactionsOptions, workers uint) {
cli := tron.NewTronClient(options.ProviderURI)
var receiverWG sync.WaitGroup
var blksCsvEncCh, txsCsvEncCh, trc10CsvEncCh chan any
if options.blksOutput != nil {
blksCsvWriter := csv.NewWriter(options.blksOutput)
defer blksCsvWriter.Flush()
blksCsvEncoder := csvutil.NewEncoder(blksCsvWriter)
blksCsvEncCh = createCSVEncodeCh(&receiverWG, blksCsvEncoder, workers)
}
if options.txsOutput != nil {
txsCsvWriter := csv.NewWriter(options.txsOutput)
defer txsCsvWriter.Flush()
txsCsvEncoder := csvutil.NewEncoder(txsCsvWriter)
txsCsvEncCh = createCSVEncodeCh(&receiverWG, txsCsvEncoder, workers)
}
if options.trc10Output != nil {
trc10CsvWriter := csv.NewWriter(options.trc10Output)
defer trc10CsvWriter.Flush()
trc10CsvEncoder := csvutil.NewEncoder(trc10CsvWriter)
trc10CsvEncCh = createCSVEncodeCh(&receiverWG, trc10CsvEncoder, workers)
}
log.Printf("try parsing blocks and transactions from block %d to %d", options.StartBlock, options.EndBlock)
exportWork := func(wg *sync.WaitGroup, workerID uint) {
for number := options.StartBlock + uint64(workerID); number <= options.EndBlock; number += uint64(workers) {
num := new(big.Int).SetUint64(number)
jsonblock := cli.GetJSONBlockByNumberWithTxs(num)
httpblock := cli.GetHTTPBlockByNumber(num)
blockTime := uint64(httpblock.BlockHeader.RawData.Timestamp)
csvBlock := NewCsvBlock(jsonblock, httpblock)
blockHash := csvBlock.Hash
if options.txsOutput != nil || options.trc10Output != nil {
for txIndex, jsontx := range jsonblock.Transactions {
httptx := httpblock.Transactions[txIndex]
if options.txsOutput != nil {
csvTx := NewCsvTransaction(blockTime, txIndex, &jsontx, &httptx)
txsCsvEncCh <- csvTx
}
if options.trc10Output != nil {
for callIndex, contractCall := range httptx.RawData.Contract {
if contractCall.ContractType == "TransferAssetContract" ||
contractCall.ContractType == "TransferContract" {
var tfParams tron.TRC10TransferParams
err := json.Unmarshal(contractCall.Parameter.Value, &tfParams)
chk(err)
csvTf := NewCsvTRC10Transfer(blockHash, number, txIndex, callIndex, &httpblock.Transactions[txIndex], &tfParams)
trc10CsvEncCh <- csvTf
}
}
}
}
}
blksCsvEncCh <- csvBlock
log.Printf("parsed block %d", number)
}
wg.Done()
}
var senderWG sync.WaitGroup
for workerID := uint(0); workerID < workers; workerID++ {
senderWG.Add(1)
go exportWork(&senderWG, workerID)
}
senderWG.Wait()
if options.blksOutput != nil {
close(blksCsvEncCh)
}
if options.txsOutput != nil {
close(txsCsvEncCh)
}
if trc10CsvEncCh != nil {
close(trc10CsvEncCh)
}
receiverWG.Wait()
}