evm: fix BlockBloom not found for latest height (#263)

This commit is contained in:
Federico Kunze Küllmer 2021-07-12 06:25:15 -04:00 committed by GitHub
parent fa77bae105
commit c6dae31231
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 8 deletions

View File

@ -9,9 +9,11 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix" "github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/types/query"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethermint "github.com/tharsis/ethermint/types" ethermint "github.com/tharsis/ethermint/types"
"github.com/tharsis/ethermint/x/evm/types" "github.com/tharsis/ethermint/x/evm/types"
@ -268,11 +270,19 @@ func (k Keeper) BlockBloom(c context.Context, _ *types.QueryBlockBloomRequest) (
bloom, found := k.GetBlockBloom(ctx, ctx.BlockHeight()) bloom, found := k.GetBlockBloom(ctx, ctx.BlockHeight())
if !found { 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( return nil, status.Error(
codes.NotFound, types.ErrBloomNotFound.Error(), codes.NotFound, sdkerrors.Wrapf(types.ErrBloomNotFound, "height: %d", ctx.BlockHeight()).Error(),
) )
} }
bloom = ethtypes.BytesToBloom(bloomInt.Bytes())
}
return &types.QueryBlockBloomResponse{ return &types.QueryBlockBloomResponse{
Bloom: bloom.Bytes(), Bloom: bloom.Bytes(),
}, nil }, nil

View File

@ -544,12 +544,23 @@ func (suite *KeeperTestSuite) TestQueryBlockBloom() {
func() {}, func() {},
false, 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", {"bloom not found for height",
func() { func() {
req = &types.QueryBlockBloomRequest{} req = &types.QueryBlockBloomRequest{}
bloom := ethtypes.BytesToBloom([]byte("bloom")) bloom := ethtypes.BytesToBloom([]byte("bloom"))
expBloom = bloom.Bytes() expBloom = bloom.Bytes()
suite.ctx = suite.ctx.WithBlockHeight(10) suite.ctx = suite.ctx.WithBlockHeight(100)
suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 2, bloom) suite.app.EvmKeeper.SetBlockBloom(suite.ctx, 2, bloom)
}, },
false, false,

View File

@ -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 // GetBlockBloomTransient returns bloom bytes for the current block height
func (k Keeper) GetBlockBloomTransient() *big.Int { func (k Keeper) GetBlockBloomTransient() *big.Int {
store := k.ctx.TransientStore(k.transientKey) store := prefix.NewStore(k.ctx.TransientStore(k.transientKey), types.KeyPrefixTransientBloom)
bz := store.Get(types.KeyPrefixTransientBloom) heightBz := sdk.Uint64ToBigEndian(uint64(k.ctx.BlockHeight()))
bz := store.Get(heightBz)
if len(bz) == 0 { if len(bz) == 0 {
return big.NewInt(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 // SetBlockBloomTransient sets the given bloom bytes to the transient store. This value is reset on
// every block. // every block.
func (k Keeper) SetBlockBloomTransient(bloom *big.Int) { func (k Keeper) SetBlockBloomTransient(bloom *big.Int) {
store := k.ctx.TransientStore(k.transientKey) store := prefix.NewStore(k.ctx.TransientStore(k.transientKey), types.KeyPrefixTransientBloom)
store.Set(types.KeyPrefixTransientBloom, bloom.Bytes()) heightBz := sdk.Uint64ToBigEndian(uint64(k.ctx.BlockHeight()))
store.Set(heightBz, bloom.Bytes())
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------