Inform the PVF with the latest relevant relay chain state (#279)

* Update polkadot

* Extend cumulus primitives with some relay chain exports

Follow https://github.com/paritytech/polkadot/pull/2194 to see the
polkadot PR

* collator: collect the state proof

This commit changes cumulus-collator so that it takes the relay chain
state at the relay parent and creates a storage proof that contains all
the required data for PVF.

* parachain-upgrade: use the proofs instead

This change is needed to make cumulus logic to not longer depend on the
transient validation data. As part of this change, in order to preserve
the current behavior `code_upgrade_allowed` now is computed on the
parachain side, rather than provided by polkadot.

Turned out that this requires to know the self parachain id so it was
added where needed.

* message-broker: use relay state to track limits

this should make sending messages safe from accidentally running over
the relay chain limits that were previously unknown.

* Update polkadot

So that `relay_storage_root` is available through `ValidationParams`

* Check `relay_storage_root` matches expected

Check that `relay_storage_root` submitted by the collator matches the
one that we receive in `validate_block` through `ValidationParams`

* Add a missing check for `dmq_mqc_head` while we are at it

* Update polkadot

* Fix tests that use the relay storage root

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update message-broker/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Remove unneeded (&_)

* Fix unwraps

* Polish basti's suggestion

* Fix merge

* Bring back the System::can_set_code check

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Sergei Shulepov
2021-01-13 14:40:26 +01:00
committed by GitHub
parent af07b819d1
commit b424d0f5e1
19 changed files with 793 additions and 136 deletions
+1
View File
@@ -33,6 +33,7 @@ sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-executor = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" }
cumulus-test-relay-sproof-builder = { path = "../test/relay-sproof-builder" }
cumulus-test-client = { path = "../test/client" }
env_logger = "0.7.1"
@@ -215,6 +215,14 @@ impl<'a, B: BlockT> WitnessExt<'a, B> {
self.params.hrmp_mqc_heads,
validation_data.persisted.hrmp_mqc_heads
);
assert_eq!(
self.params.dmq_mqc_head,
validation_data.persisted.dmq_mqc_head,
);
assert_eq!(
self.params.relay_storage_root,
validation_data.persisted.relay_storage_root,
);
}
}
+43 -17
View File
@@ -22,6 +22,7 @@ use cumulus_test_client::{
transfer, Client, DefaultTestClientBuilderExt, InitBlockBuilder, LongestChain,
TestClientBuilder, TestClientBuilderExt,
};
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use parachain::primitives::{BlockData, HeadData, ValidationParams, ValidationResult};
use sc_executor::{
error::Result, sp_wasm_interface::HostFunctions, WasmExecutionMethod, WasmExecutor,
@@ -41,6 +42,7 @@ use codec::{Decode, Encode};
fn call_validate_block(
parent_head: Header,
block_data: ParachainBlockData<Block>,
relay_storage_root: Hash,
) -> Result<Header> {
let mut ext = TestExternalities::default();
let mut ext_ext = ext.ext();
@@ -48,9 +50,9 @@ fn call_validate_block(
block_data: BlockData(block_data.encode()),
parent_head: HeadData(parent_head.encode()),
relay_chain_height: 1,
relay_storage_root,
hrmp_mqc_heads: Vec::new(),
dmq_mqc_head: Default::default(),
relay_storage_root: Default::default(),
}
.encode();
@@ -82,11 +84,19 @@ fn create_test_client() -> (Client, LongestChain) {
.build_with_longest_chain()
}
fn build_block_with_proof(
struct TestBlockData {
block: Block,
witness: sp_trie::StorageProof,
relay_storage_root: Hash,
}
fn build_block_with_witness(
client: &Client,
extra_extrinsics: Vec<UncheckedExtrinsic>,
parent_head: Header,
) -> (Block, sp_trie::StorageProof) {
) -> TestBlockData {
let sproof_builder = RelayStateSproofBuilder::default();
let (relay_storage_root, _) = sproof_builder.clone().into_state_root_and_proof();
let block_id = BlockId::Hash(client.info().best_hash);
let mut builder = client.init_block_builder_at(
&block_id,
@@ -98,6 +108,7 @@ fn build_block_with_proof(
},
..Default::default()
}),
sproof_builder,
);
extra_extrinsics
@@ -106,12 +117,13 @@ fn build_block_with_proof(
let built_block = builder.build().expect("Creates block");
(
built_block.block,
built_block
TestBlockData {
block: built_block.block,
witness: built_block
.proof
.expect("We enabled proof recording before."),
)
relay_storage_root,
}
}
#[test]
@@ -120,12 +132,17 @@ fn validate_block_no_extra_extrinsics() {
let (client, longest_chain) = create_test_client();
let parent_head = longest_chain.best_chain().expect("Best block exists");
let (block, witness_data) = build_block_with_proof(&client, vec![], parent_head.clone());
let TestBlockData {
block,
witness,
relay_storage_root,
} = build_block_with_witness(&client, vec![], parent_head.clone());
let (header, extrinsics) = block.deconstruct();
let block_data = ParachainBlockData::new(header.clone(), extrinsics, witness_data);
let block_data = ParachainBlockData::new(header.clone(), extrinsics, witness);
let res_header = call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
let res_header = call_validate_block(parent_head, block_data, relay_storage_root)
.expect("Calls `validate_block`");
assert_eq!(header, res_header);
}
@@ -141,13 +158,17 @@ fn validate_block_with_extra_extrinsics() {
transfer(&client, Charlie, Alice, 500),
];
let (block, witness_data) =
build_block_with_proof(&client, extra_extrinsics, parent_head.clone());
let TestBlockData {
block,
witness,
relay_storage_root,
} = build_block_with_witness(&client, extra_extrinsics, parent_head.clone());
let (header, extrinsics) = block.deconstruct();
let block_data = ParachainBlockData::new(header.clone(), extrinsics, witness_data);
let block_data = ParachainBlockData::new(header.clone(), extrinsics, witness);
let res_header = call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
let res_header = call_validate_block(parent_head, block_data, relay_storage_root)
.expect("Calls `validate_block`");
assert_eq!(header, res_header);
}
@@ -158,10 +179,15 @@ fn validate_block_invalid_parent_hash() {
let (client, longest_chain) = create_test_client();
let parent_head = longest_chain.best_chain().expect("Best block exists");
let (block, witness_data) = build_block_with_proof(&client, vec![], parent_head.clone());
let TestBlockData {
block,
witness,
relay_storage_root,
} = build_block_with_witness(&client, vec![], parent_head.clone());
let (mut header, extrinsics) = block.deconstruct();
header.set_parent_hash(Hash::from_low_u64_be(1));
let block_data = ParachainBlockData::new(header, extrinsics, witness_data);
call_validate_block(parent_head, block_data).expect("Calls `validate_block`");
let block_data = ParachainBlockData::new(header, extrinsics, witness);
call_validate_block(parent_head, block_data, relay_storage_root)
.expect("Calls `validate_block`");
}