git.delta.rocks / unique-network / refs/commits / 5f858e1b2194

difftreelog

fix PoV estimate RPC

Daniel Shiposha2022-11-22parent: #5b6b612.patch.diff
in: master

8 files changed

modifiedCargo.lockdiffbeforeafterboth
12843 "sp-blockchain",12843 "sp-blockchain",
12844 "sp-core",12844 "sp-core",
12845 "sp-externalities",12845 "sp-externalities",
12846 "sp-keystore",
12846 "sp-rpc",12847 "sp-rpc",
12847 "sp-runtime",12848 "sp-runtime",
12848 "sp-state-machine",12849 "sp-state-machine",
modifiedCargo.tomldiffbeforeafterboth
11 'runtime/unique',11 'runtime/unique',
12 'runtime/tests',12 'runtime/tests',
13]13]
14default-members = ['node/*', 'runtime/opal']14default-members = ['node/*', 'client/*', 'runtime/opal']
15package.version = "0.9.36"15package.version = "0.9.36"
1616
17[profile.release]17[profile.release]
modifiedclient/rpc/Cargo.tomldiffbeforeafterboth
25sp-api = { default-features = false, 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" }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" }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" }
28sp-rpc = { 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" }
29sp-runtime = { 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" }
30pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.36" }31pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.36" }
modifiedclient/rpc/src/pov_estimate.rsdiffbeforeafterboth
1616
17use std::sync::Arc;17use std::sync::Arc;
1818
19use codec::Encode;19use codec::{Encode, Decode};
20use sp_externalities::Extensions;
2021
21use up_pov_estimate_rpc::{PovEstimateApi as PovEstimateRuntimeApi};22use up_pov_estimate_rpc::{PovEstimateApi as PovEstimateRuntimeApi};
22use up_common::types::opaque::RuntimeId;23use up_common::types::opaque::RuntimeId;
2627
27use jsonrpsee::{28use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc};
28 core::RpcResult as Result,
29 proc_macros::rpc,
30};
31use anyhow::anyhow;29use anyhow::anyhow;
3230
33use sc_client_api::backend::Backend;31use sc_client_api::backend::Backend;
34use sp_blockchain::HeaderBackend;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};
35use sp_api::{AsTrieBackend, BlockId, BlockT, ProvideRuntimeApi};43use sp_api::{AsTrieBackend, BlockId, BlockT, ProvideRuntimeApi};
3644
37use sc_executor::NativeElseWasmExecutor;45use sc_executor::NativeElseWasmExecutor;
113#[rpc(server)]121#[rpc(server)]
114#[async_trait]122#[async_trait]
115pub trait PovEstimateApi<BlockHash> {123pub trait PovEstimateApi<BlockHash> {
116 #[method(name = "unique_povEstimate")]124 #[method(name = "unique_estimateExtrinsicPoV")]
117 fn pov_estimate(&self, encoded_xt: Vec<u8>, at: Option<BlockHash>) -> Result<PovInfo>;125 fn estimate_extrinsic_pov(&self, encoded_xt: Bytes, at: Option<BlockHash>) -> Result<PovInfo>;
118}126}
119127
120#[allow(deprecated)]128#[allow(deprecated)]
126 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,133 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
127 C::Api: PovEstimateRuntimeApi<Block>,134 C::Api: PovEstimateRuntimeApi<Block>,
128{135{
129 fn pov_estimate(&self, encoded_xt: Vec<u8>, at: Option<<Block as BlockT>::Hash>,) -> Result<PovInfo> {136 fn estimate_extrinsic_pov(
137 &self,
138 encoded_xt: Bytes,
139 at: Option<<Block as BlockT>::Hash>,
140 ) -> Result<PovInfo> {
130 self.deny_unsafe.check_if_safe()?;141 self.deny_unsafe.check_if_safe()?;
145 }172 }
146}173}
174
175fn 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));
184
185 extensions
186}
147187
148fn execute_extrinsic_in_sandbox<Block, D>(state: StateOf<Block>, exec_params: &ExecutorParams, encoded_xt: Vec<u8>) -> Result<PovInfo>188fn execute_extrinsic_in_sandbox<Block, D>(
189 state: StateOf<Block>,
190 exec_params: &ExecutorParams,
191 encoded_xt: Bytes,
192) -> Result<PovInfo>
149where193where
150 Block: BlockT,194 Block: BlockT,
170 );216 );
171 let execution = ExecutionStrategy::NativeElseWasm;217 let execution = ExecutionStrategy::NativeElseWasm;
218
219 let encoded_bytes = encoded_xt.encode();
172220
173 StateMachine::new(221 let xt_result = StateMachine::new(
174 &proving_backend,222 &proving_backend,
175 &mut changes,223 &mut changes,
176 &executor,224 &executor,
177 "PovEstimateApi_pov_estimate",225 "PovEstimateApi_pov_estimate",
178 encoded_xt.as_slice(),226 encoded_bytes.as_slice(),
179 sp_externalities::Extensions::default(),227 full_extensions(),
180 &runtime_code,228 &runtime_code,
181 sp_core::testing::TaskExecutor::new(),229 sp_core::testing::TaskExecutor::new(),
182 )230 )
183 .execute(execution.into())231 .execute(execution.into())
184 .map_err(|e| anyhow!("failed to execute the extrinsic {:?}", e))?;232 .map_err(|e| anyhow!("failed to execute the extrinsic {:?}", e))?;
233
234 let xt_result = Decode::decode(&mut &*xt_result)
235 .map_err(|e| anyhow!("failed to decode the extrinsic result {:?}", e))?;
185236
186 let proof = proving_backend237 let proof = proving_backend
187 .extract_proof()238 .extract_proof()
201 proof_size: proof_size as u64,252 proof_size: proof_size as u64,
202 compact_proof_size: compact_proof_size as u64,253 compact_proof_size: compact_proof_size as u64,
203 compressed_proof_size: compressed_proof_size as u64,254 compressed_proof_size: compressed_proof_size as u64,
255 result: xt_result,
204 })256 })
205}257}
206258
modifiednode/cli/src/service.rsdiffbeforeafterboth
6868
69use up_common::types::opaque::*;69use up_common::types::opaque::*;
70
71#[cfg(feature = "pov-estimate")]
70use crate::chain_spec::RuntimeIdentification;72use crate::chain_spec::RuntimeIdentification;
7173
72// RMRK74// RMRK
517 .for_each(|()| futures::future::ready(())),519 .for_each(|()| futures::future::ready(())),
518 );520 );
519521
522 #[cfg(feature = "pov-estimate")]
520 let rpc_backend = backend.clone();523 let rpc_backend = backend.clone();
524
525 #[cfg(feature = "pov-estimate")]
521 let runtime_id = parachain_config.chain_spec.runtime_id();526 let runtime_id = parachain_config.chain_spec.runtime_id();
527
522 let rpc_builder = Box::new(move |deny_unsafe, subscription_task_executor| {528 let rpc_builder = Box::new(move |deny_unsafe, subscription_task_executor| {
1059 let rpc_network = network.clone();1065 let rpc_network = network.clone();
1060 let rpc_frontier_backend = frontier_backend.clone();1066 let rpc_frontier_backend = frontier_backend.clone();
1067
1068 #[cfg(feature = "pov-estimate")]
1061 let rpc_backend = backend.clone();1069 let rpc_backend = backend.clone();
1070
1071 #[cfg(feature = "pov-estimate")]
1062 let runtime_id = config.chain_spec.runtime_id();1072 let runtime_id = config.chain_spec.runtime_id();
1073
1063 let rpc_builder = Box::new(move |deny_unsafe, subscription_executor| {1074 let rpc_builder = Box::new(move |deny_unsafe, subscription_executor| {
modifiednode/rpc/src/lib.rsdiffbeforeafterboth
48 RmrkPartType, RmrkTheme,48 RmrkPartType, RmrkTheme,
49};49};
5050
51#[cfg(feature = "pov-estimate")]
51type FullBackend = sc_service::TFullBackend<Block>;52type FullBackend = sc_service::TFullBackend<Block>;
5253
53/// Extra dependencies for GRANDPA54/// Extra dependencies for GRANDPA
modifiedprimitives/pov-estimate-rpc/src/lib.rsdiffbeforeafterboth
1616
17#![cfg_attr(not(feature = "std"), no_std)]17#![cfg_attr(not(feature = "std"), no_std)]
1818
19use codec::{Decode, Encode, MaxEncodedLen};
20use scale_info::TypeInfo;19use scale_info::TypeInfo;
2120
22#[cfg(feature = "std")]21#[cfg(feature = "std")]
23use serde::Serialize;22use serde::Serialize;
2423
25use sp_runtime::ApplyExtrinsicResult;24use sp_runtime::ApplyExtrinsicResult;
25use sp_core::Bytes;
2626
27#[cfg_attr(feature = "std", derive(Serialize))]27#[cfg_attr(feature = "std", derive(Serialize))]
28#[derive(Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]28#[derive(Debug, TypeInfo)]
29pub struct PovInfo {29pub struct PovInfo {
30 pub proof_size: u64,30 pub proof_size: u64,
31 pub compact_proof_size: u64,31 pub compact_proof_size: u64,
32 pub compressed_proof_size: u64,32 pub compressed_proof_size: u64,
33 pub result: ApplyExtrinsicResult,
33}34}
3435
35sp_api::decl_runtime_apis! {36sp_api::decl_runtime_apis! {
36 pub trait PovEstimateApi {37 pub trait PovEstimateApi {
37 fn pov_estimate(uxt: Block::Extrinsic) -> ApplyExtrinsicResult;38 fn pov_estimate(uxt: Bytes) -> ApplyExtrinsicResult;
38 }39 }
39}40}
4041
modifiedruntime/common/runtime_apis.rsdiffbeforeafterboth
35 ) => {35 ) => {
36 use sp_std::prelude::*;36 use sp_std::prelude::*;
37 use sp_api::impl_runtime_apis;37 use sp_api::impl_runtime_apis;
38 use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160};38 use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160, Bytes};
39 use sp_runtime::{39 use sp_runtime::{
40 Permill,40 Permill,
41 traits::Block as BlockT,41 traits::Block as BlockT,
42 transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError, InvalidTransaction},42 transaction_validity::{TransactionSource, TransactionValidity},
43 ApplyExtrinsicResult, DispatchError,43 ApplyExtrinsicResult, DispatchError,
44 };44 };
45 use fp_rpc::TransactionStatus;45 use fp_rpc::TransactionStatus;
780780
781 impl up_pov_estimate_rpc::PovEstimateApi<Block> for Runtime {781 impl up_pov_estimate_rpc::PovEstimateApi<Block> for Runtime {
782 #[allow(unused_variables)]782 #[allow(unused_variables)]
783 fn pov_estimate(uxt: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {783 fn pov_estimate(uxt: Bytes) -> ApplyExtrinsicResult {
784 #[cfg(feature = "pov-estimate")]784 #[cfg(feature = "pov-estimate")]
785 {
786 use codec::Decode;
787
788 let uxt_decode = <<Block as BlockT>::Extrinsic as Decode>::decode(&mut &*uxt)
789 .map_err(|_| DispatchError::Other("failed to decode the extrinsic"));
790
791 let uxt = match uxt_decode {
792 Ok(uxt) => uxt,
785 return Executive::apply_extrinsic(uxt);793 Err(err) => return Ok(err.into()),
794 };
795
796 Executive::apply_extrinsic(uxt)
797 }
786798
787 #[cfg(not(feature = "pov-estimate"))]799 #[cfg(not(feature = "pov-estimate"))]
788 return Err(TransactionValidityError::Invalid(InvalidTransaction::Call))800 return Ok(unsupported!());
789 }801 }
790 }802 }
791803