From 956b18f45eb47ea52313e82882ddb68c7f446ff5 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Wed, 26 May 2021 06:33:24 -0400 Subject: [PATCH] rpc: fix empty root and uncle hash (#45) * rpc: fix empty root and uncle hash * changelog --- CHANGELOG.md | 4 ++++ ethereum/rpc/types/utils.go | 36 ++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87a3fca2..8bcf8f1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (evm) [tharsis#24](https://github.com/tharsis/ethermint/pull/24)Implement metrics for `MsgEthereumTx`, state transtitions, `BeginBlock` and `EndBlock`. * (deps) [\#602](https://github.com/cosmos/ethermint/pull/856) Bump tendermint version to [v0.39.3](https://github.com/tendermint/tendermint/releases/tag/v0.39.3) +### Bug Fixes + +* (rpc) [tharsis#45](https://github.com/tharsis/ethermint/pull/45) Use `EmptyUncleHash` and `EmptyRootHash` for empty ethereum `Header` fields. + ## [v0.4.1] - 2021-03-01 ### API Breaking diff --git a/ethereum/rpc/types/utils.go b/ethereum/rpc/types/utils.go index 228fafdc..c14ba6b2 100644 --- a/ethereum/rpc/types/utils.go +++ b/ethereum/rpc/types/utils.go @@ -108,15 +108,22 @@ func NewTransaction(tx *ethtypes.Transaction, blockHash common.Hash, blockNumber // EthHeaderFromTendermint is an util function that returns an Ethereum Header // from a tendermint Header. func EthHeaderFromTendermint(header tmtypes.Header) *ethtypes.Header { + txHash := ethtypes.EmptyRootHash + if len(header.DataHash) == 0 { + txHash = common.BytesToHash(header.DataHash) + } return ðtypes.Header{ ParentHash: common.BytesToHash(header.LastBlockID.Hash.Bytes()), - UncleHash: common.Hash{}, + UncleHash: ethtypes.EmptyUncleHash, Coinbase: common.Address{}, Root: common.BytesToHash(header.AppHash), - TxHash: common.BytesToHash(header.DataHash), - ReceiptHash: common.Hash{}, - Difficulty: nil, + TxHash: txHash, + ReceiptHash: ethtypes.EmptyRootHash, + Bloom: ethtypes.Bloom{}, + Difficulty: big.NewInt(0), Number: big.NewInt(header.Height), + GasLimit: 0, + GasUsed: 0, Time: uint64(header.Time.Unix()), Extra: nil, MixDigest: common.Hash{}, @@ -176,23 +183,24 @@ func FormatBlock( "number": hexutil.Uint64(header.Height), "hash": hexutil.Bytes(header.Hash()), "parentHash": hexutil.Bytes(header.LastBlockID.Hash), - "nonce": hexutil.Uint64(0), // PoW specific - "sha3Uncles": common.Hash{}, // No uncles in Tendermint + "nonce": hexutil.Uint64(0), // PoW specific + "sha3Uncles": ethtypes.EmptyUncleHash, // No uncles in Tendermint "logsBloom": bloom, - "transactionsRoot": hexutil.Bytes(header.DataHash), "stateRoot": hexutil.Bytes(header.AppHash), "miner": common.Address{}, "mixHash": common.Hash{}, - "difficulty": 0, - "totalDifficulty": 0, + "difficulty": (*hexutil.Big)(big.NewInt(0)), "extraData": hexutil.Uint64(0), "size": hexutil.Uint64(size), "gasLimit": hexutil.Uint64(gasLimit), // Static gas limit "gasUsed": (*hexutil.Big)(gasUsed), "timestamp": hexutil.Uint64(header.Time.Unix()), - "transactions": transactions.([]common.Hash), - "uncles": []string{}, - "receiptsRoot": common.Hash{}, + "transactionsRoot": hexutil.Bytes(header.DataHash), + "receiptsRoot": ethtypes.EmptyRootHash, + + "uncles": []common.Hash{}, + "transactions": transactions.([]common.Hash), + "totalDifficulty": (*hexutil.Big)(big.NewInt(0)), } } @@ -308,6 +316,10 @@ func NewTransactionFromData( to = &recipient } + if txHash == (common.Hash{}) { + txHash = ethtypes.EmptyRootHash + } + rpcTx := &RPCTransaction{ From: from, Gas: hexutil.Uint64(txData.GasLimit),