git.delta.rocks / unique-network / refs/commits / be09f662cdcf

difftreelog

feat retrieve key values from proof trie

Daniel Shiposha2022-11-24parent: #254f58f.patch.diff
in: master

4 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -12847,6 +12847,8 @@
  "sp-rpc",
  "sp-runtime",
  "sp-state-machine",
+ "sp-trie",
+ "trie-db",
  "unique-runtime",
  "up-common",
  "up-data-structs",
modifiedclient/rpc/Cargo.tomldiffbeforeafterboth
before · client/rpc/Cargo.toml
1[package]2name = "uc-rpc"3version = "0.1.4"4license = "GPLv3"5edition = "2021"67[dependencies]8pallet-common = { default-features = false, path = '../../pallets/common' }9up-data-structs = { default-features = false, path = '../../primitives/data-structs' }10up-common = { default-features = false, path = '../../primitives/common' }11up-rpc = { path = "../../primitives/rpc" }12app-promotion-rpc = { path = "../../primitives/app_promotion_rpc" }13rmrk-rpc = { path = "../../primitives/rmrk-rpc" }14up-pov-estimate-rpc = { path = "../../primitives/pov-estimate-rpc", optional = true }15codec = { package = "parity-scale-codec", version = "3.1.2" }16jsonrpsee = { version = "0.16.2", features = ["server", "macros"] }17anyhow = "1.0.57"18zstd = { version = "0.11.2", default-features = false }1920sc-rpc-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }21sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }22sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }23sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }24sp-externalities = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }25sp-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }26sp-blockchain = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }27sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }28sp-keystore = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }29sp-rpc = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }30sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }31pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.36" }3233frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }3435sc-executor = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }3637unique-runtime = { path = '../../runtime/unique', optional = true }38quartz-runtime = { path = '../../runtime/quartz', optional = true }39opal-runtime = { path = '../../runtime/opal' }4041[features]42pov-estimate = [43    'up-pov-estimate-rpc',44    'unique-runtime?/pov-estimate',45    'quartz-runtime?/pov-estimate',46    'opal-runtime/pov-estimate',47]
after · client/rpc/Cargo.toml
1[package]2name = "uc-rpc"3version = "0.1.4"4license = "GPLv3"5edition = "2021"67[dependencies]8pallet-common = { default-features = false, path = '../../pallets/common' }9up-data-structs = { default-features = false, path = '../../primitives/data-structs' }10up-common = { default-features = false, path = '../../primitives/common' }11up-rpc = { path = "../../primitives/rpc" }12app-promotion-rpc = { path = "../../primitives/app_promotion_rpc" }13rmrk-rpc = { path = "../../primitives/rmrk-rpc" }14up-pov-estimate-rpc = { path = "../../primitives/pov-estimate-rpc", optional = true }15codec = { package = "parity-scale-codec", version = "3.1.2" }16jsonrpsee = { version = "0.16.2", features = ["server", "macros"] }17anyhow = "1.0.57"18zstd = { version = "0.11.2", default-features = false }19trie-db = { version = "0.24.0", default-features = false }2021sc-rpc-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }22sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }23sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }24sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }25sp-externalities = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }26sp-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }27sp-blockchain = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }28sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }29sp-keystore = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }30sp-rpc = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }31sp-trie = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }32sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }33pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.36" }3435frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }3637sc-executor = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }3839unique-runtime = { path = '../../runtime/unique', optional = true }40quartz-runtime = { path = '../../runtime/quartz', optional = true }41opal-runtime = { path = '../../runtime/opal' }4243[features]44pov-estimate = [45    'up-pov-estimate-rpc',46    'unique-runtime?/pov-estimate',47    'quartz-runtime?/pov-estimate',48    'opal-runtime/pov-estimate',49]
modifiedclient/rpc/src/pov_estimate.rsdiffbeforeafterboth
--- a/client/rpc/src/pov_estimate.rs
+++ b/client/rpc/src/pov_estimate.rs
@@ -24,6 +24,7 @@
 
 use sc_service::{NativeExecutionDispatch, config::ExecutionStrategy};
 use sp_state_machine::{StateMachine, TrieBackendBuilder};
+use trie_db::{Trie, TrieDBBuilder};
 
 use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc};
 use anyhow::anyhow;
@@ -47,7 +48,7 @@
 
 use sp_runtime::traits::Header;
 
-use up_pov_estimate_rpc::PovInfo;
+use up_pov_estimate_rpc::{PovInfo, TrieKeyValue};
 
 use crate::define_struct_for_server_api;
 
@@ -244,10 +245,31 @@
 		results.push(xt_result);
 	}
 
+	let root = proving_backend.root().clone();
+
 	let proof = proving_backend
 		.extract_proof()
 		.expect("A recorder was set and thus, a storage proof can be extracted; qed");
 	let proof_size = proof.encoded_size();
+
+	let memory_db = proof.clone().into_memory_db();
+
+	let tree_db =
+		TrieDBBuilder::<sp_trie::LayoutV1<HasherOf<Block>>>::new(&memory_db, &root).build();
+
+	let key_values = tree_db
+		.iter()
+		.map_err(|e| anyhow!("failed to retrieve tree db key values: {:?}", e))?
+		.filter_map(|item| {
+			let item = item.ok()?;
+
+			Some(TrieKeyValue {
+				key: item.0,
+				value: item.1,
+			})
+		})
+		.collect();
+
 	let compact_proof = proof
 		.clone()
 		.into_compact_proof::<HasherOf<Block>>(pre_root)
@@ -263,5 +285,6 @@
 		compact_proof_size: compact_proof_size as u64,
 		compressed_proof_size: compressed_proof_size as u64,
 		results,
+		key_values,
 	})
 }
modifiedprimitives/pov-estimate-rpc/src/lib.rsdiffbeforeafterboth
--- a/primitives/pov-estimate-rpc/src/lib.rs
+++ b/primitives/pov-estimate-rpc/src/lib.rs
@@ -32,6 +32,14 @@
 	pub compact_proof_size: u64,
 	pub compressed_proof_size: u64,
 	pub results: Vec<ApplyExtrinsicResult>,
+	pub key_values: Vec<TrieKeyValue>,
+}
+
+#[cfg_attr(feature = "std", derive(Serialize))]
+#[derive(Debug, TypeInfo)]
+pub struct TrieKeyValue {
+	pub key: Vec<u8>,
+	pub value: Vec<u8>,
 }
 
 sp_api::decl_runtime_apis! {