This document specifies the timerstore
module of Lava Protocol.
This module primarily serves as a utility for other modules in the Lava Protocol. It's not designed for direct user interaction, except for a limited set of queries intended for debugging. As such, it functions as an essential support utility within the protocol's ecosystem.
The timerstore
allows other modules to create timers, that triggers callback functions.
A timer defined in this module can be one of two types: BlockHeight or BlockTime.
The BlockHeight timers operate based on block counts, while BlockTime timers are measured in seconds.
The callback function can be triggered either at BeginBlock or EndBlock.
The core of this module is the TimerStore
object, encapsulating all essential logic:
type Keeper struct {
timerStoresBegin []*timerstoretypes.TimerStore
timerStoresEnd []*timerstoretypes.TimerStore
cdc codec.BinaryCodec
}
This structure reflects the dual nature of timer triggers: at the beginning or end of a block.
To initialize a TimerStore
, use NewTimerStoreBeginBlock
or NewTimerStoreEndBlock
. These methods set up the store to trigger timers at either the start or end of a block, respectively.
Callbacks are set using WithCallbackByBlockHeight
for block-height-based timers or WithCallbackByBlockTime
for time-based timers. These methods link the specified callback function to the timer, ensuring it's executed at the right moment.
Note: The following code snippet demonstrates a common pattern used in the initialization of a timer in the codebase. This approach typically involves creating a TimerStore
instance and immediately configuring its callback function, streamlining the setup process:
callbackFunction := func(ctx sdk.Context, key, data []byte) {
// callback logic here
}
timerStore := timerStoreKeeper.NewTimerStoreBeginBlock(storeKey, timerPrefix).
WithCallbackByBlockTime(callbackFunction)
Note on Timer Keys and Data:
- Subkey as Timer Identifier: The
key
argument in the callback function refers to the timer's subkey. It's important to note that this subkey does not need to be unique across all timers. Thetimerstore
module internally manages a unique primary key for each timer, ensuring distinct identification. - Data Attachment: The
data
argument allows for attaching relevant information to the timer. This data is then accessible when the callback function is triggered, enabling context-specific actions based on the timer's purpose.
After establishing the TimerStore
, it's used to create and manage individual timers. This section delves into the operational aspect of the TimerStore
.
There are two types of timers:
BlockHeight
timers trigger based on the count of blocks.BlockTime
timers operate based on elapsed time in seconds.
Each type serves different use cases, offering flexibility in scheduling tasks.
Timers are added to the TimerStore
via AddTimerByBlockHeight
and AddTimerByBlockTime
. These functions create timers that will trigger either at a specified block height or after a set time:
// Add a BlockHeight timer
AddTimerByBlockHeight(ctx sdk.Context, block uint64, key, data []byte)
// Add a BlockTime timer
AddTimerByBlockTime(ctx sdk.Context, timestamp uint64, key, data []byte)
When using AddTimerByBlockHeight
and AddTimerByBlockTime
, it's important to understand the key
argument is the timer's subkey, which doesn't need to be unique. The unique primary key of each timer is internally managed by the timerstore. The data
argument is for attaching additional information to the timer, available when the callback is triggered.
Once set, a timer remains active until triggered, after which it's automatically deleted. For recurring events, it's necessary to create a new timer each time.
The timerstore
module does not contain parameters.
The timerstore
module supports the following queries:
Query | Arguments | What it does |
---|---|---|
all-timers |
store-key (string), prefix (string) | Shows all timers of a specific timer store |
next |
store-key (string), prefix (string) | Shows the next timeout of a specific timer store |
store-keys |
none | Shows all timer store keys and prefixes |
The timerstore
module does not support any transaction.
The timerstore
module does not support any proposals.