/
console_reader.go
358 lines (302 loc) · 9.22 KB
/
console_reader.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package codec
import (
"bufio"
"fmt"
"io"
"math/big"
"strconv"
"strings"
"time"
"github.com/ElrondNetwork/firehose-multiversx/types"
pbmultiversx "github.com/ElrondNetwork/firehose-multiversx/types/pb/sf/multiversx/type/v1"
"github.com/streamingfast/bstream"
"go.uber.org/zap"
)
// ConsoleReader is what reads the `geth` output directly. It builds
// up some LogEntry objects. See `LogReader to read those entries .
type ConsoleReader struct {
lines chan string
close func()
ctx *parseCtx
done chan interface{}
logger *zap.Logger
}
func NewConsoleReader(logger *zap.Logger, lines chan string) (*ConsoleReader, error) {
l := &ConsoleReader{
lines: lines,
close: func() {},
done: make(chan interface{}),
logger: logger,
}
return l, nil
}
//todo: WTF?
func (r *ConsoleReader) Done() <-chan interface{} {
return r.done
}
func (r *ConsoleReader) Close() {
r.close()
}
type parsingStats struct {
startAt time.Time
blockNum uint64
data map[string]int
logger *zap.Logger
}
func newParsingStats(logger *zap.Logger, block uint64) *parsingStats {
return &parsingStats{
startAt: time.Now(),
blockNum: block,
data: map[string]int{},
logger: logger,
}
}
func (s *parsingStats) log() {
s.logger.Info("reader block stats",
zap.Uint64("block_num", s.blockNum),
zap.Int64("duration", int64(time.Since(s.startAt))),
zap.Reflect("stats", s.data),
)
}
func (s *parsingStats) inc(key string) {
if s == nil {
return
}
k := strings.ToLower(key)
value := s.data[k]
value++
s.data[k] = value
}
type parseCtx struct {
currentBlock *pbmultiversx.Block
stats *parsingStats
logger *zap.Logger
}
func newContext(logger *zap.Logger, height uint64) *parseCtx {
return &parseCtx{
currentBlock: &pbmultiversx.Block{
Height: height,
Transactions: []*pbmultiversx.Transaction{},
},
stats: newParsingStats(logger, height),
logger: logger,
}
}
func (r *ConsoleReader) ReadBlock() (out *bstream.Block, err error) {
block, err := r.next()
if err != nil {
return nil, err
}
return types.BlockFromProto(block)
}
const (
LogPrefix = "FIRE"
LogBeginBlock = "BLOCK_BEGIN"
LogBeginTrx = "BEGIN_TRX"
LogBeginEvent = "TRX_BEGIN_EVENT"
LogEventAttr = "TRX_EVENT_ATTR"
LogEndTrx = "END_TRX"
LogEndBlock = "BLOCK_END"
)
func (r *ConsoleReader) next() (out *pbmultiversx.Block, err error) {
for line := range r.lines {
if !strings.HasPrefix(line, LogPrefix) {
continue
}
// This code assumes that distinct element do not contains space. This can happen
// for example when exchanging JSON object (although we strongly discourage usage of
// JSON, use serialized Protobuf object). If you happen to have spaces in the last element,
// refactor the code here to avoid the split and perform the split in the line handler directly
// instead.
tokens := strings.Split(line[len(LogPrefix)+1:], " ")
if len(tokens) < 2 {
return nil, fmt.Errorf("invalid log line %q, expecting at least two tokens", line)
}
// Order the case from most occurring line prefix to least occurring
switch tokens[0] {
case LogBeginEvent:
err = r.ctx.eventBegin(tokens[1:])
case LogEventAttr:
err = r.ctx.eventAttr(tokens[1:])
case LogBeginTrx:
err = r.ctx.trxBegin(tokens[1:])
case LogBeginBlock:
err = r.blockBegin(tokens[1:])
case LogEndBlock:
// This end the execution of the reading loop as we have a full block here
return r.ctx.readBlockEnd(tokens[1:])
default:
if r.logger.Core().Enabled(zap.DebugLevel) {
r.logger.Debug("skipping unknown deep mind log line", zap.String("line", line))
}
continue
}
if err != nil {
chunks := strings.SplitN(line, " ", 2)
return nil, fmt.Errorf("%s: %w (line %q)", chunks[0], err, line)
}
}
r.logger.Info("lines channel has been closed")
return nil, io.EOF
}
func (r *ConsoleReader) processData(reader io.Reader) error {
scanner := r.buildScanner(reader)
for scanner.Scan() {
line := scanner.Text()
r.lines <- line
}
if scanner.Err() == nil {
close(r.lines)
return io.EOF
}
return scanner.Err()
}
func (r *ConsoleReader) buildScanner(reader io.Reader) *bufio.Scanner {
buf := make([]byte, 50*1024*1024)
scanner := bufio.NewScanner(reader)
scanner.Buffer(buf, 50*1024*1024)
return scanner
}
// Format:
// FIRE BLOCK_BEGIN <NUM>
func (r *ConsoleReader) blockBegin(params []string) error {
if err := validateChunk(params, 1); err != nil {
return fmt.Errorf("invalid log line length: %w", err)
}
blockHeight, err := strconv.ParseUint(params[0], 10, 64)
if err != nil {
return fmt.Errorf("invalid block num: %w", err)
}
//Push new block meta
r.ctx = newContext(r.logger, blockHeight)
return nil
}
// Format:
// FIRE BLOCK_BEGIN <HASH> <TYPE> <FROM> <TO> <AMOUNT> <FEE> <SUCCESS>
func (ctx *parseCtx) trxBegin(params []string) error {
if err := validateChunk(params, 7); err != nil {
return fmt.Errorf("invalid log line length: %w", err)
}
if ctx == nil {
return fmt.Errorf("did not process a BLOCK_BEGIN")
}
trx := &pbmultiversx.Transaction{
Type: params[1],
Hash: params[0],
Sender: params[2],
Receiver: params[3],
Success: params[6] == "true",
Events: []*pbmultiversx.Event{},
}
v, ok := new(big.Int).SetString(params[4], 16)
if !ok {
return fmt.Errorf("unable to parse trx amount %s", params[4])
}
trx.Amount = &pbmultiversx.BigInt{Bytes: v.Bytes()}
v, ok = new(big.Int).SetString(params[5], 16)
if !ok {
return fmt.Errorf("unable to parse trx amount %s", params[4])
}
trx.Fee = &pbmultiversx.BigInt{Bytes: v.Bytes()}
ctx.currentBlock.Transactions = append(ctx.currentBlock.Transactions, trx)
return nil
}
// Format:
// FIRE TRX_BEGIN_EVENT <TRX_HASH> <TYPE>
func (ctx *parseCtx) eventBegin(params []string) error {
if err := validateChunk(params, 2); err != nil {
return fmt.Errorf("invalid log line length: %w", err)
}
if ctx == nil {
return fmt.Errorf("did not process a BLOCK_BEGIN")
}
if len(ctx.currentBlock.Transactions) == 0 {
return fmt.Errorf("did not process a BEGIN_TRX")
}
trx := ctx.currentBlock.Transactions[len(ctx.currentBlock.Transactions)-1]
if trx.Hash != params[0] {
return fmt.Errorf("last transaction hash %q does not match the event trx hash %q", trx.Hash, params[0])
}
trx.Events = append(trx.Events, &pbmultiversx.Event{
Type: params[1],
Attributes: []*pbmultiversx.Attribute{},
})
ctx.currentBlock.Transactions[len(ctx.currentBlock.Transactions)-1] = trx
return nil
}
// Format:
// FIRE TRX_EVENT_ATTR <TRX_HASH> <EVENT_INDEX> <KEY> <VALUE>
func (ctx *parseCtx) eventAttr(params []string) error {
if err := validateChunk(params, 4); err != nil {
return fmt.Errorf("invalid log line length: %w", err)
}
if ctx == nil {
return fmt.Errorf("did not process a BLOCK_BEGIN")
}
if len(ctx.currentBlock.Transactions) == 0 {
return fmt.Errorf("did not process a BEGIN_TRX")
}
trx := ctx.currentBlock.Transactions[len(ctx.currentBlock.Transactions)-1]
if trx.Hash != params[0] {
return fmt.Errorf("last transaction hash %q does not match the event trx hash %q", trx.Hash, params[0])
}
eventIndex, err := strconv.ParseUint(params[1], 10, 64)
if err != nil {
return fmt.Errorf("invalid event index: %w", err)
}
if len(trx.Events) < int(eventIndex) {
return fmt.Errorf("length of events array does not match event index: %d", eventIndex)
}
event := trx.Events[eventIndex]
event.Attributes = append(event.Attributes, &pbmultiversx.Attribute{
Key: params[2],
Value: params[3],
})
trx.Events[eventIndex] = event
ctx.currentBlock.Transactions[len(ctx.currentBlock.Transactions)-1] = trx
return nil
}
// Format:
// FIRE BLOCK_END <HEIGHT> <HASH> <PREV_HASH> <TIMESTAMP> <TRX-COUNT>
func (ctx *parseCtx) readBlockEnd(params []string) (*pbmultiversx.Block, error) {
if err := validateChunk(params, 5); err != nil {
return nil, fmt.Errorf("invalid log line length: %w", err)
}
if ctx.currentBlock == nil {
return nil, fmt.Errorf("current block not set")
}
blockHeight, err := strconv.ParseUint(params[0], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse blockNum: %w", err)
}
if blockHeight != ctx.currentBlock.Height {
return nil, fmt.Errorf("end block height does not match active block height, got block height %d but current is block height %d", blockHeight, ctx.currentBlock.Height)
}
trxCount, err := strconv.ParseUint(params[4], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse blockNum: %w", err)
}
if len(ctx.currentBlock.Transactions) != int(trxCount) {
return nil, fmt.Errorf("expected %d transaction count, got %d", trxCount, len(ctx.currentBlock.Transactions))
}
timestamp, err := strconv.ParseUint(params[3], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse block timestamp: %w", err)
}
ctx.currentBlock.Hash = params[1]
ctx.currentBlock.PrevHash = params[2]
ctx.currentBlock.Timestamp = timestamp
ctx.logger.Debug("console reader read block",
zap.Uint64("height", ctx.currentBlock.Height),
zap.String("hash", ctx.currentBlock.Hash),
zap.String("prev_hash", ctx.currentBlock.PrevHash),
zap.Int("trx_count", len(ctx.currentBlock.Transactions)),
)
return ctx.currentBlock, nil
}
func validateChunk(params []string, count int) error {
if len(params) != count {
return fmt.Errorf("%d fields required but found %d", count, len(params))
}
return nil
}