Abstract
Often in the SDK, we would like to run certain code every-so often. The purpose ofepochs
module is to allow other modules to set that they would like to be signaled once every period. So another module can specify it wants to execute code once a week, starting at UTC-time = x. epochs
creates a generalized epoch interface to other modules so that they can easily be signaled upon such events.
Contents
Concepts
The epochs module defines on-chain timers that execute at fixed time intervals. Other SDK modules can then register logic to be executed at the timer ticks. We refer to the period in between two timer ticks as an “epoch”. Every timer has a unique identifier. Every epoch will have a start time, and an end time, whereend time = start time + timer interval
. On mainnet, we only utilize one identifier, with a time interval of one day
.
The timer will tick at the first block whose block time is greater than the timer end time, and set the start as the prior timer end time. (Notably, it’s not set to the block time!) This means that if the chain has been down for a while, you will get one timer tick per block, until the timer has caught up.
State
The Epochs module keeps a singleEpochInfo
per identifier. This contains the current state of the timer with the corresponding identifier. Its fields are modified at every timer tick. EpochInfos are initialized as part of genesis initialization or upgrade logic, and are only modified on begin blockers.
Events
Theepochs
module emits the following events:
BeginBlocker
Type | Attribute Key | Attribute Value |
---|---|---|
epoch_start | epoch_number | {epoch_number} |
epoch_start | start_time | {start_time} |
EndBlocker
Type | Attribute Key | Attribute Value |
---|---|---|
epoch_end | epoch_number | {epoch_number} |
Keepers
Keeper functions
Epochs keeper module provides utility functions to manage epochs.Hooks
How modules receive hooks
On hook receiver function of other modules, they need to filterepochIdentifier
and only do executions for only specific epochIdentifier. Filtering epochIdentifier could be in Params
of other modules so that they can be modified by governance.
This is the standard dev UX of this:
Panic isolation
If a given epoch hook panics, its state update is reverted, but we keep proceeding through the remaining hooks. This allows more advanced epoch logic to be used, without concern over state machine halting, or halting subsequent modules. This does mean that if there is behavior you expect from a prior epoch hook, and that epoch hook reverted, your hook may also have an issue. So do keep in mind “what if a prior hook didn’t get executed” in the safety checks you consider for a new epoch hook.Queries
The Epochs module provides the following queries to check the module’s state.Epoch Infos
Query the currently running epochInfosCurrent Epoch
Query the current epoch by the specified identifierday
epoch: