Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: get unnecessary block result when only need header #526

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (ante) [#504](https://github.com/crypto-org-chain/ethermint/pull/504) Optimize AnteHandle method to skip checks if disabledMsgs is empty.
* [#517](https://github.com/crypto-org-chain/ethermint/pull/517) Add check for integer overflow to ensure safe conversion.
* [#522](https://github.com/crypto-org-chain/ethermint/pull/522) block-stm executor support optional pre-estimations.
* [#526](https://github.com/crypto-org-chain/ethermint/pull/526) Avoid unnecessary block result in header related api call.

## v0.21.x-cronos

Expand Down
37 changes: 19 additions & 18 deletions rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,15 @@

// BlockNumberFromTendermintByHash returns the block height of given block hash
func (b *Backend) BlockNumberFromTendermintByHash(blockHash common.Hash) (*big.Int, error) {
resBlock, err := b.TendermintBlockByHash(blockHash)
sc, ok := b.clientCtx.Client.(tmrpcclient.SignClient)
if !ok {
b.logger.Error("invalid rpc client")

Check warning on line 254 in rpc/backend/blocks.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/blocks.go#L254

Added line #L254 was not covered by tests
}
resHeader, err := sc.HeaderByHash(b.ctx, blockHash.Bytes())
if err != nil {
return nil, err
}
if resBlock == nil {
return nil, errors.Errorf("block not found for hash %s", blockHash.Hex())
}
return big.NewInt(resBlock.Block.Height), nil
return big.NewInt(resHeader.Header.Height), nil
}

// EthMsgsFromTendermintBlock returns all real MsgEthereumTxs from a
Expand Down Expand Up @@ -325,7 +326,7 @@
// handle the error for pruned node.
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resBlock.Block.Height, "error", err)
}
validator, err := b.getValidatorAccount(resBlock)
validator, err := b.getValidatorAccount(&resBlock.Block.Header)
if err != nil {
return nil, err
}
Expand All @@ -335,34 +336,34 @@

// HeaderByHash returns the block header identified by hash.
func (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) {
resBlock, err := b.TendermintBlockByHash(blockHash)
sc, ok := b.clientCtx.Client.(tmrpcclient.SignClient)
if !ok {
b.logger.Error("invalid rpc client")

Check warning on line 341 in rpc/backend/blocks.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/blocks.go#L341

Added line #L341 was not covered by tests
}
resHeader, err := sc.HeaderByHash(b.ctx, blockHash.Bytes())
if err != nil {
return nil, err
}
if resBlock == nil {
return nil, errors.Errorf("block not found for hash %s", blockHash.Hex())
}

blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height)
blockRes, err := b.TendermintBlockResultByNumber(&resHeader.Header.Height)
if err != nil {
return nil, errors.Errorf("block result not found for height %d", resBlock.Block.Height)
return nil, errors.Errorf("block result not found for height %d", resHeader.Header.Height)
}

bloom, err := b.BlockBloom(blockRes)
if err != nil {
b.logger.Debug("HeaderByHash BlockBloom failed", "height", resBlock.Block.Height)
b.logger.Debug("HeaderByHash BlockBloom failed", "height", resHeader.Header.Height)
}

baseFee, err := b.BaseFee(blockRes)
if err != nil {
// handle the error for pruned node.
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resBlock.Block.Height, "error", err)
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resHeader.Header.Height, "error", err)
}
validator, err := b.getValidatorAccount(resBlock)
validator, err := b.getValidatorAccount(resHeader.Header)
if err != nil {
return nil, err
}
ethHeader := rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee, validator)
ethHeader := rpctypes.EthHeaderFromTendermint(*resHeader.Header, bloom, baseFee, validator)
return ethHeader, nil
}

Expand Down Expand Up @@ -521,7 +522,7 @@
// handle error for pruned node and log
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", height, "error", err)
}
validator, err := b.getValidatorAccount(resBlock)
validator, err := b.getValidatorAccount(&resBlock.Block.Header)
if err != nil {
return nil, err
}
Expand Down
44 changes: 17 additions & 27 deletions rpc/backend/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ func (suite *BackendTestSuite) TestGetBlockByHash() {
height := int64(1)
client := suite.backend.clientCtx.Client.(*mocks.Client)
resBlock, _ = RegisterBlockByHash(client, hash, txBz)

blockRes, _ = RegisterBlockResults(client, height)
RegisterConsensusParams(client, height)

Expand Down Expand Up @@ -674,7 +673,7 @@ func (suite *BackendTestSuite) TestTendermintBlockResultByNumber() {
}

func (suite *BackendTestSuite) TestBlockNumberFromTendermint() {
var resBlock *tmrpctypes.ResultBlock
var resHeader *tmrpctypes.ResultHeader

_, bz := suite.buildEthereumTx()
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
Expand All @@ -701,7 +700,7 @@ func (suite *BackendTestSuite) TestBlockNumberFromTendermint() {
&blockHash,
func(hash *common.Hash) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockByHashError(client, *hash, bz)
RegisterHeaderByHashError(client, *hash, bz)
},
false,
},
Expand All @@ -711,7 +710,7 @@ func (suite *BackendTestSuite) TestBlockNumberFromTendermint() {
&blockHash,
func(hash *common.Hash) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
resBlock, _ = RegisterBlockByHash(client, *hash, bz)
resHeader, _ = RegisterHeaderByHash(client, *hash, bz)
},
true,
},
Expand Down Expand Up @@ -740,7 +739,7 @@ func (suite *BackendTestSuite) TestBlockNumberFromTendermint() {
if tc.hash == nil {
suite.Require().Equal(*tc.blockNum, blockNum)
} else {
expHeight := ethrpc.NewBlockNumber(big.NewInt(resBlock.Block.Height))
expHeight := ethrpc.NewBlockNumber(big.NewInt(resHeader.Header.Height))
suite.Require().Equal(expHeight, blockNum)
}
} else {
Expand All @@ -751,7 +750,7 @@ func (suite *BackendTestSuite) TestBlockNumberFromTendermint() {
}

func (suite *BackendTestSuite) TestBlockNumberFromTendermintByHash() {
var resBlock *tmrpctypes.ResultBlock
var resHeader *tmrpctypes.ResultHeader

_, bz := suite.buildEthereumTx()
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
Expand All @@ -768,7 +767,7 @@ func (suite *BackendTestSuite) TestBlockNumberFromTendermintByHash() {
common.BytesToHash(block.Hash()),
func(hash common.Hash) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockByHashError(client, hash, bz)
RegisterHeaderByHashError(client, hash, bz)
},
false,
},
Expand All @@ -777,7 +776,7 @@ func (suite *BackendTestSuite) TestBlockNumberFromTendermintByHash() {
common.BytesToHash(emptyBlock.Hash()),
func(hash common.Hash) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
resBlock, _ = RegisterBlockByHash(client, hash, bz)
resHeader, _ = RegisterHeaderByHash(client, hash, bz)
},
true,
},
Expand All @@ -786,7 +785,7 @@ func (suite *BackendTestSuite) TestBlockNumberFromTendermintByHash() {
common.BytesToHash(block.Hash()),
func(hash common.Hash) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
resBlock, _ = RegisterBlockByHash(client, hash, bz)
resHeader, _ = RegisterHeaderByHash(client, hash, bz)
},
true,
},
Expand All @@ -798,7 +797,7 @@ func (suite *BackendTestSuite) TestBlockNumberFromTendermintByHash() {
tc.registerMock(tc.hash)
blockNum, err := suite.backend.BlockNumberFromTendermintByHash(tc.hash)
if tc.expPass {
expHeight := big.NewInt(resBlock.Block.Height)
expHeight := big.NewInt(resHeader.Header.Height)
suite.Require().NoError(err)
suite.Require().Equal(expHeight, blockNum)
} else {
Expand Down Expand Up @@ -1298,7 +1297,7 @@ func (suite *BackendTestSuite) TestHeaderByNumber() {
}

func (suite *BackendTestSuite) TestHeaderByHash() {
var expResultBlock *tmrpctypes.ResultBlock
var resHeader *tmrpctypes.ResultHeader

_, bz := suite.buildEthereumTx()
block := tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil)
Expand All @@ -1319,17 +1318,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
sdkmath.NewInt(1).BigInt(),
func(hash common.Hash, baseFee sdkmath.Int) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockByHashError(client, hash, bz)
},
false,
},
{
"fail - block not found for height",
common.BytesToHash(block.Hash()),
sdkmath.NewInt(1).BigInt(),
func(hash common.Hash, baseFee sdkmath.Int) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockByHashNotFound(client, hash, bz)
RegisterHeaderByHashError(client, hash, bz)
},
false,
},
Expand All @@ -1340,8 +1329,9 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
func(hash common.Hash, baseFee sdkmath.Int) {
height := int64(1)
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockByHash(client, hash, bz)
RegisterHeaderByHash(client, hash, bz)
RegisterBlockResultsError(client, height)
RegisterHeaderByHashError(client, hash, bz)
},
false,
},
Expand All @@ -1352,7 +1342,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
func(hash common.Hash, baseFee sdkmath.Int) {
height := int64(1)
client := suite.backend.clientCtx.Client.(*mocks.Client)
expResultBlock, _ = RegisterBlockByHash(client, hash, bz)
resHeader, _ = RegisterHeaderByHash(client, hash, bz)
RegisterBlockResults(client, height)

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
Expand All @@ -1368,7 +1358,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
func(hash common.Hash, baseFee sdkmath.Int) {
height := int64(1)
client := suite.backend.clientCtx.Client.(*mocks.Client)
expResultBlock, _ = RegisterBlockByHash(client, hash, nil)
resHeader, _ = RegisterHeaderByHash(client, hash, bz)
RegisterBlockResults(client, height)

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
Expand All @@ -1384,7 +1374,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
func(hash common.Hash, baseFee sdkmath.Int) {
height := int64(1)
client := suite.backend.clientCtx.Client.(*mocks.Client)
expResultBlock, _ = RegisterBlockByHash(client, hash, bz)
resHeader, _ = RegisterHeaderByHash(client, hash, bz)
RegisterBlockResults(client, height)

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
Expand All @@ -1402,7 +1392,7 @@ func (suite *BackendTestSuite) TestHeaderByHash() {
header, err := suite.backend.HeaderByHash(tc.hash)

if tc.expPass {
expHeader := ethrpc.EthHeaderFromTendermint(expResultBlock.Block.Header, ethtypes.Bloom{}, tc.baseFee, validator)
expHeader := ethrpc.EthHeaderFromTendermint(*resHeader.Header, ethtypes.Bloom{}, tc.baseFee, validator)
suite.Require().NoError(err)
suite.Require().Equal(expHeader, header)
} else {
Expand Down
19 changes: 19 additions & 0 deletions rpc/backend/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,25 @@ func RegisterBlockByHashNotFound(client *mocks.Client, hash common.Hash, tx []by
Return(nil, nil)
}

// HeaderByHash
func RegisterHeaderByHash(
client *mocks.Client,
hash common.Hash,
tx []byte,
) (*tmrpctypes.ResultHeader, error) {
block := types.MakeBlock(1, []types.Tx{tx}, nil, nil)
resHeader := &tmrpctypes.ResultHeader{Header: &block.Header}

client.On("HeaderByHash", rpc.ContextWithHeight(1), bytes.HexBytes(hash.Bytes())).
Return(resHeader, nil)
return resHeader, nil
}

func RegisterHeaderByHashError(client *mocks.Client, hash common.Hash, tx []byte) {
client.On("HeaderByHash", rpc.ContextWithHeight(1), bytes.HexBytes(hash.Bytes())).
Return(nil, errortypes.ErrInvalidRequest)
}

func RegisterABCIQueryWithOptions(client *mocks.Client, height int64, path string, data bytes.HexBytes, opts tmrpcclient.ABCIQueryOptions) {
client.On("ABCIQueryWithOptions", context.Background(), path, data, opts).
Return(&tmrpctypes.ResultABCIQuery{
Expand Down
7 changes: 4 additions & 3 deletions rpc/backend/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
cmttypes "github.com/cometbft/cometbft/types"

"github.com/cometbft/cometbft/proto/tendermint/crypto"
"github.com/evmos/ethermint/rpc/types"
Expand Down Expand Up @@ -326,11 +327,11 @@ func GetHexProofs(proof *crypto.ProofOps) []string {
return proofs
}

func (b *Backend) getValidatorAccount(resBlock *tmrpctypes.ResultBlock) (sdk.AccAddress, error) {
func (b *Backend) getValidatorAccount(header *cmttypes.Header) (sdk.AccAddress, error) {
res, err := b.queryClient.ValidatorAccount(
types.ContextWithHeight(resBlock.Block.Header.Height),
types.ContextWithHeight(header.Height),
&evmtypes.QueryValidatorAccountRequest{
ConsAddress: sdk.ConsAddress(resBlock.Block.Header.ProposerAddress).String(),
ConsAddress: sdk.ConsAddress(header.ProposerAddress).String(),
},
)
if err != nil {
Expand Down
Loading