difftreelog
fix PoV estimate RPC
in: master
8 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -12843,6 +12843,7 @@
"sp-blockchain",
"sp-core",
"sp-externalities",
+ "sp-keystore",
"sp-rpc",
"sp-runtime",
"sp-state-machine",
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,7 +11,7 @@
'runtime/unique',
'runtime/tests',
]
-default-members = ['node/*', 'runtime/opal']
+default-members = ['node/*', 'client/*', 'runtime/opal']
package.version = "0.9.36"
[profile.release]
client/rpc/Cargo.tomldiffbeforeafterboth--- a/client/rpc/Cargo.toml
+++ b/client/rpc/Cargo.toml
@@ -25,6 +25,7 @@
sp-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-blockchain = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
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-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" }
client/rpc/src/pov_estimate.rsdiffbeforeafterboth1// 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;2021use up_pov_estimate_rpc::{PovEstimateApi as PovEstimateRuntimeApi};22use up_common::types::opaque::RuntimeId;2324use sc_service::{NativeExecutionDispatch, config::ExecutionStrategy};25use sp_state_machine::{StateMachine, TrieBackendBuilder};2627use jsonrpsee::{28 core::RpcResult as Result,29 proc_macros::rpc,30};31use anyhow::anyhow;3233use sc_client_api::backend::Backend;34use sp_blockchain::HeaderBackend;35use sp_api::{AsTrieBackend, BlockId, BlockT, ProvideRuntimeApi};3637use sc_executor::NativeElseWasmExecutor;38use sc_rpc_api::DenyUnsafe;3940use sp_runtime::traits::Header;4142use up_pov_estimate_rpc::PovInfo;4344use crate::define_struct_for_server_api;4546type HasherOf<Block> = <<Block as BlockT>::Header as Header>::Hashing;47type StateOf<Block> = <sc_service::TFullBackend<Block> as Backend<Block>>::State;4849pub struct ExecutorParams {50 pub wasm_method: sc_service::config::WasmExecutionMethod,51 pub default_heap_pages: Option<u64>,52 pub max_runtime_instances: usize,53 pub runtime_cache_size: u8,54}5556#[cfg(feature = "unique-runtime")]57pub struct UniqueRuntimeExecutor;5859#[cfg(feature = "quartz-runtime")]60pub struct QuartzRuntimeExecutor;6162pub struct OpalRuntimeExecutor;6364#[cfg(feature = "unique-runtime")]65impl NativeExecutionDispatch for UniqueRuntimeExecutor {66 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;6768 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {69 unique_runtime::api::dispatch(method, data)70 }7172 fn native_version() -> sc_executor::NativeVersion {73 unique_runtime::native_version()74 }75}7677#[cfg(feature = "quartz-runtime")]78impl NativeExecutionDispatch for QuartzRuntimeExecutor {79 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;8081 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {82 quartz_runtime::api::dispatch(method, data)83 }8485 fn native_version() -> sc_executor::NativeVersion {86 quartz_runtime::native_version()87 }88}8990impl NativeExecutionDispatch for OpalRuntimeExecutor {91 type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;9293 fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {94 opal_runtime::api::dispatch(method, data)95 }9697 fn native_version() -> sc_executor::NativeVersion {98 opal_runtime::native_version()99 }100}101102#[cfg(feature = "pov-estimate")]103define_struct_for_server_api! {104 PovEstimate {105 client: Arc<Client>,106 backend: Arc<sc_service::TFullBackend<Block>>,107 deny_unsafe: DenyUnsafe,108 exec_params: ExecutorParams,109 runtime_id: RuntimeId,110 }111}112113#[rpc(server)]114#[async_trait]115pub trait PovEstimateApi<BlockHash> {116 #[method(name = "unique_povEstimate")]117 fn pov_estimate(&self, encoded_xt: Vec<u8>, at: Option<BlockHash>) -> Result<PovInfo>;118}119120#[allow(deprecated)]121#[cfg(feature = "pov-estimate")]122impl<C, Block>123 PovEstimateApiServer<<Block as BlockT>::Hash> for PovEstimate<C, Block>124where125 Block: BlockT,126 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,127 C::Api: PovEstimateRuntimeApi<Block>,128{129 fn pov_estimate(&self, encoded_xt: Vec<u8>, at: Option<<Block as BlockT>::Hash>,) -> Result<PovInfo> {130 self.deny_unsafe.check_if_safe()?;131132 let at = BlockId::<Block>::hash(at.unwrap_or_else(|| self.client.info().best_hash));133 let state = self.backend.state_at(at).map_err(|_| anyhow!("unable to fetch the state at {at:?}"))?;134 match &self.runtime_id {135 #[cfg(feature = "unique-runtime")]136 RuntimeId::Unique => execute_extrinsic_in_sandbox::<Block, UniqueRuntimeExecutor>(state, &self.exec_params, encoded_xt),137138 #[cfg(feature = "quartz-runtime")]139 RuntimeId::Quartz => execute_extrinsic_in_sandbox::<Block, QuartzRuntimeExecutor>(state, &self.exec_params, encoded_xt),140141 RuntimeId::Opal => execute_extrinsic_in_sandbox::<Block, OpalRuntimeExecutor>(state, &self.exec_params, encoded_xt),142143 runtime_id => Err(anyhow!("unknown runtime id {:?}", runtime_id).into()),144 }145 }146}147148fn execute_extrinsic_in_sandbox<Block, D>(state: StateOf<Block>, exec_params: &ExecutorParams, encoded_xt: Vec<u8>) -> Result<PovInfo>149where150 Block: BlockT,151 D: NativeExecutionDispatch + 'static,152{153 let backend = state.as_trie_backend().clone();154 let mut changes = Default::default();155 let runtime_code_backend = sp_state_machine::backend::BackendRuntimeCode::new(backend);156157 let proving_backend =158 TrieBackendBuilder::wrap(&backend).with_recorder(Default::default()).build();159160 let runtime_code = runtime_code_backend.runtime_code()161 .map_err(|_| anyhow!("runtime code backend creation failed"))?;162163 let pre_root = *backend.root();164165 let executor = NativeElseWasmExecutor::<D>::new(166 exec_params.wasm_method,167 exec_params.default_heap_pages,168 exec_params.max_runtime_instances,169 exec_params.runtime_cache_size,170 );171 let execution = ExecutionStrategy::NativeElseWasm;172173 StateMachine::new(174 &proving_backend,175 &mut changes,176 &executor,177 "PovEstimateApi_pov_estimate",178 encoded_xt.as_slice(),179 sp_externalities::Extensions::default(),180 &runtime_code,181 sp_core::testing::TaskExecutor::new(),182 )183 .execute(execution.into())184 .map_err(|e| anyhow!("failed to execute the extrinsic {:?}", e))?;185186 let proof = proving_backend187 .extract_proof()188 .expect("A recorder was set and thus, a storage proof can be extracted; qed");189 let proof_size = proof.encoded_size();190 let compact_proof = proof191 .clone()192 .into_compact_proof::<HasherOf<Block>>(pre_root)193 .map_err(|e| anyhow!("failed to generate compact proof {:?}", e))?;194 let compact_proof_size = compact_proof.encoded_size();195196 let compressed_proof = zstd::stream::encode_all(&compact_proof.encode()[..], 0)197 .map_err(|e| anyhow!("failed to generate compact proof {:?}", e))?;198 let compressed_proof_size = compressed_proof.len();199200 Ok(PovInfo {201 proof_size: proof_size as u64,202 compact_proof_size: compact_proof_size as u64,203 compressed_proof_size: compressed_proof_size as u64,204 })205}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}node/cli/src/service.rsdiffbeforeafterboth--- a/node/cli/src/service.rs
+++ b/node/cli/src/service.rs
@@ -67,6 +67,8 @@
use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};
use up_common::types::opaque::*;
+
+#[cfg(feature = "pov-estimate")]
use crate::chain_spec::RuntimeIdentification;
// RMRK
@@ -517,8 +519,12 @@
.for_each(|()| futures::future::ready(())),
);
+ #[cfg(feature = "pov-estimate")]
let rpc_backend = backend.clone();
+
+ #[cfg(feature = "pov-estimate")]
let runtime_id = parachain_config.chain_spec.runtime_id();
+
let rpc_builder = Box::new(move |deny_unsafe, subscription_task_executor| {
let full_deps = unique_rpc::FullDeps {
#[cfg(feature = "pov-estimate")]
@@ -1058,8 +1064,13 @@
let rpc_pool = transaction_pool.clone();
let rpc_network = network.clone();
let rpc_frontier_backend = frontier_backend.clone();
+
+ #[cfg(feature = "pov-estimate")]
let rpc_backend = backend.clone();
+
+ #[cfg(feature = "pov-estimate")]
let runtime_id = config.chain_spec.runtime_id();
+
let rpc_builder = Box::new(move |deny_unsafe, subscription_executor| {
let full_deps = unique_rpc::FullDeps {
#[cfg(feature = "pov-estimate")]
node/rpc/src/lib.rsdiffbeforeafterboth--- a/node/rpc/src/lib.rs
+++ b/node/rpc/src/lib.rs
@@ -48,6 +48,7 @@
RmrkPartType, RmrkTheme,
};
+#[cfg(feature = "pov-estimate")]
type FullBackend = sc_service::TFullBackend<Block>;
/// Extra dependencies for GRANDPA
@@ -84,7 +85,7 @@
pub deny_unsafe: DenyUnsafe,
/// EthFilterApi pool.
pub filter_pool: Option<FilterPool>,
-
+
#[cfg(feature = "pov-estimate")]
pub runtime_id: RuntimeId,
/// Executor params for PoV estimating
@@ -271,7 +272,16 @@
io.merge(Rmrk::new(client.clone()).into_rpc())?;
#[cfg(feature = "pov-estimate")]
- io.merge(PovEstimate::new(client.clone(), backend, deny_unsafe, exec_params, runtime_id).into_rpc())?;
+ io.merge(
+ PovEstimate::new(
+ client.clone(),
+ backend,
+ deny_unsafe,
+ exec_params,
+ runtime_id,
+ )
+ .into_rpc(),
+ )?;
if let Some(filter_pool) = filter_pool {
io.merge(
primitives/pov-estimate-rpc/src/lib.rsdiffbeforeafterboth--- a/primitives/pov-estimate-rpc/src/lib.rs
+++ b/primitives/pov-estimate-rpc/src/lib.rs
@@ -16,24 +16,25 @@
#![cfg_attr(not(feature = "std"), no_std)]
-use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::Serialize;
use sp_runtime::ApplyExtrinsicResult;
+use sp_core::Bytes;
#[cfg_attr(feature = "std", derive(Serialize))]
-#[derive(Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
+#[derive(Debug, TypeInfo)]
pub struct PovInfo {
- pub proof_size: u64,
- pub compact_proof_size: u64,
- pub compressed_proof_size: u64,
+ pub proof_size: u64,
+ pub compact_proof_size: u64,
+ pub compressed_proof_size: u64,
+ pub result: ApplyExtrinsicResult,
}
sp_api::decl_runtime_apis! {
- pub trait PovEstimateApi {
- fn pov_estimate(uxt: Block::Extrinsic) -> ApplyExtrinsicResult;
- }
+ pub trait PovEstimateApi {
+ fn pov_estimate(uxt: Bytes) -> ApplyExtrinsicResult;
+ }
}
runtime/common/runtime_apis.rsdiffbeforeafterboth--- a/runtime/common/runtime_apis.rs
+++ b/runtime/common/runtime_apis.rs
@@ -35,11 +35,11 @@
) => {
use sp_std::prelude::*;
use sp_api::impl_runtime_apis;
- use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160};
+ use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160, Bytes};
use sp_runtime::{
Permill,
traits::Block as BlockT,
- transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError, InvalidTransaction},
+ transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, DispatchError,
};
use fp_rpc::TransactionStatus;
@@ -780,12 +780,24 @@
impl up_pov_estimate_rpc::PovEstimateApi<Block> for Runtime {
#[allow(unused_variables)]
- fn pov_estimate(uxt: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {
+ fn pov_estimate(uxt: Bytes) -> ApplyExtrinsicResult {
#[cfg(feature = "pov-estimate")]
- return Executive::apply_extrinsic(uxt);
+ {
+ use codec::Decode;
+
+ let uxt_decode = <<Block as BlockT>::Extrinsic as Decode>::decode(&mut &*uxt)
+ .map_err(|_| DispatchError::Other("failed to decode the extrinsic"));
+
+ let uxt = match uxt_decode {
+ Ok(uxt) => uxt,
+ Err(err) => return Ok(err.into()),
+ };
+
+ Executive::apply_extrinsic(uxt)
+ }
#[cfg(not(feature = "pov-estimate"))]
- return Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
+ return Ok(unsupported!());
}
}