rooch
rooch copied to clipboard
Tick solution
For a fully on-chain game, a mechanism is needed to drive the game logic automatically. However, current on-chain contracts can only be triggered by user transactions. To address this, a Tick solution is required to enable the scheduled execution of transactions by the system.
- Synchronize block time from Layer 1 (L1).
- The Sequencer, based on L1's time, triggers a Tick transaction periodically. This transaction includes an offset time and needs to be on the Data Availability (DA) layer.
- Provide a Tick system contract to store a list of user contracts that need to be triggered by the Tick.
- The VM needs to handle Tick Transactions, executing them based on the ticked list stored in the contract state.
How to define a tick contract
- static register based
module mygame::my_module{
fun init(ctx: &mut StorageContext){
rooch_framework::tick::register(module: mygame::my_module, interval: 10 seconds)
}
fun tick(ctx: &mut StorageContext){
}
}
- one-time callback based
module mygame::my_module{
fun game_loop(ctx: &mut StorageContext){
//so something
rooch_framework::tick::set_timeout(function: mygame::MyModule::game_loop, time: 10 seconds);
}
}
good idea!
Another solution is to provide a message-based trigger, a contract can watch an Event, and the tick is also an Event.