git.delta.rocks / unique-network / refs/commits / 254f58fefc7b

difftreelog

feat estimate multiple extrinsics PoV

Daniel Shiposha2022-11-23parent: #cff7f42.patch.diff
in: master

3 files changed

modifiedclient/rpc/src/pov_estimate.rsdiffbeforeafterboth
before · client/rpc/src/pov_estimate.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/>.1617use std::sync::Arc;1819use codec::{Encode, Decode};20use sp_externalities::Extensions;2122use up_pov_estimate_rpc::{PovEstimateApi as PovEstimateRuntimeApi};23use up_common::types::opaque::RuntimeId;2425use sc_service::{NativeExecutionDispatch, config::ExecutionStrategy};26use sp_state_machine::{StateMachine, TrieBackendBuilder};2728use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc};29use anyhow::anyhow;3031use sc_client_api::backend::Backend;32use sp_blockchain::HeaderBackend;33use sp_core::{34	Bytes,35	offchain::{36		testing::{TestOffchainExt, TestTransactionPoolExt},37		OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,38	},39	testing::TaskExecutor,40	traits::TaskExecutorExt,41};42use sp_keystore::{testing::KeyStore, KeystoreExt};43use sp_api::{AsTrieBackend, BlockId, BlockT, ProvideRuntimeApi};4445use sc_executor::NativeElseWasmExecutor;46use sc_rpc_api::DenyUnsafe;4748use sp_runtime::traits::Header;4950use up_pov_estimate_rpc::PovInfo;5152use crate::define_struct_for_server_api;5354type HasherOf<Block> = <<Block as BlockT>::Header as Header>::Hashing;55type StateOf<Block> = <sc_service::TFullBackend<Block> as Backend<Block>>::State;5657pub struct ExecutorParams {58	pub wasm_method: sc_service::config::WasmExecutionMethod,59	pub default_heap_pages: Option<u64>,60	pub max_runtime_instances: usize,61	pub runtime_cache_size: u8,62}6364#[cfg(feature = "unique-runtime")]65pub struct UniqueRuntimeExecutor;6667#[cfg(feature = "quartz-runtime")]68pub struct QuartzRuntimeExecutor;6970pub struct OpalRuntimeExecutor;7172#[cfg(feature = "unique-runtime")]73impl NativeExecutionDispatch for UniqueRuntimeExecutor {74	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7576	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {77		unique_runtime::api::dispatch(method, data)78	}7980	fn native_version() -> sc_executor::NativeVersion {81		unique_runtime::native_version()82	}83}8485#[cfg(feature = "quartz-runtime")]86impl NativeExecutionDispatch for QuartzRuntimeExecutor {87	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8889	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {90		quartz_runtime::api::dispatch(method, data)91	}9293	fn native_version() -> sc_executor::NativeVersion {94		quartz_runtime::native_version()95	}96}9798impl NativeExecutionDispatch for OpalRuntimeExecutor {99	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;100101	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {102		opal_runtime::api::dispatch(method, data)103	}104105	fn native_version() -> sc_executor::NativeVersion {106		opal_runtime::native_version()107	}108}109110#[cfg(feature = "pov-estimate")]111define_struct_for_server_api! {112	PovEstimate {113		client: Arc<Client>,114		backend: Arc<sc_service::TFullBackend<Block>>,115		deny_unsafe: DenyUnsafe,116		exec_params: ExecutorParams,117		runtime_id: RuntimeId,118	}119}120121#[rpc(server)]122#[async_trait]123pub trait PovEstimateApi<BlockHash> {124	#[method(name = "unique_estimateExtrinsicPoV")]125	fn estimate_extrinsic_pov(&self, encoded_xt: Bytes, at: Option<BlockHash>) -> Result<PovInfo>;126}127128#[allow(deprecated)]129#[cfg(feature = "pov-estimate")]130impl<C, Block> PovEstimateApiServer<<Block as BlockT>::Hash> for PovEstimate<C, Block>131where132	Block: BlockT,133	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,134	C::Api: PovEstimateRuntimeApi<Block>,135{136	fn estimate_extrinsic_pov(137		&self,138		encoded_xt: Bytes,139		at: Option<<Block as BlockT>::Hash>,140	) -> Result<PovInfo> {141		self.deny_unsafe.check_if_safe()?;142143		let at = BlockId::<Block>::hash(at.unwrap_or_else(|| self.client.info().best_hash));144		let state = self145			.backend146			.state_at(at)147			.map_err(|_| anyhow!("unable to fetch the state at {at:?}"))?;148149		match &self.runtime_id {150			#[cfg(feature = "unique-runtime")]151			RuntimeId::Unique => execute_extrinsic_in_sandbox::<Block, UniqueRuntimeExecutor>(152				state,153				&self.exec_params,154				encoded_xt,155			),156157			#[cfg(feature = "quartz-runtime")]158			RuntimeId::Quartz => execute_extrinsic_in_sandbox::<Block, QuartzRuntimeExecutor>(159				state,160				&self.exec_params,161				encoded_xt,162			),163164			RuntimeId::Opal => execute_extrinsic_in_sandbox::<Block, OpalRuntimeExecutor>(165				state,166				&self.exec_params,167				encoded_xt,168			),169170			runtime_id => Err(anyhow!("unknown runtime id {:?}", runtime_id).into()),171		}172	}173}174175fn full_extensions() -> Extensions {176	let mut extensions = Extensions::default();177	extensions.register(TaskExecutorExt::new(TaskExecutor::new()));178	let (offchain, _offchain_state) = TestOffchainExt::new();179	let (pool, _pool_state) = TestTransactionPoolExt::new();180	extensions.register(OffchainDbExt::new(offchain.clone()));181	extensions.register(OffchainWorkerExt::new(offchain));182	extensions.register(KeystoreExt(std::sync::Arc::new(KeyStore::new())));183	extensions.register(TransactionPoolExt::new(pool));184185	extensions186}187188fn execute_extrinsic_in_sandbox<Block, D>(189	state: StateOf<Block>,190	exec_params: &ExecutorParams,191	encoded_xt: Bytes,192) -> Result<PovInfo>193where194	Block: BlockT,195	D: NativeExecutionDispatch + 'static,196{197	let backend = state.as_trie_backend().clone();198	let mut changes = Default::default();199	let runtime_code_backend = sp_state_machine::backend::BackendRuntimeCode::new(backend);200201	let proving_backend = TrieBackendBuilder::wrap(&backend)202		.with_recorder(Default::default())203		.build();204205	let runtime_code = runtime_code_backend206		.runtime_code()207		.map_err(|_| anyhow!("runtime code backend creation failed"))?;208209	let pre_root = *backend.root();210211	let executor = NativeElseWasmExecutor::<D>::new(212		exec_params.wasm_method,213		exec_params.default_heap_pages,214		exec_params.max_runtime_instances,215		exec_params.runtime_cache_size,216	);217	let execution = ExecutionStrategy::NativeElseWasm;218219	let encoded_bytes = encoded_xt.encode();220221	let xt_result = StateMachine::new(222		&proving_backend,223		&mut changes,224		&executor,225		"PovEstimateApi_pov_estimate",226		encoded_bytes.as_slice(),227		full_extensions(),228		&runtime_code,229		sp_core::testing::TaskExecutor::new(),230	)231	.execute(execution.into())232	.map_err(|e| anyhow!("failed to execute the extrinsic {:?}", e))?;233234	let xt_result = Decode::decode(&mut &*xt_result)235		.map_err(|e| anyhow!("failed to decode the extrinsic result {:?}", e))?;236237	let proof = proving_backend238		.extract_proof()239		.expect("A recorder was set and thus, a storage proof can be extracted; qed");240	let proof_size = proof.encoded_size();241	let compact_proof = proof242		.clone()243		.into_compact_proof::<HasherOf<Block>>(pre_root)244		.map_err(|e| anyhow!("failed to generate compact proof {:?}", e))?;245	let compact_proof_size = compact_proof.encoded_size();246247	let compressed_proof = zstd::stream::encode_all(&compact_proof.encode()[..], 0)248		.map_err(|e| anyhow!("failed to generate compact proof {:?}", e))?;249	let compressed_proof_size = compressed_proof.len();250251	Ok(PovInfo {252		proof_size: proof_size as u64,253		compact_proof_size: compact_proof_size as u64,254		compressed_proof_size: compressed_proof_size as u64,255		result: xt_result,256	})257}
after · client/rpc/src/pov_estimate.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/>.1617use std::sync::Arc;1819use codec::{Encode, Decode};20use sp_externalities::Extensions;2122use up_pov_estimate_rpc::{PovEstimateApi as PovEstimateRuntimeApi};23use up_common::types::opaque::RuntimeId;2425use sc_service::{NativeExecutionDispatch, config::ExecutionStrategy};26use sp_state_machine::{StateMachine, TrieBackendBuilder};2728use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc};29use anyhow::anyhow;3031use sc_client_api::backend::Backend;32use sp_blockchain::HeaderBackend;33use sp_core::{34	Bytes,35	offchain::{36		testing::{TestOffchainExt, TestTransactionPoolExt},37		OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,38	},39	testing::TaskExecutor,40	traits::TaskExecutorExt,41};42use sp_keystore::{testing::KeyStore, KeystoreExt};43use sp_api::{AsTrieBackend, BlockId, BlockT, ProvideRuntimeApi};4445use sc_executor::NativeElseWasmExecutor;46use sc_rpc_api::DenyUnsafe;4748use sp_runtime::traits::Header;4950use up_pov_estimate_rpc::PovInfo;5152use crate::define_struct_for_server_api;5354type HasherOf<Block> = <<Block as BlockT>::Header as Header>::Hashing;55type StateOf<Block> = <sc_service::TFullBackend<Block> as Backend<Block>>::State;5657pub struct ExecutorParams {58	pub wasm_method: sc_service::config::WasmExecutionMethod,59	pub default_heap_pages: Option<u64>,60	pub max_runtime_instances: usize,61	pub runtime_cache_size: u8,62}6364#[cfg(feature = "unique-runtime")]65pub struct UniqueRuntimeExecutor;6667#[cfg(feature = "quartz-runtime")]68pub struct QuartzRuntimeExecutor;6970pub struct OpalRuntimeExecutor;7172#[cfg(feature = "unique-runtime")]73impl NativeExecutionDispatch for UniqueRuntimeExecutor {74	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;7576	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {77		unique_runtime::api::dispatch(method, data)78	}7980	fn native_version() -> sc_executor::NativeVersion {81		unique_runtime::native_version()82	}83}8485#[cfg(feature = "quartz-runtime")]86impl NativeExecutionDispatch for QuartzRuntimeExecutor {87	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8889	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {90		quartz_runtime::api::dispatch(method, data)91	}9293	fn native_version() -> sc_executor::NativeVersion {94		quartz_runtime::native_version()95	}96}9798impl NativeExecutionDispatch for OpalRuntimeExecutor {99	type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;100101	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {102		opal_runtime::api::dispatch(method, data)103	}104105	fn native_version() -> sc_executor::NativeVersion {106		opal_runtime::native_version()107	}108}109110#[cfg(feature = "pov-estimate")]111define_struct_for_server_api! {112	PovEstimate {113		client: Arc<Client>,114		backend: Arc<sc_service::TFullBackend<Block>>,115		deny_unsafe: DenyUnsafe,116		exec_params: ExecutorParams,117		runtime_id: RuntimeId,118	}119}120121#[rpc(server)]122#[async_trait]123pub trait PovEstimateApi<BlockHash> {124	#[method(name = "unique_estimateExtrinsicPoV")]125	fn estimate_extrinsic_pov(126		&self,127		encoded_xts: Vec<Bytes>,128		at: Option<BlockHash>,129	) -> Result<PovInfo>;130}131132#[allow(deprecated)]133#[cfg(feature = "pov-estimate")]134impl<C, Block> PovEstimateApiServer<<Block as BlockT>::Hash> for PovEstimate<C, Block>135where136	Block: BlockT,137	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,138	C::Api: PovEstimateRuntimeApi<Block>,139{140	fn estimate_extrinsic_pov(141		&self,142		encoded_xts: Vec<Bytes>,143		at: Option<<Block as BlockT>::Hash>,144	) -> Result<PovInfo> {145		self.deny_unsafe.check_if_safe()?;146147		let at = BlockId::<Block>::hash(at.unwrap_or_else(|| self.client.info().best_hash));148		let state = self149			.backend150			.state_at(at)151			.map_err(|_| anyhow!("unable to fetch the state at {at:?}"))?;152153		match &self.runtime_id {154			#[cfg(feature = "unique-runtime")]155			RuntimeId::Unique => execute_extrinsic_in_sandbox::<Block, UniqueRuntimeExecutor>(156				state,157				&self.exec_params,158				encoded_xts,159			),160161			#[cfg(feature = "quartz-runtime")]162			RuntimeId::Quartz => execute_extrinsic_in_sandbox::<Block, QuartzRuntimeExecutor>(163				state,164				&self.exec_params,165				encoded_xts,166			),167168			RuntimeId::Opal => execute_extrinsic_in_sandbox::<Block, OpalRuntimeExecutor>(169				state,170				&self.exec_params,171				encoded_xts,172			),173174			runtime_id => Err(anyhow!("unknown runtime id {:?}", runtime_id).into()),175		}176	}177}178179fn full_extensions() -> Extensions {180	let mut extensions = Extensions::default();181	extensions.register(TaskExecutorExt::new(TaskExecutor::new()));182	let (offchain, _offchain_state) = TestOffchainExt::new();183	let (pool, _pool_state) = TestTransactionPoolExt::new();184	extensions.register(OffchainDbExt::new(offchain.clone()));185	extensions.register(OffchainWorkerExt::new(offchain));186	extensions.register(KeystoreExt(std::sync::Arc::new(KeyStore::new())));187	extensions.register(TransactionPoolExt::new(pool));188189	extensions190}191192fn execute_extrinsic_in_sandbox<Block, D>(193	state: StateOf<Block>,194	exec_params: &ExecutorParams,195	encoded_xts: Vec<Bytes>,196) -> Result<PovInfo>197where198	Block: BlockT,199	D: NativeExecutionDispatch + 'static,200{201	let backend = state.as_trie_backend().clone();202	let mut changes = Default::default();203	let runtime_code_backend = sp_state_machine::backend::BackendRuntimeCode::new(backend);204205	let proving_backend = TrieBackendBuilder::wrap(&backend)206		.with_recorder(Default::default())207		.build();208209	let runtime_code = runtime_code_backend210		.runtime_code()211		.map_err(|_| anyhow!("runtime code backend creation failed"))?;212213	let pre_root = *backend.root();214215	let executor = NativeElseWasmExecutor::<D>::new(216		exec_params.wasm_method,217		exec_params.default_heap_pages,218		exec_params.max_runtime_instances,219		exec_params.runtime_cache_size,220	);221	let execution = ExecutionStrategy::NativeElseWasm;222223	let mut results = Vec::new();224225	for encoded_xt in encoded_xts {226		let encoded_bytes = encoded_xt.encode();227228		let xt_result = StateMachine::new(229			&proving_backend,230			&mut changes,231			&executor,232			"PovEstimateApi_pov_estimate",233			encoded_bytes.as_slice(),234			full_extensions(),235			&runtime_code,236			sp_core::testing::TaskExecutor::new(),237		)238		.execute(execution.into())239		.map_err(|e| anyhow!("failed to execute the extrinsic {:?}", e))?;240241		let xt_result = Decode::decode(&mut &*xt_result)242			.map_err(|e| anyhow!("failed to decode the extrinsic result {:?}", e))?;243244		results.push(xt_result);245	}246247	let proof = proving_backend248		.extract_proof()249		.expect("A recorder was set and thus, a storage proof can be extracted; qed");250	let proof_size = proof.encoded_size();251	let compact_proof = proof252		.clone()253		.into_compact_proof::<HasherOf<Block>>(pre_root)254		.map_err(|e| anyhow!("failed to generate compact proof {:?}", e))?;255	let compact_proof_size = compact_proof.encoded_size();256257	let compressed_proof = zstd::stream::encode_all(&compact_proof.encode()[..], 0)258		.map_err(|e| anyhow!("failed to generate compact proof {:?}", e))?;259	let compressed_proof_size = compressed_proof.len();260261	Ok(PovInfo {262		proof_size: proof_size as u64,263		compact_proof_size: compact_proof_size as u64,264		compressed_proof_size: compressed_proof_size as u64,265		results,266	})267}
modifiedprimitives/pov-estimate-rpc/src/lib.rsdiffbeforeafterboth
--- a/primitives/pov-estimate-rpc/src/lib.rs
+++ b/primitives/pov-estimate-rpc/src/lib.rs
@@ -17,6 +17,7 @@
 #![cfg_attr(not(feature = "std"), no_std)]
 
 use scale_info::TypeInfo;
+use sp_std::vec::Vec;
 
 #[cfg(feature = "std")]
 use serde::Serialize;
@@ -30,7 +31,7 @@
 	pub proof_size: u64,
 	pub compact_proof_size: u64,
 	pub compressed_proof_size: u64,
-	pub result: ApplyExtrinsicResult,
+	pub results: Vec<ApplyExtrinsicResult>,
 }
 
 sp_api::decl_runtime_apis! {
modifiedtests/src/interfaces/unique/definitions.tsdiffbeforeafterboth
--- a/tests/src/interfaces/unique/definitions.ts
+++ b/tests/src/interfaces/unique/definitions.ts
@@ -175,28 +175,10 @@
       [collectionParam, tokenParam],
       'Option<u128>',
     ),
-<<<<<<< HEAD
-<<<<<<< HEAD
     allowanceForAll: fun(
       'Tells whether the given `owner` approves the `operator`.',
       [collectionParam, crossAccountParam('owner'), crossAccountParam('operator')],
       'Option<bool>',
-=======
-    povEstimate: fun(
-      'Estimate PoV size',
-      [{name: 'encodedXt', type: 'Vec<u8>'}],
-=======
-    estimateExtrinsicPoV: fun(
-      'Estimate PoV size of an encoded extrinsic',
-      [{name: 'encodedXt', type: 'Bytes'}],
-      'UpPovEstimateRpcPovInfo',
-    ),
-    estimateCallPoV: fun(
-      'Estimate PoV size of an encoded call',
-      [{name: 'encodedCall', type: 'Bytes'}],
->>>>>>> chore: regenerate types
-      'UpPovEstimateRpcPovInfo',
->>>>>>> fix: update polkadot types and definitions
     ),
   },
 };