3

We are using offchain indexing to store some value that will be read in offchain worker .

The keys that we are using to store data via offchain indexing is as following:

key = some_prefix + current_block_number + unique_index_for_current_block

One of the problems with the mentioned key naming is that in case of forks for the same block number there is a possibility that some keys might get overwitten.

My question is as following:

Is it a safe to assume that in case of forks for offchain indexing writes, a storage will always get overwritten by the canonical block eventually?

in other words:

Is it safe to assume that the canonical block would be the last block (with the same block_number) that gets imported in case of a fork?

If the answer to those questions is yes then we should be fine since the data stored in offchain storage will eventually be correct for any block number (we will access them after the block got finalized) but if the answer is no then

Is there a unique value per fork that we can use for our offchain keys to make them fork-aware?

It looks like we don't have access to current block hash in extrinsics so we might need some other value.

1 Answer 1

1

Is it a safe to assume that in case of forks for offchain indexing writes, a storage will always get overwritten by the canonical block eventually?

Correct, off-chain indexing is called from on-chain extrinsics which implies that the data written locally is expected to be consistent across all nodes in the network. (ref)

I'm not sure (and am interested to) how you ensure the block is finalized of which the key is built to read local storage, but that would be a nice safeguard.

1
  • Thanks for the answer. So based on github.com/paritytech/substrate/issues/6742 to get the finalized block hash, we are calling local node chain_getFinalizedHead rpc from OCW and using frame_system::block_hash trying to match retrieved finalized block hash with a block number. It's hacky but gets the job done. If you know a better way of passing finalized block number/hash to OCW please let me know.
    – Aramik M
    Jan 11 at 18:15

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.