5

We are running 2 OCWs each in separate instances and we have an extrinsic which mutates a storage in runtime which is done twice from the 2 OCWs but we want only one OCW to sign. How to do that? For example, if we want to increase a value in storage by +1 each hour, it is doing +2 since 2 OCWs are running. How to solve this?

2 Answers 2

6

It sounds to me that you are using offchain workers incorrectly. They should not be thought of as trusted applications which can modify your runtime's state or behave in expected ways. In fact, you should expect that every malicious user in your network is running their own custom version of your offchain worker and abusing it in every way possible.

Thus, the interface between your offchain worker and your runtime, usually an extrinsic, should have the appropriate checks to ensure that any offchain worker call is appropriate. In the case of updating a value every hour, probably you should keep track of the timestamp / block number where the last update happened, and check that the next update does not happen sooner than you expect.

2

So the idea of OCW's is that it will do some off-chain "work" and ultimately submit a transaction that will call a public function in the runtime to get the result of that work on-chain.

OCW's do have direct access to read runtime storage.

OCW's also have a separate storage of their own which can be used as a basis to "strategize" multiple OCW threads running at the same time.

You may have two nodes with the CLI flag opted in for OCW's, that does not mean you need both OCW's doing work after every block import. You can have logic that if certain conditions are met, only then execute the OCW logic. You can use a combination of listening to events, checking OCW local storage for some value, checking if a threshold was met, etc.

Here are a couple examples:

Also keep in mind:

It's not guaranteed for offchain workers to run on EVERY block, there might be cases where some blocks are skipped, or for some the worker runs twice (re-orgs), so the code should be able to handle that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.