diff --git a/CHANGELOG.md b/CHANGELOG.md index 03cb15059c..0f8b22daed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (client, client/rpc, x/auth/tx) [#24551](https://github.com/cosmos/cosmos-sdk/pull/24551) Handle cancellation properly when supplying context to client methods. * (x/authz) [#24638](https://github.com/cosmos/cosmos-sdk/pull/24638) Fixed a minor bug where the grant key was cast as a string and dumped directly into the error message leading to an error string possibly containing invalid UTF-8. * (client, client/rpc, x/auth/tx) [#24551](https://github.com/cosmos/cosmos-sdk/pull/24551) Handle cancellation properly when supplying context to client methods. +* (x/epochs) [#24770](https://github.com/cosmos/cosmos-sdk/pull/24770) Fix register of epoch hooks in `InvokeSetHooks`. ### Deprecated diff --git a/x/epochs/depinject.go b/x/epochs/depinject.go index d50401850d..7c4fd2a53f 100644 --- a/x/epochs/depinject.go +++ b/x/epochs/depinject.go @@ -49,8 +49,8 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { return ModuleOutputs{EpochKeeper: k, Module: m} } -func InvokeSetHooks(keeper keeper.Keeper, hooks map[string]types.EpochHooksWrapper) error { - if hooks == nil { +func InvokeSetHooks(keeper *keeper.Keeper, hooks map[string]types.EpochHooksWrapper) error { + if keeper == nil || hooks == nil { return nil } diff --git a/x/epochs/depinject_test.go b/x/epochs/depinject_test.go new file mode 100644 index 0000000000..ac67263a0d --- /dev/null +++ b/x/epochs/depinject_test.go @@ -0,0 +1,60 @@ +package epochs_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + + storetypes "cosmossdk.io/store/types" + + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/epochs" + "github.com/cosmos/cosmos-sdk/x/epochs/keeper" + "github.com/cosmos/cosmos-sdk/x/epochs/types" +) + +type testEpochHooks struct{} + +func (h testEpochHooks) AfterEpochEnd(ctx context.Context, epochIdentifier string, epochNumber int64) error { + return nil +} + +func (h testEpochHooks) BeforeEpochStart(ctx context.Context, epochIdentifier string, epochNumber int64) error { + return nil +} + +func TestInvokeSetHooks(t *testing.T) { + // Create a mock keeper + key := storetypes.NewKVStoreKey(types.StoreKey) + storeService := runtime.NewKVStoreService(key) + encCfg := testutil.MakeTestEncodingConfig() + mockKeeper := keeper.NewKeeper(storeService, encCfg.Codec) + + // Create mock hooks + hook1 := types.EpochHooksWrapper{ + EpochHooks: testEpochHooks{}, + } + hook2 := types.EpochHooksWrapper{ + EpochHooks: testEpochHooks{}, + } + hooks := map[string]types.EpochHooksWrapper{ + "moduleA": hook1, + "moduleB": hook2, + } + + // Call InvokeSetHooks + err := epochs.InvokeSetHooks(&mockKeeper, hooks) + require.NoError(t, err) + + // Verify that hooks were set correctly + require.NotNil(t, mockKeeper.Hooks()) + require.IsType(t, types.MultiEpochHooks{}, mockKeeper.Hooks()) + + // Verify the order of hooks (lexical order by module name) + multiHooks := mockKeeper.Hooks().(types.MultiEpochHooks) + require.Equal(t, 2, len(multiHooks)) + require.Equal(t, hook1, multiHooks[0]) + require.Equal(t, hook2, multiHooks[1]) +}