Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Introduce capabilities filtering for off-chain runtime calls.#3454

Merged
bkchr merged 10 commits intomasterfrom
td-offchain-context
Aug 27, 2019
Merged

Introduce capabilities filtering for off-chain runtime calls.#3454
bkchr merged 10 commits intomasterfrom
td-offchain-context

Conversation

@tomusdrw
Copy link
Contributor

If we call into the runtime, we can pass various ExecutionContexts to identify where is it coming from.
Initially this was only affecting how we run the call (a.k.a execution strategy = native / wasm), with introduction of OffchainWorkers this also controls the APIs that are available for that part of the runtime.

Recently in #3296 we've introduced additional keystore APIs that must not be available for consensus-critical code, but are very useful for off-chain calls, like rotating the keys - they are only valid in ExecutionContext::Other.

This PR introduces a possibility to further control what APIs (Capabilties) are available for particular off-chain call and tries to make it more explicit. This will come handy to implement equivocation reporting for Grandpa/Babe, since we will be able to call into the runtime, produce, sign and submit transaction to the pool in one call. In the future we even want the off-chain call to be able to read (And possibly) write the offchain worker DB, for instance to retrieve the merkle proofs for session membership.

@tomusdrw tomusdrw added A0-please_review Pull request needs code review. M4-core labels Aug 21, 2019
@tomusdrw tomusdrw requested review from bkchr and rphmeier August 21, 2019 14:31
@tomusdrw tomusdrw changed the title Introduce capabilities filtering for offchain calls. Introduce capabilities filtering for off-chain calls. Aug 21, 2019
@tomusdrw tomusdrw changed the title Introduce capabilities filtering for off-chain calls. Introduce capabilities filtering for off-chain runtime calls. Aug 21, 2019
///
/// This should include all capabilities of `offchain::Externalities`.
OffchainWorker(Box<dyn offchain::Externalities>),
/// Context used for other calls.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all of these exclusive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

let crate_ = self.crate_;
let context_other = quote!( #crate_::runtime_api::ExecutionContext::Other );
let context_other = quote!( #crate_::runtime_api::ExecutionContext::OffchainCall );
let fn_impl = self.create_method_runtime_api_impl(method.clone());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename context_other.

Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general looking good, but I really think that we should panic when something tries to access a function without the required capabilities. Otherwise I see us at some point debugging a problem that will have its root cause in such a function.

@tomusdrw
Copy link
Contributor Author

Otherwise I see us at some point debugging a problem that will have its root cause in such a function.

That's why we print a warning if such function is being used. I'm really scared of panicking in the code that:

  1. Is very mixed up with regular, consensus-critical runtime code (it's just RuntimeApi)
  2. Is being called very frequently by the node
  3. Most likely will not run in wasm sandbox, but as native code.

Any mistake in a code like this will cause the entire network to brick forever.

@bkchr
Copy link
Member

bkchr commented Aug 22, 2019

For that exists testing, if you deploy code that panics it is your own fault. You should not use these functions from within the chain.
If you print a warning, no one will see it or just ignore it. We panic anyway when no offchain context is set, so I don't see the difference.

@tomusdrw
Copy link
Contributor Author

tomusdrw commented Aug 22, 2019

If you print a warning, no one will see it or just ignore it. We panic anyway when no offchain context is set, so I don't see the difference.

Offchain workers are much better separate from other parts of the runtime code IMHO (it's one special function that is being called). Regular RuntimeApis are usually a code that is shared with consensus-crticial code.

For that exists testing, if you deploy code that panics it is your own fault. You should not use these functions from within the chain.

IMHO we've experienced multiple times that testing is never enough, and just saying: "It's your fault to brick a $1M network" is a lame excuse to introduce security measures to prevent that.

That said, I'm fine to bend to pressure as I'm also torn apart between having a clean code / easy debugging experience and catering for possible future (possibly unlikely) mistakes. Will update the code shortly.

@bkchr
Copy link
Member

bkchr commented Aug 22, 2019

IMHO we've experienced multiple times that testing is never enough, and just saying: "It's your fault to brick a $1M network" is a lame excuse to introduce security measures to prevent that.

That is correct, but the code will fail anyway when you try to call it, as the offchain context is not set.

@bkchr
Copy link
Member

bkchr commented Aug 22, 2019

We can also return a Result and fail in sr-io, as we do it for the other stuff as well.

@bkchr bkchr merged commit 3f6cbc8 into master Aug 27, 2019
@bkchr bkchr deleted the td-offchain-context branch August 27, 2019 08:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

A0-please_review Pull request needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants