From 4492aca5bd6f01c6ad96931e9dcfaa6614210551 Mon Sep 17 00:00:00 2001 From: Duncan Dam Date: Thu, 7 Nov 2024 16:30:24 +0700 Subject: [PATCH] [BLOCK-2572] add reader for new block log --- codec/consolereader.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/codec/consolereader.go b/codec/consolereader.go index 11818293..a351adc2 100644 --- a/codec/consolereader.go +++ b/codec/consolereader.go @@ -217,6 +217,14 @@ func (l *ConsoleReader) Read() (out interface{}, err error) { case strings.HasPrefix(line, "DTRX_OP FAILED"): err = ctx.readFailedDTrxOp(line) + case strings.HasPrefix(line, "ACCEPTED_BLOCK_V2"): + block, err := ctx.readAcceptedBlockV2(line) + if err != nil { + return nil, l.formatError(line, err) + } + + return block, nil + case strings.HasPrefix(line, "ACCEPTED_BLOCK"): block, err := ctx.readAcceptedBlock(line) if err != nil { @@ -503,6 +511,46 @@ func (ctx *parseCtx) readAcceptedBlock(line string) (*pbcodec.Block, error) { return block, nil } +// Line format: +// +// ACCEPTED_BLOCK_V2 ${id} ${block_num} ${lib} ${block_state_hex} +func (ctx *parseCtx) readAcceptedBlockV2(line string) (*pbcodec.Block, error) { + chunks := strings.SplitN(line, " ", 8) + if len(chunks) != 8 { + return nil, fmt.Errorf("expected 8 fields, got %d", len(chunks)) + } + + blockNum, err := strconv.ParseInt(chunks[2], 10, 64) + if err != nil { + return nil, fmt.Errorf("block_num not a valid string, got: %q", chunks[2]) + } + + if ctx.activeBlockNum != blockNum { + return nil, fmt.Errorf("block_num %d doesn't match the active block num (%d)", blockNum, ctx.activeBlockNum) + } + + blockStateHex, err := hex.DecodeString(chunks[4]) + if err != nil { + return nil, fmt.Errorf("unable to decode block %d state hex: %w", blockNum, err) + } + + if err := ctx.hydrator.HydrateBlock(ctx.block, blockStateHex); err != nil { + return nil, fmt.Errorf("hydrate block %d: %w", blockNum, err) + } + + block := ctx.block + + zlog.Debug("blocking until abi decoder has decoded every transaction pushed to it") + err = ctx.abiDecoder.endBlock(ctx.block) + if err != nil { + return nil, fmt.Errorf("abi decoding post-process failed: %w", err) + } + + zlog.Debug("abi decoder terminated all decoding operations, resetting block") + ctx.resetBlock() + return block, nil +} + // Line format: // // APPLIED_TRANSACTION ${block_num} ${trace_hex}