Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
change set hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Sep 20, 2023
1 parent 7e9b570 commit e3d6229
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
13 changes: 7 additions & 6 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,13 @@ func NewApp(
keys[epochsmoduletypes.MemStoreKey],
app.GetSubspace(epochsmoduletypes.ModuleName),
)

app.EpochsKeeper.SetHooks(
epochsmoduletypes.NewMultiEpochHooks(
app.RegistryKeeper.Hooks(),
// insert hooks here
))

epochsModule := epochsmodule.NewAppModule(appCodec, app.EpochsKeeper, app.AccountKeeper, app.BankKeeper)

app.RegistryKeeper = *registrymodulekeeper.NewKeeper(
Expand Down Expand Up @@ -726,12 +733,6 @@ func NewApp(
),
)

app.EpochsKeeper.SetHooks(
epochsmoduletypes.NewMultiEpochHooks(
app.RegistryKeeper.Hooks(),
// insert hooks here
))

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down
10 changes: 8 additions & 2 deletions x/epochs/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// AfterEpochEnd executes the indicated hook after epochs ends
// AfterEpochEnd epoch hook
func (k Keeper) AfterEpochEnd(ctx sdk.Context, identifier string, epochNumber int64) {
if k.hooks == nil {
panic("hooks not set in keeper")
}
k.hooks.AfterEpochEnd(ctx, identifier, epochNumber)
}

// BeforeEpochStart executes the indicated hook before the epochs
// BeforeEpochStart epoch hook
func (k Keeper) BeforeEpochStart(ctx sdk.Context, identifier string, epochNumber int64) {
if k.hooks == nil {
panic("hooks not set in keeper")
}
k.hooks.BeforeEpochStart(ctx, identifier, epochNumber)
}
26 changes: 22 additions & 4 deletions x/epochs/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func (suite *KeeperTestSuite) TestAfterEpochHooks() {
}{
{
expBeforeEpochStartEvents: []ExpEvent{
{
EpochNumber: "2",
},
},
expAfterEpochEndEvents: []ExpEvent{
{
EpochNumber: "1",
},
Expand All @@ -70,15 +75,23 @@ func (suite *KeeperTestSuite) TestAfterEpochHooks() {
// Check if curent epoch is expected
epochInfo, found := suite.app.EpochsKeeper.GetEpochInfo(suite.ctx, types.DayEpochId)
suite.Require().True(found)
suite.Require().Equal(int64(1), epochInfo.CurrentEpoch)
suite.Require().Equal(int64(2), epochInfo.CurrentEpoch)
},
},
{
expBeforeEpochStartEvents: []ExpEvent{
{
EpochNumber: "1",
EpochNumber: "2",
},
{
EpochNumber: "3",
},
},
expAfterEpochEndEvents: []ExpEvent{
{
EpochNumber: "1",
},
{
EpochNumber: "2",
},
},
Expand All @@ -90,11 +103,16 @@ func (suite *KeeperTestSuite) TestAfterEpochHooks() {
// Check if curent epoch is expected
epochInfo, found := suite.app.EpochsKeeper.GetEpochInfo(suite.ctx, types.DayEpochId)
suite.Require().True(found)
suite.Require().Equal(int64(1), epochInfo.CurrentEpoch)
suite.Require().Equal(int64(2), epochInfo.CurrentEpoch)

// Begin second block
suite.ctx = suite.ctx.WithBlockHeight(3).WithBlockTime(now.Add(oneDayDuration))
suite.app.EpochsKeeper.BeginBlocker(suite.ctx)

// Check if curent epoch is expected
epochInfo, found = suite.app.EpochsKeeper.GetEpochInfo(suite.ctx, types.DayEpochId)
suite.Require().True(found)
suite.Require().Equal(int64(3), epochInfo.CurrentEpoch)
},
},
}
Expand Down Expand Up @@ -131,7 +149,7 @@ func (suite *KeeperTestSuite) TestAfterEpochHooks() {
if len(tc.expAfterEpochEndEvents) != 0 {
afterEpochEndEvents, found := testutil.FindEventsByType(suite.ctx.EventManager().Events(), AfterEpochEndEventType)
suite.Require().True(found)
for i, expEvent := range tc.expBeforeEpochStartEvents {
for i, expEvent := range tc.expAfterEpochEndEvents {
event := afterEpochEndEvents[i]
suite.Require().Equal(AfterEpochEndEventType, event.Type)
suite.Require().Equal(EpochIdentifier, event.Attributes[0].Value)
Expand Down

0 comments on commit e3d6229

Please sign in to comment.