Compact proof. (#295)

* compact, need to be made optional and look into/compress child trie
roots/state.

* proto with child trie support

* Missing set_offchain_storage overload.

* right name

* Ignore offchain indexing in validation function.

* patch trie-db

* decompress from iter

* use compressed proof

* remove wasm blob from proof (no inject plugged yet)

* change lock

* update trie

* change in toml

* Revert "change in toml"

This reverts commit e588583b79e1a64e2a54157b40968aa6b586fbbe.

* use patch to branches

* i

* i:wq

* switch branch

* ii

* ok, needed to patch the runtime by putting substrate patch in polkadot
project.

* test passing with this conf

* actual lazy code fetch

* patch issue

* Code reorg

* restore commented tests.

* update deps.

* remove polka patch

* fixes

* remove patch

* revert cargo.lock

* cargo update -p sp-trie polkadot-service

* fix collator test (using parent state root).

* Update pallets/parachain-system/src/validate_block/implementation.rs

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

* Remove encode_witness test function.

* Update pallets/parachain-system/src/validate_block/implementation.rs

* Fix compilation

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Bastian Köcher <info@kchr.de>
This commit is contained in:
cheme
2021-06-08 11:51:32 +02:00
committed by GitHub
parent acb2bc4f00
commit e8b4b36446
6 changed files with 274 additions and 249 deletions
+231 -231
View File
File diff suppressed because it is too large Load Diff
+17 -3
View File
@@ -25,7 +25,7 @@ use sp_consensus::BlockStatus;
use sp_core::traits::SpawnNamed;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT, Zero},
traits::{Block as BlockT, Header as HeaderT, Zero, HashFor},
};
use cumulus_client_consensus_common::ParachainConsensus;
@@ -225,8 +225,19 @@ where
let (header, extrinsics) = candidate.block.deconstruct();
let compact_proof = match candidate.proof.into_compact_proof::<HashFor<Block>>(
last_head.state_root().clone(),
) {
Ok(proof) => proof,
Err(e) => {
tracing::error!(target: "cumulus-collator", "Failed to compact proof: {:?}", e);
return None;
}
};
// Create the parachain block data for the validators.
let b = ParachainBlockData::<Block>::new(header, extrinsics, candidate.proof);
let b = ParachainBlockData::<Block>::new(header, extrinsics, compact_proof);
tracing::debug!(
target: LOG_TARGET,
@@ -440,7 +451,10 @@ mod tests {
assert_eq!(1, *block.header().number());
// Ensure that we did not include `:code` in the proof.
let db = block.storage_proof().clone().into_memory_db();
let db = block.storage_proof()
.to_storage_proof::<BlakeTwo256>(Some(header.state_root()))
.unwrap().0
.into_memory_db();
let backend =
sp_state_machine::new_in_mem::<BlakeTwo256>().update_backend(*header.state_root(), db);
+2 -1
View File
@@ -138,9 +138,10 @@ where
P::Signature: TryFrom<Vec<u8>> + Hash + Member + Encode + Decode,
{
let worker =
sc_consensus_aura::build_aura_worker::<P, _, _, _, _, _, _, _>(BuildAuraWorkerParams {
sc_consensus_aura::build_aura_worker::<P, _, _, _, _, _, _, _, _>(BuildAuraWorkerParams {
client: para_client,
block_import: ParachainBlockImport::new(block_import),
justification_sync_link: (),
proposer_factory,
sync_oracle,
force_authoring,
@@ -65,11 +65,17 @@ pub fn validate_block<B: BlockT, E: ExecuteBlock<B>, PSC: crate::Config>(
"Invalid parent hash",
);
let db = storage_proof.into_memory_db();
let root = parent_head.state_root().clone();
if !HashDB::<HashFor<B>, _>::contains(&db, &root, EMPTY_PREFIX) {
panic!("Witness data does not contain given storage root.");
}
// Uncompress
let mut db = MemoryDB::default();
let root = match sp_trie::decode_compact::<sp_trie::Layout<HashFor<B>>, _, _>(
&mut db,
storage_proof.iter_compact_encoded_nodes(),
Some(parent_head.state_root()),
) {
Ok(root) => root,
Err(_) => panic!("Compact proof decoding failure."),
};
let backend = sp_state_machine::TrieBackend::new(db, root);
let mut overlay = sp_state_machine::OverlayedChanges::default();
let mut cache = Default::default();
@@ -86,7 +86,7 @@ fn create_test_client() -> (Client, LongestChain) {
struct TestBlockData {
block: Block,
witness: sp_trie::StorageProof,
witness: sp_trie::CompactProof,
validation_data: PersistedValidationData,
}
@@ -114,11 +114,15 @@ fn build_block_with_witness(
let built_block = builder.build().expect("Creates block");
let witness = built_block
.proof
.expect("We enabled proof recording before.")
.into_compact_proof::<<Header as HeaderT>::Hashing>(*parent_head.state_root())
.unwrap();
TestBlockData {
block: built_block.block,
witness: built_block
.proof
.expect("We enabled proof recording before."),
witness,
validation_data,
}
}
+5 -5
View File
@@ -198,7 +198,7 @@ pub struct ParachainBlockData<B: BlockT> {
/// The extrinsics of the parachain block.
extrinsics: sp_std::vec::Vec<B::Extrinsic>,
/// The data that is required to emulate the storage accesses executed by all extrinsics.
storage_proof: sp_trie::StorageProof,
storage_proof: sp_trie::CompactProof,
}
impl<B: BlockT> ParachainBlockData<B> {
@@ -206,7 +206,7 @@ impl<B: BlockT> ParachainBlockData<B> {
pub fn new(
header: <B as BlockT>::Header,
extrinsics: sp_std::vec::Vec<<B as BlockT>::Extrinsic>,
storage_proof: sp_trie::StorageProof,
storage_proof: sp_trie::CompactProof,
) -> Self {
Self {
header,
@@ -235,13 +235,13 @@ impl<B: BlockT> ParachainBlockData<B> {
&self.extrinsics
}
/// Returns the [`StorageProof`](sp_trie::StorageProof).
pub fn storage_proof(&self) -> &sp_trie::StorageProof {
/// Returns the [`CompactProof`](sp_trie::CompactProof).
pub fn storage_proof(&self) -> &sp_trie::CompactProof {
&self.storage_proof
}
/// Deconstruct into the inner parts.
pub fn deconstruct(self) -> (B::Header, sp_std::vec::Vec<B::Extrinsic>, sp_trie::StorageProof) {
pub fn deconstruct(self) -> (B::Header, sp_std::vec::Vec<B::Extrinsic>, sp_trie::CompactProof) {
(self.header, self.extrinsics, self.storage_proof)
}
}