6

It's stated that fn offchain_worker function is called every time by all nodes after a block import. Imagine the case where in fn offchain_worker we make a http call to fetch some not deterministic value from a remote server, and once we get the result we call to pub fn onchain_callback to sign a transaction to include that result in the blockchain state.

If Off-chain workers are executed by all validators after each block import, would I end up with one new signed transaction per validator with different results (remember is not deterministic).

Example. My Off-chain worker fetch a random number from a remote server and callback the result signing a new transaction. If I have in my network 10 validators... questions:

1.- I would end up with 10 new transactions with different random numbers?

2.- It would be executed only by the validators or also by all the full nodes connected to the blockchain?

3.- Is it possible to trigger the Off-chain workers only when a certain extrinsic is included in the block, instead of after every block import?

Dan Forbes
  • 2,734
  • 3
  • 30
  • 60

1 Answers1

4
  1. Yes, if the validators run with default off-chain workers settings. If this is not desired, your OCW can pick a validator or introduce a random delay & extra conditions between different runs. We do that for im-online pallet in substrate repo or offchain phragmen elections.
  2. Other nodes can opt-in with a CLI flag (and most likely extra keys to sign transactions), but you can also put a guard in your OCW code to run only in case sp_io::offchain::is_validator() == true
  3. That has to be done manually for now - off-chain worker has full state access so it can inspect the Events in frame_system and only run in case some specific one is there. I believe there are some examples in substrate-recipies repo fo that.

More information here: Role of off-chain workers

NachoPal
  • 196
  • 4