Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit 6d11e23

Browse files
authored
rpc: fix method to calculate block hash and fix mismatch block hash in eth.getBlock response (#755)
1 parent e1349e4 commit 6d11e23

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
4343
* (evm) [\#670](https://github.com/cosmos/ethermint/pull/670) Migrate types to the ones defined by the protobuf messages, which are required for the stargate release.
4444

4545
### Bug Fixes
46-
46+
* (evm) [\#751](https://github.com/cosmos/ethermint/issues/751) Fix misused method to calculate block hash in evm related function.
47+
* (evm) [\#721](https://github.com/cosmos/ethermint/issues/721) Fix mismatch block hash in rpc response when use eht.getBlock.
4748
* (evm) [\#730](https://github.com/cosmos/ethermint/issues/730) Fix 'EIP2028' not open when Istanbul version has been enabled.
4849
* (evm) [\#749](https://github.com/cosmos/ethermint/issues/749) Fix panic in `AnteHandler` when gas price larger than 100000
4950
* (evm) [\#747](https://github.com/cosmos/ethermint/issues/747) Fix format errors in String() of QueryETHLogs

rpc/namespaces/eth/api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,11 @@ func (api *PublicEthereumAPI) GetBlockByNumber(blockNum rpctypes.BlockNumber, fu
689689
ChainID: api.clientCtx.ChainID,
690690
Height: height + 1,
691691
Time: time.Unix(0, 0),
692-
LastBlockID: latestBlock.BlockID,
692+
LastBlockID: latestBlock.Block.LastBlockID,
693693
ValidatorsHash: latestBlock.Block.NextValidatorsHash,
694694
},
695695
0,
696+
latestBlock.Block.Hash(),
696697
0,
697698
gasUsed,
698699
pendingTxs,
@@ -731,7 +732,7 @@ func (api *PublicEthereumAPI) GetTransactionByHash(hash common.Hash) (*rpctypes.
731732
return nil, err
732733
}
733734

734-
blockHash := common.BytesToHash(block.Block.Header.Hash())
735+
blockHash := common.BytesToHash(block.Block.Hash())
735736

736737
ethTx, err := rpctypes.RawTxToEthTx(api.clientCtx, tx.Tx)
737738
if err != nil {
@@ -817,7 +818,7 @@ func (api *PublicEthereumAPI) getTransactionByBlockAndIndex(block *tmtypes.Block
817818

818819
height := uint64(block.Height)
819820
txHash := common.BytesToHash(block.Txs[idx].Hash())
820-
blockHash := common.BytesToHash(block.Header.Hash())
821+
blockHash := common.BytesToHash(block.Hash())
821822
return rpctypes.NewTransaction(ethTx, txHash, blockHash, height, uint64(idx))
822823
}
823824

@@ -836,7 +837,7 @@ func (api *PublicEthereumAPI) GetTransactionReceipt(hash common.Hash) (map[strin
836837
return nil, err
837838
}
838839

839-
blockHash := common.BytesToHash(block.Block.Header.Hash())
840+
blockHash := common.BytesToHash(block.Block.Hash())
840841

841842
// Convert tx bytes to eth transaction
842843
ethTx, err := rpctypes.RawTxToEthTx(api.clientCtx, tx.Tx)

rpc/types/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func EthBlockFromTendermint(clientCtx clientcontext.CLIContext, block *tmtypes.B
9090

9191
bloom := bloomRes.Bloom
9292

93-
return FormatBlock(block.Header, block.Size(), gasLimit, gasUsed, transactions, bloom), nil
93+
return FormatBlock(block.Header, block.Size(), block.Hash(), gasLimit, gasUsed, transactions, bloom), nil
9494
}
9595

9696
// EthHeaderFromTendermint is an util function that returns an Ethereum Header
@@ -153,7 +153,7 @@ func BlockMaxGasFromConsensusParams(_ context.Context, clientCtx clientcontext.C
153153
// FormatBlock creates an ethereum block from a tendermint header and ethereum-formatted
154154
// transactions.
155155
func FormatBlock(
156-
header tmtypes.Header, size int, gasLimit int64,
156+
header tmtypes.Header, size int, curBlockHash tmbytes.HexBytes, gasLimit int64,
157157
gasUsed *big.Int, transactions interface{}, bloom ethtypes.Bloom,
158158
) map[string]interface{} {
159159
if len(header.DataHash) == 0 {
@@ -162,7 +162,7 @@ func FormatBlock(
162162

163163
return map[string]interface{}{
164164
"number": hexutil.Uint64(header.Height),
165-
"hash": hexutil.Bytes(header.Hash()),
165+
"hash": hexutil.Bytes(curBlockHash),
166166
"parentHash": hexutil.Bytes(header.LastBlockID.Hash),
167167
"nonce": hexutil.Uint64(0), // PoW specific
168168
"sha3Uncles": common.Hash{}, // No uncles in Tendermint

x/evm/client/rest/rest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func getEthTransactionByHash(cliCtx context.CLIContext, hashHex string) ([]byte,
8282
return nil, err
8383
}
8484

85-
blockHash := common.BytesToHash(block.Block.Header.Hash())
85+
blockHash := common.BytesToHash(block.Block.Hash())
8686

8787
ethTx, err := rpctypes.RawTxToEthTx(cliCtx, tx.Tx)
8888
if err != nil {

0 commit comments

Comments
 (0)