I want to read a single value from OffchainStorage of a live substrate node. Is it possible? Assume I can not use localStorageGet
rpc request because it is an unsafe rpc.
2 Answers
You can access the data stored in the OffchainStorage using offchain worker threads that have read and write access or through the on-chain logic using offchain indexing.
Read more about Offchain Indexing in the docs.
Offchain indexing allows the runtime to write directly to the offchain storage without using offchain worker threads. You can enable this functionality to persist data by starting a Substrate node with the --enable-offchain-indexing command-line option.
And take a look at this example substrate-offchain-worker-demo.
Before start working with Offchain Workers, I'd recommend read this post in the Polkadot Forum Offchain Workers: Design Assumptions & Vulnerabilities, to understand better possibles vulnerabilities.
-
Thank you. Can you please see my comment under Xavier's answer. I don't get how to use indexing for reading values. Basically I have a key and I want to read corresponding value through some arbitrary interaction with a node @AlexBean Apr 19 at 18:31
I think that's impossible.
offchain-indexing
is an option. The node can access the offchain storage only when it enabled this feature. Not every node enables that.
Also, you might notice that there is also a localStorageSet
call, anybody can set the storage if you public this call.
Moreover, the node needs to build the offchain DB from the importing logic from block #0. Every node maintains its own offchain DB. The offchain DB can not sync from other nodes.
So, I think that's why it was marked as unsafe.
-
Thank you. Sorry but how is
offchain-indexing
an option? I thought its write-only. Let's say target node has offchain indexing enabled, how do I go about reading some offchain value externally from my machine/app/service? using indexing @AurevoirXavier Apr 19 at 17:47 -
I suggest you maintain one and enable the unsafe rpc. Otherwise, I suggest you use subquerry instead of offchain worker. Last year my team switch to subquery from offchain worker. Because there are a lot of issues while interacting with offchain DB. Apr 19 at 17:56