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

Conversation

@bkchr
Copy link
Member

@bkchr bkchr commented Feb 25, 2020

This pr changes the behavior of automatically including :code in storage proofs. Before this pr, the :code was extracted by the wasm-executor before executing a function in the runtime. Now, the runtime code is passed as a parameter from the outside. This makes it possible to extract the :code from the backend, before the storage recorder tracks all accesses to the backend (which resulted in inclusion of :code in the proof).

However, the execution_proof function of Client still adds the :code to the proof, this is required by the light client, until #5047 is implemented.

Closes: paritytech/cumulus#8

@bkchr bkchr added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes labels Feb 25, 2020
@bkchr bkchr requested a review from svyatonik February 25, 2020 20:45
// backwards compatible.
//
// TODO: Remove when solved: https://github.com/paritytech/substrate/issues/5047
let code_proof = self.read_proof(
Copy link
Member Author

Choose a reason for hiding this comment

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

@svyatonik here I'm currently forcing that the :code is still added to the proof.

assert!(
backend.storage(&sp_core::storage::well_known_keys::CODE)
.unwrap_err()
.contains("Database missing expected key"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably just leave .unwrap_err() - comparing strings seems a bit fragile?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I wanted to match on the error, however it is a string already. The full error message is much longer, but this part is the important one (as long as no one changes the display impl for the error, it should be fine).

Copy link
Contributor

@pepyakin pepyakin left a comment

Choose a reason for hiding this comment

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

Looks great! A couple of nits

where H::Out: Encode,
{
let code = backend.storage(well_known_keys::CODE)
.ok()
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this overindented?

let code = backend.storage(well_known_keys::CODE)
.ok()
.flatten()
.ok_or_else(|| "`:code` not found")?;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: .ok_or can work here as well

Suggested change
.ok_or_else(|| "`:code` not found")?;
.ok_or("`:code` not found")?;

@bkchr bkchr added this to the 2.0 milestone Mar 4, 2020
@bkchr bkchr added A8-looksgood and removed A0-please_review Pull request needs code review. labels Mar 4, 2020
@gnunicorn gnunicorn merged commit a193a19 into master Mar 4, 2020
@gnunicorn gnunicorn deleted the bkchr-dont-include-runtime-everytime branch March 4, 2020 19:26
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Mar 4, 2020
* Adds test to verify that the runtime currently is always contained in
the proof

* Start passing the runtime wasm code from the outside

* Fix compilation

* More build fixes

* Make the test work as expected now :)

* Last fixes

* Fixes benchmarks

* Review feedback

* Apply suggestions from code review

Co-Authored-By: Sergei Pepyakin <[email protected]>

* Review feedback

* Fix compilation

Co-authored-by: Sergei Pepyakin <[email protected]>
@arkpar
Copy link
Member

arkpar commented Mar 4, 2020

So now to make a call one is required to query code from storage and keep it in memory to avoid re-querying it each time? This just wastes memory. Why not just add filter to the proof recorder?

@arkpar
Copy link
Member

arkpar commented Mar 4, 2020

This pr changes the behavior of automatically including :code in storage proofs. Before this pr, the :code was extracted by the wasm-executor before executing a function in the runtime.

That's not true. It was only extracted if the hash does not match the available instance. When there's matching version of compiled code or wasmi instance we don't need to keep original WASM bytecode in memory.

@bkchr
Copy link
Member Author

bkchr commented Mar 4, 2020

This pr changes the behavior of automatically including :code in storage proofs. Before this pr, the :code was extracted by the wasm-executor before executing a function in the runtime.

That's not true. It was only extracted if the hash does not match the available instance. When there's matching version of compiled code or wasmi instance we don't need to keep original WASM bytecode in memory.

Yeah okay, after checking again the code, I found the code that caches the storage_hash. I thought that this wasn't done. However, given this code, the :code is also cached.

I don't think that this wastes any more memory than the implementation before. We don't cache the code anywhere else as before.

Why not just add filter to the proof recorder?

This doesn't work, as you also can have legit accesses from the runtime to :code and that requires that the proof recorder records them. From my point of view neither the state machine nor the executor should care where the runtime code comes from. It is just instructed to execute the given code and should not be responsible for fetching it from the backend.

@arkpar
Copy link
Member

arkpar commented Mar 5, 2020

This doesn't work, as you also can have legit accesses from the runtime to :code and that requires that the proof recorder records them.

Does it? Whoever verifies the proof has the code anyway and they can just return it when it is queried from the proof storage. Is that right @svyatonik?

From my point of view neither the state machine nor the executor should care where the runtime code comes from. It is just instructed to execute the given code and should not be responsible for fetching it from the backend.

Needlessly fetching the code on each call is expensive. The executor decides when it actually needs the code and should use either externalities or some other interface to fetch it only when needed

@svyatonik
Copy link
Contributor

Does it? Whoever verifies the proof has the code anyway and they can just return it when it is queried from the proof storage. Is that right @svyatonik?

No - there's no code on light client and that's why tthe ":code" was a part of execution proof since very beginning. There's an issue to add code caching #5047 , mentioned in the PR description.

@arkpar
Copy link
Member

arkpar commented Mar 5, 2020

No - there's no code on light client and that's why tthe ":code" was a part of execution proof since very beginning. There's an issue to add code caching #5047 , mentioned in the PR description.

I'm asking about parachain validation here. The proof is useless without the code. So if it is excluded from the proof whoever is verifying it must have the code from some other source already. That also means when you have "legit accesses from the runtime to :code" they can just use the code they already have and there's no need to include it in the proof. Is this technically correct?

@bkchr
Copy link
Member Author

bkchr commented Mar 5, 2020

For parachain validation, the runtime code is part of the validation blob that is stored on the relay chain. So, we don't need :code to be part of the proof.

bkchr added a commit that referenced this pull request Mar 5, 2020
arkpar added a commit that referenced this pull request Mar 5, 2020
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Mar 6, 2020
bkchr pushed a commit that referenced this pull request Mar 7, 2020
* Adds test to verify that the runtime currently is always contained in
the proof

* Start passing the runtime wasm code from the outside

* Fix compilation

* More build fixes

* Make the test work as expected now :)

* Last fixes

* Fixes benchmarks

* Review feedback

* Apply suggestions from code review

Co-Authored-By: Sergei Pepyakin <[email protected]>

* Review feedback

* Fix compilation

Co-authored-by: Sergei Pepyakin <[email protected]>
gavofyork pushed a commit that referenced this pull request Mar 10, 2020
* Don't include `:code` by default in storage proofs (#5060)

* Adds test to verify that the runtime currently is always contained in
the proof

* Start passing the runtime wasm code from the outside

* Fix compilation

* More build fixes

* Make the test work as expected now :)

* Last fixes

* Fixes benchmarks

* Review feedback

* Apply suggestions from code review

Co-Authored-By: Sergei Pepyakin <[email protected]>

* Review feedback

* Fix compilation

Co-authored-by: Sergei Pepyakin <[email protected]>

* Fix compilation and change the way `RuntimeCode` works

* Fix tests

* Switch to `Cow`

Co-authored-by: Benjamin Kampmann <[email protected]>
Co-authored-by: Sergei Pepyakin <[email protected]>
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Mar 12, 2020
* Don't include `:code` by default in storage proofs (paritytech#5060)

* Adds test to verify that the runtime currently is always contained in
the proof

* Start passing the runtime wasm code from the outside

* Fix compilation

* More build fixes

* Make the test work as expected now :)

* Last fixes

* Fixes benchmarks

* Review feedback

* Apply suggestions from code review

Co-Authored-By: Sergei Pepyakin <[email protected]>

* Review feedback

* Fix compilation

Co-authored-by: Sergei Pepyakin <[email protected]>

* Fix compilation and change the way `RuntimeCode` works

* Fix tests

* Switch to `Cow`

Co-authored-by: Benjamin Kampmann <[email protected]>
Co-authored-by: Sergei Pepyakin <[email protected]>
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Mar 17, 2020
* Adds test to verify that the runtime currently is always contained in
the proof

* Start passing the runtime wasm code from the outside

* Fix compilation

* More build fixes

* Make the test work as expected now :)

* Last fixes

* Fixes benchmarks

* Review feedback

* Apply suggestions from code review

Co-Authored-By: Sergei Pepyakin <[email protected]>

* Review feedback

* Fix compilation

Co-authored-by: Sergei Pepyakin <[email protected]>
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Mar 17, 2020
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Mar 17, 2020
* Don't include `:code` by default in storage proofs (paritytech#5060)

* Adds test to verify that the runtime currently is always contained in
the proof

* Start passing the runtime wasm code from the outside

* Fix compilation

* More build fixes

* Make the test work as expected now :)

* Last fixes

* Fixes benchmarks

* Review feedback

* Apply suggestions from code review

Co-Authored-By: Sergei Pepyakin <[email protected]>

* Review feedback

* Fix compilation

Co-authored-by: Sergei Pepyakin <[email protected]>

* Fix compilation and change the way `RuntimeCode` works

* Fix tests

* Switch to `Cow`

Co-authored-by: Benjamin Kampmann <[email protected]>
Co-authored-by: Sergei Pepyakin <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

B0-silent Changes should not be mentioned in any release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CODE is part of the proof

6 participants