rooch icon indicating copy to clipboard operation
rooch copied to clipboard

Tick solution

Open jolestar opened this issue 2 years ago • 2 comments

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

  1. 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){
	
  }

}
  1. 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);
  }
}

jolestar avatar Sep 08 '23 03:09 jolestar

good idea!

Zombieliu avatar Nov 11 '23 18:11 Zombieliu

Another solution is to provide a message-based trigger, a contract can watch an Event, and the tick is also an Event.

jolestar avatar Nov 12 '23 03:11 jolestar