From c6dae312319ce2f29015baff9dcfcf9f7c61e520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Kunze=20K=C3=BCllmer?= <31522760+fedekunze@users.noreply.github.com> Date: Mon, 12 Jul 2021 06:25:15 -0400 Subject: [PATCH] evm: fix `BlockBloom` not found for latest height (#263) --- x/evm/keeper/grpc_query.go | 16 +++++++++++++--- x/evm/keeper/grpc_query_test.go | 13 ++++++++++++- x/evm/keeper/keeper.go | 10 ++++++---- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/x/evm/keeper/grpc_query.go b/x/evm/keeper/grpc_query.go index e0b2dda5..66d2e5dc 100644 --- a/x/evm/keeper/grpc_query.go +++ b/x/evm/keeper/grpc_query.go @@ -9,9 +9,11 @@ import ( "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/query" ethcmn "github.com/ethereum/go-ethereum/common" + ethtypes "github.com/ethereum/go-ethereum/core/types" ethermint "github.com/tharsis/ethermint/types" "github.com/tharsis/ethermint/x/evm/types" @@ -268,9 +270,17 @@ func (k Keeper) BlockBloom(c context.Context, _ *types.QueryBlockBloomRequest) ( bloom, found := k.GetBlockBloom(ctx, ctx.BlockHeight()) if !found { - return nil, status.Error( - codes.NotFound, types.ErrBloomNotFound.Error(), - ) + // 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(), + ) + } + + bloom = ethtypes.BytesToBloom(bloomInt.Bytes()) } return &types.QueryBlockBloomResponse{ diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index a21a2fe1..58d98abf 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -544,12 +544,23 @@ func (suite *KeeperTestSuite) TestQueryBlockBloom() { func() {}, false, }, + { + "bloom from transient store", + func() { + req = &types.QueryBlockBloomRequest{} + bloom := ethtypes.BytesToBloom([]byte("bloom")) + expBloom = bloom.Bytes() + suite.app.EvmKeeper.WithContext(suite.ctx.WithBlockHeight(1)) + suite.app.EvmKeeper.SetBlockBloomTransient(bloom.Big()) + }, + true, + }, {"bloom not found for height", func() { req = &types.QueryBlockBloomRequest{} bloom := ethtypes.BytesToBloom([]byte("bloom")) expBloom = bloom.Bytes() - suite.ctx = suite.ctx.WithBlockHeight(10) + suite.ctx = suite.ctx.WithBlockHeight(100) suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 2, bloom) }, false, diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index a73e2118..19852ffa 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -130,8 +130,9 @@ func (k Keeper) SetBlockBloom(ctx sdk.Context, height int64, bloom ethtypes.Bloo // GetBlockBloomTransient returns bloom bytes for the current block height func (k Keeper) GetBlockBloomTransient() *big.Int { - store := k.ctx.TransientStore(k.transientKey) - bz := store.Get(types.KeyPrefixTransientBloom) + store := prefix.NewStore(k.ctx.TransientStore(k.transientKey), types.KeyPrefixTransientBloom) + heightBz := sdk.Uint64ToBigEndian(uint64(k.ctx.BlockHeight())) + bz := store.Get(heightBz) if len(bz) == 0 { return big.NewInt(0) } @@ -142,8 +143,9 @@ func (k Keeper) GetBlockBloomTransient() *big.Int { // SetBlockBloomTransient sets the given bloom bytes to the transient store. This value is reset on // every block. func (k Keeper) SetBlockBloomTransient(bloom *big.Int) { - store := k.ctx.TransientStore(k.transientKey) - store.Set(types.KeyPrefixTransientBloom, bloom.Bytes()) + store := prefix.NewStore(k.ctx.TransientStore(k.transientKey), types.KeyPrefixTransientBloom) + heightBz := sdk.Uint64ToBigEndian(uint64(k.ctx.BlockHeight())) + store.Set(heightBz, bloom.Bytes()) } // ----------------------------------------------------------------------------