epochs
Abstract
This document specifies the internal x/epochs module of the LagomChain Hub.
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.
Contents
Concepts
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.
State
State Objects
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:
identifierkeeps an epoch identification stringstart_timekeeps the start time for epoch counting: if block height passesstart_time, thenepoch_counting_startedis setdurationkeeps the target epoch durationcurrent_epochkeeps the current active epoch numbercurrent_epoch_start_timekeeps the start time of the current epochepoch_counting_startedis a flag set withstart_time, at which pointepoch_numberwill be countedcurrent_epoch_start_heightkeeps 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:
Events
The x/epochs module emits the following events:
BeginBlocker
epoch_start
"epoch_number"
{epoch_number}
epoch_start
"start_time"
{start_time}
EndBlocker
epoch_end
"epoch_number"
{epoch_number}
Keepers
The x/epochs module only exposes one keeper, the epochs keeper, which can be used to manage epochs.
Epochs Keeper
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.
Hooks
The x/epochs module implements hooks so that other modules can use epochs to allow facets of the Cosmos SDK to run on specific schedules.
Hooks Implementation
Recieving Hooks
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.
Queries
The x/epochs module provides the following queries to check the module's state.
Future Improvements
Correct Usage
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.
Block-Time Drifts
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.
Last updated
