allow try-runtime and TestExternalities to report PoV size (#10372)

* allow try-runtime and test-externalities to report proof size

* self review

* fix test

* Fix humanized dispaly of bytes

* Fix some test

* Fix some review grumbles

* last of the review comments

* fmt

* remove unused import

* move test

* fix import

* Update primitives/state-machine/src/testing.rs

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

* last touches

* fix

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Kian Paimani
2021-12-04 07:11:25 +01:00
committed by GitHub
parent b2f1374487
commit 4775f11edc
15 changed files with 699 additions and 128 deletions
+47
View File
@@ -916,9 +916,13 @@ impl<R> TransactionOutcome<R> {
#[cfg(test)]
mod tests {
use crate::traits::BlakeTwo256;
use super::*;
use codec::{Decode, Encode};
use sp_core::crypto::Pair;
use sp_io::TestExternalities;
use sp_state_machine::create_proof_check_backend;
#[test]
fn opaque_extrinsic_serialization() {
@@ -1019,4 +1023,47 @@ mod tests {
panic!("Hey, I'm an error");
});
}
#[test]
fn execute_and_generate_proof_works() {
use codec::Encode;
use sp_state_machine::Backend;
let mut ext = TestExternalities::default();
ext.insert(b"a".to_vec(), vec![1u8; 33]);
ext.insert(b"b".to_vec(), vec![2u8; 33]);
ext.insert(b"c".to_vec(), vec![3u8; 33]);
ext.insert(b"d".to_vec(), vec![4u8; 33]);
let pre_root = ext.backend.root().clone();
let (_, proof) = ext.execute_and_prove(|| {
sp_io::storage::get(b"a");
sp_io::storage::get(b"b");
sp_io::storage::get(b"v");
sp_io::storage::get(b"d");
});
let compact_proof = proof.clone().into_compact_proof::<BlakeTwo256>(pre_root).unwrap();
let compressed_proof = zstd::stream::encode_all(&compact_proof.encode()[..], 0).unwrap();
// just an example of how you'd inspect the size of the proof.
println!("proof size: {:?}", proof.encoded_size());
println!("compact proof size: {:?}", compact_proof.encoded_size());
println!("zstd-compressed compact proof size: {:?}", &compressed_proof.len());
// create a new trie-backed from the proof and make sure it contains everything
let proof_check = create_proof_check_backend::<BlakeTwo256>(pre_root, proof).unwrap();
assert_eq!(proof_check.storage(b"a",).unwrap().unwrap(), vec![1u8; 33]);
let _ = ext.execute_and_prove(|| {
sp_io::storage::set(b"a", &vec![1u8; 44]);
});
// ensure that these changes are propagated to the backend.
ext.execute_with(|| {
assert_eq!(sp_io::storage::get(b"a").unwrap(), vec![1u8; 44]);
assert_eq!(sp_io::storage::get(b"b").unwrap(), vec![2u8; 33]);
});
}
}