x/epochs
module in the Cosmos SDK.
Often, when working with the Cosmos SDK, we would like to run certain pieces of code every so often.
The purpose of the epochs
module is to allow other modules to maintain that they would like to be signaled once in a time period. So, another module can specify it wants to execute certain code once a week, starting at UTC-time = x. epochs
creates a generalized epoch interface to other modules so they can be more easily signaled upon such events.
epochs
module defines on-chain timers that execute at fixed time intervals. Other Cosmos 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, and every epoch will have a start time and an end time, where end time = start time + timer interval
.
x/epochs
module keeps the following objects in state
:
State Object | Description | Key | Value | Store |
---|---|---|---|---|
EpochInfo | Epoch info bytecode | []byte{identifier} | []byte{epochInfo} | KV |
EpochInfo
defines several variables:
identifier
keeps an epoch identification stringstart_time
keeps the start time for epoch counting: if block height passes start_time
, then epoch_counting_started
is setduration
keeps the target epoch durationcurrent_epoch
keeps the current active epoch numbercurrent_epoch_start_time
keeps the start time of the current epochepoch_counting_started
is a flag set with start_time
, at which point epoch_number
will be countedcurrent_epoch_start_height
keeps the start block height of the current epochepochs
module keeps these EpochInfo
objects in state, which are initialized at genesis and are modified on begin blockers or end blockers.
x/epochs
module’s GenesisState
defines the state necessary for initializing the chain from a previously exported height. It contains a slice containing all the EpochInfo
objects kept in state:
x/epochs
module emits the following events:
Type | Attribute Key | Attribute Value |
---|---|---|
epoch_start | "epoch_number" | {epoch_number} |
epoch_start | "start_time" | {start_time} |
Type | Attribute Key | Attribute Value |
---|---|---|
epoch_end | "epoch_number" | {epoch_number} |
x/epochs
module only exposes one keeper, the epochs keeper, which can be used to manage epochs.
EpochInfo
for all epochs, and to iterate over all stored epochs.
x/epochs
module implements hooks so that other modules can use epochs to allow facets of the Cosmos SDK to run on specific schedules.
x/epochs
) recieve hooks, they need to filter the value epochIdentifier
, and only do executions for a specific epochIdentifier
.
The filtered values from epochIdentifier
could be stored in the Params
of other modules, so they can be modified by governance.
Governance can change epoch periods from week
to day
as needed.
x/epochs
module provides the following queries to check the module’s state.
max(block_time x 2, epoch_duration)
. For example: if the epoch_duration
is set to 1s
, and block_time
is 5s
, actual epoch time should be 10s
.
It is recommended to configure epoch_duration
to be more than two times the block_time
, to use this module correctly. If there is a mismatch between the epoch_duration
and the actual epoch time, as in the example above, then module logic could become invalid.
x/epochs
module has block-time drifts based on the value of block_time
. For example: if we have an epoch of 100 units that ends at t=100
, and we have a block at t=97
and a block at t=104
and t=110
, this epoch ends at t=104
, and the new epoch will start at t=110
.
There are time drifts here, varying about 1-2 blocks time, which will slow down epochs.