From d675cee64afde28850703a1ab0c97e9911945a24 Mon Sep 17 00:00:00 2001 From: cheme Date: Tue, 8 Jun 2021 11:51:32 +0200 Subject: [PATCH] Compact proof. (#295) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 aa5c56850ef461ce3cb774213a92af4b1dd6cee5. * 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 * Remove encode_witness test function. * Update pallets/parachain-system/src/validate_block/implementation.rs * Fix compilation Co-authored-by: Bastian Köcher Co-authored-by: Bastian Köcher --- Cargo.lock | 8 ++++---- client/collator/src/lib.rs | 20 ++++++++++++++++--- client/consensus/aura/src/lib.rs | 3 ++- .../src/validate_block/implementation.rs | 16 ++++++++++----- .../src/validate_block/tests.rs | 12 +++++++---- primitives/core/src/lib.rs | 10 +++++----- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 89378ed8746..d8c17362d45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5920,9 +5920,9 @@ dependencies = [ [[package]] name = "parity-db" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "495197c078e54b8735181aa35c00a327f7f3a3cc00a1ee8c95926dd010f0ec6b" +checksum = "2e337f62db341435f0da05b8f6b97e984ef4ea5800510cd07c2d624688c40b47" dependencies = [ "blake2-rfc", "crc32fast", @@ -11641,9 +11641,9 @@ checksum = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41" [[package]] name = "trie-db" -version = "0.22.2" +version = "0.22.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cc176c377eb24d652c9c69c832c832019011b6106182bf84276c66b66d5c9a6" +checksum = "cd81fe0c8bc2b528a51c9d2c31dae4483367a26a723a3c9a4a8120311d7774e3" dependencies = [ "hash-db", "hashbrown", diff --git a/client/collator/src/lib.rs b/client/collator/src/lib.rs index 57d512d32ba..5c905b353f8 100644 --- a/client/collator/src/lib.rs +++ b/client/collator/src/lib.rs @@ -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::>( + 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::::new(header, extrinsics, candidate.proof); + let b = ParachainBlockData::::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::(Some(header.state_root())) + .unwrap().0 + .into_memory_db(); let backend = sp_state_machine::new_in_mem::().update_backend(*header.state_root(), db); diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 143ba64c698..638c08bec23 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -138,9 +138,10 @@ where P::Signature: TryFrom> + Hash + Member + Encode + Decode, { let worker = - sc_consensus_aura::build_aura_worker::(BuildAuraWorkerParams { + sc_consensus_aura::build_aura_worker::(BuildAuraWorkerParams { client: para_client, block_import: ParachainBlockImport::new(block_import), + justification_sync_link: (), proposer_factory, sync_oracle, force_authoring, diff --git a/pallets/parachain-system/src/validate_block/implementation.rs b/pallets/parachain-system/src/validate_block/implementation.rs index 1e935da0464..2caacb97869 100644 --- a/pallets/parachain-system/src/validate_block/implementation.rs +++ b/pallets/parachain-system/src/validate_block/implementation.rs @@ -65,11 +65,17 @@ pub fn validate_block, PSC: crate::Config>( "Invalid parent hash", ); - let db = storage_proof.into_memory_db(); - let root = parent_head.state_root().clone(); - if !HashDB::, _>::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::>, _, _>( + &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(); diff --git a/pallets/parachain-system/src/validate_block/tests.rs b/pallets/parachain-system/src/validate_block/tests.rs index def67f408cd..d2aa547b21d 100644 --- a/pallets/parachain-system/src/validate_block/tests.rs +++ b/pallets/parachain-system/src/validate_block/tests.rs @@ -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::<
::Hashing>(*parent_head.state_root()) + .unwrap(); + TestBlockData { block: built_block.block, - witness: built_block - .proof - .expect("We enabled proof recording before."), + witness, validation_data, } } diff --git a/primitives/core/src/lib.rs b/primitives/core/src/lib.rs index c97feea0a3a..09722c7ac70 100644 --- a/primitives/core/src/lib.rs +++ b/primitives/core/src/lib.rs @@ -198,7 +198,7 @@ pub struct ParachainBlockData { /// The extrinsics of the parachain block. extrinsics: sp_std::vec::Vec, /// 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 ParachainBlockData { @@ -206,7 +206,7 @@ impl ParachainBlockData { pub fn new( header: ::Header, extrinsics: sp_std::vec::Vec<::Extrinsic>, - storage_proof: sp_trie::StorageProof, + storage_proof: sp_trie::CompactProof, ) -> Self { Self { header, @@ -235,13 +235,13 @@ impl ParachainBlockData { &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, sp_trie::StorageProof) { + pub fn deconstruct(self) -> (B::Header, sp_std::vec::Vec, sp_trie::CompactProof) { (self.header, self.extrinsics, self.storage_proof) } }