Skip to content

Commit

Permalink
fix format block to handle fullTx false case (#53)
Browse files Browse the repository at this point in the history
* fix format block to handle fullTx false case

* fill parent hash
  • Loading branch information
beer-1 authored Aug 27, 2024
1 parent 4adcd17 commit 9d70636
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
8 changes: 8 additions & 0 deletions indexer/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
47 changes: 26 additions & 21 deletions jsonrpc/backend/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit 9d70636

Please sign in to comment.