Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
rpc: fix Bloom filter response (#321)
Browse files Browse the repository at this point in the history
* fix bloomfilter in rpc response

* add comments
  • Loading branch information
thomas-nguy authored Jul 20, 2021
1 parent bb6622b commit 0276f34
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 92 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (eth) [\#845](https://github.com/cosmos/ethermint/pull/845) The `eth` namespace must be included in the list of API's as default to run the rpc server without error.
* (evm) [#202](https://github.com/tharsis/ethermint/pull/202) Web3 api `SendTransaction`/`SendRawTransaction` returns ethereum compatible transaction hash, and query api `GetTransaction*` also accept that.
* (rpc) [tharsis#258](https://github.com/tharsis/ethermint/pull/258) Return empty `BloomFilter` instead of throwing an error when it cannot be found (`nil` or empty).
* (rpc) [tharsis#277](https://github.com/tharsis/ethermint/pull/321) Fix `BloomFilter` response.

### Improvements

Expand Down
5 changes: 5 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,11 @@ QueryBlockBloomRequest is the request type for the Query/BlockBloom RPC
method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `height` | [int64](#int64) | | height of the block which we want to query the bloom filter. Tendermint always replace the query request header by the current context header, height cannot be extracted from there, so we need to explicitly pass it in parameter. |





Expand Down
6 changes: 3 additions & 3 deletions ethereum/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (e *EVMBackend) EthBlockFromTendermint(
}
}

blockBloomResp, err := e.queryClient.BlockBloom(types.ContextWithHeight(block.Height), &evmtypes.QueryBlockBloomRequest{})
blockBloomResp, err := e.queryClient.BlockBloom(types.ContextWithHeight(block.Height), &evmtypes.QueryBlockBloomRequest{Height: block.Height})
if err != nil {
e.logger.Debug("failed to query BlockBloom", "height", block.Height, "error", err.Error())

Expand Down Expand Up @@ -266,7 +266,7 @@ func (e *EVMBackend) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Heade
return nil, err
}

req := &evmtypes.QueryBlockBloomRequest{}
req := &evmtypes.QueryBlockBloomRequest{Height: resBlock.Block.Height}

blockBloomResp, err := e.queryClient.BlockBloom(types.ContextWithHeight(resBlock.Block.Height), req)
if err != nil {
Expand All @@ -287,7 +287,7 @@ func (e *EVMBackend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, erro
return nil, err
}

req := &evmtypes.QueryBlockBloomRequest{}
req := &evmtypes.QueryBlockBloomRequest{Height: resBlock.Block.Height}

blockBloomResp, err := e.queryClient.BlockBloom(types.ContextWithHeight(resBlock.Block.Height), req)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions ethereum/rpc/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewBlockNumber(n *big.Int) BlockNumber {

// ContextWithHeight wraps a context with the a gRPC block height header. If the provided height is
// 0, it will return an empty context and the gRPC query will use the latest block height for querying.
// Note that all metadata are processed and removed by tendermint layer, so it wont be accessible at gRPC server level.
func ContextWithHeight(height int64) context.Context {
if height == 0 {
return context.Background()
Expand Down
8 changes: 7 additions & 1 deletion proto/ethermint/evm/v1alpha1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ message QueryBlockLogsResponse {

// QueryBlockBloomRequest is the request type for the Query/BlockBloom RPC
// method.
message QueryBlockBloomRequest {}
message QueryBlockBloomRequest {
// height of the block which we want to query the bloom filter.
// Tendermint always replace the query request header by the current context
// header, height cannot be extracted from there, so we need to explicitly pass
// it in parameter.
int64 height = 1;
}

// QueryBlockBloomResponse is the response type for the Query/BlockBloom RPC
// method.
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,18 @@ func (k Keeper) BlockLogs(c context.Context, req *types.QueryBlockLogsRequest) (
}

// BlockBloom implements the Query/BlockBloom gRPC method
func (k Keeper) BlockBloom(c context.Context, _ *types.QueryBlockBloomRequest) (*types.QueryBlockBloomResponse, error) {
func (k Keeper) BlockBloom(c context.Context, req *types.QueryBlockBloomRequest) (*types.QueryBlockBloomResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

bloom, found := k.GetBlockBloom(ctx, ctx.BlockHeight())
bloom, found := k.GetBlockBloom(ctx, req.Height)
if !found {
// if the bloom is not found, query the transient store at the current height
k.ctx = ctx
bloomInt := k.GetBlockBloomTransient()

if bloomInt.Sign() == 0 {
return nil, status.Error(
codes.NotFound, sdkerrors.Wrapf(types.ErrBloomNotFound, "height: %d", ctx.BlockHeight()).Error(),
codes.NotFound, sdkerrors.Wrapf(types.ErrBloomNotFound, "height: %d", req.Height).Error(),
)
}

Expand Down
16 changes: 9 additions & 7 deletions x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,16 @@ func (suite *KeeperTestSuite) TestQueryBlockBloom() {
malleate func()
expPass bool
}{
{"marshal error",
func() {},
{"bad height",
func() {
req = &types.QueryBlockBloomRequest{Height: -2}
},
false,
},
{
"bloom from transient store",
func() {
req = &types.QueryBlockBloomRequest{}
req = &types.QueryBlockBloomRequest{Height: 1}
bloom := ethtypes.BytesToBloom([]byte("bloom"))
expBloom = bloom.Bytes()
suite.app.EvmKeeper.WithContext(suite.ctx.WithBlockHeight(1))
Expand All @@ -557,7 +559,7 @@ func (suite *KeeperTestSuite) TestQueryBlockBloom() {
},
{"bloom not found for height",
func() {
req = &types.QueryBlockBloomRequest{}
req = &types.QueryBlockBloomRequest{Height: 100}
bloom := ethtypes.BytesToBloom([]byte("bloom"))
expBloom = bloom.Bytes()
suite.ctx = suite.ctx.WithBlockHeight(100)
Expand All @@ -568,11 +570,11 @@ func (suite *KeeperTestSuite) TestQueryBlockBloom() {
{
"success",
func() {
req = &types.QueryBlockBloomRequest{}
req = &types.QueryBlockBloomRequest{Height: 3}
bloom := ethtypes.BytesToBloom([]byte("bloom"))
expBloom = bloom.Bytes()
suite.ctx = suite.ctx.WithBlockHeight(1)
suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 1, bloom)
suite.ctx = suite.ctx.WithBlockHeight(3)
suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 3, bloom)
},
true,
},
Expand Down
Loading

0 comments on commit 0276f34

Please sign in to comment.