Search code examples
nearprotocol

Off-Chain Worker Framework


I haven’t entirely given up on the idea of validators moonlighting as oracles for off-chain computation…based on this extensive discussion: https://gov.near.org/t/off-chain-computation-framework/1400/6

So far from studying Sputnik’s code, I have figured out the mechanics of how to upload a blob to a smart contract. Let's say that a blob represents a storage-less contract, having only stateless functions that act only on input to the function, and return those inputs modified.

Now I’m missing the piece of how Validators can download and execute the blob. As mentioned by Ilya in the link above, the NearSDK would be able to interpret the blob (if the blob is essentially a compiled contract), but it needs to be a modified version of the SDK...

Think of this like sandbox mode…blob cannot modify state of any other contract, but can read state (forget about the internet access part for now). Results of the blob execution are then fed back to a smart contract, where they have to match the results of every other validator who executed the blob. This can be done by hash comparison (rather than looping through the results individually), so it’s not an expensive comparison, especially because it’s all or nothing.

Question: how can a Validator download the blob and execute it via a sandboxed SDK, and post the result via the regular SDK to the blockchain? I am missing a lot of architectural context…and this is bringing me to the edge of giving on the idea. Please help prevent that from happening!


Solution

  • If you are implementing this as a separate binary, your binary will be doing next things:

    1. Use RPC to load the WASM file from the blockchain. See RPC reference
    2. Use runtime-standalone to run this WASM with specific inputs. An example of using runtime standalone is here, but you will need to customize this with few things.
    3. The result should be sent as a transaction signed by this binary again via RPC.

    If you want these WASM files to have access to state, you will need to load state inside this binary. There are two options:

    1. Modify a nearcore node to also do the above items
    2. Run nearcore in parallel, and open the database on read when you are initializing Trie (e.g. here load from disk instead).

    If you want to add more host functions (like accessing internet), you will need to fork runtime-standalone to expose those functions.