paritytech / substrate

Substrate: The platform for blockchain innovators
Apache License 2.0
8.39k stars 2.65k forks source link

Allow off-chain worker to know what's the best finalized block #6742

Open stiiifff opened 4 years ago

stiiifff commented 4 years ago

To support some use cases (e.g. integration w/ external systems using the OCW HTTP API), it would be useful for an off-chain worker to know what's the best finalized block to be able to perform some processing only up to that block. This information could be accessed either through externalities, or passed as parameter (like the current block).

tomusdrw commented 3 years ago

Since finality information is not really on-chain, we can't pass it to the OCW during it's execution.

That leaves us with an off-chain specific host function (see sp_io::offchain::*).

However, getting finality information is just part of the story, next might be access to raw, historical block data, etc, which may end up adding more and more functions.

I think OCWs should simply have access to the same information that is available over local RPC (in theory they can even make requests to localhost:9933 and hope the JSON-RPC server is running), hence I think it would be best to make this access explicit and expose a single host function which would be handled by the RPC server directly, something like this:

sp_io::offchain::rpc_query(bytes: &str/&[u8]) -> Vec<u8>

and OCW would simply use it by constructing a regular RPC request and getting a response (see also RpcHandlers::rpc_query in client/service/src/lib.rs).