epochs
Last updated
Last updated
This document specifies the internal x/epochs
module of the LagomChain Hub.
Often, when working with the , 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.
The epochs
module defines on-chain timers that execute at fixed time intervals. Other LagomChain 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
.
The x/epochs
module keeps the following objects in state
:
EpochInfo
Epoch info bytecode
[]byte{identifier}
[]byte{epochInfo}
KV
EpochInfo
An EpochInfo
defines several variables:
identifier
keeps an epoch identification string
start_time
keeps the start time for epoch counting: if block height passes start_time
, then epoch_counting_started
is set
duration
keeps the target epoch duration
current_epoch
keeps the current active epoch number
current_epoch_start_time
keeps the start time of the current epoch
epoch_counting_started
is a flag set with start_time
, at which point epoch_number
will be counted
current_epoch_start_height
keeps the start block height of the current epoch
The epochs
module keeps these EpochInfo
objects in state, which are initialized at genesis and are modified on begin blockers or end blockers.
Genesis State
The 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:
The x/epochs
module emits the following events:
epoch_start
"epoch_number"
{epoch_number}
epoch_start
"start_time"
{start_time}
epoch_end
"epoch_number"
{epoch_number}
The x/epochs
module only exposes one keeper, the epochs keeper, which can be used to manage epochs.
Presently only one fully-permissioned epochs keeper is exposed, which has the ability to both read and write the EpochInfo
for all epochs, and to iterate over all stored epochs.
When other modules (outside of 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.
The x/epochs
module provides the following queries to check the module's state.
In the current design, each epoch should be at least two blocks, as the start block should be different from the endblock. Because of this, the time allocated to each epoch will be 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.
This implementation of the 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.
The x/epochs
module implements hooks so that other modules can use epochs to allow facets of the to run on specific schedules.