paritytech / polkadot-sdk

The Parity Polkadot Blockchain SDK
https://polkadot.network/
1.88k stars 687 forks source link

Scheduler for off-chain workers execution #477

Open tomusdrw opened 4 years ago

tomusdrw commented 4 years ago

Offchain workshop follow up task.

There is a common pattern emerging in the way we run offchain workers. Usually we want them to be triggered:

  1. After a block number set in the storage
  2. Only once after that threshold,
  3. But retry after the previous run does not seem to complete.
  4. Randomly for different validators, etc

That could be easily made into a re-usable piece of code that would schedule the runs. Rough (and incomplete) proposal from @kianenigma

fn offchain_worker() {
    // simplest case. It basically adds a `should_run` impl that always return true
    let OffchainLimit::default()::build();

    // Run every N block
    let OffchainLimit::default()::with_interval(10)

    // We can also implement maybe your idea of 
    let OffchainLimit::default().with_interval(10).randomise(2)

        /// after some point in time
    let OffchainLimit::after(Storage::get(()).with_interval(10).randomise(2)

    // Any more attributes. I really think the idea of _composability_ can be very neat here, which
    // the builder pattern does very well.
kianenigma commented 4 years ago

Is this to be grabbed or is @bkchr working on it?

bkchr commented 4 years ago

No, you can work on this.

kianenigma commented 4 years ago

Adding this to the NPoS project as it will (with a very high probability) be used by the OCW workers there.