diff --git a/testutil/keeper/unit_test_helpers.go b/testutil/keeper/unit_test_helpers.go index a4dd366139..4bb221ce28 100644 --- a/testutil/keeper/unit_test_helpers.go +++ b/testutil/keeper/unit_test_helpers.go @@ -2,17 +2,18 @@ package keeper import ( "testing" + time "time" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/store" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" - capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" consumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper" providerkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper" "github.com/cosmos/interchain-security/x/ccv/types" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/log" @@ -22,169 +23,173 @@ 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" + + clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" + commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types" + ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types" + providertypes "github.com/cosmos/interchain-security/x/ccv/provider/types" ) -// Constructs a provider keeper and context object for unit tests, backed by an in-memory db. -func GetProviderKeeperAndCtx(t testing.TB) (providerkeeper.Keeper, sdk.Context) { +// Parameters needed to instantiate an in-memory keeper +type InMemKeeperParams struct { + Cdc *codec.ProtoCodec + StoreKey *storetypes.KVStoreKey + ParamsSubspace *paramstypes.Subspace + Ctx sdk.Context +} - cdc, storeKey, paramsSubspace, ctx := SetupInMemKeeper(t) +// NewInMemKeeperParams instantiates in-memory keeper params with default values +func NewInMemKeeperParams(t testing.TB) InMemKeeperParams { + storeKey := sdk.NewKVStoreKey(types.StoreKey) + memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) + + db := tmdb.NewMemDB() + stateStore := store.NewCommitMultiStore(db) + stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db) + stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil) + require.NoError(t, stateStore.LoadLatestVersion()) + + registry := codectypes.NewInterfaceRegistry() + cdc := codec.NewProtoCodec(registry) - k := providerkeeper.NewKeeper( - cdc, + paramsSubspace := paramstypes.NewSubspace(cdc, + codec.NewLegacyAmino(), storeKey, - paramsSubspace, - &MockScopedKeeper{}, - &MockChannelKeeper{}, - &MockPortKeeper{}, - &MockConnectionKeeper{}, - &MockClientKeeper{}, - &MockStakingKeeper{}, - &MockSlashingKeeper{}, - &MockAccountKeeper{}, - "", + memStoreKey, + paramstypes.ModuleName, ) - return k, ctx -} + ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) -// Constructs a consumer keeper and context object for unit tests, backed by an in-memory db. -func GetConsumerKeeperAndCtx(t testing.TB) (consumerkeeper.Keeper, sdk.Context) { + return InMemKeeperParams{ + Cdc: cdc, + StoreKey: storeKey, + ParamsSubspace: ¶msSubspace, + Ctx: ctx, + } +} - cdc, storeKey, paramsSubspace, ctx := SetupInMemKeeper(t) +// A struct holding pointers to any mocked external keeper needed for provider/consumer keeper setup. +type MockedKeepers struct { + *MockScopedKeeper + *MockChannelKeeper + *MockPortKeeper + *MockConnectionKeeper + *MockClientKeeper + *MockStakingKeeper + *MockSlashingKeeper + *MockAccountKeeper + *MockBankKeeper + *MockIBCTransferKeeper + *MockIBCCoreKeeper +} - k := consumerkeeper.NewKeeper( - cdc, - storeKey, - paramsSubspace, - &MockScopedKeeper{}, - &MockChannelKeeper{}, - &MockPortKeeper{}, - &MockConnectionKeeper{}, - &MockClientKeeper{}, - &MockSlashingKeeper{}, - &MockBankKeeper{}, - &MockAccountKeeper{}, - &MockIBCTransferKeeper{}, - &MockIBCCoreKeeper{}, - "", - ) - return k, ctx +// NewMockedKeepers instantiates a struct with pointers to properly instantiated mocked keepers. +func NewMockedKeepers(ctrl *gomock.Controller) MockedKeepers { + return MockedKeepers{ + MockScopedKeeper: NewMockScopedKeeper(ctrl), + MockChannelKeeper: NewMockChannelKeeper(ctrl), + MockPortKeeper: NewMockPortKeeper(ctrl), + MockConnectionKeeper: NewMockConnectionKeeper(ctrl), + MockClientKeeper: NewMockClientKeeper(ctrl), + MockStakingKeeper: NewMockStakingKeeper(ctrl), + MockSlashingKeeper: NewMockSlashingKeeper(ctrl), + MockAccountKeeper: NewMockAccountKeeper(ctrl), + MockBankKeeper: NewMockBankKeeper(ctrl), + MockIBCTransferKeeper: NewMockIBCTransferKeeper(ctrl), + MockIBCCoreKeeper: NewMockIBCCoreKeeper(ctrl), + } } -// Constructs a provider keeper for unit tests, backed by an in-memory db, -// with ability to pass mocked or otherwise manipulated parameters. -func GetProviderKeeperWithMocks( - cdc *codec.ProtoCodec, - storeKey *storetypes.KVStoreKey, - paramsSubspace paramstypes.Subspace, - capabilityKeeper capabilitykeeper.ScopedKeeper, - channelKeeper types.ChannelKeeper, - portKeeper types.PortKeeper, - connectionKeeper types.ConnectionKeeper, - clientKeeper types.ClientKeeper, - stakingKeeper types.StakingKeeper, - slashingKeeper types.SlashingKeeper, - accountKeeper types.AccountKeeper, -) providerkeeper.Keeper { +// NewInMemProviderKeeper instantiates an in-mem provider keeper from params and mocked keepers +func NewInMemProviderKeeper(params InMemKeeperParams, mocks MockedKeepers) providerkeeper.Keeper { return providerkeeper.NewKeeper( - cdc, - storeKey, - paramsSubspace, - capabilityKeeper, - channelKeeper, - portKeeper, - connectionKeeper, - clientKeeper, - stakingKeeper, - slashingKeeper, - accountKeeper, + params.Cdc, + params.StoreKey, + *params.ParamsSubspace, + mocks.MockScopedKeeper, + mocks.MockChannelKeeper, + mocks.MockPortKeeper, + mocks.MockConnectionKeeper, + mocks.MockClientKeeper, + mocks.MockStakingKeeper, + mocks.MockSlashingKeeper, + mocks.MockAccountKeeper, "", ) } -// Constructs a consumer keeper for unit tests, backed by an in-memory db, -// with ability to pass mocked or otherwise manipulated parameters. -func GetCustomConsumerKeeperWithMocks( - cdc *codec.ProtoCodec, - storeKey *storetypes.KVStoreKey, - paramsSubspace paramstypes.Subspace, - capabilityKeeper types.ScopedKeeper, - channelKeeper types.ChannelKeeper, - portKeeper types.PortKeeper, - connectionKeeper types.ConnectionKeeper, - clientKeeper types.ClientKeeper, - slashingKeeper types.SlashingKeeper, - bankKeeper types.BankKeeper, - accountKeeper types.AccountKeeper, - ibcTransferKeeper types.IBCTransferKeeper, - ibcCoreKeeper types.IBCCoreKeeper, -) consumerkeeper.Keeper { +// NewInMemConsumerKeeper instantiates an in-mem consumer keeper from params and mocked keepers +func NewInMemConsumerKeeper(params InMemKeeperParams, mocks MockedKeepers) consumerkeeper.Keeper { return consumerkeeper.NewKeeper( - cdc, - storeKey, - paramsSubspace, - capabilityKeeper, - channelKeeper, - portKeeper, - connectionKeeper, - clientKeeper, - slashingKeeper, - bankKeeper, - accountKeeper, - ibcTransferKeeper, - ibcCoreKeeper, + params.Cdc, + params.StoreKey, + *params.ParamsSubspace, + mocks.MockScopedKeeper, + mocks.MockChannelKeeper, + mocks.MockPortKeeper, + mocks.MockConnectionKeeper, + mocks.MockClientKeeper, + mocks.MockSlashingKeeper, + mocks.MockBankKeeper, + mocks.MockAccountKeeper, + mocks.MockIBCTransferKeeper, + mocks.MockIBCCoreKeeper, "", ) } -// Constructs a consumer keeper for unit tests, backed by an in-memory db, -// with ability to pass manipulated parameters, but no mocked keepers. -func GetCustomConsumerKeeper( - cdc *codec.ProtoCodec, - storeKey *storetypes.KVStoreKey, - paramsSubspace paramstypes.Subspace, -) consumerkeeper.Keeper { +// The minimum boilerplate way to obtain an in-memory provider keeper, context, and controller. +// +// Note: Calling ctrl.Finish() at the end of a test function ensures that +// no unexpected calls to external keepers are made. +func GetProviderKeeperAndCtx(t *testing.T) (providerkeeper.Keeper, sdk.Context, *gomock.Controller) { + params := NewInMemKeeperParams(t) + ctrl := gomock.NewController(t) + mocks := NewMockedKeepers(ctrl) + return NewInMemProviderKeeper(params, mocks), params.Ctx, ctrl +} - return consumerkeeper.NewKeeper( - cdc, - storeKey, - paramsSubspace, - &MockScopedKeeper{}, - &MockChannelKeeper{}, - &MockPortKeeper{}, - &MockConnectionKeeper{}, - &MockClientKeeper{}, - &MockSlashingKeeper{}, - &MockBankKeeper{}, - &MockAccountKeeper{}, - &MockIBCTransferKeeper{}, - &MockIBCCoreKeeper{}, - "", - ) +// The minimum boilerplate way to obtain an-in memory consumer keeper, context, and controller. +// +// Note: Calling ctrl.Finish() at the end of a test function ensures that +// no unexpected calls to external keepers are made. +func GetConsumerKeeperAndCtx(t *testing.T) (consumerkeeper.Keeper, sdk.Context, *gomock.Controller) { + params := NewInMemKeeperParams(t) + ctrl := gomock.NewController(t) + mocks := NewMockedKeepers(ctrl) + return NewInMemConsumerKeeper(params, mocks), params.Ctx, ctrl } -func SetupInMemKeeper(t testing.TB) (*codec.ProtoCodec, *storetypes.KVStoreKey, paramstypes.Subspace, sdk.Context) { - storeKey := sdk.NewKVStoreKey(types.StoreKey) - memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) +// Sets a template client state for a params subspace so that the provider's +// GetTemplateClient method will be satisfied. +func SetTemplateClientState(ctx sdk.Context, subspace *paramstypes.Subspace) { - db := tmdb.NewMemDB() - stateStore := store.NewCommitMultiStore(db) - stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db) - stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil) - require.NoError(t, stateStore.LoadLatestVersion()) + keyTable := paramstypes.NewKeyTable(paramstypes.NewParamSetPair( + providertypes.KeyTemplateClient, &ibctmtypes.ClientState{}, + func(value interface{}) error { return nil })) - registry := codectypes.NewInterfaceRegistry() - cdc := codec.NewProtoCodec(registry) + *subspace = subspace.WithKeyTable(keyTable) - paramsSubspace := paramstypes.NewSubspace(cdc, - codec.NewLegacyAmino(), - storeKey, - memStoreKey, - paramstypes.ModuleName, - ) - ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) - return cdc, storeKey, paramsSubspace, ctx + templateClientState := + ibctmtypes.NewClientState("", ibctmtypes.DefaultTrustLevel, 0, 0, + time.Second*10, clienttypes.Height{}, commitmenttypes.GetSDKSpecs(), + []string{"upgrade", "upgradedIBCState"}, true, true) + + subspace.Set(ctx, providertypes.KeyTemplateClient, templateClientState) +} + +// Registers proto interfaces for params.Cdc +// +// For now, we explicitly force certain unit tests to register sdk crypto interfaces. +// TODO: This function will be executed automatically once https://github.com/cosmos/interchain-security/issues/273 is solved. +func RegisterSdkCryptoCodecInterfaces(params *InMemKeeperParams) { + ir := codectypes.NewInterfaceRegistry() + // Public key implementation registered here + cryptocodec.RegisterInterfaces(ir) + // Replace default cdc, with a custom (registered) codec + params.Cdc = codec.NewProtoCodec(ir) } type PrivateKey struct { diff --git a/x/ccv/consumer/keeper/keeper_test.go b/x/ccv/consumer/keeper/keeper_test.go index 6c40a6b0bd..cac65fed2b 100644 --- a/x/ccv/consumer/keeper/keeper_test.go +++ b/x/ccv/consumer/keeper/keeper_test.go @@ -4,12 +4,11 @@ import ( "testing" "time" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" testkeeper "github.com/cosmos/interchain-security/testutil/keeper" "github.com/cosmos/interchain-security/x/ccv/consumer/types" ccv "github.com/cosmos/interchain-security/x/ccv/types" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" @@ -18,7 +17,10 @@ import ( // TestUnbondingTime tests getter and setter functionality for the unbonding period of a consumer chain func TestUnbondingTime(t *testing.T) { - consumerKeeper, ctx := testkeeper.GetConsumerKeeperAndCtx(t) + + consumerKeeper, ctx, ctrl := testkeeper.GetConsumerKeeperAndCtx(t) + defer ctrl.Finish() + _, ok := consumerKeeper.GetUnbondingTime(ctx) require.False(t, ok) unbondingPeriod := time.Hour * 24 * 7 * 3 @@ -30,7 +32,10 @@ func TestUnbondingTime(t *testing.T) { // TestProviderClientID tests getter and setter functionality for the client ID stored on consumer keeper func TestProviderClientID(t *testing.T) { - consumerKeeper, ctx := testkeeper.GetConsumerKeeperAndCtx(t) + + consumerKeeper, ctx, ctrl := testkeeper.GetConsumerKeeperAndCtx(t) + defer ctrl.Finish() + _, ok := consumerKeeper.GetProviderClientID(ctx) require.False(t, ok) consumerKeeper.SetProviderClientID(ctx, "someClientID") @@ -41,7 +46,10 @@ func TestProviderClientID(t *testing.T) { // TestProviderChannel tests getter and setter functionality for the channel ID stored on consumer keeper func TestProviderChannel(t *testing.T) { - consumerKeeper, ctx := testkeeper.GetConsumerKeeperAndCtx(t) + + consumerKeeper, ctx, ctrl := testkeeper.GetConsumerKeeperAndCtx(t) + defer ctrl.Finish() + _, ok := consumerKeeper.GetProviderChannel(ctx) require.False(t, ok) consumerKeeper.SetProviderChannel(ctx, "channelID") @@ -72,7 +80,9 @@ func TestPendingChanges(t *testing.T) { nil, ) - consumerKeeper, ctx := testkeeper.GetConsumerKeeperAndCtx(t) + consumerKeeper, ctx, ctrl := testkeeper.GetConsumerKeeperAndCtx(t) + defer ctrl.Finish() + err = consumerKeeper.SetPendingChanges(ctx, pd) require.NoError(t, err) gotPd, ok := consumerKeeper.GetPendingChanges(ctx) @@ -86,7 +96,10 @@ func TestPendingChanges(t *testing.T) { // TestPacketMaturityTime tests getter, setter, and iterator functionality for the packet maturity time of a received VSC packet func TestPacketMaturityTime(t *testing.T) { - consumerKeeper, ctx := testkeeper.GetConsumerKeeperAndCtx(t) + + consumerKeeper, ctx, ctrl := testkeeper.GetConsumerKeeperAndCtx(t) + defer ctrl.Finish() + consumerKeeper.SetPacketMaturityTime(ctx, 1, 10) consumerKeeper.SetPacketMaturityTime(ctx, 2, 25) consumerKeeper.SetPacketMaturityTime(ctx, 5, 15) @@ -116,19 +129,13 @@ func TestPacketMaturityTime(t *testing.T) { func TestCrossChainValidator(t *testing.T) { // Construct a keeper with a custom codec - // TODO: Ensure all custom interfaces are registered in prod, see https://github.com/cosmos/interchain-security/issues/273 - _, storeKey, paramsSubspace, ctx := testkeeper.SetupInMemKeeper(t) - ir := codectypes.NewInterfaceRegistry() - - // Public key implementation must be registered - cryptocodec.RegisterInterfaces(ir) - cdc := codec.NewProtoCodec(ir) - - consumerKeeper := testkeeper.GetCustomConsumerKeeper( - cdc, - storeKey, - paramsSubspace, - ) + keeperParams := testkeeper.NewInMemKeeperParams(t) + // Explicitly register public key interface + testkeeper.RegisterSdkCryptoCodecInterfaces(&keeperParams) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + consumerKeeper := testkeeper.NewInMemConsumerKeeper(keeperParams, testkeeper.NewMockedKeepers(ctrl)) + ctx := keeperParams.Ctx // should return false _, found := consumerKeeper.GetCCValidator(ctx, ed25519.GenPrivKey().PubKey().Address()) @@ -165,7 +172,9 @@ func TestCrossChainValidator(t *testing.T) { // TestPendingSlashRequests tests the getter, setter, appending method, and deletion method for pending slash requests func TestPendingSlashRequests(t *testing.T) { - consumerKeeper, ctx := testkeeper.GetConsumerKeeperAndCtx(t) + + consumerKeeper, ctx, ctrl := testkeeper.GetConsumerKeeperAndCtx(t) + defer ctrl.Finish() // prepare test setup by storing 10 pending slash requests request := []types.SlashRequest{} diff --git a/x/ccv/consumer/keeper/params_test.go b/x/ccv/consumer/keeper/params_test.go index 3de23cb73f..a30866c121 100644 --- a/x/ccv/consumer/keeper/params_test.go +++ b/x/ccv/consumer/keeper/params_test.go @@ -10,7 +10,8 @@ import ( // TestParams tests the default params set for a consumer chain, and related getters/setters func TestParams(t *testing.T) { - consumerKeeper, ctx := testkeeper.GetConsumerKeeperAndCtx(t) + consumerKeeper, ctx, ctrl := testkeeper.GetConsumerKeeperAndCtx(t) + defer ctrl.Finish() consumerKeeper.SetParams(ctx, types.DefaultParams()) expParams := types.NewParams(false, 1000, "", "") // these are the default params, IBC suite independently sets enabled=true diff --git a/x/ccv/consumer/keeper/relay_test.go b/x/ccv/consumer/keeper/relay_test.go index 95e5b7a2cd..88af8983da 100644 --- a/x/ccv/consumer/keeper/relay_test.go +++ b/x/ccv/consumer/keeper/relay_test.go @@ -109,29 +109,8 @@ func TestOnRecvVSCPacket(t *testing.T) { }, } - // Instantiate custom keeper with mocks - ctrl := gomock.NewController(t) + consumerKeeper, ctx, ctrl := testkeeper.GetConsumerKeeperAndCtx(t) defer ctrl.Finish() - cdc, storeKey, paramsSubspace, ctx := testkeeper.SetupInMemKeeper(t) - - mockScopedKeeper := testkeeper.NewMockScopedKeeper(ctrl) - mockChannelKeeper := testkeeper.NewMockChannelKeeper(ctrl) - - consumerKeeper := testkeeper.GetCustomConsumerKeeperWithMocks( - cdc, - storeKey, - paramsSubspace, - mockScopedKeeper, - mockChannelKeeper, - testkeeper.NewMockPortKeeper(ctrl), - testkeeper.NewMockConnectionKeeper(ctrl), - testkeeper.NewMockClientKeeper(ctrl), - testkeeper.NewMockSlashingKeeper(ctrl), - testkeeper.NewMockBankKeeper(ctrl), - testkeeper.NewMockAccountKeeper(ctrl), - testkeeper.NewMockIBCTransferKeeper(ctrl), - testkeeper.NewMockIBCCoreKeeper(ctrl), - ) // Set channel to provider, still in context of consumer chain consumerKeeper.SetProviderChannel(ctx, consumerCCVChannelID) @@ -181,29 +160,13 @@ func TestOnAcknowledgementPacket(t *testing.T) { // Channel ID on destination (counter party) chain channelIDOnDest := "ChannelIDOnDest" - // Instantiate custom keeper with mocks + // Instantiate in-mem keeper with mocks ctrl := gomock.NewController(t) defer ctrl.Finish() - cdc, storeKey, paramsSubspace, ctx := testkeeper.SetupInMemKeeper(t) - - mockScopedKeeper := testkeeper.NewMockScopedKeeper(ctrl) - mockChannelKeeper := testkeeper.NewMockChannelKeeper(ctrl) - - consumerKeeper := testkeeper.GetCustomConsumerKeeperWithMocks( - cdc, - storeKey, - paramsSubspace, - mockScopedKeeper, - mockChannelKeeper, - testkeeper.NewMockPortKeeper(ctrl), - testkeeper.NewMockConnectionKeeper(ctrl), - testkeeper.NewMockClientKeeper(ctrl), - testkeeper.NewMockSlashingKeeper(ctrl), - testkeeper.NewMockBankKeeper(ctrl), - testkeeper.NewMockAccountKeeper(ctrl), - testkeeper.NewMockIBCTransferKeeper(ctrl), - testkeeper.NewMockIBCCoreKeeper(ctrl), - ) + keeperParams := testkeeper.NewInMemKeeperParams(t) + mocks := testkeeper.NewMockedKeepers(ctrl) + consumerKeeper := testkeeper.NewInMemConsumerKeeper(keeperParams, mocks) + ctx := keeperParams.Ctx // Set an established provider channel for later in test consumerKeeper.SetProviderChannel(ctx, channelIDToProvider) @@ -235,20 +198,20 @@ func TestOnAcknowledgementPacket(t *testing.T) { dummyCap := &capabilitytypes.Capability{} gomock.InOrder( - mockScopedKeeper.EXPECT().GetCapability( + mocks.MockScopedKeeper.EXPECT().GetCapability( ctx, host.ChannelCapabilityPath(ccv.ConsumerPortID, channelIDToDestChain), ).Return(dummyCap, true).Times(1), // Due to input error ack, ChanCloseInit is called on channel to destination chain - mockChannelKeeper.EXPECT().ChanCloseInit( + mocks.MockChannelKeeper.EXPECT().ChanCloseInit( ctx, ccv.ConsumerPortID, channelIDToDestChain, dummyCap, ).Return(nil).Times(1), - mockScopedKeeper.EXPECT().GetCapability( + mocks.MockScopedKeeper.EXPECT().GetCapability( ctx, host.ChannelCapabilityPath(ccv.ConsumerPortID, channelIDToProvider), ).Return(dummyCap, true).Times(1), // Due to input error ack and existence of established channel to provider, // ChanCloseInit is called on channel to provider - mockChannelKeeper.EXPECT().ChanCloseInit( + mocks.MockChannelKeeper.EXPECT().ChanCloseInit( ctx, ccv.ConsumerPortID, channelIDToProvider, dummyCap, ).Return(nil).Times(1), ) diff --git a/x/ccv/consumer/keeper/validators_test.go b/x/ccv/consumer/keeper/validators_test.go index 15f14ed1b5..b5ef2003fb 100644 --- a/x/ccv/consumer/keeper/validators_test.go +++ b/x/ccv/consumer/keeper/validators_test.go @@ -3,14 +3,13 @@ package keeper_test import ( "testing" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" testkeeper "github.com/cosmos/interchain-security/testutil/keeper" "github.com/cosmos/interchain-security/x/ccv/consumer/keeper" "github.com/cosmos/interchain-security/x/ccv/consumer/types" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" tmrand "github.com/tendermint/tendermint/libs/rand" @@ -20,18 +19,15 @@ import ( // TestApplyCCValidatorChanges tests the ApplyCCValidatorChanges method for a consumer keeper func TestApplyCCValidatorChanges(t *testing.T) { // Construct a keeper with a custom codec - _, storeKey, paramsSubspace, ctx := testkeeper.SetupInMemKeeper(t) - ir := codectypes.NewInterfaceRegistry() + keeperParams := testkeeper.NewInMemKeeperParams(t) - // Public key implementation must be registered - cryptocodec.RegisterInterfaces(ir) - cdc := codec.NewProtoCodec(ir) + // Explicitly register public key interface + testkeeper.RegisterSdkCryptoCodecInterfaces(&keeperParams) - consumerKeeper := testkeeper.GetCustomConsumerKeeper( - cdc, - storeKey, - paramsSubspace, - ) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + consumerKeeper := testkeeper.NewInMemConsumerKeeper(keeperParams, testkeeper.NewMockedKeepers(ctrl)) + ctx := keeperParams.Ctx // utility functions getCCVals := func() (vals []types.CrossChainValidator) { @@ -117,20 +113,14 @@ func TestApplyCCValidatorChanges(t *testing.T) { func TestHistoricalInfo(t *testing.T) { // Construct a keeper with a custom codec - _, storeKey, paramsSubspace, ctx := testkeeper.SetupInMemKeeper(t) - ir := codectypes.NewInterfaceRegistry() - - // Public key implementation must be registered - cryptocodec.RegisterInterfaces(ir) - cdc := codec.NewProtoCodec(ir) - - consumerKeeper := testkeeper.GetCustomConsumerKeeper( - cdc, - storeKey, - paramsSubspace, - ) - - ctx = ctx.WithBlockHeight(15) + keeperParams := testkeeper.NewInMemKeeperParams(t) + // Explicitly register public key interface + testkeeper.RegisterSdkCryptoCodecInterfaces(&keeperParams) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + consumerKeeper := testkeeper.NewInMemConsumerKeeper(keeperParams, testkeeper.NewMockedKeepers(ctrl)) + + ctx := keeperParams.Ctx.WithBlockHeight(15) // Generate test validators, save them to store, and retrieve stored records validators := GenerateValidators(t) diff --git a/x/ccv/provider/keeper/keeper_test.go b/x/ccv/provider/keeper/keeper_test.go index 6bd62e2b8d..8f4c6f5f66 100644 --- a/x/ccv/provider/keeper/keeper_test.go +++ b/x/ccv/provider/keeper/keeper_test.go @@ -3,7 +3,6 @@ package keeper_test import ( "testing" - capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" "github.com/golang/mock/gomock" @@ -24,7 +23,8 @@ import ( // TestValsetUpdateBlockHeight tests the getter, setter, and deletion methods for valset updates mapped to block height func TestValsetUpdateBlockHeight(t *testing.T) { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() blockHeight, found := providerKeeper.GetValsetUpdateBlockHeight(ctx, uint64(0)) require.False(t, found) @@ -49,7 +49,8 @@ func TestValsetUpdateBlockHeight(t *testing.T) { // TestSlashAcks tests the getter, setter, iteration, and deletion methods for stored slash acknowledgements func TestSlashAcks(t *testing.T) { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() var chainsAcks [][]string @@ -91,7 +92,8 @@ func TestSlashAcks(t *testing.T) { // TestAppendSlashAck tests the append method for stored slash acknowledgements func TestAppendSlashAck(t *testing.T) { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() p := []string{"alice", "bob", "charlie"} chains := []string{"c1", "c2"} @@ -110,7 +112,8 @@ func TestAppendSlashAck(t *testing.T) { // TestPendingVSCs tests the getter, appending, and deletion methods for stored pending VSCs func TestPendingVSCs(t *testing.T) { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() chainID := "consumer" @@ -164,7 +167,8 @@ func TestPendingVSCs(t *testing.T) { // TestInitHeight tests the getter and setter methods for the stored block heights (on provider) when a given consumer chain was started func TestInitHeight(t *testing.T) { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() tc := []struct { chainID string @@ -186,13 +190,12 @@ func TestInitHeight(t *testing.T) { // TestHandleSlashPacketDoubleSigning tests the handling of a double-signing related slash packet, with mocks and unit tests func TestHandleSlashPacketDoubleSigning(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() chainId := "consumer" infractionHeight := int64(5) - cdc, storeKey, paramsSubspace, ctx := testkeeper.SetupInMemKeeper(t) + keeperParams := testkeeper.NewInMemKeeperParams(t) + ctx := keeperParams.Ctx slashPacket := ccv.NewSlashPacketData( abci.Validator{Address: ed25519.GenPrivKey().PubKey().Address(), @@ -201,8 +204,11 @@ func TestHandleSlashPacketDoubleSigning(t *testing.T) { stakingtypes.DoubleSign, ) - mockStakingKeeper := testkeeper.NewMockStakingKeeper(ctrl) - mockSlashingKeeper := testkeeper.NewMockSlashingKeeper(ctrl) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mocks := testkeeper.NewMockedKeepers(ctrl) + mockSlashingKeeper := mocks.MockSlashingKeeper + mockStakingKeeper := mocks.MockStakingKeeper // Setup expected mock calls gomock.InOrder( @@ -235,19 +241,7 @@ func TestHandleSlashPacketDoubleSigning(t *testing.T) { evidencetypes.DoubleSignJailEndTime).Times(1), ) - providerKeeper := testkeeper.GetProviderKeeperWithMocks( - cdc, - storeKey, - paramsSubspace, - capabilitykeeper.ScopedKeeper{}, - testkeeper.NewMockChannelKeeper(ctrl), - testkeeper.NewMockPortKeeper(ctrl), - testkeeper.NewMockConnectionKeeper(ctrl), - testkeeper.NewMockClientKeeper(ctrl), - mockStakingKeeper, - mockSlashingKeeper, - testkeeper.NewMockAccountKeeper(ctrl), - ) + providerKeeper := testkeeper.NewInMemProviderKeeper(keeperParams, mocks) providerKeeper.SetInitChainHeight(ctx, chainId, uint64(infractionHeight)) @@ -257,7 +251,10 @@ func TestHandleSlashPacketDoubleSigning(t *testing.T) { } func TestIterateOverUnbondingOpIndex(t *testing.T) { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() + chainID := "6" // mock an unbonding index @@ -280,7 +277,9 @@ func TestIterateOverUnbondingOpIndex(t *testing.T) { } func TestMaturedUnbondingOps(t *testing.T) { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() ids, err := providerKeeper.GetMaturedUnbondingOps(ctx) require.NoError(t, err) diff --git a/x/ccv/provider/keeper/params_test.go b/x/ccv/provider/keeper/params_test.go index b67c856398..685e3cb71f 100644 --- a/x/ccv/provider/keeper/params_test.go +++ b/x/ccv/provider/keeper/params_test.go @@ -4,43 +4,28 @@ import ( "testing" "time" - capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" - paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types" ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types" testkeeper "github.com/cosmos/interchain-security/testutil/keeper" "github.com/cosmos/interchain-security/x/ccv/provider/types" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" ) +// TestParams tests the default params of the keeper, and getting/setting new params. func TestParams(t *testing.T) { defaultParams := types.DefaultParams() - // Constuct our own params subspace - cdc, storeKey, paramsSubspace, ctx := testkeeper.SetupInMemKeeper(t) - keyTable := paramstypes.NewKeyTable(paramstypes.NewParamSetPair(types.KeyTemplateClient, &ibctmtypes.ClientState{}, func(value interface{}) error { return nil })) - paramsSubspace = paramsSubspace.WithKeyTable(keyTable) - - expectedClientState := - ibctmtypes.NewClientState("", ibctmtypes.DefaultTrustLevel, 0, 0, - time.Second*10, clienttypes.Height{}, commitmenttypes.GetSDKSpecs(), []string{"upgrade", "upgradedIBCState"}, true, true) - - paramsSubspace.Set(ctx, types.KeyTemplateClient, expectedClientState) - - providerKeeper := testkeeper.GetProviderKeeperWithMocks( - cdc, - storeKey, - paramsSubspace, - capabilitykeeper.ScopedKeeper{}, - &testkeeper.MockChannelKeeper{}, - &testkeeper.MockPortKeeper{}, - &testkeeper.MockConnectionKeeper{}, - &testkeeper.MockClientKeeper{}, - &testkeeper.MockStakingKeeper{}, - &testkeeper.MockSlashingKeeper{}, - &testkeeper.MockAccountKeeper{}, - ) + // Construct an in-mem keeper with a default template client state set + keeperParams := testkeeper.NewInMemKeeperParams(t) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mocks := testkeeper.NewMockedKeepers(ctrl) + ctx := keeperParams.Ctx + // Populate template client state to test against + testkeeper.SetTemplateClientState(ctx, keeperParams.ParamsSubspace) + providerKeeper := testkeeper.NewInMemProviderKeeper(keeperParams, mocks) params := providerKeeper.GetParams(ctx) require.Equal(t, defaultParams, params) diff --git a/x/ccv/provider/keeper/proposal_test.go b/x/ccv/provider/keeper/proposal_test.go index f62f246458..945fca19f5 100644 --- a/x/ccv/provider/keeper/proposal_test.go +++ b/x/ccv/provider/keeper/proposal_test.go @@ -25,7 +25,8 @@ func TestPendingStopProposalDeletion(t *testing.T) { ExpDeleted: false, }, } - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() for _, tc := range testCases { providerKeeper.SetPendingStopProposal(ctx, tc.ChainId, tc.StopTime) @@ -96,7 +97,8 @@ func TestPendingStopProposalsOrder(t *testing.T) { } for _, tc := range testCases { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() ctx = ctx.WithBlockTime(tc.accessTime) for _, prop := range tc.propSubmitOrder { @@ -122,7 +124,8 @@ func TestPendingCreateProposalsDeletion(t *testing.T) { ExpDeleted: false, }, } - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() for _, tc := range testCases { err := providerKeeper.SetPendingCreateProposal(ctx, &tc.CreateConsumerChainProposal) @@ -194,7 +197,9 @@ func TestPendingCreateProposalsOrder(t *testing.T) { } for _, tc := range testCases { - providerKeeper, ctx := testkeeper.GetProviderKeeperAndCtx(t) + providerKeeper, ctx, ctrl := testkeeper.GetProviderKeeperAndCtx(t) + defer ctrl.Finish() + ctx = ctx.WithBlockTime(tc.accessTime) for _, prop := range tc.propSubmitOrder {