git.delta.rocks / unique-network / refs/commits / 4da04995c680

difftreelog

source

node/cli/src/rpc.rs7.7 KiBsourcehistory
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 fc_mapping_sync::{EthereumBlockNotification, EthereumBlockNotificationSinks};20use fc_rpc::{EthBlockDataCacheTask, EthConfig, OverrideHandle};21use fc_rpc_core::types::{FeeHistoryCache, FilterPool};22use fp_rpc::NoTransactionConverter;23use jsonrpsee::RpcModule;24use sc_client_api::{25	backend::{AuxStore, StorageProvider},26	client::BlockchainEvents,27	UsageProvider,28};29use sc_network::NetworkService;30use sc_network_sync::SyncingService;31use sc_rpc::SubscriptionTaskExecutor;32pub use sc_rpc_api::DenyUnsafe;33use sc_service::TransactionPool;34use sc_transaction_pool::{ChainApi, Pool};35use sp_api::ProvideRuntimeApi;36use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};37use sp_inherents::CreateInherentDataProviders;38use sp_runtime::traits::BlakeTwo256;39use up_common::types::opaque::*;4041use crate::service::RuntimeApiDep;4243#[cfg(feature = "pov-estimate")]44type FullBackend = sc_service::TFullBackend<Block>;4546/// Full client dependencies.47pub struct FullDeps<C, P, SC> {48	/// The client instance to use.49	pub client: Arc<C>,50	/// Transaction pool instance.51	pub pool: Arc<P>,52	/// The SelectChain Strategy53	pub select_chain: SC,54	/// Whether to deny unsafe calls55	pub deny_unsafe: DenyUnsafe,5657	/// Runtime identification (read from the chain spec)58	pub runtime_id: RuntimeId,59	/// Executor params for PoV estimating60	#[cfg(feature = "pov-estimate")]61	pub exec_params: uc_rpc::pov_estimate::ExecutorParams,62	/// Substrate Backend.63	#[cfg(feature = "pov-estimate")]64	pub backend: Arc<FullBackend>,65}6667/// Instantiate all Full RPC extensions.68pub fn create_full<C, P, SC, R, B>(69	io: &mut RpcModule<()>,70	deps: FullDeps<C, P, SC>,71) -> Result<(), Box<dyn std::error::Error + Send + Sync>>72where73	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,74	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,75	C: Send + Sync + 'static,76	C: BlockchainEvents<Block>,77	C::Api: RuntimeApiDep<R>,78	B: sc_client_api::Backend<Block> + Send + Sync + 'static,79	P: TransactionPool<Block = Block> + 'static,80	R: RuntimeInstance + Send + Sync + 'static,81	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,82	C: sp_api::CallApiAt<83		sp_runtime::generic::Block<84			sp_runtime::generic::Header<u32, BlakeTwo256>,85			sp_runtime::OpaqueExtrinsic,86		>,87	>,88	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,89{90	// use pallet_contracts_rpc::{Contracts, ContractsApi};91	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};92	use substrate_frame_rpc_system::{System, SystemApiServer};93	#[cfg(feature = "pov-estimate")]94	use uc_rpc::pov_estimate::{PovEstimate, PovEstimateApiServer};95	use uc_rpc::{AppPromotion, AppPromotionApiServer, Unique, UniqueApiServer};9697	let FullDeps {98		client,99		pool,100		select_chain: _,101		deny_unsafe,102103		runtime_id: _,104105		#[cfg(feature = "pov-estimate")]106		exec_params,107108		#[cfg(feature = "pov-estimate")]109		backend,110	} = deps;111112	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;113	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;114115	io.merge(Unique::new(client.clone()).into_rpc())?;116117	io.merge(AppPromotion::new(client).into_rpc())?;118119	#[cfg(feature = "pov-estimate")]120	io.merge(121		PovEstimate::new(122			client.clone(),123			backend,124			deny_unsafe,125			exec_params,126			runtime_id,127		)128		.into_rpc(),129	)?;130131	Ok(())132}133134pub struct EthDeps<C, P, CA: ChainApi, CIDP> {135	/// The client instance to use.136	pub client: Arc<C>,137	/// Transaction pool instance.138	pub pool: Arc<P>,139	/// Graph pool instance.140	pub graph: Arc<Pool<CA>>,141	/// Syncing service142	pub sync: Arc<SyncingService<Block>>,143	/// The Node authority flag144	pub is_authority: bool,145	/// Network service146	pub network: Arc<NetworkService<Block, Hash>>,147148	/// Ethereum Backend.149	pub eth_backend: Arc<dyn fc_api::Backend<Block> + Send + Sync>,150	/// Maximum number of logs in a query.151	pub max_past_logs: u32,152	/// Maximum fee history cache size.153	pub fee_history_limit: u64,154	/// Fee history cache.155	pub fee_history_cache: FeeHistoryCache,156	pub eth_block_data_cache: Arc<EthBlockDataCacheTask<Block>>,157	/// EthFilterApi pool.158	pub eth_filter_pool: Option<FilterPool>,159	pub eth_pubsub_notification_sinks:160		Arc<EthereumBlockNotificationSinks<EthereumBlockNotification<Block>>>,161	/// Whether to enable eth dev signer162	pub enable_dev_signer: bool,163164	pub overrides: Arc<OverrideHandle<Block>>,165	pub pending_create_inherent_data_providers: CIDP,166}167168pub fn create_eth<C, R, P, CA, B, CIDP, EC>(169	io: &mut RpcModule<()>,170	deps: EthDeps<C, P, CA, CIDP>,171	subscription_task_executor: SubscriptionTaskExecutor,172) -> Result<(), Box<dyn std::error::Error + Send + Sync>>173where174	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,175	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,176	C: Send + Sync + 'static,177	C: BlockchainEvents<Block>,178	C: UsageProvider<Block>,179	C::Api: RuntimeApiDep<R>,180	P: TransactionPool<Block = Block> + 'static,181	CA: ChainApi<Block = Block> + 'static,182	B: sc_client_api::Backend<Block> + Send + Sync + 'static,183	C: sp_api::CallApiAt<Block>,184	CIDP: CreateInherentDataProviders<Block, ()> + Send + 'static,185	EC: EthConfig<Block, C>,186	R: RuntimeInstance,187{188	use fc_rpc::{189		Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,190		EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,191	};192193	let EthDeps {194		client,195		pool,196		graph,197		eth_backend,198		max_past_logs,199		fee_history_limit,200		fee_history_cache,201		eth_block_data_cache,202		eth_filter_pool,203		eth_pubsub_notification_sinks,204		enable_dev_signer,205		sync,206		is_authority,207		network,208		overrides,209		pending_create_inherent_data_providers,210	} = deps;211212	let mut signers = Vec::new();213	if enable_dev_signer {214		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);215	}216	let execute_gas_limit_multiplier = 10;217	io.merge(218		Eth::<_, _, _, _, _, _, _, EC>::new(219			client.clone(),220			pool.clone(),221			graph.clone(),222			// We have no runtimes old enough to only accept converted transactions.223			None::<NoTransactionConverter>,224			sync.clone(),225			signers,226			overrides.clone(),227			eth_backend.clone(),228			is_authority,229			eth_block_data_cache.clone(),230			fee_history_cache,231			fee_history_limit,232			execute_gas_limit_multiplier,233			None,234			pending_create_inherent_data_providers,235			// Our extrinsics have nothing to do with consensus digest items yet.236			None,237		)238		.into_rpc(),239	)?;240241	if let Some(filter_pool) = eth_filter_pool {242		io.merge(243			EthFilter::new(244				client.clone(),245				eth_backend,246				graph,247				filter_pool,248				500_usize, // max stored filters249				max_past_logs,250				eth_block_data_cache,251			)252			.into_rpc(),253		)?;254	}255	io.merge(256		Net::new(257			client.clone(),258			network,259			// Whether to format the `peer_count` response as Hex (default) or not.260			true,261		)262		.into_rpc(),263	)?;264	io.merge(Web3::new(client.clone()).into_rpc())?;265	io.merge(266		EthPubSub::new(267			pool,268			client,269			sync,270			subscription_task_executor,271			overrides,272			eth_pubsub_notification_sinks,273		)274		.into_rpc(),275	)?;276277	Ok(())278}