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
--- a/client/rpc/Cargo.toml
+++ b/client/rpc/Cargo.toml
@@ -16,6 +16,7 @@
 jsonrpsee = { version = "0.16.2", features = ["server", "macros"] }
 anyhow = "1.0.57"
 zstd = { version = "0.11.2", default-features = false }
+trie-db = { version = "0.24.0", default-features = false }
 
 sc-rpc-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
 sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
@@ -27,6 +28,7 @@
 sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
 sp-keystore = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
 sp-rpc = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
+sp-trie = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
 sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
 pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.36" }
 
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
before · primitives/pov-estimate-rpc/src/lib.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use scale_info::TypeInfo;20use sp_std::vec::Vec;2122#[cfg(feature = "std")]23use serde::Serialize;2425use sp_runtime::ApplyExtrinsicResult;26use sp_core::Bytes;2728#[cfg_attr(feature = "std", derive(Serialize))]29#[derive(Debug, TypeInfo)]30pub struct PovInfo {31	pub proof_size: u64,32	pub compact_proof_size: u64,33	pub compressed_proof_size: u64,34	pub results: Vec<ApplyExtrinsicResult>,35}3637sp_api::decl_runtime_apis! {38	pub trait PovEstimateApi {39		fn pov_estimate(uxt: Bytes) -> ApplyExtrinsicResult;40	}41}