git.delta.rocks / unique-network / refs/commits / 52f3d47930d0

difftreelog

source

node/rpc/src/lib.rs9.2 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 sp_runtime::traits::BlakeTwo256;18use fc_rpc::{19	EthBlockDataCacheTask, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override,20	StorageOverride, SchemaV2Override, SchemaV3Override,21};22use jsonrpsee::RpcModule;23use fc_rpc_core::types::{FilterPool, FeeHistoryCache};24use fp_storage::EthereumStorageSchema;25use sc_client_api::{26	backend::{AuxStore, StorageProvider},27	client::BlockchainEvents,28	StateBackend, Backend,29};30use sc_consensus_grandpa::{31	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,32};33use sc_network::NetworkService;34use sc_network_sync::SyncingService;35use sc_rpc::SubscriptionTaskExecutor;36pub use sc_rpc_api::DenyUnsafe;37use sc_transaction_pool::{ChainApi, Pool};38use sp_api::ProvideRuntimeApi;39use sp_block_builder::BlockBuilder;40use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};41use sc_service::TransactionPool;42use std::{collections::BTreeMap, sync::Arc};4344use up_common::types::opaque::*;4546#[cfg(feature = "pov-estimate")]47type FullBackend = sc_service::TFullBackend<Block>;4849/// Extra dependencies for GRANDPA50pub struct GrandpaDeps<B> {51	/// Voting round info.52	pub shared_voter_state: SharedVoterState,53	/// Authority set info.54	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,55	/// Receives notifications about justification events from Grandpa.56	pub justification_stream: GrandpaJustificationStream<Block>,57	/// Executor to drive the subscription manager in the Grandpa RPC handler.58	pub subscription_executor: SubscriptionTaskExecutor,59	/// Finality proof provider.60	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,61}6263/// Full client dependencies.64pub struct FullDeps<C, P, SC, CA: ChainApi> {65	/// The client instance to use.66	pub client: Arc<C>,67	/// Transaction pool instance.68	pub pool: Arc<P>,69	/// Graph pool instance.70	pub graph: Arc<Pool<CA>>,71	/// The SelectChain Strategy72	pub select_chain: SC,73	/// The Node authority flag74	pub is_authority: bool,75	/// Whether to enable dev signer76	pub enable_dev_signer: bool,77	/// Network service78	pub network: Arc<NetworkService<Block, Hash>>,79	/// Syncing service80	pub sync: Arc<SyncingService<Block>>,81	/// Whether to deny unsafe calls82	pub deny_unsafe: DenyUnsafe,83	/// EthFilterApi pool.84	pub filter_pool: Option<FilterPool>,8586	/// Runtime identification (read from the chain spec)87	pub runtime_id: RuntimeId,88	/// Executor params for PoV estimating89	#[cfg(feature = "pov-estimate")]90	pub exec_params: uc_rpc::pov_estimate::ExecutorParams,91	/// Substrate Backend.92	#[cfg(feature = "pov-estimate")]93	pub backend: Arc<FullBackend>,9495	/// Ethereum Backend.96	pub eth_backend: Arc<fc_db::Backend<Block>>,97	/// Maximum number of logs in a query.98	pub max_past_logs: u32,99	/// Maximum fee history cache size.100	pub fee_history_limit: u64,101	/// Fee history cache.102	pub fee_history_cache: FeeHistoryCache,103	/// Cache for Ethereum block data.104	pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,105106	pub pubsub_notification_sinks: Arc<107		fc_mapping_sync::EthereumBlockNotificationSinks<108			fc_mapping_sync::EthereumBlockNotification<Block>,109		>,110	>,111}112113pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>114where115	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,116	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,117	C: Send + Sync + 'static,118	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,119	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,120	BE: Backend<Block> + 'static,121	BE::State: StateBackend<BlakeTwo256>,122	R: RuntimeInstance + Send + Sync + 'static,123{124	let mut overrides_map = BTreeMap::new();125	overrides_map.insert(126		EthereumStorageSchema::V1,127		Box::new(SchemaV1Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,128	);129	overrides_map.insert(130		EthereumStorageSchema::V2,131		Box::new(SchemaV2Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,132	);133	overrides_map.insert(134		EthereumStorageSchema::V3,135		Box::new(SchemaV3Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,136	);137138	Arc::new(OverrideHandle {139		schemas: overrides_map,140		fallback: Box::new(RuntimeApiStorageOverride::new(client)),141	})142}143144/// Instantiate all Full RPC extensions.145pub fn create_full<C, P, SC, CA, R, A, B>(146	deps: FullDeps<C, P, SC, CA>,147	subscription_task_executor: SubscriptionTaskExecutor,148) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>149where150	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,151	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,152	C: Send + Sync + 'static,153	C: BlockchainEvents<Block>,154	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,155	C::Api: BlockBuilder<Block>,156	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,157	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,158	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,159	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,160	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,161	C::Api: app_promotion_rpc::AppPromotionApi<162		Block,163		BlockNumber,164		<R as RuntimeInstance>::CrossAccountId,165		AccountId,166	>,167	C::Api: up_pov_estimate_rpc::PovEstimateApi<Block>,168	B: sc_client_api::Backend<Block> + Send + Sync + 'static,169	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,170	P: TransactionPool<Block = Block> + 'static,171	CA: ChainApi<Block = Block> + 'static,172	R: RuntimeInstance + Send + Sync + 'static,173	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,174	C: sp_api::CallApiAt<175		sp_runtime::generic::Block<176			sp_runtime::generic::Header<u32, BlakeTwo256>,177			sp_runtime::OpaqueExtrinsic,178		>,179	>,180	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,181{182	use fc_rpc::{183		Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,184		EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,185	};186	use uc_rpc::{UniqueApiServer, Unique};187188	use uc_rpc::{AppPromotionApiServer, AppPromotion};189190	#[cfg(feature = "pov-estimate")]191	use uc_rpc::pov_estimate::{PovEstimateApiServer, PovEstimate};192193	// use pallet_contracts_rpc::{Contracts, ContractsApi};194	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};195	use substrate_frame_rpc_system::{System, SystemApiServer};196197	let mut io = RpcModule::new(());198	let FullDeps {199		client,200		pool,201		graph,202		select_chain: _,203		fee_history_limit,204		fee_history_cache,205		block_data_cache,206		enable_dev_signer,207		is_authority,208		network,209		sync,210		deny_unsafe,211		filter_pool,212213		runtime_id: _,214215		#[cfg(feature = "pov-estimate")]216		exec_params,217218		#[cfg(feature = "pov-estimate")]219		backend,220221		eth_backend,222		max_past_logs,223		pubsub_notification_sinks,224	} = deps;225226	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;227	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;228229	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));230231	let mut signers = Vec::new();232	if enable_dev_signer {233		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);234	}235236	let overrides = overrides_handle::<_, _, R>(client.clone());237238	let execute_gas_limit_multiplier = 10;239	io.merge(240		Eth::new(241			client.clone(),242			pool.clone(),243			graph,244			Some(<R as RuntimeInstance>::get_transaction_converter()),245			sync.clone(),246			signers,247			overrides.clone(),248			eth_backend.clone(),249			is_authority,250			block_data_cache.clone(),251			fee_history_cache,252			fee_history_limit,253			execute_gas_limit_multiplier,254			None,255		)256		.into_rpc(),257	)?;258259	io.merge(Unique::new(client.clone()).into_rpc())?;260261	io.merge(AppPromotion::new(client.clone()).into_rpc())?;262263	#[cfg(feature = "pov-estimate")]264	io.merge(265		PovEstimate::new(266			client.clone(),267			backend,268			deny_unsafe,269			exec_params,270			runtime_id,271		)272		.into_rpc(),273	)?;274275	if let Some(filter_pool) = filter_pool {276		io.merge(277			EthFilter::new(278				client.clone(),279				eth_backend,280				filter_pool,281				500_usize, // max stored filters282				max_past_logs,283				block_data_cache,284			)285			.into_rpc(),286		)?;287	}288289	io.merge(290		Net::new(291			client.clone(),292			network.clone(),293			// Whether to format the `peer_count` response as Hex (default) or not.294			true,295		)296		.into_rpc(),297	)?;298299	io.merge(Web3::new(client.clone()).into_rpc())?;300301	io.merge(302		EthPubSub::new(303			pool,304			client,305			sync,306			subscription_task_executor,307			overrides,308			pubsub_notification_sinks,309		)310		.into_rpc(),311	)?;312313	Ok(io)314}