referrerpolicy=no-referrer-when-downgrade

polkadot_omni_node_lib/common/
rpc.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Parachain-specific RPCs implementation.
18
19#![warn(missing_docs)]
20
21use crate::common::{
22	types::{AccountId, Balance, Nonce, ParachainBackend, ParachainClient},
23	ConstructNodeRuntimeApi,
24};
25use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
26use sc_rpc::{
27	dev::{Dev, DevApiServer},
28	statement::{StatementApiServer, StatementStore},
29};
30use sp_runtime::traits::Block as BlockT;
31use std::{marker::PhantomData, sync::Arc};
32use substrate_frame_rpc_system::{System, SystemApiServer};
33use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer};
34
35/// A type representing all RPC extensions.
36pub type RpcExtension = jsonrpsee::RpcModule<()>;
37
38pub(crate) trait BuildRpcExtensions<Client, Backend, Pool, StatementStore> {
39	fn build_rpc_extensions(
40		client: Arc<Client>,
41		backend: Arc<Backend>,
42		pool: Arc<Pool>,
43		statement_store: Option<Arc<StatementStore>>,
44	) -> sc_service::error::Result<RpcExtension>;
45}
46
47pub(crate) struct BuildParachainRpcExtensions<Block, RuntimeApi>(PhantomData<(Block, RuntimeApi)>);
48
49impl<Block: BlockT, RuntimeApi>
50	BuildRpcExtensions<
51		ParachainClient<Block, RuntimeApi>,
52		ParachainBackend<Block>,
53		sc_transaction_pool::TransactionPoolHandle<Block, ParachainClient<Block, RuntimeApi>>,
54		sc_statement_store::Store,
55	> for BuildParachainRpcExtensions<Block, RuntimeApi>
56where
57	RuntimeApi:
58		ConstructNodeRuntimeApi<Block, ParachainClient<Block, RuntimeApi>> + Send + Sync + 'static,
59	RuntimeApi::RuntimeApi: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
60		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
61{
62	fn build_rpc_extensions(
63		client: Arc<ParachainClient<Block, RuntimeApi>>,
64		backend: Arc<ParachainBackend<Block>>,
65		pool: Arc<
66			sc_transaction_pool::TransactionPoolHandle<Block, ParachainClient<Block, RuntimeApi>>,
67		>,
68		statement_store: Option<Arc<sc_statement_store::Store>>,
69	) -> sc_service::error::Result<RpcExtension> {
70		let build = || -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>> {
71			let mut module = RpcExtension::new(());
72
73			module.merge(System::new(client.clone(), pool).into_rpc())?;
74			module.merge(TransactionPayment::new(client.clone()).into_rpc())?;
75			module.merge(StateMigration::new(client.clone(), backend).into_rpc())?;
76			if let Some(statement_store) = statement_store {
77				module.merge(StatementStore::new(statement_store).into_rpc())?;
78			}
79			module.merge(Dev::new(client).into_rpc())?;
80
81			Ok(module)
82		};
83		build().map_err(Into::into)
84	}
85}