From fcb7c114d0057f7a682ed60d06168003a0eb2887 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Mon, 7 Jun 2021 07:02:52 -0400 Subject: [PATCH] rpc: fix `eth_getBlockByNumber` (#78) --- ethereum/rpc/backend.go | 59 +++++++++++++++++++------------------ ethereum/rpc/types/utils.go | 2 +- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/ethereum/rpc/backend.go b/ethereum/rpc/backend.go index 9178b232bc..3e15ee0e77 100644 --- a/ethereum/rpc/backend.go +++ b/ethereum/rpc/backend.go @@ -137,44 +137,47 @@ func (e *EVMBackend) EthBlockFromTendermint( txReceiptsResp, err := queryClient.TxReceiptsByBlockHeight(types.ContextWithHeight(block.Height), req) if err != nil { - e.logger.Debugf("TxReceiptsByBlockHeight fail: %s", err.Error()) - return nil, err + e.logger.WithError(err).Debugln("TxReceiptsByBlockHeight failed") } gasUsed := big.NewInt(0) - ethRPCTxs := make([]interface{}, 0, len(txReceiptsResp.Receipts)) - - for _, receipt := range txReceiptsResp.Receipts { - hash := common.HexToHash(receipt.Hash) - if fullTx { - // full txs from receipts - tx, err := types.NewTransactionFromData( - receipt.Data, - common.HexToAddress(receipt.From), - hash, - common.HexToHash(receipt.BlockHash), - receipt.BlockHeight, - receipt.Index, - ) - - if err != nil { - e.logger.WithError(err).Warningf("NewTransactionFromData for receipt %s failed", hash) - continue + ethRPCTxs := []interface{}{} + + if txReceiptsResp != nil { + + for _, receipt := range txReceiptsResp.Receipts { + hash := common.HexToHash(receipt.Hash) + if fullTx { + // full txs from receipts + tx, err := types.NewTransactionFromData( + receipt.Data, + common.HexToAddress(receipt.From), + hash, + common.HexToHash(receipt.BlockHash), + receipt.BlockHeight, + receipt.Index, + ) + + if err != nil { + e.logger.WithError(err).Warningf("NewTransactionFromData for receipt %s failed", hash) + continue + } + + ethRPCTxs = append(ethRPCTxs, tx) + gasUsed.Add(gasUsed, new(big.Int).SetUint64(receipt.Result.GasUsed)) + } else { + // simply hashes + ethRPCTxs = append(ethRPCTxs, hash) } - - ethRPCTxs = append(ethRPCTxs, tx) - gasUsed.Add(gasUsed, new(big.Int).SetUint64(receipt.Result.GasUsed)) - } else { - // simply hashes - ethRPCTxs = append(ethRPCTxs, hash) } } blockBloomResp, err := queryClient.BlockBloom(types.ContextWithHeight(block.Height), &evmtypes.QueryBlockBloomRequest{}) if err != nil { - e.logger.WithError(err).Debugln("failed to query BlockBloom at height", block.Height) - blockBloomResp.Bloom = ethtypes.Bloom{}.Bytes() + e.logger.WithError(err).Debugln("failed to query BlockBloom", "height", block.Height) + + blockBloomResp = &evmtypes.QueryBlockBloomResponse{Bloom: ethtypes.Bloom{}.Bytes()} } bloom := ethtypes.BytesToBloom(blockBloomResp.Bloom) diff --git a/ethereum/rpc/types/utils.go b/ethereum/rpc/types/utils.go index c14ba6b202..e9aed2cf3d 100644 --- a/ethereum/rpc/types/utils.go +++ b/ethereum/rpc/types/utils.go @@ -199,7 +199,7 @@ func FormatBlock( "receiptsRoot": ethtypes.EmptyRootHash, "uncles": []common.Hash{}, - "transactions": transactions.([]common.Hash), + "transactions": transactions, "totalDifficulty": (*hexutil.Big)(big.NewInt(0)), } }