From 9d70636c81d1b1f830b1b5a7eb787016890ad900 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:03:31 +0900 Subject: [PATCH] fix format block to handle fullTx false case (#53) * fix format block to handle fullTx false case * fill parent hash --- indexer/abci.go | 8 +++++++ jsonrpc/backend/block.go | 47 ++++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/indexer/abci.go b/indexer/abci.go index 36ed4a6c..3c35cfec 100644 --- a/indexer/abci.go +++ b/indexer/abci.go @@ -137,6 +137,14 @@ func (e *EVMIndexerImpl) ListenFinalizeBlock(ctx context.Context, req abci.Reque Extra: []byte{}, } + // fill parent hash + if blockHeight > 1 { + parentHeader, err := e.BlockHeaderMap.Get(sdkCtx, uint64(blockHeight-1)) + if err == nil { + blockHeader.ParentHash = parentHeader.Hash() + } + } + blockHash := blockHeader.Hash() for txIndex, ethTx := range ethTxs { txHash := ethTx.Hash() diff --git a/jsonrpc/backend/block.go b/jsonrpc/backend/block.go index 729f6d61..0224fdfa 100644 --- a/jsonrpc/backend/block.go +++ b/jsonrpc/backend/block.go @@ -117,17 +117,15 @@ func (b *JSONRPCBackend) GetBlockByNumber(ethBlockNum rpc.BlockNumber, fullTx bo } txs := []*rpctypes.RPCTransaction{} - if fullTx { - err = b.app.EVMIndexer().IterateBlockTxs(queryCtx, blockNumber, func(tx *rpctypes.RPCTransaction) (bool, error) { - txs = append(txs, tx) - return false, nil - }) - if err != nil { - return nil, err - } + err = b.app.EVMIndexer().IterateBlockTxs(queryCtx, blockNumber, func(tx *rpctypes.RPCTransaction) (bool, error) { + txs = append(txs, tx) + return false, nil + }) + if err != nil { + return nil, err } - return formatBlock(header, txs), nil + return formatBlock(header, txs, fullTx), nil } func (b *JSONRPCBackend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) { @@ -145,23 +143,30 @@ func (b *JSONRPCBackend) GetBlockByHash(hash common.Hash, fullTx bool) (map[stri } txs := []*rpctypes.RPCTransaction{} - if fullTx { - blockNumber := header.Number.Uint64() - err = b.app.EVMIndexer().IterateBlockTxs(queryCtx, blockNumber, func(tx *rpctypes.RPCTransaction) (bool, error) { - txs = append(txs, tx) - return false, nil - }) - if err != nil { - return nil, err - } + blockNumber := header.Number.Uint64() + err = b.app.EVMIndexer().IterateBlockTxs(queryCtx, blockNumber, func(tx *rpctypes.RPCTransaction) (bool, error) { + txs = append(txs, tx) + return false, nil + }) + if err != nil { + return nil, err } - return formatBlock(header, txs), nil + return formatBlock(header, txs, fullTx), nil } -func formatBlock(header *coretypes.Header, txs []*rpctypes.RPCTransaction) map[string]interface{} { +func formatBlock(header *coretypes.Header, txs []*rpctypes.RPCTransaction, fullTx bool) map[string]interface{} { fields := formatHeader(header) - fields["transactions"] = txs + + if fullTx { + fields["transactions"] = txs + } else { + txsHashes := []common.Hash{} + for _, tx := range txs { + txsHashes = append(txsHashes, tx.Hash) + } + fields["transactions"] = txsHashes + } // empty values fields["size"] = hexutil.Uint64(0)