From b073b945e317999ea9fd2bc8f101279b12ad73ae Mon Sep 17 00:00:00 2001 From: Bernd Mueller Date: Mon, 13 Nov 2023 09:20:04 +0100 Subject: [PATCH] Changed to store service --- app/consumer-democracy/app.go | 6 +- app/consumer/app.go | 6 +- app/provider/app.go | 3 +- tests/integration/channel_init.go | 5 +- tests/integration/distribution.go | 32 ++- tests/integration/throttle.go | 22 +- testutil/keeper/unit_test_helpers.go | 7 +- x/ccv/consumer/ibc_module_test.go | 6 +- x/ccv/consumer/keeper/distribution.go | 8 +- x/ccv/consumer/keeper/genesis.go | 6 +- x/ccv/consumer/keeper/genesis_test.go | 12 +- x/ccv/consumer/keeper/grpc_query.go | 5 +- x/ccv/consumer/keeper/keeper.go | 279 ++++++++++++---------- x/ccv/consumer/keeper/migration.go | 10 +- x/ccv/consumer/keeper/params.go | 131 +++++----- x/ccv/consumer/keeper/params_test.go | 6 +- x/ccv/consumer/keeper/soft_opt_out.go | 8 +- x/ccv/consumer/keeper/throttle_retry.go | 12 +- x/ccv/consumer/keeper/validators.go | 10 +- x/ccv/provider/keeper/distribution.go | 28 ++- x/ccv/provider/keeper/genesis.go | 5 +- x/ccv/provider/keeper/genesis_test.go | 6 +- x/ccv/provider/keeper/keeper.go | 303 +++++++++++++----------- x/ccv/provider/keeper/key_assignment.go | 89 ++++--- x/ccv/provider/keeper/params.go | 114 +++++---- x/ccv/provider/keeper/params_test.go | 6 +- x/ccv/provider/keeper/proposal.go | 64 +++-- x/ccv/provider/keeper/relay.go | 6 +- x/ccv/provider/keeper/throttle.go | 75 +++--- x/ccv/provider/module_test.go | 4 +- x/ccv/provider/types/params.go | 5 - x/ccv/types/params.go | 35 --- 32 files changed, 755 insertions(+), 559 deletions(-) diff --git a/app/consumer-democracy/app.go b/app/consumer-democracy/app.go index ed4e8d81fa..91d5ed333b 100644 --- a/app/consumer-democracy/app.go +++ b/app/consumer-democracy/app.go @@ -431,8 +431,7 @@ func New( // communication over IBC is not affected by these changes app.ConsumerKeeper = consumerkeeper.NewNonZeroKeeper( appCodec, - keys[consumertypes.StoreKey], - app.GetSubspace(consumertypes.ModuleName), + runtime.NewKVStoreService(keys[consumertypes.StoreKey]), ) app.IBCKeeper = ibckeeper.NewKeeper( @@ -448,8 +447,7 @@ func New( // Create CCV consumer and modules app.ConsumerKeeper = consumerkeeper.NewKeeper( appCodec, - keys[consumertypes.StoreKey], - app.GetSubspace(consumertypes.ModuleName), + runtime.NewKVStoreService(keys[consumertypes.StoreKey]), scopedIBCConsumerKeeper, app.IBCKeeper.ChannelKeeper, app.IBCKeeper.PortKeeper, diff --git a/app/consumer/app.go b/app/consumer/app.go index f6f354c777..0029e75cf3 100644 --- a/app/consumer/app.go +++ b/app/consumer/app.go @@ -357,8 +357,7 @@ func New( // communication over IBC is not affected by these changes app.ConsumerKeeper = ibcconsumerkeeper.NewNonZeroKeeper( appCodec, - keys[ibcconsumertypes.StoreKey], - app.GetSubspace(ibcconsumertypes.ModuleName), + runtime.NewKVStoreService(keys[ibcconsumertypes.StoreKey]), ) app.IBCKeeper = ibckeeper.NewKeeper( @@ -374,8 +373,7 @@ func New( // initialize the actual consumer keeper app.ConsumerKeeper = ibcconsumerkeeper.NewKeeper( appCodec, - keys[ibcconsumertypes.StoreKey], - app.GetSubspace(ibcconsumertypes.ModuleName), + runtime.NewKVStoreService(keys[ibcconsumertypes.StoreKey]), scopedIBCConsumerKeeper, app.IBCKeeper.ChannelKeeper, app.IBCKeeper.PortKeeper, diff --git a/app/provider/app.go b/app/provider/app.go index 00c775043e..fa4912a186 100644 --- a/app/provider/app.go +++ b/app/provider/app.go @@ -436,8 +436,7 @@ func New( app.ProviderKeeper = icsproviderkeeper.NewKeeper( appCodec, - keys[providertypes.StoreKey], - app.GetSubspace(providertypes.ModuleName), + runtime.NewKVStoreService(keys[providertypes.StoreKey]), scopedIBCProviderKeeper, app.IBCKeeper.ChannelKeeper, app.IBCKeeper.PortKeeper, diff --git a/tests/integration/channel_init.go b/tests/integration/channel_init.go index e9a41c66f5..0c67136896 100644 --- a/tests/integration/channel_init.go +++ b/tests/integration/channel_init.go @@ -60,7 +60,10 @@ func (suite *CCVTestSuite) TestInitTimeout() { for i, tc := range testCases { providerKeeper := suite.providerApp.GetProviderKeeper() - initTimeout := providerKeeper.GetParams(suite.providerCtx()).InitTimeoutPeriod + params, err := providerKeeper.GetParams(suite.providerCtx()) + suite.Require().NoError(err) + initTimeout := params.InitTimeoutPeriod + chainID := suite.consumerChain.ChainID // check that the init timeout timestamp is set diff --git a/tests/integration/distribution.go b/tests/integration/distribution.go index 410837fb5b..ef072c9cb3 100644 --- a/tests/integration/distribution.go +++ b/tests/integration/distribution.go @@ -28,7 +28,8 @@ func (s *CCVTestSuite) TestRewardsDistribution() { s.providerChain.NextBlock() // register a consumer reward denom - params := s.consumerApp.GetConsumerKeeper().GetConsumerParams(s.consumerCtx()) + params, err := s.consumerApp.GetConsumerKeeper().GetConsumerParams(s.consumerCtx()) + s.Require().NoError(err) params.RewardDenoms = []string{sdk.DefaultBondDenom} s.consumerApp.GetConsumerKeeper().SetParams(s.consumerCtx(), params) @@ -49,7 +50,7 @@ func (s *CCVTestSuite) TestRewardsDistribution() { consumerFeePoolAddr := consumerAccountKeeper.GetModuleAccount(s.consumerCtx(), authtypes.FeeCollectorName).GetAddress() feePoolTokensOld := consumerBankKeeper.GetAllBalances(s.consumerCtx(), consumerFeePoolAddr) fees := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100))) - err := consumerBankKeeper.SendCoinsFromAccountToModule(s.consumerCtx(), s.consumerChain.SenderAccount.GetAddress(), authtypes.FeeCollectorName, fees) + err = consumerBankKeeper.SendCoinsFromAccountToModule(s.consumerCtx(), s.consumerChain.SenderAccount.GetAddress(), authtypes.FeeCollectorName, fees) s.Require().NoError(err) feePoolTokens := consumerBankKeeper.GetAllBalances(s.consumerCtx(), consumerFeePoolAddr) s.Require().Equal(math.NewInt(100).Add(feePoolTokensOld.AmountOf(sdk.DefaultBondDenom)), feePoolTokens.AmountOf(sdk.DefaultBondDenom)) @@ -139,7 +140,9 @@ func (s *CCVTestSuite) TestSendRewardsRetries() { s.providerChain.NextBlock() // Register denom on consumer chain - params := s.consumerApp.GetConsumerKeeper().GetConsumerParams(s.consumerCtx()) + params, err := s.consumerApp.GetConsumerKeeper().GetConsumerParams(s.consumerCtx()) + s.Require().NoError(err) + params.RewardDenoms = []string{sdk.DefaultBondDenom} s.consumerApp.GetConsumerKeeper().SetParams(s.consumerCtx(), params) @@ -155,7 +158,7 @@ func (s *CCVTestSuite) TestSendRewardsRetries() { // fill fee pool fees := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100))) - err := consumerBankKeeper.SendCoinsFromAccountToModule(s.consumerCtx(), + err = consumerBankKeeper.SendCoinsFromAccountToModule(s.consumerCtx(), s.consumerChain.SenderAccount.GetAddress(), authtypes.FeeCollectorName, fees) s.Require().NoError(err) @@ -268,7 +271,8 @@ func (s *CCVTestSuite) TestEndBlockRD() { s.providerChain.NextBlock() if tc.denomRegistered { - params := s.consumerApp.GetConsumerKeeper().GetConsumerParams(s.consumerCtx()) + params, err := s.consumerApp.GetConsumerKeeper().GetConsumerParams(s.consumerCtx()) + s.Require().NoError(err) params.RewardDenoms = []string{sdk.DefaultBondDenom} s.consumerApp.GetConsumerKeeper().SetParams(s.consumerCtx(), params) } @@ -338,12 +342,13 @@ func (s *CCVTestSuite) TestSendRewardsToProvider() { s.SetupTransferChannel() // register a consumer reward denom - params := keeper.GetConsumerParams(ctx) + params, err := keeper.GetConsumerParams(ctx) + s.Require().NoError(err) params.RewardDenoms = []string{sdk.DefaultBondDenom} keeper.SetParams(ctx, params) // send coins to the pool which is used for collect reward distributions to be sent to the provider - err := bankKeeper.SendCoinsFromAccountToModule( + err = bankKeeper.SendCoinsFromAccountToModule( ctx, s.consumerChain.SenderAccount.GetAddress(), consumertypes.ConsumerToSendToProviderName, @@ -375,7 +380,8 @@ func (s *CCVTestSuite) TestSendRewardsToProvider() { s.SetupTransferChannel() // register a consumer reward denom - params := keeper.GetConsumerParams(ctx) + params, err := keeper.GetConsumerParams(ctx) + s.Require().NoError(err) params.RewardDenoms = []string{"uatom"} keeper.SetParams(ctx, params) @@ -391,13 +397,14 @@ func (s *CCVTestSuite) TestSendRewardsToProvider() { s.SetupTransferChannel() // register a consumer reward denom - params := keeper.GetConsumerParams(ctx) + params, err := keeper.GetConsumerParams(ctx) + s.Require().NoError(err) params.RewardDenoms = []string{sdk.DefaultBondDenom} params.DistributionTransmissionChannel = "" keeper.SetParams(ctx, params) // send coins to the pool which is used for collect reward distributions to be sent to the provider - err := bankKeeper.SendCoinsFromAccountToModule( + err = bankKeeper.SendCoinsFromAccountToModule( ctx, s.consumerChain.SenderAccount.GetAddress(), consumertypes.ConsumerToSendToProviderName, @@ -414,13 +421,14 @@ func (s *CCVTestSuite) TestSendRewardsToProvider() { s.SetupTransferChannel() // register a consumer reward denom - params := keeper.GetConsumerParams(ctx) + params, err := keeper.GetConsumerParams(ctx) + s.Require().NoError(err) params.RewardDenoms = []string{sdk.DefaultBondDenom} params.ProviderFeePoolAddrStr = "" keeper.SetParams(ctx, params) // send coins to the pool which is used for collect reward distributions to be sent to the provider - err := bankKeeper.SendCoinsFromAccountToModule( + err = bankKeeper.SendCoinsFromAccountToModule( ctx, s.consumerChain.SenderAccount.GetAddress(), consumertypes.ConsumerToSendToProviderName, diff --git a/tests/integration/throttle.go b/tests/integration/throttle.go index 73762b6b59..1b5ad63752 100644 --- a/tests/integration/throttle.go +++ b/tests/integration/throttle.go @@ -308,7 +308,8 @@ func (s *CCVTestSuite) TestPacketSpam() { // Explicitly set params, initialize slash meter providerKeeper := s.providerApp.GetProviderKeeper() - params := providerKeeper.GetParams(s.providerCtx()) + params, err := providerKeeper.GetParams(s.providerCtx()) + s.Require().NoError(err) params.SlashMeterReplenishFraction = "0.75" // Allow 3/4 of validators to be jailed providerKeeper.SetParams(s.providerCtx(), params) providerKeeper.InitializeSlashMeter(s.providerCtx()) @@ -376,7 +377,8 @@ func (s *CCVTestSuite) TestDoubleSignDoesNotAffectThrottling() { // Explicitly set params, initialize slash meter providerKeeper := s.providerApp.GetProviderKeeper() - params := providerKeeper.GetParams(s.providerCtx()) + params, err := providerKeeper.GetParams(s.providerCtx()) + s.Require().NoError(err) params.SlashMeterReplenishFraction = "0.1" providerKeeper.SetParams(s.providerCtx(), params) providerKeeper.InitializeSlashMeter(s.providerCtx()) @@ -469,7 +471,8 @@ func (s *CCVTestSuite) TestQueueOrdering() { // Explicitly set params, initialize slash meter providerKeeper := s.providerApp.GetProviderKeeper() - params := providerKeeper.GetParams(s.providerCtx()) + params, err := providerKeeper.GetParams(s.providerCtx()) + s.Require().NoError(err) params.SlashMeterReplenishFraction = "0.05" // 5% total power can be jailed providerKeeper.SetParams(s.providerCtx(), params) providerKeeper.InitializeSlashMeter(s.providerCtx()) @@ -706,7 +709,8 @@ func (s *CCVTestSuite) TestSlashMeterAllowanceChanges() { s.Require().Equal(int64(200), providerKeeper.GetSlashMeterAllowance(s.providerCtx()).Int64()) // Now we change replenish fraction and assert new expected allowance. - params := providerKeeper.GetParams(s.providerCtx()) + params, err := providerKeeper.GetParams(s.providerCtx()) + s.Require().NoError(err) params.SlashMeterReplenishFraction = "0.3" providerKeeper.SetParams(s.providerCtx(), params) s.Require().Equal(int64(1200), providerKeeper.GetSlashMeterAllowance(s.providerCtx()).Int64()) @@ -725,7 +729,8 @@ func (s *CCVTestSuite) TestSlashSameValidator() { providerKeeper := s.providerApp.GetProviderKeeper() // Set replenish fraction to 1.0 so that all sent packets should handled immediately (no throttling) - params := providerKeeper.GetParams(s.providerCtx()) + params, err := providerKeeper.GetParams(s.providerCtx()) + s.Require().NoError(err) params.SlashMeterReplenishFraction = fullSlashMeterString // needs to be const for linter providerKeeper.SetParams(s.providerCtx(), params) providerKeeper.InitializeSlashMeter(s.providerCtx()) @@ -785,7 +790,8 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes providerKeeper := s.providerApp.GetProviderKeeper() // Set replenish fraction to 1.0 so that all sent packets should be handled immediately (no throttling) - params := providerKeeper.GetParams(s.providerCtx()) + params, err := providerKeeper.GetParams(s.providerCtx()) + s.Require().NoError(err) params.SlashMeterReplenishFraction = fullSlashMeterString // needs to be const for linter providerKeeper.SetParams(s.providerCtx(), params) providerKeeper.InitializeSlashMeter(s.providerCtx()) @@ -939,7 +945,9 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() { providerKeeper := s.providerApp.GetProviderKeeper() // Set replenish fraction to 1.0 so that all sent packets should be handled immediately (no jail throttling) - params := providerKeeper.GetParams(s.providerCtx()) + params, err := providerKeeper.GetParams(s.providerCtx()) + s.Require().NoError(err) + params.SlashMeterReplenishFraction = fullSlashMeterString // needs to be const for linter providerKeeper.SetParams(s.providerCtx(), params) providerKeeper.InitializeSlashMeter(s.providerCtx()) diff --git a/testutil/keeper/unit_test_helpers.go b/testutil/keeper/unit_test_helpers.go index d808689436..59e7be67ae 100644 --- a/testutil/keeper/unit_test_helpers.go +++ b/testutil/keeper/unit_test_helpers.go @@ -19,6 +19,7 @@ import ( cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -116,8 +117,7 @@ func NewMockedKeepers(ctrl *gomock.Controller) MockedKeepers { func NewInMemProviderKeeper(params InMemKeeperParams, mocks MockedKeepers) providerkeeper.Keeper { return providerkeeper.NewKeeper( params.Cdc, - params.StoreKey, - *params.ParamsSubspace, + runtime.NewKVStoreService(params.StoreKey), mocks.MockScopedKeeper, mocks.MockChannelKeeper, mocks.MockPortKeeper, @@ -139,8 +139,7 @@ func NewInMemProviderKeeper(params InMemKeeperParams, mocks MockedKeepers) provi func NewInMemConsumerKeeper(params InMemKeeperParams, mocks MockedKeepers) consumerkeeper.Keeper { return consumerkeeper.NewKeeper( params.Cdc, - params.StoreKey, - *params.ParamsSubspace, + runtime.NewKVStoreService(params.StoreKey), mocks.MockScopedKeeper, mocks.MockChannelKeeper, mocks.MockPortKeeper, diff --git a/x/ccv/consumer/ibc_module_test.go b/x/ccv/consumer/ibc_module_test.go index b84c30b117..1005bca573 100644 --- a/x/ccv/consumer/ibc_module_test.go +++ b/x/ccv/consumer/ibc_module_test.go @@ -1,6 +1,7 @@ package consumer_test import ( + "fmt" "testing" transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" @@ -226,7 +227,10 @@ func TestOnChanOpenAck(t *testing.T) { transfertypes.PortID, "", // signer unused ) - + err := keeper.SetParams(params.ctx, ccv.DefaultParams()) + if err != nil { + panic(fmt.Sprintf("Setting params failed %v", err)) + } transferChannelID := "" keeper.SetDistributionTransmissionChannel(params.ctx, transferChannelID) diff --git a/x/ccv/consumer/keeper/distribution.go b/x/ccv/consumer/keeper/distribution.go index 64240990cb..b60bd61afe 100644 --- a/x/ccv/consumer/keeper/distribution.go +++ b/x/ccv/consumer/keeper/distribution.go @@ -204,10 +204,10 @@ func (k Keeper) AllowedRewardDenoms(ctx sdk.Context) []string { } func (k Keeper) GetLastTransmissionBlockHeight(ctx sdk.Context) ccv.LastTransmissionBlockHeight { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.LastDistributionTransmissionKey()) + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.LastDistributionTransmissionKey()) ltbh := ccv.LastTransmissionBlockHeight{} - if bz != nil { + if err == nil && bz != nil { if err := ltbh.Unmarshal(bz); err != nil { panic(fmt.Errorf("failed to unmarshal LastTransmissionBlockHeight: %w", err)) } @@ -216,7 +216,7 @@ func (k Keeper) GetLastTransmissionBlockHeight(ctx sdk.Context) ccv.LastTransmis } func (k Keeper) SetLastTransmissionBlockHeight(ctx sdk.Context, ltbh ccv.LastTransmissionBlockHeight) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := ltbh.Marshal() if err != nil { panic(fmt.Errorf("failed to marshal LastTransmissionBlockHeight: %w", err)) diff --git a/x/ccv/consumer/keeper/genesis.go b/x/ccv/consumer/keeper/genesis.go index 2ac38f650c..ca5c60515f 100644 --- a/x/ccv/consumer/keeper/genesis.go +++ b/x/ccv/consumer/keeper/genesis.go @@ -116,7 +116,11 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state *ccv.GenesisState) []abci.Val // ExportGenesis returns the CCV consumer module's exported genesis func (k Keeper) ExportGenesis(ctx sdk.Context) (genesis *ccv.GenesisState) { - params := k.GetConsumerParams(ctx) + params, err := k.GetConsumerParams(ctx) + if err != nil { + panic(fmt.Sprintf("failed getting consumer parameters: %v", err)) + } + if !params.Enabled { return ccv.DefaultGenesisState() } diff --git a/x/ccv/consumer/keeper/genesis_test.go b/x/ccv/consumer/keeper/genesis_test.go index 6bb67b10c3..7cae90b0d8 100644 --- a/x/ccv/consumer/keeper/genesis_test.go +++ b/x/ccv/consumer/keeper/genesis_test.go @@ -125,7 +125,9 @@ func TestInitGenesis(t *testing.T) { assertHeightValsetUpdateIDs(t, ctx, &ck, defaultHeightValsetUpdateIDs) require.Equal(t, validator.Address.Bytes(), ck.GetAllCCValidator(ctx)[0].Address) - require.Equal(t, gs.Params, ck.GetConsumerParams(ctx)) + params, err := ck.GetConsumerParams(ctx) + require.NoError(t, err) + require.Equal(t, gs.Params, params) }, }, { "restart a chain without an established CCV channel", @@ -157,7 +159,9 @@ func TestInitGenesis(t *testing.T) { assertHeightValsetUpdateIDs(t, ctx, &ck, defaultHeightValsetUpdateIDs) assertProviderClientID(t, ctx, &ck, provClientID) require.Equal(t, validator.Address.Bytes(), ck.GetAllCCValidator(ctx)[0].Address) - require.Equal(t, gs.Params, ck.GetConsumerParams(ctx)) + params, err := ck.GetConsumerParams(ctx) + require.NoError(t, err) + require.Equal(t, gs.Params, params) }, }, { "restart a chain with an established CCV channel", @@ -206,7 +210,9 @@ func TestInitGenesis(t *testing.T) { assertHeightValsetUpdateIDs(t, ctx, &ck, updatedHeightValsetUpdateIDs) assertProviderClientID(t, ctx, &ck, provClientID) - require.Equal(t, gs.Params, ck.GetConsumerParams(ctx)) + params, err := ck.GetConsumerParams(ctx) + require.NoError(t, err) + require.Equal(t, gs.Params, params) }, }, } diff --git a/x/ccv/consumer/keeper/grpc_query.go b/x/ccv/consumer/keeper/grpc_query.go index 80e6b695a9..7c0be1a7ab 100644 --- a/x/ccv/consumer/keeper/grpc_query.go +++ b/x/ccv/consumer/keeper/grpc_query.go @@ -36,7 +36,10 @@ func (k Keeper) QueryParams(c context.Context, //nolint:golint return nil, status.Errorf(codes.InvalidArgument, "empty request") } - p := k.GetConsumerParams(ctx) + p, err := k.GetConsumerParams(ctx) + if err != nil { + return nil, status.Errorf(codes.NotFound, "no consumer parameters found") + } return &types.QueryParamsResponse{Params: p}, nil } diff --git a/x/ccv/consumer/keeper/keeper.go b/x/ccv/consumer/keeper/keeper.go index 84167e61f8..624a4a2ebc 100644 --- a/x/ccv/consumer/keeper/keeper.go +++ b/x/ccv/consumer/keeper/keeper.go @@ -19,7 +19,6 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" @@ -36,10 +35,9 @@ type Keeper struct { // should be the x/gov module account. authority string - storeKey storetypes.StoreKey // TODO: maybe needs to be removed? - storeService store.KVStoreService - cdc codec.BinaryCodec - paramStore paramtypes.Subspace + storeService store.KVStoreService + cdc codec.BinaryCodec + //paramStore paramtypes.Subspace scopedKeeper ccv.ScopedKeeper channelKeeper ccv.ChannelKeeper portKeeper ccv.PortKeeper @@ -65,7 +63,7 @@ type Keeper struct { // NOTE: the feeCollectorName is in reference to the consumer-chain fee // collector (and not the provider chain) func NewKeeper( - cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramtypes.Subspace, + cdc codec.BinaryCodec, storeService store.KVStoreService, scopedKeeper ccv.ScopedKeeper, channelKeeper ccv.ChannelKeeper, portKeeper ccv.PortKeeper, connectionKeeper ccv.ConnectionKeeper, clientKeeper ccv.ClientKeeper, @@ -74,16 +72,10 @@ func NewKeeper( feeCollectorName, authority string, validatorAddressCodec, consensusAddressCodec addresscodec.Codec, ) Keeper { - // set KeyTable if it has not already been set - if !paramSpace.HasKeyTable() { - paramSpace = paramSpace.WithKeyTable(ccv.ParamKeyTable()) - } - k := Keeper{ authority: authority, - storeKey: key, + storeService: storeService, cdc: cdc, - paramStore: paramSpace, scopedKeeper: scopedKeeper, channelKeeper: channelKeeper, portKeeper: portKeeper, @@ -104,61 +96,57 @@ func NewKeeper( return k } -// Returns a keeper with cdc, key and paramSpace set it does not raise any panics during registration (eg with IBCKeeper). +// Returns a keeper with cdc, key it does not raise any panics during registration (eg with IBCKeeper). // Used only in testing. -func NewNonZeroKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramtypes.Subspace) Keeper { +func NewNonZeroKeeper(cdc codec.BinaryCodec, storeService store.KVStoreService) Keeper { return Keeper{ - storeKey: key, - cdc: cdc, - paramStore: paramSpace, + storeService: storeService, + cdc: cdc, } } +func (k *Keeper) GetAuthority() string { + return k.authority +} + // SetStandaloneStakingKeeper sets the standalone staking keeper for the consumer chain. // This method should only be called for previously standalone chains that are now consumers. func (k *Keeper) SetStandaloneStakingKeeper(sk ccv.StakingKeeper) { k.standaloneStakingKeeper = sk } -// SetParamSpace sets the param space for the consumer keeper. -// Note: this is only used for testing! -func (k *Keeper) SetParamSpace(ctx sdk.Context, ps paramtypes.Subspace) { - k.paramStore = ps -} - // Validates that the consumer keeper is initialized with non-zero and // non-nil values for all its fields. Otherwise this method will panic. func (k Keeper) mustValidateFields() { // Ensures no fields are missed in this validation // TODO: @MSalopek hangle this better - if reflect.ValueOf(k).NumField() != 20 { - panic(fmt.Sprintf("number of fields in consumer keeper is not 19 - have %d", reflect.ValueOf(k).NumField())) // incorrect number + if reflect.ValueOf(k).NumField() != 18 { + panic(fmt.Sprintf("number of fields in consumer keeper is not 18 - have %d", reflect.ValueOf(k).NumField())) // incorrect number } if k.validatorAddressCodec == nil || k.consensusAddressCodec == nil { panic("validator and/or consensus address codec are nil") } - // Note 17 / 20 fields will be validated, + // Note 16 / 20 fields will be validated, // hooks are explicitly set after the constructor, // stakingKeeper is optionally set after the constructor, - ccv.PanicIfZeroOrNil(k.storeKey, "storeKey") // 1 + ccv.PanicIfZeroOrNil(k.storeService, "storeService") // 1 ccv.PanicIfZeroOrNil(k.cdc, "cdc") // 2 - ccv.PanicIfZeroOrNil(k.paramStore, "paramStore") // 3 - ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 4 - ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 5 - ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 6 - ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 7 - ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 8 - ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 9 - ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 10 - ccv.PanicIfZeroOrNil(k.authKeeper, "authKeeper") // 11 - ccv.PanicIfZeroOrNil(k.ibcTransferKeeper, "ibcTransferKeeper") // 12 - ccv.PanicIfZeroOrNil(k.ibcCoreKeeper, "ibcCoreKeeper") // 13 - ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 14 - ccv.PanicIfZeroOrNil(k.authority, "authority") // 15 - ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 16 - ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 17 + ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 3 + ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 4 + ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 5 + ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 6 + ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 7 + ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 8 + ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 9 + ccv.PanicIfZeroOrNil(k.authKeeper, "authKeeper") // 10 + ccv.PanicIfZeroOrNil(k.ibcTransferKeeper, "ibcTransferKeeper") // 11 + ccv.PanicIfZeroOrNil(k.ibcCoreKeeper, "ibcCoreKeeper") // 12 + ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 13 + ccv.PanicIfZeroOrNil(k.authority, "authority") // 14 + ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 15 + ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 16 } // Logger returns a module-specific logger. @@ -214,13 +202,17 @@ func (k Keeper) BindPort(ctx sdk.Context, portID string) error { // GetPort returns the portID for the transfer module. Used in ExportGenesis func (k Keeper) GetPort(ctx sdk.Context) string { - store := ctx.KVStore(k.storeKey) - return string(store.Get(types.PortKey())) + store := k.storeService.OpenKVStore(ctx) + port, err := store.Get(types.PortKey()) + if err != nil || port == nil { + return "" + } + return string(port) } // SetPort sets the portID for the CCV module. Used in InitGenesis func (k Keeper) SetPort(ctx sdk.Context, portID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.PortKey(), []byte(portID)) } @@ -237,15 +229,15 @@ func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability // SetProviderClientID sets the clientID for the client to the provider. // Set in InitGenesis func (k Keeper) SetProviderClientID(ctx sdk.Context, clientID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.ProviderClientIDKey(), []byte(clientID)) } // GetProviderClientID gets the clientID for the client to the provider. func (k Keeper) GetProviderClientID(ctx sdk.Context) (string, bool) { - store := ctx.KVStore(k.storeKey) - clientIdBytes := store.Get(types.ProviderClientIDKey()) - if clientIdBytes == nil { + store := k.storeService.OpenKVStore(ctx) + clientIdBytes, err := store.Get(types.ProviderClientIDKey()) + if err != nil || clientIdBytes == nil { return "", false } return string(clientIdBytes), true @@ -253,15 +245,15 @@ func (k Keeper) GetProviderClientID(ctx sdk.Context) (string, bool) { // SetProviderChannel sets the channelID for the channel to the provider. func (k Keeper) SetProviderChannel(ctx sdk.Context, channelID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.ProviderChannelKey(), []byte(channelID)) } // GetProviderChannel gets the channelID for the channel to the provider. func (k Keeper) GetProviderChannel(ctx sdk.Context) (string, bool) { - store := ctx.KVStore(k.storeKey) - channelIdBytes := store.Get(types.ProviderChannelKey()) - if len(channelIdBytes) == 0 { + store := k.storeService.OpenKVStore(ctx) + channelIdBytes, err := store.Get(types.ProviderChannelKey()) + if err != nil || len(channelIdBytes) == 0 { return "", false } return string(channelIdBytes), true @@ -269,13 +261,13 @@ func (k Keeper) GetProviderChannel(ctx sdk.Context) (string, bool) { // DeleteProviderChannel deletes the channelID for the channel to the provider. func (k Keeper) DeleteProviderChannel(ctx sdk.Context) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ProviderChannelKey()) } // SetPendingChanges sets the pending validator set change packet that haven't been flushed to ABCI func (k Keeper) SetPendingChanges(ctx sdk.Context, updates ccv.ValidatorSetChangePacketData) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := updates.Marshal() if err != nil { // This should never happen @@ -286,9 +278,9 @@ func (k Keeper) SetPendingChanges(ctx sdk.Context, updates ccv.ValidatorSetChang // GetPendingChanges gets the pending changes that haven't been flushed over ABCI func (k Keeper) GetPendingChanges(ctx sdk.Context) (*ccv.ValidatorSetChangePacketData, bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.PendingChangesKey()) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.PendingChangesKey()) + if err != nil || bz == nil { return nil, false } var data ccv.ValidatorSetChangePacketData @@ -302,14 +294,14 @@ func (k Keeper) GetPendingChanges(ctx sdk.Context) (*ccv.ValidatorSetChangePacke // DeletePendingChanges deletes the pending changes after they've been flushed to ABCI func (k Keeper) DeletePendingChanges(ctx sdk.Context) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.PendingChangesKey()) } func (k Keeper) GetInitGenesisHeight(ctx sdk.Context) int64 { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.InitGenesisHeightKey()) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.InitGenesisHeightKey()) + if err != nil || bz == nil { panic("last standalone height not set") } height := sdk.BigEndianToUint64(bz) @@ -318,29 +310,29 @@ func (k Keeper) GetInitGenesisHeight(ctx sdk.Context) int64 { func (k Keeper) SetInitGenesisHeight(ctx sdk.Context, height int64) { bz := sdk.Uint64ToBigEndian(uint64(height)) - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.InitGenesisHeightKey(), bz) } func (k Keeper) IsPreCCV(ctx sdk.Context) bool { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.PreCCVKey()) - return bz != nil + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.PreCCVKey()) + return err == nil && bz != nil } func (k Keeper) SetPreCCVTrue(ctx sdk.Context) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz := sdk.Uint64ToBigEndian(uint64(1)) store.Set(types.PreCCVKey(), bz) } func (k Keeper) DeletePreCCV(ctx sdk.Context) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.PreCCVKey()) } func (k Keeper) SetInitialValSet(ctx sdk.Context, initialValSet []tmtypes.ValidatorUpdate) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) initialValSetState := ccv.GenesisState{ InitialValSet: initialValSet, } @@ -349,10 +341,10 @@ func (k Keeper) SetInitialValSet(ctx sdk.Context, initialValSet []tmtypes.Valida } func (k Keeper) GetInitialValSet(ctx sdk.Context) []tmtypes.ValidatorUpdate { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) initialValSet := ccv.GenesisState{} - bz := store.Get(types.InitialValSetKey()) - if bz != nil { + bz, err := store.Get(types.InitialValSetKey()) + if err == nil && bz != nil { k.cdc.MustUnmarshal(bz, &initialValSet) return initialValSet.InitialValSet } @@ -369,8 +361,13 @@ func (k Keeper) GetLastStandaloneValidators(ctx sdk.Context) ([]stakingtypes.Val // GetElapsedPacketMaturityTimes returns a slice of already elapsed PacketMaturityTimes, sorted by maturity times, // i.e., the slice contains the IDs of the matured VSCPackets. func (k Keeper) GetElapsedPacketMaturityTimes(ctx sdk.Context) (maturingVSCPackets []ccv.MaturingVSCPacket) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PacketMaturityTimeBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.PacketMaturityTimeBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting elapsed PacketMaturityTimes: %v", err) + return []ccv.MaturingVSCPacket{} + } defer iterator.Close() @@ -401,8 +398,13 @@ func (k Keeper) GetElapsedPacketMaturityTimes(ctx sdk.Context) (maturingVSCPacke // Thus, the returned array is in ascending order of maturityTimes. // If two entries have the same maturityTime, then they are ordered by vscID. func (k Keeper) GetAllPacketMaturityTimes(ctx sdk.Context) (maturingVSCPackets []ccv.MaturingVSCPacket) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PacketMaturityTimeBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.PacketMaturityTimeBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all PacketMaturityTimes: %v", err) + return []ccv.MaturingVSCPacket{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -420,7 +422,7 @@ func (k Keeper) GetAllPacketMaturityTimes(ctx sdk.Context) (maturingVSCPackets [ // SetPacketMaturityTime sets the maturity time for a given received VSC packet id func (k Keeper) SetPacketMaturityTime(ctx sdk.Context, vscId uint64, maturityTime time.Time) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) maturingVSCPacket := ccv.MaturingVSCPacket{ VscId: vscId, MaturityTime: maturityTime, @@ -438,14 +440,14 @@ func (k Keeper) SetPacketMaturityTime(ctx sdk.Context, vscId uint64, maturityTim // // Note: this method is only used in testing. func (k Keeper) PacketMaturityTimeExists(ctx sdk.Context, vscId uint64, maturityTime time.Time) bool { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.PacketMaturityTimeKey(vscId, maturityTime)) - return bz != nil + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.PacketMaturityTimeKey(vscId, maturityTime)) + return err == nil && bz != nil } // DeletePacketMaturityTimes deletes the packet maturity time for a given vscId and maturityTime func (k Keeper) DeletePacketMaturityTimes(ctx sdk.Context, vscId uint64, maturityTime time.Time) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.PacketMaturityTimeKey(vscId, maturityTime)) } @@ -474,7 +476,7 @@ func (k Keeper) VerifyProviderChain(ctx sdk.Context, connectionHops []string) er // SetHeightValsetUpdateID sets the valset update id for a given block height func (k Keeper) SetHeightValsetUpdateID(ctx sdk.Context, height, valsetUpdateId uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) valBytes := make([]byte, 8) binary.BigEndian.PutUint64(valBytes, valsetUpdateId) store.Set(types.HeightValsetUpdateIDKey(height), valBytes) @@ -482,9 +484,9 @@ func (k Keeper) SetHeightValsetUpdateID(ctx sdk.Context, height, valsetUpdateId // GetHeightValsetUpdateID gets the valset update id recorded for a given block height func (k Keeper) GetHeightValsetUpdateID(ctx sdk.Context, height uint64) uint64 { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.HeightValsetUpdateIDKey(height)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.HeightValsetUpdateIDKey(height)) + if err != nil || bz == nil { return 0 } return binary.BigEndian.Uint64(bz) @@ -492,7 +494,7 @@ func (k Keeper) GetHeightValsetUpdateID(ctx sdk.Context, height uint64) uint64 { // DeleteHeightValsetUpdateID deletes the valset update id for a given block height func (k Keeper) DeleteHeightValsetUpdateID(ctx sdk.Context, height uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.HeightValsetUpdateIDKey(height)) } @@ -502,8 +504,13 @@ func (k Keeper) DeleteHeightValsetUpdateID(ctx sdk.Context, height uint64) { // HeightValsetUpdateIDBytePrefix | height // Thus, the returned array is in ascending order of heights. func (k Keeper) GetAllHeightToValsetUpdateIDs(ctx sdk.Context) (heightToValsetUpdateIDs []ccv.HeightToValsetUpdateID) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.HeightValsetUpdateIDBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.HeightValsetUpdateIDBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all heights to ValSet IDs: %v", err) + return []ccv.HeightToValsetUpdateID{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -521,14 +528,14 @@ func (k Keeper) GetAllHeightToValsetUpdateIDs(ctx sdk.Context) (heightToValsetUp // OutstandingDowntime returns the outstanding downtime flag for a given validator func (k Keeper) OutstandingDowntime(ctx sdk.Context, address sdk.ConsAddress) bool { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.OutstandingDowntimeKey(address)) - return bz != nil + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.OutstandingDowntimeKey(address)) + return err == nil && bz != nil } // SetOutstandingDowntime sets the outstanding downtime flag for a given validator func (k Keeper) SetOutstandingDowntime(ctx sdk.Context, address sdk.ConsAddress) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.OutstandingDowntimeKey(address), []byte{}) } @@ -538,7 +545,7 @@ func (k Keeper) DeleteOutstandingDowntime(ctx sdk.Context, consAddress string) { if err != nil { return // TODO: this should panic with appropriate tests to validate the panic wont happen in normal cases. } - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.OutstandingDowntimeKey(consAddr)) } @@ -548,8 +555,13 @@ func (k Keeper) DeleteOutstandingDowntime(ctx sdk.Context, consAddress string) { // OutstandingDowntimeBytePrefix | consAddress // Thus, the returned array is in ascending order of consAddresses. func (k Keeper) GetAllOutstandingDowntimes(ctx sdk.Context) (downtimes []ccv.OutstandingDowntime) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.OutstandingDowntimeBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.OutstandingDowntimeBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting validator addresses of outstanding downtimes: %v", err) + return []ccv.OutstandingDowntime{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -566,7 +578,7 @@ func (k Keeper) GetAllOutstandingDowntimes(ctx sdk.Context) (downtimes []ccv.Out // SetCCValidator sets a cross-chain validator under its validator address func (k Keeper) SetCCValidator(ctx sdk.Context, v types.CrossChainValidator) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz := k.cdc.MustMarshal(&v) store.Set(types.CrossChainValidatorKey(v.Address), bz) @@ -574,9 +586,9 @@ func (k Keeper) SetCCValidator(ctx sdk.Context, v types.CrossChainValidator) { // GetCCValidator returns a cross-chain validator for a given address func (k Keeper) GetCCValidator(ctx sdk.Context, addr []byte) (validator types.CrossChainValidator, found bool) { - store := ctx.KVStore(k.storeKey) - v := store.Get(types.CrossChainValidatorKey(addr)) - if v == nil { + store := k.storeService.OpenKVStore(ctx) + v, err := store.Get(types.CrossChainValidatorKey(addr)) + if err != nil || v == nil { return } k.cdc.MustUnmarshal(v, &validator) @@ -587,7 +599,7 @@ func (k Keeper) GetCCValidator(ctx sdk.Context, addr []byte) (validator types.Cr // DeleteCCValidator deletes a cross-chain validator for a given address func (k Keeper) DeleteCCValidator(ctx sdk.Context, addr []byte) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.CrossChainValidatorKey(addr)) } @@ -597,10 +609,15 @@ func (k Keeper) DeleteCCValidator(ctx sdk.Context, addr []byte) { // CrossChainValidatorBytePrefix | address // Thus, the returned array is in ascending order of addresses. func (k Keeper) GetAllCCValidator(ctx sdk.Context) (validators []types.CrossChainValidator) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.CrossChainValidatorBytePrefix}) - + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.CrossChainValidatorBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all cross-chain validators: %v", err) + return []types.CrossChainValidator{} + } defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { val := types.CrossChainValidator{} k.cdc.MustUnmarshal(iterator.Value(), &val) @@ -613,9 +630,13 @@ func (k Keeper) GetAllCCValidator(ctx sdk.Context) (validators []types.CrossChai // Implement from stakingkeeper interface func (k Keeper) GetAllValidators(ctx context.Context) (validators []stakingtypes.Validator, err error) { sdkCtx := sdk.UnwrapSDKContext(ctx) - store := sdkCtx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(sdkCtx) - iterator := storetypes.KVStorePrefixIterator(store, stakingtypes.ValidatorsKey) + prefix := stakingtypes.ValidatorsKey + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + return nil, err + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -629,9 +650,9 @@ func (k Keeper) GetAllValidators(ctx context.Context) (validators []stakingtypes // getAndIncrementPendingPacketsIdx returns the current pending packets index and increments it. // This index is used for implementing a FIFO queue of pending packets in the KV store. func (k Keeper) getAndIncrementPendingPacketsIdx(ctx sdk.Context) (toReturn uint64) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.PendingPacketsIndexKey()) - if bz != nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.PendingPacketsIndexKey()) + if err == nil && bz != nil { toReturn = sdk.BigEndianToUint64(bz) } toStore := toReturn + 1 @@ -641,8 +662,13 @@ func (k Keeper) getAndIncrementPendingPacketsIdx(ctx sdk.Context) (toReturn uint // DeleteHeadOfPendingPackets deletes the head of the pending packets queue. func (k Keeper) DeleteHeadOfPendingPackets(ctx sdk.Context) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.PendingDataPacketsBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error deleting the head of pending packets: %v", err) + return + } defer iterator.Close() if !iterator.Valid() { return @@ -673,11 +699,17 @@ type ConsumerPacketDataWithIdx struct { // with indexes relevant to the pending packets queue. func (k Keeper) GetAllPendingPacketsWithIdx(ctx sdk.Context) []ConsumerPacketDataWithIdx { packets := []ConsumerPacketDataWithIdx{} - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) // Note: PendingDataPacketsBytePrefix is the correct prefix, NOT PendingDataPacketsByteKey. // See consistency with PendingDataPacketsKey(). - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix}) + prefix := []byte{types.PendingDataPacketsBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all pending consumer packets: %v", err) + return []ConsumerPacketDataWithIdx{} + } defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { var packet ccv.ConsumerPacketData bz := iterator.Value() @@ -698,17 +730,22 @@ func (k Keeper) GetAllPendingPacketsWithIdx(ctx sdk.Context) []ConsumerPacketDat // DeletePendingDataPackets deletes pending data packets with given indexes func (k Keeper) DeletePendingDataPackets(ctx sdk.Context, idxs ...uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) for _, idx := range idxs { store.Delete(types.PendingDataPacketsKey(idx)) } } func (k Keeper) DeleteAllPendingDataPackets(ctx sdk.Context) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) // Note: PendingDataPacketsBytePrefix is the correct prefix, NOT PendingDataPacketsByteKey. // See consistency with PendingDataPacketsKey(). - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix}) + prefix := []byte{types.PendingDataPacketsBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error deleting all pending data packets: %v", err) + return + } keysToDel := [][]byte{} defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -723,7 +760,7 @@ func (k Keeper) DeleteAllPendingDataPackets(ctx sdk.Context) { func (k Keeper) AppendPendingPacket(ctx sdk.Context, packetType ccv.ConsumerPacketDataType, data ccv.ExportedIsConsumerPacketData_Data) { idx := k.getAndIncrementPendingPacketsIdx(ctx) // for FIFO queue key := types.PendingDataPacketsKey(idx) - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) cpd := ccv.NewConsumerPacketData(packetType, data) bz, err := cpd.Marshal() if err != nil { @@ -734,11 +771,13 @@ func (k Keeper) AppendPendingPacket(ctx sdk.Context, packetType ccv.ConsumerPack } func (k Keeper) MarkAsPrevStandaloneChain(ctx sdk.Context) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.PrevStandaloneChainKey(), []byte{}) } func (k Keeper) IsPrevStandaloneChain(ctx sdk.Context) bool { - store := ctx.KVStore(k.storeKey) - return store.Has(types.PrevStandaloneChainKey()) + store := k.storeService.OpenKVStore(ctx) + + exists, err := store.Has(types.PrevStandaloneChainKey()) + return err == nil && exists } diff --git a/x/ccv/consumer/keeper/migration.go b/x/ccv/consumer/keeper/migration.go index 361bb2a62f..929082b9c8 100644 --- a/x/ccv/consumer/keeper/migration.go +++ b/x/ccv/consumer/keeper/migration.go @@ -28,13 +28,13 @@ func NewMigrator(ccvConsumerKeeper Keeper, ccvConsumerParamSpace paramtypes.Subs func (k Keeper) MigrateConsumerPacketData(ctx sdk.Context) { // deserialize packet data from old format var depreciatedType ccvtypes.ConsumerPacketDataList - store := ctx.KVStore(k.storeKey) - bz := store.Get([]byte{consumertypes.PendingDataPacketsBytePrefix}) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get([]byte{consumertypes.PendingDataPacketsBytePrefix}) + if err != nil || bz == nil { ctx.Logger().Info("no pending data packets to migrate") return } - err := depreciatedType.Unmarshal(bz) + err = depreciatedType.Unmarshal(bz) if err != nil { // An error here would indicate something is very wrong panic(fmt.Errorf("failed to unmarshal pending data packets: %w", err)) @@ -60,7 +60,7 @@ func PendingDataPacketsKeyOnlyForTesting() []byte { // Note: a better test of the old functionality would be to directly reference the old ICS version, // including the version of ccv.ConsumerPacketDataList has a list of ccv.ConsumerPacketData without indexes. func (k Keeper) SetPendingPacketsOnlyForTesting(ctx sdk.Context, packets ccvtypes.ConsumerPacketDataList) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := packets.Marshal() if err != nil { // This should never happen diff --git a/x/ccv/consumer/keeper/params.go b/x/ccv/consumer/keeper/params.go index 9bd0d1fa10..8ec4e76104 100644 --- a/x/ccv/consumer/keeper/params.go +++ b/x/ccv/consumer/keeper/params.go @@ -7,31 +7,31 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types" ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types" ) // GetParams returns the params for the consumer ccv module // NOTE: it is different from the GetParams method which is required to implement StakingKeeper interface -func (k Keeper) GetConsumerParams(ctx sdk.Context) ccvtypes.Params { - return ccvtypes.NewParams( - k.GetEnabled(ctx), - k.GetBlocksPerDistributionTransmission(ctx), - k.GetDistributionTransmissionChannel(ctx), - k.GetProviderFeePoolAddrStr(ctx), - k.GetCCVTimeoutPeriod(ctx), - k.GetTransferTimeoutPeriod(ctx), - k.GetConsumerRedistributionFrac(ctx), - k.GetHistoricalEntries(ctx), - k.GetUnbondingPeriod(ctx), - k.GetSoftOptOutThreshold(ctx), - k.GetRewardDenoms(ctx), - k.GetProviderRewardDenoms(ctx), - ) +func (k Keeper) GetConsumerParams(ctx sdk.Context) (ccvtypes.Params, error) { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ParametersKey()) + if err != nil { + return ccvtypes.Params{}, err + } + var params ccvtypes.Params + k.cdc.MustUnmarshal(bz, ¶ms) + return params, nil } // SetParams sets the paramset for the consumer module -func (k Keeper) SetParams(ctx sdk.Context, params ccvtypes.Params) { - k.paramStore.SetParamSet(ctx, ¶ms) +func (k Keeper) SetParams(ctx sdk.Context, params ccvtypes.Params) error { + if err := params.Validate(); err != nil { + return err + } + store := k.storeService.OpenKVStore(ctx) + bz := k.cdc.MustMarshal(¶ms) + return store.Set(types.ParametersKey(), bz) } // GetParams implements StakingKeeper GetParams interface method @@ -44,98 +44,115 @@ func (k Keeper) GetParams(context.Context) (stakingtypes.Params, error) { // GetEnabled returns the enabled flag for the consumer module func (k Keeper) GetEnabled(ctx sdk.Context) bool { - var enabled bool - k.paramStore.Get(ctx, ccvtypes.KeyEnabled, &enabled) - return enabled + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.Enabled } func (k Keeper) GetBlocksPerDistributionTransmission(ctx sdk.Context) int64 { - var bpdt int64 - k.paramStore.Get(ctx, ccvtypes.KeyBlocksPerDistributionTransmission, &bpdt) - return bpdt + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.BlocksPerDistributionTransmission } func (k Keeper) SetBlocksPerDistributionTransmission(ctx sdk.Context, bpdt int64) { - k.paramStore.Set(ctx, ccvtypes.KeyBlocksPerDistributionTransmission, bpdt) + params, err := k.GetConsumerParams(ctx) + if err != nil { + //TODO @bermuell: Check behaviour for error case + return + } + params.BlocksPerDistributionTransmission = bpdt + k.SetParams(ctx, params) } func (k Keeper) GetDistributionTransmissionChannel(ctx sdk.Context) string { - var s string - k.paramStore.Get(ctx, ccvtypes.KeyDistributionTransmissionChannel, &s) - return s + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.DistributionTransmissionChannel } func (k Keeper) SetDistributionTransmissionChannel(ctx sdk.Context, channel string) { - k.paramStore.Set(ctx, ccvtypes.KeyDistributionTransmissionChannel, channel) + params, err := k.GetConsumerParams(ctx) + if err != nil { + return + //TODO @bermuell: Check behaviour for error case + } + params.DistributionTransmissionChannel = channel + k.SetParams(ctx, params) } func (k Keeper) GetProviderFeePoolAddrStr(ctx sdk.Context) string { - var s string - k.paramStore.Get(ctx, ccvtypes.KeyProviderFeePoolAddrStr, &s) - return s + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.ProviderFeePoolAddrStr + } func (k Keeper) SetProviderFeePoolAddrStr(ctx sdk.Context, addr string) { - k.paramStore.Set(ctx, ccvtypes.KeyProviderFeePoolAddrStr, addr) + params, err := k.GetConsumerParams(ctx) + if err != nil { + //TODO @bermuell: Check behaviour for error case + return + } + params.ProviderFeePoolAddrStr = addr + k.SetParams(ctx, params) } // GetCCVTimeoutPeriod returns the timeout period for sent ccv related ibc packets func (k Keeper) GetCCVTimeoutPeriod(ctx sdk.Context) time.Duration { - var p time.Duration - k.paramStore.Get(ctx, ccvtypes.KeyCCVTimeoutPeriod, &p) - return p + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.CcvTimeoutPeriod } // GetTransferTimeoutPeriod returns the timeout period for sent transfer related ibc packets func (k Keeper) GetTransferTimeoutPeriod(ctx sdk.Context) time.Duration { - var p time.Duration - k.paramStore.Get(ctx, ccvtypes.KeyTransferTimeoutPeriod, &p) - return p + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.TransferTimeoutPeriod + } // GetConsumerRedistributionFrac returns the fraction of tokens allocated to the consumer redistribution // address during distribution events. The fraction is a string representing a // decimal number. For example "0.75" would represent 75%. func (k Keeper) GetConsumerRedistributionFrac(ctx sdk.Context) string { - var str string - k.paramStore.Get(ctx, ccvtypes.KeyConsumerRedistributionFrac, &str) - return str + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.ConsumerRedistributionFraction + } // GetHistoricalEntries returns the number of historical info entries to persist in store func (k Keeper) GetHistoricalEntries(ctx sdk.Context) int64 { - var n int64 - k.paramStore.Get(ctx, ccvtypes.KeyHistoricalEntries, &n) - return n + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.HistoricalEntries } // Only used to set an unbonding period in diff tests +// TODO @bermuell: move this to testutil func (k Keeper) SetUnbondingPeriod(ctx sdk.Context, period time.Duration) { - k.paramStore.Set(ctx, ccvtypes.KeyConsumerUnbondingPeriod, period) + params, err := k.GetConsumerParams(ctx) + if err != nil { + //TODO @bermuell: Check behaviour for error case + return + } + params.UnbondingPeriod = period + k.SetParams(ctx, params) } +// GetUnbondingPeriod returns the unbonding period of the consumer func (k Keeper) GetUnbondingPeriod(ctx sdk.Context) time.Duration { - var period time.Duration - k.paramStore.Get(ctx, ccvtypes.KeyConsumerUnbondingPeriod, &period) - return period + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.UnbondingPeriod } // GetSoftOptOutThreshold returns the percentage of validators at the bottom of the set // that can opt out of running the consumer chain func (k Keeper) GetSoftOptOutThreshold(ctx sdk.Context) string { - var str string - k.paramStore.Get(ctx, ccvtypes.KeySoftOptOutThreshold, &str) - return str + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.SoftOptOutThreshold } func (k Keeper) GetRewardDenoms(ctx sdk.Context) []string { - var denoms []string - k.paramStore.Get(ctx, ccvtypes.KeyRewardDenoms, &denoms) - return denoms + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.RewardDenoms } func (k Keeper) GetProviderRewardDenoms(ctx sdk.Context) []string { - var denoms []string - k.paramStore.Get(ctx, ccvtypes.KeyProviderRewardDenoms, &denoms) - return denoms + params, _ := k.GetConsumerParams(ctx) //TODO @bermuell: Check behaviour for error case + return params.ProviderRewardDenoms } diff --git a/x/ccv/consumer/keeper/params_test.go b/x/ccv/consumer/keeper/params_test.go index 49b1816520..919980acde 100644 --- a/x/ccv/consumer/keeper/params_test.go +++ b/x/ccv/consumer/keeper/params_test.go @@ -33,14 +33,16 @@ func TestParams(t *testing.T) { provideRewardDenoms, ) // these are the default params, IBC suite independently sets enabled=true - params := consumerKeeper.GetConsumerParams(ctx) + params, err := consumerKeeper.GetConsumerParams(ctx) + require.NoError(t, err) require.Equal(t, expParams, params) newParams := ccv.NewParams(false, 1000, "channel-2", "cosmos19pe9pg5dv9k5fzgzmsrgnw9rl9asf7ddwhu7lm", 7*24*time.Hour, 25*time.Hour, "0.5", 500, 24*21*time.Hour, "0.05", []string{"untrn"}, []string{"uatom"}) consumerKeeper.SetParams(ctx, newParams) - params = consumerKeeper.GetConsumerParams(ctx) + params, err = consumerKeeper.GetConsumerParams(ctx) + require.NoError(t, err) require.Equal(t, newParams, params) consumerKeeper.SetBlocksPerDistributionTransmission(ctx, 10) diff --git a/x/ccv/consumer/keeper/soft_opt_out.go b/x/ccv/consumer/keeper/soft_opt_out.go index 41460e7917..c9c2607ac5 100644 --- a/x/ccv/consumer/keeper/soft_opt_out.go +++ b/x/ccv/consumer/keeper/soft_opt_out.go @@ -12,7 +12,7 @@ import ( // SetSmallestNonOptOutPower sets the smallest validator power that cannot soft opt out. func (k Keeper) SetSmallestNonOptOutPower(ctx sdk.Context, power uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.SmallestNonOptOutPowerKey(), sdk.Uint64ToBigEndian(power)) } @@ -67,9 +67,9 @@ func (k Keeper) UpdateSmallestNonOptOutPower(ctx sdk.Context) { // GetSmallestNonOptOutPower returns the smallest validator power that cannot soft opt out. func (k Keeper) GetSmallestNonOptOutPower(ctx sdk.Context) int64 { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.SmallestNonOptOutPowerKey()) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.SmallestNonOptOutPowerKey()) + if err != nil || bz == nil { return 0 } return int64(binary.BigEndian.Uint64(bz)) diff --git a/x/ccv/consumer/keeper/throttle_retry.go b/x/ccv/consumer/keeper/throttle_retry.go index 4c4585cb1d..5d43e71beb 100644 --- a/x/ccv/consumer/keeper/throttle_retry.go +++ b/x/ccv/consumer/keeper/throttle_retry.go @@ -83,12 +83,12 @@ func (k Keeper) UpdateSlashRecordOnBounce(ctx sdktypes.Context) { } func (k Keeper) GetSlashRecord(ctx sdktypes.Context) (record consumertypes.SlashRecord, found bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(consumertypes.SlashRecordKey()) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(consumertypes.SlashRecordKey()) + if err != nil || bz == nil { return record, false } - err := record.Unmarshal(bz) + err = record.Unmarshal(bz) if err != nil { // This should never happen panic(fmt.Sprintf("could not unmarshal slash record: %v", err)) @@ -97,7 +97,7 @@ func (k Keeper) GetSlashRecord(ctx sdktypes.Context) (record consumertypes.Slash } func (k Keeper) SetSlashRecord(ctx sdktypes.Context, record consumertypes.SlashRecord) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := record.Marshal() if err != nil { // This should never happen @@ -107,6 +107,6 @@ func (k Keeper) SetSlashRecord(ctx sdktypes.Context, record consumertypes.SlashR } func (k Keeper) ClearSlashRecord(ctx sdktypes.Context) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(consumertypes.SlashRecordKey()) } diff --git a/x/ccv/consumer/keeper/validators.go b/x/ccv/consumer/keeper/validators.go index ce2d726ca2..72dc077df8 100644 --- a/x/ccv/consumer/keeper/validators.go +++ b/x/ccv/consumer/keeper/validators.go @@ -207,11 +207,11 @@ func (k Keeper) UnbondingTime(ctx context.Context) (time.Duration, error) { // GetHistoricalInfo gets the historical info at a given height func (k Keeper) GetHistoricalInfo(ctx context.Context, height int64) (stakingtypes.HistoricalInfo, error) { sdkCtx := sdk.UnwrapSDKContext(ctx) - store := sdkCtx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(sdkCtx) key := types.HistoricalInfoKey(height) - value := store.Get(key) - if value == nil { + value, err := store.Get(key) + if err != nil || value == nil { return stakingtypes.HistoricalInfo{}, stakingtypes.ErrNoHistoricalInfo } @@ -221,7 +221,7 @@ func (k Keeper) GetHistoricalInfo(ctx context.Context, height int64) (stakingtyp // SetHistoricalInfo sets the historical info at a given height func (k Keeper) SetHistoricalInfo(ctx context.Context, height int64, hi *stakingtypes.HistoricalInfo) { sdkCtx := sdk.UnwrapSDKContext(ctx) - store := sdkCtx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(sdkCtx) key := types.HistoricalInfoKey(height) value := k.cdc.MustMarshal(hi) @@ -231,7 +231,7 @@ func (k Keeper) SetHistoricalInfo(ctx context.Context, height int64, hi *staking // DeleteHistoricalInfo deletes the historical info at a given height func (k Keeper) DeleteHistoricalInfo(ctx context.Context, height int64) error { sdkCtx := sdk.UnwrapSDKContext(ctx) - store := sdkCtx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(sdkCtx) key := types.HistoricalInfoKey(height) store.Delete(key) diff --git a/x/ccv/provider/keeper/distribution.go b/x/ccv/provider/keeper/distribution.go index 2cf6ef08f6..c0c2b56d39 100644 --- a/x/ccv/provider/keeper/distribution.go +++ b/x/ccv/provider/keeper/distribution.go @@ -1,9 +1,8 @@ package keeper import ( - sdk "github.com/cosmos/cosmos-sdk/types" - storetypes "cosmossdk.io/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/interchain-security/v3/x/ccv/provider/types" ) @@ -25,31 +24,38 @@ func (k Keeper) SetConsumerRewardDenom( ctx sdk.Context, denom string, ) { - store := ctx.KVStore(k.storeKey) - store.Set(types.ConsumerRewardDenomsKey(denom), []byte{}) + store := k.storeService.OpenKVStore(ctx) + err := store.Set(types.ConsumerRewardDenomsKey(denom), []byte{}) + if err != nil { + k.Logger(ctx).Error("Error setting consumer reward denoms: %v", err) + } } func (k Keeper) ConsumerRewardDenomExists( ctx sdk.Context, denom string, ) bool { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ConsumerRewardDenomsKey(denom)) - return bz != nil + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ConsumerRewardDenomsKey(denom)) + return err == nil && bz != nil } func (k Keeper) DeleteConsumerRewardDenom( ctx sdk.Context, denom string, ) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ConsumerRewardDenomsKey(denom)) } func (k Keeper) GetAllConsumerRewardDenoms(ctx sdk.Context) (consumerRewardDenoms []string) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ConsumerRewardDenomsBytePrefix}) - + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.ConsumerRewardDenomsBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("Error iterating over all consumer reward denoms: %v", err) + return + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { key := iterator.Key()[1:] diff --git a/x/ccv/provider/keeper/genesis.go b/x/ccv/provider/keeper/genesis.go index e86035170c..ff0c5994e7 100644 --- a/x/ccv/provider/keeper/genesis.go +++ b/x/ccv/provider/keeper/genesis.go @@ -151,7 +151,10 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { consumerAddrsToPrune = append(consumerAddrsToPrune, k.GetAllConsumerAddrsToPrune(ctx, chain.ChainId)...) } - params := k.GetParams(ctx) + params, err := k.GetParams(ctx) + if err != nil { + panic(fmt.Errorf("error getting provider parameters: %v", err)) + } return types.NewGenesisState( k.GetValidatorSetUpdateId(ctx), diff --git a/x/ccv/provider/keeper/genesis_test.go b/x/ccv/provider/keeper/genesis_test.go index e8119bef0a..5e0fca96ca 100644 --- a/x/ccv/provider/keeper/genesis_test.go +++ b/x/ccv/provider/keeper/genesis_test.go @@ -147,7 +147,9 @@ func TestInitAndExportGenesis(t *testing.T) { // Expect slash meter to be initialized to it's allowance value // (replenish fraction * mocked value defined above) slashMeter := pk.GetSlashMeter(ctx) - replenishFraction, err := math.LegacyNewDecFromStr(pk.GetParams(ctx).SlashMeterReplenishFraction) + params, err := pk.GetParams(ctx) + require.NoError(t, err) + replenishFraction, err := math.LegacyNewDecFromStr(params.SlashMeterReplenishFraction) require.NoError(t, err) expectedSlashMeterValue := math.NewInt(replenishFraction.MulInt(math.NewInt(100)).RoundInt64()) require.Equal(t, expectedSlashMeterValue, slashMeter) @@ -173,7 +175,7 @@ func TestInitAndExportGenesis(t *testing.T) { require.True(t, found) require.Equal(t, provGenesis.ConsumerAdditionProposals[0], addProp) require.True(t, pk.PendingConsumerRemovalPropExists(ctx, cChainIDs[0], oneHourFromNow)) - require.Equal(t, provGenesis.Params, pk.GetParams(ctx)) + require.Equal(t, provGenesis.Params, params) gotConsTmPubKey, found := pk.GetValidatorConsumerPubKey(ctx, cChainIDs[0], provAddr) require.True(t, found) diff --git a/x/ccv/provider/keeper/keeper.go b/x/ccv/provider/keeper/keeper.go index 996ce651cd..1a430ae8f7 100644 --- a/x/ccv/provider/keeper/keeper.go +++ b/x/ccv/provider/keeper/keeper.go @@ -22,7 +22,6 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" "cosmossdk.io/log" @@ -38,11 +37,9 @@ type Keeper struct { // should be the x/gov module account. authority string - storeKey storetypes.StoreKey // TODO: remove storeService store.KVStoreService cdc codec.BinaryCodec - paramSpace paramtypes.Subspace scopedKeeper ccv.ScopedKeeper channelKeeper ccv.ChannelKeeper portKeeper ccv.PortKeeper @@ -61,7 +58,7 @@ type Keeper struct { // NewKeeper creates a new provider Keeper instance func NewKeeper( - cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace paramtypes.Subspace, scopedKeeper ccv.ScopedKeeper, + cdc codec.BinaryCodec, storeService store.KVStoreService, scopedKeeper ccv.ScopedKeeper, channelKeeper ccv.ChannelKeeper, portKeeper ccv.PortKeeper, connectionKeeper ccv.ConnectionKeeper, clientKeeper ccv.ClientKeeper, stakingKeeper ccv.StakingKeeper, slashingKeeper ccv.SlashingKeeper, @@ -70,16 +67,10 @@ func NewKeeper( feeCollectorName, authority string, validatorAddressCodec, consensusAddressCodec addresscodec.Codec, ) Keeper { - // set KeyTable if it has not already been set - if !paramSpace.HasKeyTable() { - paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable()) - } - k := Keeper{ cdc: cdc, - storeKey: key, + storeService: storeService, authority: authority, - paramSpace: paramSpace, scopedKeeper: scopedKeeper, channelKeeper: channelKeeper, portKeeper: portKeeper, @@ -114,19 +105,13 @@ func (k Keeper) ConsensusAddressCodec() addresscodec.Codec { return k.consensusAddressCodec } -// SetParamSpace sets the param space for the provider keeper. -// Note: this is only used for testing! -func (k *Keeper) SetParamSpace(ctx sdk.Context, ps paramtypes.Subspace) { - k.paramSpace = ps -} - // TODO: @MSalopek -> redo validation; some fields will be removed // Validates that the provider keeper is initialized with non-zero and // non-nil values for all its fields. Otherwise this method will panic. func (k Keeper) mustValidateFields() { // Ensures no fields are missed in this validation - if reflect.ValueOf(k).NumField() != 18 { - panic("number of fields in provider keeper is not 18") + if reflect.ValueOf(k).NumField() != 16 { + panic("number of fields in provider keeper is not 16") } // TODO: @MSalopek -> validate once connected and AccountKeeper interface is updated @@ -140,24 +125,23 @@ func (k Keeper) mustValidateFields() { } ccv.PanicIfZeroOrNil(k.cdc, "cdc") // 1 - ccv.PanicIfZeroOrNil(k.storeKey, "storeKey") // 2 - ccv.PanicIfZeroOrNil(k.paramSpace, "paramSpace") // 3 - ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 4 - ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 5 - ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 6 - ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 7 - ccv.PanicIfZeroOrNil(k.accountKeeper, "accountKeeper") // 8 - ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 9 - ccv.PanicIfZeroOrNil(k.stakingKeeper, "stakingKeeper") // 10 - ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 11 - ccv.PanicIfZeroOrNil(k.distributionKeeper, "distributionKeeper") // 12 - ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 13 - ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 14 - ccv.PanicIfZeroOrNil(k.authority, "authority") // 15 - ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 16 - ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 17 + ccv.PanicIfZeroOrNil(k.storeService, "storeService") // 2 + ccv.PanicIfZeroOrNil(k.scopedKeeper, "scopedKeeper") // 3 + ccv.PanicIfZeroOrNil(k.channelKeeper, "channelKeeper") // 4 + ccv.PanicIfZeroOrNil(k.portKeeper, "portKeeper") // 5 + ccv.PanicIfZeroOrNil(k.connectionKeeper, "connectionKeeper") // 6 + ccv.PanicIfZeroOrNil(k.accountKeeper, "accountKeeper") // 7 + ccv.PanicIfZeroOrNil(k.clientKeeper, "clientKeeper") // 8 + ccv.PanicIfZeroOrNil(k.stakingKeeper, "stakingKeeper") // 9 + ccv.PanicIfZeroOrNil(k.slashingKeeper, "slashingKeeper") // 10 + ccv.PanicIfZeroOrNil(k.distributionKeeper, "distributionKeeper") // 11 + ccv.PanicIfZeroOrNil(k.bankKeeper, "bankKeeper") // 12 + ccv.PanicIfZeroOrNil(k.feeCollectorName, "feeCollectorName") // 13 + ccv.PanicIfZeroOrNil(k.authority, "authority") // 14 + ccv.PanicIfZeroOrNil(k.validatorAddressCodec, "validatorAddressCodec") // 15 + ccv.PanicIfZeroOrNil(k.consensusAddressCodec, "consensusAddressCodec") // 16 // TODO: @MSalopek -> validate once connected - // ccv.PanicIfZeroOrNil(k.storeService, "storeService") // 18 + // ccv.PanicIfZeroOrNil(k.storeService, "storeService") // 17 } // Logger returns a module-specific logger. @@ -183,14 +167,22 @@ func (k Keeper) BindPort(ctx sdk.Context, portID string) error { // GetPort returns the portID for the CCV module. Used in ExportGenesis func (k Keeper) GetPort(ctx sdk.Context) string { - store := ctx.KVStore(k.storeKey) - return string(store.Get(types.PortKey())) + store := k.storeService.OpenKVStore(ctx) + port, err := store.Get(types.PortKey()) + if err != nil { + k.Logger(ctx).Error("error getting port ID: %v", err) + return "" + } + return string(port) } // SetPort sets the portID for the CCV module. Used in InitGenesis func (k Keeper) SetPort(ctx sdk.Context, portID string) { - store := ctx.KVStore(k.storeKey) - store.Set(types.PortKey(), []byte(portID)) + store := k.storeService.OpenKVStore(ctx) + err := store.Set(types.PortKey(), []byte(portID)) + if err != nil { + k.Logger(ctx).Error("error setting port ID: %v", err) + } } // AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function @@ -206,15 +198,15 @@ func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability // SetChainToChannel sets the mapping from a consumer chainID to the CCV channel ID for that consumer chain. func (k Keeper) SetChainToChannel(ctx sdk.Context, chainID, channelID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.ChainToChannelKey(chainID), []byte(channelID)) } // GetChainToChannel gets the CCV channelID for the given consumer chainID func (k Keeper) GetChainToChannel(ctx sdk.Context, chainID string) (string, bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ChainToChannelKey(chainID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ChainToChannelKey(chainID)) + if err != nil || bz == nil { return "", false } return string(bz), true @@ -222,7 +214,7 @@ func (k Keeper) GetChainToChannel(ctx sdk.Context, chainID string) (string, bool // DeleteChainToChannel deletes the CCV channel ID for the given consumer chain ID func (k Keeper) DeleteChainToChannel(ctx sdk.Context, chainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ChainToChannelKey(chainID)) } @@ -233,8 +225,13 @@ func (k Keeper) DeleteChainToChannel(ctx sdk.Context, chainID string) { // ChainToClientBytePrefix | chainID // Thus, the returned array is in ascending order of chainIDs. func (k Keeper) GetAllConsumerChains(ctx sdk.Context) (chains []types.Chain) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ChainToClientBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.ChainToClientBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting consumer chains: %v", err) + return []types.Chain{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -253,15 +250,15 @@ func (k Keeper) GetAllConsumerChains(ctx sdk.Context) (chains []types.Chain) { // SetChannelToChain sets the mapping from the CCV channel ID to the consumer chainID. func (k Keeper) SetChannelToChain(ctx sdk.Context, channelID, chainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.ChannelToChainKey(channelID), []byte(chainID)) } // GetChannelToChain gets the consumer chainID for a given CCV channelID func (k Keeper) GetChannelToChain(ctx sdk.Context, channelID string) (string, bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ChannelToChainKey(channelID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ChannelToChainKey(channelID)) + if err != nil || bz == nil { return "", false } return string(bz), true @@ -269,7 +266,7 @@ func (k Keeper) GetChannelToChain(ctx sdk.Context, channelID string) (string, bo // DeleteChannelToChain deletes the consumer chain ID for a given CCV channelID func (k Keeper) DeleteChannelToChain(ctx sdk.Context, channelID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ChannelToChainKey(channelID)) } @@ -281,8 +278,13 @@ func (k Keeper) DeleteChannelToChain(ctx sdk.Context, channelID string) { // ChannelToChainBytePrefix | channelID // Thus, the returned array is in ascending order of channelIDs. func (k Keeper) GetAllChannelToChains(ctx sdk.Context) (channels []types.ChannelToChain) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ChannelToChainBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.ChannelToChainBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting channel to chain mapping: %v", err) + return []types.ChannelToChain{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -300,7 +302,7 @@ func (k Keeper) GetAllChannelToChains(ctx sdk.Context) (channels []types.Channel } func (k Keeper) SetConsumerGenesis(ctx sdk.Context, chainID string, gen ccv.GenesisState) error { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := gen.Marshal() if err != nil { return err @@ -311,9 +313,9 @@ func (k Keeper) SetConsumerGenesis(ctx sdk.Context, chainID string, gen ccv.Gene } func (k Keeper) GetConsumerGenesis(ctx sdk.Context, chainID string) (ccv.GenesisState, bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ConsumerGenesisKey(chainID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ConsumerGenesisKey(chainID)) + if err != nil || bz == nil { return ccv.GenesisState{}, false } @@ -327,7 +329,7 @@ func (k Keeper) GetConsumerGenesis(ctx sdk.Context, chainID string) (ccv.Genesis } func (k Keeper) DeleteConsumerGenesis(ctx sdk.Context, chainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ConsumerGenesisKey(chainID)) } @@ -408,7 +410,7 @@ func (k Keeper) SetConsumerChain(ctx sdk.Context, channelID string) error { // SetUnbondingOp sets the UnbondingOp by its unique ID func (k Keeper) SetUnbondingOp(ctx sdk.Context, unbondingOp types.UnbondingOp) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := unbondingOp.Marshal() if err != nil { // An error here would indicate something is very wrong, @@ -422,9 +424,9 @@ func (k Keeper) SetUnbondingOp(ctx sdk.Context, unbondingOp types.UnbondingOp) { // GetUnbondingOp gets a UnbondingOp by its unique ID func (k Keeper) GetUnbondingOp(ctx sdk.Context, id uint64) (types.UnbondingOp, bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.UnbondingOpKey(id)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.UnbondingOpKey(id)) + if err != nil || bz == nil { return types.UnbondingOp{}, false } @@ -440,7 +442,7 @@ func (k Keeper) GetUnbondingOp(ctx sdk.Context, id uint64) (types.UnbondingOp, b // DeleteUnbondingOp deletes a UnbondingOp given its ID func (k Keeper) DeleteUnbondingOp(ctx sdk.Context, id uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.UnbondingOpKey(id)) } @@ -452,8 +454,13 @@ func (k Keeper) DeleteUnbondingOp(ctx sdk.Context, id uint64) { // UnbondingOpBytePrefix | ID // Thus, the iteration is in ascending order of IDs. func (k Keeper) GetAllUnbondingOps(ctx sdk.Context) (ops []types.UnbondingOp) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.UnbondingOpBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.UnbondingOpBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all UnbondingOps: %v", err) + return []types.UnbondingOp{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -519,7 +526,7 @@ func removeStringFromSlice(slice []string, x string) (newSlice []string, numRemo // SetUnbondingOpIndex sets the IDs of unbonding operations that are waiting for // a VSCMaturedPacket with vscID from a consumer with chainID func (k Keeper) SetUnbondingOpIndex(ctx sdk.Context, chainID string, vscID uint64, ids []uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) vscUnbondingOps := types.VscUnbondingOps{ VscId: vscID, @@ -543,8 +550,13 @@ func (k Keeper) SetUnbondingOpIndex(ctx sdk.Context, chainID string, vscID uint6 // UnbondingOpIndexBytePrefix | len(chainID) | chainID | vscID // Thus, the returned array is in ascending order of vscIDs. func (k Keeper) GetAllUnbondingOpIndexes(ctx sdk.Context, chainID string) (indexes []types.VscUnbondingOps) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.UnbondingOpIndexBytePrefix, chainID)) + store := k.storeService.OpenKVStore(ctx) + prefix := types.ChainIdWithLenKey(types.UnbondingOpIndexBytePrefix, chainID) + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all unbonding indexes: %v", err) + return []types.VscUnbondingOps{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -567,10 +579,10 @@ func (k Keeper) GetAllUnbondingOpIndexes(ctx sdk.Context, chainID string) (index // GetUnbondingOpIndex gets the IDs of unbonding operations that are waiting for // a VSCMaturedPacket with vscID from a consumer with chainID func (k Keeper) GetUnbondingOpIndex(ctx sdk.Context, chainID string, vscID uint64) ([]uint64, bool) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) - bz := store.Get(types.UnbondingOpIndexKey(chainID, vscID)) - if bz == nil { + bz, err := store.Get(types.UnbondingOpIndexKey(chainID, vscID)) + if err != nil || bz == nil { return []uint64{}, false } @@ -587,7 +599,7 @@ func (k Keeper) GetUnbondingOpIndex(ctx sdk.Context, chainID string, vscID uint6 // DeleteUnbondingOpIndex deletes the IDs of unbonding operations that are waiting for // a VSCMaturedPacket with vscID from a consumer with chainID func (k Keeper) DeleteUnbondingOpIndex(ctx sdk.Context, chainID string, vscID uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.UnbondingOpIndexKey(chainID, vscID)) } @@ -613,9 +625,9 @@ func (k Keeper) GetUnbondingOpsFromIndex(ctx sdk.Context, chainID string, valset // GetMaturedUnbondingOps returns the list of matured unbonding operation ids func (k Keeper) GetMaturedUnbondingOps(ctx sdk.Context) (ids []uint64) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.MaturedUnbondingOpsKey()) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.MaturedUnbondingOpsKey()) + if err != nil || bz == nil { // Note that every call to ConsumeMaturedUnbondingOps // deletes the MaturedUnbondingOpsKey, which means that // the first call to GetMaturedUnbondingOps after that @@ -642,7 +654,7 @@ func (k Keeper) AppendMaturedUnbondingOps(ctx sdk.Context, ids []uint64) { Ids: append(existingIds, ids...), } - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := maturedOps.Marshal() if err != nil { // An error here would indicate something is very wrong, @@ -655,7 +667,7 @@ func (k Keeper) AppendMaturedUnbondingOps(ctx sdk.Context, ids []uint64) { // ConsumeMaturedUnbondingOps empties and returns list of matured unbonding operation ids (if it exists) func (k Keeper) ConsumeMaturedUnbondingOps(ctx sdk.Context) []uint64 { ids := k.GetMaturedUnbondingOps(ctx) - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.MaturedUnbondingOpsKey()) return ids } @@ -699,7 +711,7 @@ func (k Keeper) IncrementValidatorSetUpdateId(ctx sdk.Context) { } func (k Keeper) SetValidatorSetUpdateId(ctx sdk.Context, valUpdateID uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) // Convert back into bytes for storage bz := make([]byte, 8) @@ -709,10 +721,10 @@ func (k Keeper) SetValidatorSetUpdateId(ctx sdk.Context, valUpdateID uint64) { } func (k Keeper) GetValidatorSetUpdateId(ctx sdk.Context) (validatorSetUpdateId uint64) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ValidatorSetUpdateIdKey()) + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ValidatorSetUpdateIdKey()) - if bz == nil { + if err != nil || bz == nil { validatorSetUpdateId = 0 } else { // Unmarshal @@ -724,7 +736,7 @@ func (k Keeper) GetValidatorSetUpdateId(ctx sdk.Context) (validatorSetUpdateId u // SetValsetUpdateBlockHeight sets the block height for a given valset update id func (k Keeper) SetValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId, blockHeight uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) heightBytes := make([]byte, 8) binary.BigEndian.PutUint64(heightBytes, blockHeight) store.Set(types.ValsetUpdateBlockHeightKey(valsetUpdateId), heightBytes) @@ -732,9 +744,9 @@ func (k Keeper) SetValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId, bloc // GetValsetUpdateBlockHeight gets the block height for a given valset update id func (k Keeper) GetValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId uint64) (uint64, bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ValsetUpdateBlockHeightKey(valsetUpdateId)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ValsetUpdateBlockHeightKey(valsetUpdateId)) + if err != nil || bz == nil { return 0, false } return binary.BigEndian.Uint64(bz), true @@ -746,10 +758,15 @@ func (k Keeper) GetValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId uint6 // ValsetUpdateBlockHeightBytePrefix | vscID // Thus, the returned array is in ascending order of vscIDs. func (k Keeper) GetAllValsetUpdateBlockHeights(ctx sdk.Context) (valsetUpdateBlockHeights []types.ValsetUpdateIdToHeight) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ValsetUpdateBlockHeightBytePrefix}) - + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.ValsetUpdateBlockHeightBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all block heights for valset updates: %v", err) + return []types.ValsetUpdateIdToHeight{} + } defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { valsetUpdateId := binary.BigEndian.Uint64(iterator.Key()[1:]) height := binary.BigEndian.Uint64(iterator.Value()) @@ -765,7 +782,7 @@ func (k Keeper) GetAllValsetUpdateBlockHeights(ctx sdk.Context) (valsetUpdateBlo // DeleteValsetUpdateBlockHeight deletes the block height value for a given vaset update id func (k Keeper) DeleteValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ValsetUpdateBlockHeightKey(valsetUpdateId)) } @@ -774,7 +791,7 @@ func (k Keeper) DeleteValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId ui // TODO: SlashAcks should be persisted as a list of ConsumerConsAddr types, not strings. // See https://github.com/cosmos/interchain-security/issues/728 func (k Keeper) SetSlashAcks(ctx sdk.Context, chainID string, acks []string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) sa := types.SlashAcks{ Addresses: acks, @@ -793,9 +810,9 @@ func (k Keeper) SetSlashAcks(ctx sdk.Context, chainID string, acks []string) { // TODO: SlashAcks should be persisted as a list of ConsumerConsAddr types, not strings. // See https://github.com/cosmos/interchain-security/issues/728 func (k Keeper) GetSlashAcks(ctx sdk.Context, chainID string) []string { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.SlashAcksKey(chainID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.SlashAcksKey(chainID)) + if err != nil || bz == nil { return nil } var acks types.SlashAcks @@ -814,14 +831,14 @@ func (k Keeper) ConsumeSlashAcks(ctx sdk.Context, chainID string) (acks []string if len(acks) < 1 { return } - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.SlashAcksKey(chainID)) return } // DeleteSlashAcks deletes the slash acks for a given chain ID func (k Keeper) DeleteSlashAcks(ctx sdk.Context, chainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.SlashAcksKey(chainID)) } @@ -836,7 +853,7 @@ func (k Keeper) AppendSlashAck(ctx sdk.Context, chainID, // SetInitChainHeight sets the provider block height when the given consumer chain was initiated func (k Keeper) SetInitChainHeight(ctx sdk.Context, chainID string, height uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) heightBytes := make([]byte, 8) binary.BigEndian.PutUint64(heightBytes, height) @@ -845,9 +862,9 @@ func (k Keeper) SetInitChainHeight(ctx sdk.Context, chainID string, height uint6 // GetInitChainHeight returns the provider block height when the given consumer chain was initiated func (k Keeper) GetInitChainHeight(ctx sdk.Context, chainID string) (uint64, bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.InitChainHeightKey(chainID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.InitChainHeightKey(chainID)) + if err != nil || bz == nil { return 0, false } @@ -856,7 +873,7 @@ func (k Keeper) GetInitChainHeight(ctx sdk.Context, chainID string) (uint64, boo // DeleteInitChainHeight deletes the block height value for which the given consumer chain's channel was established func (k Keeper) DeleteInitChainHeight(ctx sdk.Context, chainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.InitChainHeightKey(chainID)) } @@ -864,9 +881,9 @@ func (k Keeper) DeleteInitChainHeight(ctx sdk.Context, chainID string) { func (k Keeper) GetPendingVSCPackets(ctx sdk.Context, chainID string) []ccv.ValidatorSetChangePacketData { var packets types.ValidatorSetChangePackets - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.PendingVSCsKey(chainID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.PendingVSCsKey(chainID)) + if err != nil || bz == nil { return []ccv.ValidatorSetChangePacketData{} } if err := packets.Unmarshal(bz); err != nil { @@ -882,7 +899,7 @@ func (k Keeper) GetPendingVSCPackets(ctx sdk.Context, chainID string) []ccv.Vali func (k Keeper) AppendPendingVSCPackets(ctx sdk.Context, chainID string, newPackets ...ccv.ValidatorSetChangePacketData) { pds := append(k.GetPendingVSCPackets(ctx, chainID), newPackets...) - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) packets := types.ValidatorSetChangePackets{List: pds} buf, err := packets.Marshal() if err != nil { @@ -895,21 +912,21 @@ func (k Keeper) AppendPendingVSCPackets(ctx sdk.Context, chainID string, newPack // DeletePendingVSCPackets deletes the list of pending ValidatorSetChange packets for chain ID func (k Keeper) DeletePendingVSCPackets(ctx sdk.Context, chainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.PendingVSCsKey(chainID)) } // SetConsumerClientId sets the client ID for the given chain ID func (k Keeper) SetConsumerClientId(ctx sdk.Context, chainID, clientID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.ChainToClientKey(chainID), []byte(clientID)) } // GetConsumerClientId returns the client ID for the given chain ID. func (k Keeper) GetConsumerClientId(ctx sdk.Context, chainID string) (string, bool) { - store := ctx.KVStore(k.storeKey) - clientIdBytes := store.Get(types.ChainToClientKey(chainID)) - if clientIdBytes == nil { + store := k.storeService.OpenKVStore(ctx) + clientIdBytes, err := store.Get(types.ChainToClientKey(chainID)) + if err != nil || clientIdBytes == nil { return "", false } return string(clientIdBytes), true @@ -917,13 +934,13 @@ func (k Keeper) GetConsumerClientId(ctx sdk.Context, chainID string) (string, bo // DeleteConsumerClientId removes from the store the clientID for the given chainID. func (k Keeper) DeleteConsumerClientId(ctx sdk.Context, chainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ChainToClientKey(chainID)) } // SetInitTimeoutTimestamp sets the init timeout timestamp for the given chain ID func (k Keeper) SetInitTimeoutTimestamp(ctx sdk.Context, chainID string, ts uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) tsBytes := make([]byte, 8) binary.BigEndian.PutUint64(tsBytes, ts) store.Set(types.InitTimeoutTimestampKey(chainID), tsBytes) @@ -932,9 +949,9 @@ func (k Keeper) SetInitTimeoutTimestamp(ctx sdk.Context, chainID string, ts uint // GetInitTimeoutTimestamp returns the init timeout timestamp for the given chain ID. // This method is used only in testing. func (k Keeper) GetInitTimeoutTimestamp(ctx sdk.Context, chainID string) (uint64, bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.InitTimeoutTimestampKey(chainID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.InitTimeoutTimestampKey(chainID)) + if err != nil || bz == nil { return 0, false } return binary.BigEndian.Uint64(bz), true @@ -942,7 +959,7 @@ func (k Keeper) GetInitTimeoutTimestamp(ctx sdk.Context, chainID string) (uint64 // DeleteInitTimeoutTimestamp removes from the store the init timeout timestamp for the given chainID. func (k Keeper) DeleteInitTimeoutTimestamp(ctx sdk.Context, chainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.InitTimeoutTimestampKey(chainID)) } @@ -952,8 +969,13 @@ func (k Keeper) DeleteInitTimeoutTimestamp(ctx sdk.Context, chainID string) { // InitTimeoutTimestampBytePrefix | chainID // Thus, the returned array is in ascending order of chainIDs (NOT in timestamp order). func (k Keeper) GetAllInitTimeoutTimestamps(ctx sdk.Context) (initTimeoutTimestamps []types.InitTimeoutTimestamp) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.InitTimeoutTimestampBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.InitTimeoutTimestampBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all init timeout timestamps: %v", err) + return []types.InitTimeoutTimestamp{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -977,7 +999,7 @@ func (k Keeper) SetVscSendTimestamp( vscID uint64, timestamp time.Time, ) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) // Convert timestamp into bytes for storage timeBz := sdk.FormatTimeBytes(timestamp) @@ -989,10 +1011,10 @@ func (k Keeper) SetVscSendTimestamp( // // Note: This method is used only for testing. func (k Keeper) GetVscSendTimestamp(ctx sdk.Context, chainID string, vscID uint64) (time.Time, bool) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) - timeBz := store.Get(types.VscSendingTimestampKey(chainID, vscID)) - if timeBz == nil { + timeBz, err := store.Get(types.VscSendingTimestampKey(chainID, vscID)) + if err != nil || timeBz == nil { return time.Time{}, false } @@ -1006,7 +1028,7 @@ func (k Keeper) GetVscSendTimestamp(ctx sdk.Context, chainID string, vscID uint6 // DeleteVscSendTimestamp removes from the store a specific VSC send timestamp // for the given chainID and vscID. func (k Keeper) DeleteVscSendTimestamp(ctx sdk.Context, chainID string, vscID uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.VscSendingTimestampKey(chainID, vscID)) } @@ -1016,8 +1038,13 @@ func (k Keeper) DeleteVscSendTimestamp(ctx sdk.Context, chainID string, vscID ui // VscSendTimestampBytePrefix | len(chainID) | chainID | vscID // Thus, the iteration is in ascending order of vscIDs, and as a result in send timestamp order. func (k Keeper) GetAllVscSendTimestamps(ctx sdk.Context, chainID string) (vscSendTimestamps []types.VscSendTimestamp) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, chainID)) + store := k.storeService.OpenKVStore(ctx) + prefix := types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, chainID) + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all vsc send timestamps: %v", err) + return []types.VscSendTimestamp{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -1045,8 +1072,13 @@ func (k Keeper) GetAllVscSendTimestamps(ctx sdk.Context, chainID string) (vscSen // DeleteVscSendTimestampsForConsumer deletes all VSC send timestamps for a given consumer chain func (k Keeper) DeleteVscSendTimestampsForConsumer(ctx sdk.Context, consumerChainID string) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, consumerChainID)) + store := k.storeService.OpenKVStore(ctx) + prefix := types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, consumerChainID) + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error deleting all VSC send timestamps: %v", err) + return + } defer iterator.Close() keysToDel := [][]byte{} @@ -1062,8 +1094,13 @@ func (k Keeper) DeleteVscSendTimestampsForConsumer(ctx sdk.Context, consumerChai // GetFirstVscSendTimestamp gets the vsc send timestamp with the lowest vscID for the given chainID. func (k Keeper) GetFirstVscSendTimestamp(ctx sdk.Context, chainID string) (vscSendTimestamp types.VscSendTimestamp, found bool) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, chainID)) + store := k.storeService.OpenKVStore(ctx) + prefix := types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, chainID) + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting first VSC send timestamps: %v", err) + return types.VscSendTimestamp{}, false + } defer iterator.Close() if iterator.Valid() { @@ -1096,7 +1133,7 @@ func (k Keeper) SetSlashLog( ctx sdk.Context, providerAddr types.ProviderConsAddress, ) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Set(types.SlashLogKey(providerAddr), []byte{}) } @@ -1106,9 +1143,9 @@ func (k Keeper) GetSlashLog( ctx sdk.Context, providerAddr types.ProviderConsAddress, ) (found bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.SlashLogKey(providerAddr)) - return bz != nil + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.SlashLogKey(providerAddr)) + return err == nil && bz != nil } func (k Keeper) BondDenom(ctx sdk.Context) (string, error) { diff --git a/x/ccv/provider/keeper/key_assignment.go b/x/ccv/provider/keeper/key_assignment.go index 564a48f305..e07cf4f623 100644 --- a/x/ccv/provider/keeper/key_assignment.go +++ b/x/ccv/provider/keeper/key_assignment.go @@ -22,12 +22,12 @@ func (k Keeper) GetValidatorConsumerPubKey( chainID string, providerAddr types.ProviderConsAddress, ) (consumerKey tmprotocrypto.PublicKey, found bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ConsumerValidatorsKey(chainID, providerAddr)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ConsumerValidatorsKey(chainID, providerAddr)) + if err != nil || bz == nil { return consumerKey, false } - err := consumerKey.Unmarshal(bz) + err = consumerKey.Unmarshal(bz) if err != nil { // An error here would indicate something is very wrong, // the consumer key is assumed to be correctly serialized in SetValidatorConsumerPubKey. @@ -43,7 +43,7 @@ func (k Keeper) SetValidatorConsumerPubKey( providerAddr types.ProviderConsAddress, consumerKey tmprotocrypto.PublicKey, ) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := consumerKey.Marshal() if err != nil { // An error here would indicate something is very wrong, @@ -62,7 +62,7 @@ func (k Keeper) SetValidatorConsumerPubKey( // - in ascending order of providerAddresses, if chainID is not nil; // - in undetermined order, if chainID is nil. func (k Keeper) GetAllValidatorConsumerPubKeys(ctx sdk.Context, chainID *string) (validatorConsumerPubKeys []types.ValidatorConsumerPubKey) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) var prefix []byte if chainID == nil { // iterate over the validators public keys assigned for all consumer chains @@ -71,8 +71,13 @@ func (k Keeper) GetAllValidatorConsumerPubKeys(ctx sdk.Context, chainID *string) // iterate over the validators public keys assigned for chainID prefix = types.ChainIdWithLenKey(types.ConsumerValidatorsBytePrefix, *chainID) } - iterator := storetypes.KVStorePrefixIterator(store, prefix) + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting public keys of a all validators of chain %s: %v", chainID, err) + return + } defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { // TODO: store chainID and provider cons address in value bytes, marshaled as protobuf type chainID, providerAddrTmp, err := types.ParseChainIdAndConsAddrKey(types.ConsumerValidatorsBytePrefix, iterator.Key()) @@ -102,7 +107,7 @@ func (k Keeper) GetAllValidatorConsumerPubKeys(ctx sdk.Context, chainID *string) // DeleteValidatorConsumerPubKey deletes a validator's public key assigned for a consumer chain func (k Keeper) DeleteValidatorConsumerPubKey(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ConsumerValidatorsKey(chainID, providerAddr)) } @@ -113,9 +118,9 @@ func (k Keeper) GetValidatorByConsumerAddr( chainID string, consumerAddr types.ConsumerConsAddress, ) (providerAddr types.ProviderConsAddress, found bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ValidatorsByConsumerAddrKey(chainID, consumerAddr)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ValidatorsByConsumerAddrKey(chainID, consumerAddr)) + if err != nil || bz == nil { return providerAddr, false } providerAddr = types.NewProviderConsAddress(bz) @@ -130,7 +135,7 @@ func (k Keeper) SetValidatorByConsumerAddr( consumerAddr types.ConsumerConsAddress, providerAddr types.ProviderConsAddress, ) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) // Cons address is a type alias for a byte string, no marshaling needed bz := providerAddr.ToSdkConsAddr() store.Set(types.ValidatorsByConsumerAddrKey(chainID, consumerAddr), bz) @@ -146,7 +151,7 @@ func (k Keeper) SetValidatorByConsumerAddr( // - in ascending order of consumerAddresses, if chainID is not nil; // - in undetermined order, if chainID is nil. func (k Keeper) GetAllValidatorsByConsumerAddr(ctx sdk.Context, chainID *string) (validatorConsumerAddrs []types.ValidatorByConsumerAddr) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) var prefix []byte if chainID == nil { // iterate over the mappings from consensus addresses on all consumer chains @@ -155,8 +160,14 @@ func (k Keeper) GetAllValidatorsByConsumerAddr(ctx sdk.Context, chainID *string) // iterate over the mappings from consensus addresses on chainID prefix = types.ChainIdWithLenKey(types.ValidatorsByConsumerAddrBytePrefix, *chainID) } - iterator := storetypes.KVStorePrefixIterator(store, prefix) + + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all validators by consumer address: %v", err) + return []types.ValidatorByConsumerAddr{} + } defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { // TODO: store chainID and consumer cons address in value bytes, marshaled as protobuf type chainID, consumerAddrTmp, err := types.ParseChainIdAndConsAddrKey(types.ValidatorsByConsumerAddrBytePrefix, iterator.Key()) @@ -181,7 +192,7 @@ func (k Keeper) GetAllValidatorsByConsumerAddr(ctx sdk.Context, chainID *string) // DeleteValidatorByConsumerAddr deletes the mapping from a validator's consensus address on a consumer // to the validator's consensus address on the provider func (k Keeper) DeleteValidatorByConsumerAddr(ctx sdk.Context, chainID string, consumerAddr types.ConsumerConsAddress) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ValidatorsByConsumerAddrKey(chainID, consumerAddr)) } @@ -194,13 +205,13 @@ func (k Keeper) GetKeyAssignmentReplacement( providerAddr types.ProviderConsAddress, ) (prevCKey tmprotocrypto.PublicKey, power int64, found bool) { var pubKeyAndPower abci.ValidatorUpdate - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.KeyAssignmentReplacementsKey(chainID, providerAddr)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.KeyAssignmentReplacementsKey(chainID, providerAddr)) + if err != nil || bz == nil { return pubKeyAndPower.PubKey, pubKeyAndPower.Power, false } - err := pubKeyAndPower.Unmarshal(bz) + err = pubKeyAndPower.Unmarshal(bz) if err != nil { // An error here would indicate something is very wrong, // the public key and power are assumed to be correctly serialized in SetKeyAssignmentReplacement. @@ -219,7 +230,7 @@ func (k Keeper) SetKeyAssignmentReplacement( prevCKey tmprotocrypto.PublicKey, power int64, ) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) pubKeyAndPower := abci.ValidatorUpdate{PubKey: prevCKey, Power: power} bz, err := pubKeyAndPower.Marshal() if err != nil { @@ -239,10 +250,15 @@ func (k Keeper) SetKeyAssignmentReplacement( // KeyAssignmentReplacementsBytePrefix | len(chainID) | chainID | providerAddress // Thus, the iteration is in ascending order of providerAddresses. func (k Keeper) GetAllKeyAssignmentReplacements(ctx sdk.Context, chainID string) (replacements []types.KeyAssignmentReplacement) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) iteratorPrefix := types.ChainIdWithLenKey(types.KeyAssignmentReplacementsBytePrefix, chainID) - iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix) + iterator, err := store.Iterator(iteratorPrefix, storetypes.PrefixEndBytes(iteratorPrefix)) + if err != nil { + k.Logger(ctx).Error("error getting all paris of previous assigned consumer keys: %v", err) + return []types.KeyAssignmentReplacement{} + } defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { // TODO: store chainID and provider cons address in value bytes, marshaled as protobuf type _, providerAddrTmp, err := types.ParseChainIdAndConsAddrKey(types.KeyAssignmentReplacementsBytePrefix, iterator.Key()) @@ -274,7 +290,7 @@ func (k Keeper) GetAllKeyAssignmentReplacements(ctx sdk.Context, chainID string) // for a provider validator for which a key assignment was received in this block. Both are // needed to update the validator's power on the consumer chain at the end of the current block. func (k Keeper) DeleteKeyAssignmentReplacement(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.KeyAssignmentReplacementsKey(chainID, providerAddr)) } @@ -287,10 +303,10 @@ func (k Keeper) DeleteKeyAssignmentReplacement(ctx sdk.Context, chainID string, // s.t. hash(ValidatorConsumerPubKey(pAddr)) = cAddr // - or there exists a vscID in ConsumerAddrsToPrune s.t. cAddr in ConsumerAddrsToPrune(vscID) func (k Keeper) AppendConsumerAddrsToPrune(ctx sdk.Context, chainID string, vscID uint64, consumerAddr types.ConsumerConsAddress) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ConsumerAddrsToPruneKey(chainID, vscID)) + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ConsumerAddrsToPruneKey(chainID, vscID)) var consumerAddrsToPrune types.AddressList - if bz != nil { + if err == nil && bz != nil { err := consumerAddrsToPrune.Unmarshal(bz) if err != nil { // An error here would indicate something is very wrong, @@ -299,7 +315,7 @@ func (k Keeper) AppendConsumerAddrsToPrune(ctx sdk.Context, chainID string, vscI } } consumerAddrsToPrune.Addresses = append(consumerAddrsToPrune.Addresses, consumerAddr.ToSdkConsAddr()) - bz, err := consumerAddrsToPrune.Marshal() + bz, err = consumerAddrsToPrune.Marshal() if err != nil { // An error here would indicate something is very wrong, // consumerAddrsToPrune is instantiated in this method and should be able to be marshaled. @@ -315,12 +331,12 @@ func (k Keeper) GetConsumerAddrsToPrune( chainID string, vscID uint64, ) (consumerAddrsToPrune types.AddressList) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.ConsumerAddrsToPruneKey(chainID, vscID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ConsumerAddrsToPruneKey(chainID, vscID)) + if err != nil || bz == nil { return } - err := consumerAddrsToPrune.Unmarshal(bz) + err = consumerAddrsToPrune.Unmarshal(bz) if err != nil { // An error here would indicate something is very wrong, // the list of consumer addresses is assumed to be correctly serialized in AppendConsumerAddrsToPrune. @@ -335,10 +351,15 @@ func (k Keeper) GetConsumerAddrsToPrune( // ConsumerAddrsToPruneBytePrefix | len(chainID) | chainID | vscID // Thus, the returned array is in ascending order of vscIDs. func (k Keeper) GetAllConsumerAddrsToPrune(ctx sdk.Context, chainID string) (consumerAddrsToPrune []types.ConsumerAddrsToPrune) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) iteratorPrefix := types.ChainIdWithLenKey(types.ConsumerAddrsToPruneBytePrefix, chainID) - iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix) + iterator, err := store.Iterator(iteratorPrefix, storetypes.PrefixEndBytes(iteratorPrefix)) + if err != nil { + k.Logger(ctx).Error("error getting all consumer addresses to be pruned: %v", err) + return + } defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { _, vscID, err := types.ParseChainIdAndUintIdKey(types.ConsumerAddrsToPruneBytePrefix, iterator.Key()) if err != nil { @@ -366,7 +387,7 @@ func (k Keeper) GetAllConsumerAddrsToPrune(ctx sdk.Context, chainID string) (con // DeleteConsumerAddrsToPrune deletes the list of consumer addresses mapped to a given VSC ID func (k Keeper) DeleteConsumerAddrsToPrune(ctx sdk.Context, chainID string, vscID uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) store.Delete(types.ConsumerAddrsToPruneKey(chainID, vscID)) } diff --git a/x/ccv/provider/keeper/params.go b/x/ccv/provider/keeper/params.go index a0bbc27679..c8514131dd 100644 --- a/x/ccv/provider/keeper/params.go +++ b/x/ccv/provider/keeper/params.go @@ -8,100 +8,126 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/interchain-security/v3/x/ccv/provider/types" - ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types" ) // GetTemplateClient returns the template client for provider proposals func (k Keeper) GetTemplateClient(ctx sdk.Context) *ibctmtypes.ClientState { - var cs ibctmtypes.ClientState - k.paramSpace.Get(ctx, types.KeyTemplateClient, &cs) - return &cs + params, err := k.GetParams(ctx) + if err != nil { + return nil // @bermuell TODO : error handling... panic or default? + } + return params.TemplateClient } // GetTrustingPeriodFraction returns a TrustingPeriodFraction // used to compute the provider IBC client's TrustingPeriod as UnbondingPeriod / TrustingPeriodFraction func (k Keeper) GetTrustingPeriodFraction(ctx sdk.Context) string { - var f string - k.paramSpace.Get(ctx, types.KeyTrustingPeriodFraction, &f) - return f + params, err := k.GetParams(ctx) + if err != nil { + return "" // @bermuell TODO : error handling... panic or default? + } + return params.TrustingPeriodFraction + } // GetCCVTimeoutPeriod returns the timeout period for sent ibc packets func (k Keeper) GetCCVTimeoutPeriod(ctx sdk.Context) time.Duration { - var p time.Duration - k.paramSpace.Get(ctx, ccvtypes.KeyCCVTimeoutPeriod, &p) - return p + params, err := k.GetParams(ctx) + if err != nil { + return types.Params{}.CcvTimeoutPeriod // @bermuell TODO : error handling... panic or default? + } + return params.CcvTimeoutPeriod } // GetInitTimeoutPeriod returns the init timeout period func (k Keeper) GetInitTimeoutPeriod(ctx sdk.Context) time.Duration { - var p time.Duration - k.paramSpace.Get(ctx, types.KeyInitTimeoutPeriod, &p) - return p + params, err := k.GetParams(ctx) + if err != nil { + return types.Params{}.InitTimeoutPeriod // @bermuell TODO : error handling... panic or default? + } + return params.InitTimeoutPeriod } // GetVscTimeoutPeriod returns the vsc timeout period func (k Keeper) GetVscTimeoutPeriod(ctx sdk.Context) time.Duration { - var p time.Duration - k.paramSpace.Get(ctx, types.KeyVscTimeoutPeriod, &p) - return p + params, err := k.GetParams(ctx) + if err != nil { + return types.Params{}.VscTimeoutPeriod // @bermuell TODO : error handling... panic or default? + } + return params.VscTimeoutPeriod } // SetVscTimeoutPeriod sets the vsc timeout period func (k Keeper) SetVscTimeoutPeriod(ctx sdk.Context, period time.Duration) { - k.paramSpace.Set(ctx, types.KeyVscTimeoutPeriod, period) + params, err := k.GetParams(ctx) + if err != nil { + // @bermuell TODO : error handling + return + } + params.VscTimeoutPeriod = period + k.SetParams(ctx, params) } // GetSlashMeterReplenishPeriod returns the period in which: // Once the slash meter becomes not-full, the slash meter is replenished after this period. func (k Keeper) GetSlashMeterReplenishPeriod(ctx sdk.Context) time.Duration { - var p time.Duration - k.paramSpace.Get(ctx, types.KeySlashMeterReplenishPeriod, &p) - return p + params, err := k.GetParams(ctx) + if err != nil { + return types.Params{}.SlashMeterReplenishPeriod // @bermuell TODO : error handling... panic or default? + } + return params.SlashMeterReplenishPeriod } // GetSlashMeterReplenishFraction returns the string fraction of total voting power that is replenished // to the slash meter every replenish period. This param also serves as a maximum fraction of total // voting power that the slash meter can hold. func (k Keeper) GetSlashMeterReplenishFraction(ctx sdk.Context) string { - var f string - k.paramSpace.Get(ctx, types.KeySlashMeterReplenishFraction, &f) - return f + params, err := k.GetParams(ctx) + if err != nil { + return types.Params{}.SlashMeterReplenishFraction // @bermuell TODO : error handling... panic or default? + } + return params.SlashMeterReplenishFraction } // GetMaxThrottledPackets returns the maximum amount of throttled slash or vsc matured packets // that can be queued for a single consumer before the provider chain halts. func (k Keeper) GetMaxThrottledPackets(ctx sdk.Context) int64 { - var p int64 - k.paramSpace.Get(ctx, types.KeyMaxThrottledPackets, &p) - return p + params, err := k.GetParams(ctx) + if err != nil { + return types.Params{}.MaxThrottledPackets // @bermuell TODO : error handling... panic or default? + } + return params.MaxThrottledPackets } func (k Keeper) GetConsumerRewardDenomRegistrationFee(ctx sdk.Context) sdk.Coin { // Due to difficulties doing migrations in coordinated upgrades, this param is hardcoded to 10 ATOM in v1.1.0-multiden. // The below code is the proper way to store the param. A future scheduled upgrade will // need to run migrations to add the param. This will allow us to change the fee by governance. - var c sdk.Coin - k.paramSpace.Get(ctx, types.KeyConsumerRewardDenomRegistrationFee, &c) - return c + params, err := k.GetParams(ctx) + if err != nil { + return types.Params{}.ConsumerRewardDenomRegistrationFee // @bermuell TODO : error handling... panic or default? + } + return params.ConsumerRewardDenomRegistrationFee } -// GetParams returns the paramset for the provider module -func (k Keeper) GetParams(ctx sdk.Context) types.Params { - return types.NewParams( - k.GetTemplateClient(ctx), - k.GetTrustingPeriodFraction(ctx), - k.GetCCVTimeoutPeriod(ctx), - k.GetInitTimeoutPeriod(ctx), - k.GetVscTimeoutPeriod(ctx), - k.GetSlashMeterReplenishPeriod(ctx), - k.GetSlashMeterReplenishFraction(ctx), - k.GetMaxThrottledPackets(ctx), - k.GetConsumerRewardDenomRegistrationFee(ctx), - ) +// GetParams returns the parameters for the provider module +func (k Keeper) GetParams(ctx sdk.Context) (types.Params, error) { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.ParametersKey()) + if err != nil { + return types.Params{}, nil //TODO @bermuell: check if default arguments or error handling should be done + } + var params types.Params + err = k.cdc.Unmarshal(bz, ¶ms) + return params, err } // SetParams sets the params for the provider module -func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { - k.paramSpace.SetParamSet(ctx, ¶ms) +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { + store := k.storeService.OpenKVStore(ctx) + bz, err := k.cdc.Marshal(¶ms) + if err != nil { + return err + } + return store.Set(types.ParametersKey(), bz) } diff --git a/x/ccv/provider/keeper/params_test.go b/x/ccv/provider/keeper/params_test.go index ec938fffe7..c1ebf83124 100644 --- a/x/ccv/provider/keeper/params_test.go +++ b/x/ccv/provider/keeper/params_test.go @@ -25,7 +25,8 @@ func TestParams(t *testing.T) { defaultParams := providertypes.DefaultParams() providerKeeper.SetParams(ctx, defaultParams) - params := providerKeeper.GetParams(ctx) + params, err := providerKeeper.GetParams(ctx) + require.NoError(t, err) require.Equal(t, defaultParams, params) newParams := providertypes.NewParams( @@ -52,6 +53,7 @@ func TestParams(t *testing.T) { }, ) providerKeeper.SetParams(ctx, newParams) - params = providerKeeper.GetParams(ctx) + params, err = providerKeeper.GetParams(ctx) + require.NoError(t, err) require.Equal(t, newParams, params) } diff --git a/x/ccv/provider/keeper/proposal.go b/x/ccv/provider/keeper/proposal.go index 8f90e14448..9d9e26e0c6 100644 --- a/x/ccv/provider/keeper/proposal.go +++ b/x/ccv/provider/keeper/proposal.go @@ -143,7 +143,11 @@ func (k Keeper) CreateConsumerClient(ctx sdk.Context, prop *types.ConsumerAdditi k.SetConsumerClientId(ctx, chainID, clientID) // add the init timeout timestamp for this consumer chain - ts := ctx.BlockTime().Add(k.GetParams(ctx).InitTimeoutPeriod) + params, err := k.GetParams(ctx) + if err != nil { + return err + } + ts := ctx.BlockTime().Add(params.InitTimeoutPeriod) k.SetInitTimeoutTimestamp(ctx, chainID, uint64(ts.UnixNano())) k.Logger(ctx).Info("consumer chain registered (client created)", @@ -382,7 +386,7 @@ func (k Keeper) MakeConsumerGenesis( // Thus, if multiple consumer addition proposal for the same chain will pass at // the same time, then only the last one will be stored. func (k Keeper) SetPendingConsumerAdditionProp(ctx sdk.Context, prop *types.ConsumerAdditionProposal) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := prop.Marshal() if err != nil { // An error here would indicate something is very wrong @@ -398,12 +402,12 @@ func (k Keeper) SetPendingConsumerAdditionProp(ctx sdk.Context, prop *types.Cons func (k Keeper) GetPendingConsumerAdditionProp(ctx sdk.Context, spawnTime time.Time, chainID string, ) (prop types.ConsumerAdditionProposal, found bool) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.PendingCAPKey(spawnTime, chainID)) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.PendingCAPKey(spawnTime, chainID)) + if err != nil || bz == nil { return prop, false } - err := prop.Unmarshal(bz) + err = prop.Unmarshal(bz) if err != nil { // An error here would indicate something is very wrong, // the ConsumerAdditionProp is assumed to be correctly serialized in SetPendingConsumerAdditionProp. @@ -451,8 +455,13 @@ func (k Keeper) BeginBlockInit(ctx sdk.Context) { // // Note: this method is split out from BeginBlockInit to be easily unit tested. func (k Keeper) GetConsumerAdditionPropsToExecute(ctx sdk.Context) (propsToExecute []types.ConsumerAdditionProposal) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingCAPBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.PendingCAPBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting the pending consumer proposals: %v", err) + return []types.ConsumerAdditionProposal{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -481,8 +490,13 @@ func (k Keeper) GetConsumerAdditionPropsToExecute(ctx sdk.Context) (propsToExecu // Thus, the returned array is in spawnTime order. If two proposals have the same spawnTime, // then they are ordered by chainID. func (k Keeper) GetAllPendingConsumerAdditionProps(ctx sdk.Context) (props []types.ConsumerAdditionProposal) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingCAPBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.PendingCAPBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting pending consumer addition proposals: %v", err) + return []types.ConsumerAdditionProposal{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -502,7 +516,7 @@ func (k Keeper) GetAllPendingConsumerAdditionProps(ctx sdk.Context) (props []typ // DeletePendingConsumerAdditionProps deletes the given consumer addition proposals func (k Keeper) DeletePendingConsumerAdditionProps(ctx sdk.Context, proposals ...types.ConsumerAdditionProposal) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) for _, p := range proposals { store.Delete(types.PendingCAPKey(p.SpawnTime, p.ChainId)) @@ -516,7 +530,7 @@ func (k Keeper) DeletePendingConsumerAdditionProps(ctx sdk.Context, proposals .. // Thus, if multiple removal addition proposal for the same chain will pass at // the same time, then only the last one will be stored. func (k Keeper) SetPendingConsumerRemovalProp(ctx sdk.Context, prop *types.ConsumerRemovalProposal) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := prop.Marshal() if err != nil { // An error here would indicate something is very wrong @@ -530,16 +544,16 @@ func (k Keeper) SetPendingConsumerRemovalProp(ctx sdk.Context, prop *types.Consu // // Note: this method is only used in testing func (k Keeper) PendingConsumerRemovalPropExists(ctx sdk.Context, chainID string, timestamp time.Time) bool { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.PendingCRPKey(timestamp, chainID)) + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(types.PendingCRPKey(timestamp, chainID)) - return bz != nil + return err == nil && bz != nil } // DeletePendingConsumerRemovalProps deletes the given pending consumer removal proposals. // This method should be called once the proposal has been acted upon. func (k Keeper) DeletePendingConsumerRemovalProps(ctx sdk.Context, proposals ...types.ConsumerRemovalProposal) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) for _, p := range proposals { store.Delete(types.PendingCRPKey(p.StopTime, p.ChainId)) @@ -589,8 +603,13 @@ func (k Keeper) GetConsumerRemovalPropsToExecute(ctx sdk.Context) []types.Consum // store the (to be) executed consumer removal proposals in order propsToExecute := []types.ConsumerRemovalProposal{} - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingCRPBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.PendingCRPBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting pending consumer removal proposals to be executed: %v", err) + return []types.ConsumerRemovalProposal{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -620,8 +639,13 @@ func (k Keeper) GetConsumerRemovalPropsToExecute(ctx sdk.Context) []types.Consum // PendingCRPBytePrefix | stopTime.UnixNano() | chainID // Thus, the returned array is in stopTime order. func (k Keeper) GetAllPendingConsumerRemovalProps(ctx sdk.Context) (props []types.ConsumerRemovalProposal) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingCRPBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{types.PendingCRPBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting pending cosumer removal proposals: %v", err) + return []types.ConsumerRemovalProposal{} + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { diff --git a/x/ccv/provider/keeper/relay.go b/x/ccv/provider/keeper/relay.go index 0d3ffa342f..75be344df9 100644 --- a/x/ccv/provider/keeper/relay.go +++ b/x/ccv/provider/keeper/relay.go @@ -514,6 +514,10 @@ func (k Keeper) EndBlockCCR(ctx sdk.Context) { } } + params, err := k.GetParams(ctx) + if err != nil { + panic(fmt.Errorf("invalid provider parameters: %v", err)) + } for _, channelToChain := range k.GetAllChannelToChains(ctx) { // Check if the first vscSendTimestamp in iterator + VscTimeoutPeriod // exceed the current block time. @@ -522,7 +526,7 @@ func (k Keeper) EndBlockCCR(ctx sdk.Context) { // Note: GetFirstVscSendTimestamp panics if the internal state is invalid vscSendTimestamp, found := k.GetFirstVscSendTimestamp(ctx, channelToChain.ChainId) if found { - timeoutTimestamp := vscSendTimestamp.Timestamp.Add(k.GetParams(ctx).VscTimeoutPeriod) + timeoutTimestamp := vscSendTimestamp.Timestamp.Add(params.VscTimeoutPeriod) if currentTime.After(timeoutTimestamp) { // vscTimeout expired // stop the consumer chain and release unbondings diff --git a/x/ccv/provider/keeper/throttle.go b/x/ccv/provider/keeper/throttle.go index 64a1e47e4e..cca808b26c 100644 --- a/x/ccv/provider/keeper/throttle.go +++ b/x/ccv/provider/keeper/throttle.go @@ -229,7 +229,7 @@ func (k Keeper) GetSlashMeterAllowance(ctx sdktypes.Context) math.Int { // between chains, whereas the chain-specific queue is used to coordinate the order of slash and vsc matured packets // relevant to each chain. func (k Keeper) QueueGlobalSlashEntry(ctx sdktypes.Context, entry providertypes.GlobalSlashEntry) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) key := providertypes.GlobalSlashEntryKey(entry) bz := entry.ProviderValConsAddr store.Set(key, bz) @@ -255,8 +255,13 @@ func (k Keeper) DeleteGlobalSlashEntriesForConsumer(ctx sdktypes.Context, consum // GlobalSlashEntryBytePrefix | uint64 recv time | ibc seq num | consumer chain id // Thus, the returned array is ordered by recv time, then ibc seq num. func (k Keeper) GetAllGlobalSlashEntries(ctx sdktypes.Context) []providertypes.GlobalSlashEntry { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, []byte{providertypes.GlobalSlashEntryBytePrefix}) + store := k.storeService.OpenKVStore(ctx) + prefix := []byte{providertypes.GlobalSlashEntryBytePrefix} + iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + if err != nil { + k.Logger(ctx).Error("error getting all global slash entries: %v", err) + return []providertypes.GlobalSlashEntry{} + } defer iterator.Close() entries := []providertypes.GlobalSlashEntry{} @@ -274,7 +279,7 @@ func (k Keeper) GetAllGlobalSlashEntries(ctx sdktypes.Context) []providertypes.G // DeleteGlobalSlashEntries deletes the given global entries from the global slash queue func (k Keeper) DeleteGlobalSlashEntries(ctx sdktypes.Context, entries ...providertypes.GlobalSlashEntry) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) for _, entry := range entries { store.Delete(providertypes.GlobalSlashEntryKey(entry)) } @@ -288,11 +293,11 @@ const ( // GetThrottledPacketDataSize returns the size of the throttled packet data queue for the given consumer chain func (k Keeper) GetThrottledPacketDataSize(ctx sdktypes.Context, consumerChainID string) uint64 { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) key := providertypes.ThrottledPacketDataSizeKey(consumerChainID) var size uint64 - bz := store.Get(key) - if bz == nil { + bz, err := store.Get(key) + if err != nil || bz == nil { size = 0 } else { size = sdktypes.BigEndianToUint64(bz) @@ -309,7 +314,7 @@ func (k Keeper) SetThrottledPacketDataSize(ctx sdktypes.Context, consumerChainID panic(fmt.Sprintf("throttled packet data queue for chain %s is too large: %d", consumerChainID, size)) } - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) key := providertypes.ThrottledPacketDataSizeKey(consumerChainID) bz := sdktypes.Uint64ToBigEndian(size) store.Set(key, bz) @@ -353,7 +358,7 @@ func (k Keeper) QueueThrottledVSCMaturedPacketData( func (k Keeper) QueueThrottledPacketData( ctx sdktypes.Context, consumerChainID string, ibcSeqNum uint64, packetData interface{}, ) error { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) var bz []byte var err error @@ -387,9 +392,13 @@ func (k Keeper) QueueThrottledPacketData( func (k Keeper) GetLeadingVSCMaturedData(ctx sdktypes.Context, consumerChainID string) ( vscMaturedData []ccvtypes.VSCMaturedPacketData, ibcSeqNums []uint64, ) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID) - iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix) + iterator, err := store.Iterator(iteratorPrefix, storetypes.PrefixEndBytes(iteratorPrefix)) + if err != nil { + k.Logger(ctx).Error("error getting leading VSC matured packets: %v", err) + return []ccvtypes.VSCMaturedPacketData{}, []uint64{} + } defer iterator.Close() // Iterate over the throttled packet data queue, @@ -437,9 +446,13 @@ func (k Keeper) GetSlashAndTrailingData(ctx sdktypes.Context, consumerChainID st // data after it has been handled. ibcSeqNums []uint64, ) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID) - iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix) + iterator, err := store.Iterator(iteratorPrefix, storetypes.PrefixEndBytes(iteratorPrefix)) + if err != nil { + k.Logger(ctx).Error("error getting first slash packet data: %v", err) + return false, slashData, []ccvtypes.VSCMaturedPacketData{}, []uint64{} + } defer iterator.Close() slashFound = false @@ -496,9 +509,13 @@ func (k Keeper) GetAllThrottledPacketData(ctx sdktypes.Context, consumerChainID rawOrderedData = []interface{}{} ibcSeqNums = []uint64{} - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID) - iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix) + iterator, err := store.Iterator(iteratorPrefix, storetypes.PrefixEndBytes(iteratorPrefix)) + if err != nil { + k.Logger(ctx).Error("error getting all throttled packets: %v", err) + return + } defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -537,9 +554,13 @@ func (k Keeper) GetAllThrottledPacketData(ctx sdktypes.Context, consumerChainID // DeleteAllPacketDataForConsumer deletes all queued packet data for the given consumer chain. func (k Keeper) DeleteThrottledPacketDataForConsumer(ctx sdktypes.Context, consumerChainID string) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID) - iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix) + iterator, err := store.Iterator(iteratorPrefix, storetypes.PrefixEndBytes(iteratorPrefix)) + if err != nil { + k.Logger(ctx).Error("error deleting all queued packets for consumer chain %s: %v", consumerChainID, err) + return + } defer iterator.Close() keysToDel := [][]byte{} @@ -558,7 +579,7 @@ func (k Keeper) DeleteThrottledPacketDataForConsumer(ctx sdktypes.Context, consu // DeleteThrottledPacketData deletes the given throttled packet data instances // (specified by their ibc seq number) from the chain-specific throttled packet data queue. func (k Keeper) DeleteThrottledPacketData(ctx sdktypes.Context, consumerChainID string, ibcSeqNumbers ...uint64) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) for _, ibcSeqNum := range ibcSeqNumbers { store.Delete(providertypes.ThrottledPacketDataKey(consumerChainID, ibcSeqNum)) } @@ -572,15 +593,15 @@ func (k Keeper) DeleteThrottledPacketData(ctx sdktypes.Context, consumerChainID // // Note: the value of this int should always be in the range of tendermint's [-MaxVotingPower, MaxVotingPower] func (k Keeper) GetSlashMeter(ctx sdktypes.Context) math.Int { - store := ctx.KVStore(k.storeKey) - bz := store.Get(providertypes.SlashMeterKey()) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(providertypes.SlashMeterKey()) + if err != nil || bz == nil { // Slash meter should be set as a part of InitGenesis and periodically updated by throttle logic, // there is no deletion method exposed, so nil bytes would indicate something is very wrong. panic("slash meter not set") } value := math.ZeroInt() - err := value.Unmarshal(bz) + err = value.Unmarshal(bz) if err != nil { // We should have obtained value bytes that were serialized in SetSlashMeter, // so an error here would indicate something is very wrong. @@ -607,7 +628,7 @@ func (k Keeper) SetSlashMeter(ctx sdktypes.Context, value math.Int) { if value.LT(math.NewInt(-tmtypes.MaxTotalVotingPower)) { panic("slash meter value cannot be less than negative tendermint's MaxTotalVotingPower") } - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) bz, err := value.Marshal() if err != nil { // A returned error for marshaling an int would indicate something is very wrong. @@ -621,9 +642,9 @@ func (k Keeper) SetSlashMeter(ctx sdktypes.Context, value math.Int) { // Note: this value is the next time the slash meter will be replenished IFF the slash meter is NOT full. // Otherwise this value will be updated in every future block until the slash meter becomes NOT full. func (k Keeper) GetSlashMeterReplenishTimeCandidate(ctx sdktypes.Context) time.Time { - store := ctx.KVStore(k.storeKey) - bz := store.Get(providertypes.SlashMeterReplenishTimeCandidateKey()) - if bz == nil { + store := k.storeService.OpenKVStore(ctx) + bz, err := store.Get(providertypes.SlashMeterReplenishTimeCandidateKey()) + if err != nil || bz == nil { // Slash meter replenish time candidate should be set as a part of InitGenesis and periodically updated by throttle logic, // there is no deletion method exposed, so nil bytes would indicate something is very wrong. panic("slash meter replenish time candidate not set") @@ -643,7 +664,7 @@ func (k Keeper) GetSlashMeterReplenishTimeCandidate(ctx sdktypes.Context) time.T // Note: this value is the next time the slash meter will be replenished IFF the slash meter is NOT full. // Otherwise this value will be updated in every future block until the slash meter becomes NOT full. func (k Keeper) SetSlashMeterReplenishTimeCandidate(ctx sdktypes.Context) { - store := ctx.KVStore(k.storeKey) + store := k.storeService.OpenKVStore(ctx) timeToStore := ctx.BlockTime().UTC().Add(k.GetSlashMeterReplenishPeriod(ctx)) store.Set(providertypes.SlashMeterReplenishTimeCandidateKey(), sdktypes.FormatTimeBytes(timeToStore)) } diff --git a/x/ccv/provider/module_test.go b/x/ccv/provider/module_test.go index ba3ec30b67..f446c35203 100644 --- a/x/ccv/provider/module_test.go +++ b/x/ccv/provider/module_test.go @@ -177,7 +177,9 @@ func TestInitGenesis(t *testing.T) { // Expect slash meter to be initialized to it's allowance value // (replenish fraction * mocked value defined above) slashMeter := providerKeeper.GetSlashMeter(ctx) - replenishFraction, err := math.LegacyNewDecFromStr(providerKeeper.GetParams(ctx).SlashMeterReplenishFraction) + params, err := providerKeeper.GetParams(ctx) + require.NoError(t, err) + replenishFraction, err := math.LegacyNewDecFromStr(params.SlashMeterReplenishFraction) require.NoError(t, err) expectedSlashMeterValue := math.NewInt(replenishFraction.MulInt(math.NewInt(100)).RoundInt64()) require.Equal(t, expectedSlashMeterValue, slashMeter) diff --git a/x/ccv/provider/types/params.go b/x/ccv/provider/types/params.go index 9a4cbdb149..3f9ed11fa2 100644 --- a/x/ccv/provider/types/params.go +++ b/x/ccv/provider/types/params.go @@ -55,11 +55,6 @@ var ( KeyConsumerRewardDenomRegistrationFee = []byte("ConsumerRewardDenomRegistrationFee") ) -// ParamKeyTable returns a key table with the necessary registered provider params -func ParamKeyTable() paramtypes.KeyTable { - return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) -} - // NewParams creates new provider parameters with provided arguments func NewParams( cs *ibctmtypes.ClientState, diff --git a/x/ccv/types/params.go b/x/ccv/types/params.go index d44ec68c80..7091e02b25 100644 --- a/x/ccv/types/params.go +++ b/x/ccv/types/params.go @@ -6,7 +6,6 @@ import ( "cosmossdk.io/math" sdktypes "github.com/cosmos/cosmos-sdk/types" - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -57,11 +56,6 @@ var ( KeyProviderRewardDenoms = []byte("ProviderRewardDenoms") ) -// ParamKeyTable type declaration for parameters -func ParamKeyTable() paramtypes.KeyTable { - return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) -} - // NewParams creates new consumer parameters with provided arguments func NewParams(enabled bool, blocksPerDistributionTransmission int64, distributionTransmissionChannel, providerFeePoolAddrStr string, @@ -146,35 +140,6 @@ func (p Params) Validate() error { return nil } -// ParamSetPairs implements params.ParamSet -func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { - return paramtypes.ParamSetPairs{ - paramtypes.NewParamSetPair(KeyEnabled, p.Enabled, ValidateBool), - paramtypes.NewParamSetPair(KeyBlocksPerDistributionTransmission, - p.BlocksPerDistributionTransmission, ValidatePositiveInt64), - paramtypes.NewParamSetPair(KeyDistributionTransmissionChannel, - p.DistributionTransmissionChannel, ValidateDistributionTransmissionChannel), - paramtypes.NewParamSetPair(KeyProviderFeePoolAddrStr, - p.ProviderFeePoolAddrStr, ValidateProviderFeePoolAddrStr), - paramtypes.NewParamSetPair(KeyCCVTimeoutPeriod, - p.CcvTimeoutPeriod, ValidateDuration), - paramtypes.NewParamSetPair(KeyTransferTimeoutPeriod, - p.TransferTimeoutPeriod, ValidateDuration), - paramtypes.NewParamSetPair(KeyConsumerRedistributionFrac, - p.ConsumerRedistributionFraction, ValidateStringFraction), - paramtypes.NewParamSetPair(KeyHistoricalEntries, - p.HistoricalEntries, ValidatePositiveInt64), - paramtypes.NewParamSetPair(KeyConsumerUnbondingPeriod, - p.UnbondingPeriod, ValidateDuration), - paramtypes.NewParamSetPair(KeySoftOptOutThreshold, - p.SoftOptOutThreshold, ValidateSoftOptOutThreshold), - paramtypes.NewParamSetPair(KeyRewardDenoms, - p.RewardDenoms, ValidateDenoms), - paramtypes.NewParamSetPair(KeyProviderRewardDenoms, - p.ProviderRewardDenoms, ValidateDenoms), - } -} - func ValidateProviderFeePoolAddrStr(i interface{}) error { // Accept empty string as valid, since this will be the default value on genesis if i == "" {