To execute logic at specific intervals, your module can implement hooks provided by the epochs module.
Available Hooks:
AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64): Called when an epoch ends.BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64): Called at the start of a new epoch.
Implementation Pattern:
Because the epochs module can manage multiple identifiers, your hook implementation must filter by the epochIdentifier you are interested in. It is recommended to store the target identifier in your module's Params so it can be managed via governance.
Panic Isolation:
If a hook panics, the state update for that specific hook is reverted, but the module continues to execute subsequent hooks. This prevents a single faulty module from halting the entire state machine. However, you should design your logic to be resilient to the possibility that a prior module's epoch hook failed to execute.
func (k MyModuleKeeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
params := k.GetParams(ctx)
if epochIdentifier == params.DistrEpochIdentifier {
// execute my specific logic here
}
}