Skip to content

Commit 2c2e1ae

Browse files
committed
fix: inconsistent block hash in json-rpc responses
Update CHANGELOG.md unify unify
1 parent d6d2957 commit 2c2e1ae

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [\#687](https://github.com/cosmos/evm/pull/687) Avoid blocking node shutdown when evm indexer is enabled, log startup failures instead of using errgroup.
2323
- [\#689](https://github.com/cosmos/evm/pull/689) Align debug addr for hex address.
2424
- [\#668](https://github.com/cosmos/evm/pull/668) Fix panic in legacy mempool when Reset() was called with a skipped header between old and new block.
25+
- [\#725](https://github.com/cosmos/evm/pull/725) Fix inconsistent block hash in json-rpc.
2526

2627
### IMPROVEMENTS
2728

rpc/backend/comet_to_eth.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ func (b *Backend) RPCHeaderFromCometBlock(
2525
resBlock *cmtrpctypes.ResultBlock,
2626
blockRes *cmtrpctypes.ResultBlockResults,
2727
) (map[string]interface{}, error) {
28-
cmtBlock := resBlock.Block
2928
ethBlock, err := b.EthBlockFromCometBlock(resBlock, blockRes)
3029
if err != nil {
3130
return nil, fmt.Errorf("failed to get rpc block from comet block: %w", err)
3231
}
3332

34-
return rpctypes.RPCMarshalHeader(ethBlock.Header(), cmtBlock.Header), nil
33+
return rpctypes.RPCMarshalHeader(ethBlock.Header(), resBlock.BlockID.Hash), nil
3534
}
3635

3736
// RPCBlockFromCometBlock returns a JSON-RPC compatible Ethereum block from a
@@ -41,14 +40,13 @@ func (b *Backend) RPCBlockFromCometBlock(
4140
blockRes *cmtrpctypes.ResultBlockResults,
4241
fullTx bool,
4342
) (map[string]interface{}, error) {
44-
cmtBlock := resBlock.Block
4543
msgs := b.EthMsgsFromCometBlock(resBlock, blockRes)
4644
ethBlock, err := b.EthBlockFromCometBlock(resBlock, blockRes)
4745
if err != nil {
4846
return nil, fmt.Errorf("failed to get rpc block from comet block: %w", err)
4947
}
5048

51-
return rpctypes.RPCMarshalBlock(ethBlock, cmtBlock, msgs, true, fullTx, b.ChainConfig())
49+
return rpctypes.RPCMarshalBlock(ethBlock, resBlock, msgs, true, fullTx, b.ChainConfig())
5250
}
5351

5452
// BlockNumberFromComet returns the BlockNumber from BlockNumberOrHash
@@ -235,7 +233,7 @@ func (b *Backend) ReceiptsFromCometBlock(
235233
b.Logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resBlock.Block.Height, "error", err)
236234
}
237235

238-
blockHash := common.BytesToHash(resBlock.Block.Header.Hash())
236+
blockHash := common.BytesToHash(resBlock.BlockID.Hash)
239237
receipts := make([]*ethtypes.Receipt, len(msgs))
240238
cumulatedGasUsed := uint64(0)
241239
for i, ethMsg := range msgs {

rpc/backend/tx_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func (b *Backend) GetTransactionByBlockAndIndex(block *cmtrpctypes.ResultBlock,
403403
index := uint64(idx) // #nosec G115 -- checked for int overflow already
404404
return rpctypes.NewTransactionFromMsg(
405405
msg,
406-
common.BytesToHash(block.Block.Hash()),
406+
common.BytesToHash(block.BlockID.Hash),
407407
height,
408408
blockTime,
409409
index,

rpc/stream/rpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (s *RPCStream) start(
165165
// TODO: After indexer improvement, we should get eth header event from indexer
166166
// Currently, many fields are missing or incorrect (e.g. bloom, receiptsRoot, ...)
167167
header := types.EthHeaderFromComet(data.Block.Header, ethtypes.Bloom{}, baseFee)
168-
s.headerStream.Add(RPCHeader{EthHeader: header, Hash: common.BytesToHash(data.Block.Header.Hash())})
168+
s.headerStream.Add(RPCHeader{EthHeader: header, Hash: common.BytesToHash(data.BlockID.Hash)})
169169

170170
case ev, ok := <-chLogs:
171171
if !ok {

rpc/types/utils.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
abci "github.com/cometbft/cometbft/abci/types"
1717
cmtrpcclient "github.com/cometbft/cometbft/rpc/client"
18+
cmtrpccore "github.com/cometbft/cometbft/rpc/core/types"
1819
cmttypes "github.com/cometbft/cometbft/types"
1920

2021
feemarkettypes "github.com/cosmos/evm/x/feemarket/types"
@@ -382,10 +383,10 @@ func CalcBaseFee(config *ethparams.ChainConfig, parent *ethtypes.Header, p feema
382383
// This method refers to internal package method of go-ethereum v1.16.3 - RPCMarshalHeader
383384
// (https://github.com/ethereum/go-ethereum/blob/d818a9af7bd5919808df78f31580f59382c53150/internal/ethapi/api.go#L888-L927)
384385
// but it uses the cometbft Header to get the block hash.
385-
func RPCMarshalHeader(head *ethtypes.Header, cmtHeader cmttypes.Header) map[string]interface{} {
386+
func RPCMarshalHeader(head *ethtypes.Header, blockHash []byte) map[string]interface{} {
386387
result := map[string]interface{}{
387388
"number": (*hexutil.Big)(head.Number),
388-
"hash": hexutil.Bytes(cmtHeader.Hash()), // use cometbft header hash
389+
"hash": hexutil.Bytes(blockHash), // use cometbft header hash
389390
"parentHash": head.ParentHash,
390391
"nonce": head.Nonce,
391392
"mixHash": head.MixDigest,
@@ -428,8 +429,9 @@ func RPCMarshalHeader(head *ethtypes.Header, cmtHeader cmttypes.Header) map[stri
428429
//
429430
// This method refers to go-ethereum v1.16.3 internal package method - RPCMarshalBlock
430431
// (https://github.com/ethereum/go-ethereum/blob/d818a9af7bd5919808df78f31580f59382c53150/internal/ethapi/api.go#L929-L962)
431-
func RPCMarshalBlock(block *ethtypes.Block, cmtBlock *cmttypes.Block, msgs []*evmtypes.MsgEthereumTx, inclTx bool, fullTx bool, config *ethparams.ChainConfig) (map[string]interface{}, error) {
432-
fields := RPCMarshalHeader(block.Header(), cmtBlock.Header)
432+
func RPCMarshalBlock(block *ethtypes.Block, cmtBlock *cmtrpccore.ResultBlock, msgs []*evmtypes.MsgEthereumTx, inclTx bool, fullTx bool, config *ethparams.ChainConfig) (map[string]interface{}, error) {
433+
blockHash := cmtBlock.BlockID.Hash.Bytes()
434+
fields := RPCMarshalHeader(block.Header(), blockHash)
433435
fields["size"] = hexutil.Uint64(block.Size())
434436

435437
if inclTx {
@@ -439,7 +441,7 @@ func RPCMarshalBlock(block *ethtypes.Block, cmtBlock *cmttypes.Block, msgs []*ev
439441
if fullTx {
440442
formatTx = func(idx int, _ *ethtypes.Transaction) interface{} {
441443
txIdx := uint64(idx) //nolint:gosec // G115
442-
return newRPCTransactionFromBlockIndex(block, txIdx, config)
444+
return newRPCTransactionFromBlockIndex(block, common.BytesToHash(blockHash), txIdx, config)
443445
}
444446
}
445447
txs := block.Transactions()
@@ -462,12 +464,12 @@ func RPCMarshalBlock(block *ethtypes.Block, cmtBlock *cmttypes.Block, msgs []*ev
462464
}
463465

464466
// newRPCTransactionFromBlockIndex returns a transaction that will serialize to the RPC representation.
465-
func newRPCTransactionFromBlockIndex(b *ethtypes.Block, index uint64, config *ethparams.ChainConfig) *RPCTransaction {
467+
func newRPCTransactionFromBlockIndex(b *ethtypes.Block, blockHash common.Hash, index uint64, config *ethparams.ChainConfig) *RPCTransaction {
466468
txs := b.Transactions()
467469
if index >= uint64(len(txs)) {
468470
return nil
469471
}
470-
return NewRPCTransaction(txs[index], b.Hash(), b.NumberU64(), b.Time(), index, b.BaseFee(), config)
472+
return NewRPCTransaction(txs[index], blockHash, b.NumberU64(), b.Time(), index, b.BaseFee(), config)
471473
}
472474

473475
// RPCMarshalReceipt marshals a transaction receipt into a JSON object.

0 commit comments

Comments
 (0)