git.delta.rocks / unique-network / refs/commits / a1bfd4b3cd67

difftreelog

source

node/rpc/src/lib.rs8.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_finality_grandpa::{31	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,32};33use sc_network::NetworkService;34use sc_rpc::SubscriptionTaskExecutor;35pub use sc_rpc_api::DenyUnsafe;36use sc_transaction_pool::{ChainApi, Pool};37use sp_api::ProvideRuntimeApi;38use sp_block_builder::BlockBuilder;39use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};40use sc_service::TransactionPool;41use std::{collections::BTreeMap, sync::Arc};4243use unique_runtime_common::types::{44	Hash, AccountId, RuntimeInstance, Index, Block, BlockNumber, Balance,45};46// RMRK47use up_data_structs::{48	RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo,49	RmrkPartType, RmrkTheme,50};5152/// Extra dependencies for GRANDPA53pub struct GrandpaDeps<B> {54	/// Voting round info.55	pub shared_voter_state: SharedVoterState,56	/// Authority set info.57	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,58	/// Receives notifications about justification events from Grandpa.59	pub justification_stream: GrandpaJustificationStream<Block>,60	/// Executor to drive the subscription manager in the Grandpa RPC handler.61	pub subscription_executor: SubscriptionTaskExecutor,62	/// Finality proof provider.63	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,64}6566/// Full client dependencies.67pub struct FullDeps<C, P, SC, CA: ChainApi> {68	/// The client instance to use.69	pub client: Arc<C>,70	/// Transaction pool instance.71	pub pool: Arc<P>,72	/// Graph pool instance.73	pub graph: Arc<Pool<CA>>,74	/// The SelectChain Strategy75	pub select_chain: SC,76	/// The Node authority flag77	pub is_authority: bool,78	/// Whether to enable dev signer79	pub enable_dev_signer: bool,80	/// Network service81	pub network: Arc<NetworkService<Block, Hash>>,82	/// Whether to deny unsafe calls83	pub deny_unsafe: DenyUnsafe,84	/// EthFilterApi pool.85	pub filter_pool: Option<FilterPool>,86	/// Backend.87	pub backend: Arc<fc_db::Backend<Block>>,88	/// Maximum number of logs in a query.89	pub max_past_logs: u32,90	/// Maximum fee history cache size.91	pub fee_history_limit: u64,92	/// Fee history cache.93	pub fee_history_cache: FeeHistoryCache,94	/// Cache for Ethereum block data.95	pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,96}9798pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>99where100	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,101	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,102	C: Send + Sync + 'static,103	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,104	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,105	BE: Backend<Block> + 'static,106	BE::State: StateBackend<BlakeTwo256>,107	R: RuntimeInstance + Send + Sync + 'static,108{109	let mut overrides_map = BTreeMap::new();110	overrides_map.insert(111		EthereumStorageSchema::V1,112		Box::new(SchemaV1Override::new(client.clone()))113			as Box<dyn StorageOverride<_> + Send + Sync>,114	);115	overrides_map.insert(116		EthereumStorageSchema::V2,117		Box::new(SchemaV2Override::new(client.clone()))118			as Box<dyn StorageOverride<_> + Send + Sync>,119	);120	overrides_map.insert(121		EthereumStorageSchema::V3,122		Box::new(SchemaV3Override::new(client.clone()))123			as Box<dyn StorageOverride<_> + Send + Sync>,124	);125126	Arc::new(OverrideHandle {127		schemas: overrides_map,128		fallback: Box::new(RuntimeApiStorageOverride::new(client)),129	})130}131132/// Instantiate all Full RPC extensions.133pub fn create_full<C, P, SC, CA, R, A, B>(134	deps: FullDeps<C, P, SC, CA>,135	subscription_task_executor: SubscriptionTaskExecutor,136) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>137where138	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,139	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,140	C: Send + Sync + 'static,141	C: BlockchainEvents<Block>,142	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,143	C::Api: BlockBuilder<Block>,144	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,145	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,146	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,147	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,148	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,149	C::Api: rmrk_rpc::RmrkApi<150		Block,151		AccountId,152		RmrkCollectionInfo<AccountId>,153		RmrkInstanceInfo<AccountId>,154		RmrkResourceInfo,155		RmrkPropertyInfo,156		RmrkBaseInfo<AccountId>,157		RmrkPartType,158		RmrkTheme,159	>,160	B: sc_client_api::Backend<Block> + Send + Sync + 'static,161	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,162	P: TransactionPool<Block = Block> + 'static,163	CA: ChainApi<Block = Block> + 'static,164	R: RuntimeInstance + Send + Sync + 'static,165	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,166	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,167{168	use fc_rpc::{169		Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,170		EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,171	};172	use uc_rpc::{UniqueApiServer, Unique};173174	#[cfg(not(feature = "unique-runtime"))]175	use uc_rpc::{RmrkApiServer, Rmrk};176177	// use pallet_contracts_rpc::{Contracts, ContractsApi};178	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};179	use substrate_frame_rpc_system::{System, SystemApiServer};180181	let mut io = RpcModule::new(());182	let FullDeps {183		client,184		pool,185		graph,186		select_chain: _,187		fee_history_limit,188		fee_history_cache,189		block_data_cache,190		enable_dev_signer,191		is_authority,192		network,193		deny_unsafe,194		filter_pool,195		backend,196		max_past_logs,197	} = deps;198199	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;200	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;201202	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));203204	let mut signers = Vec::new();205	if enable_dev_signer {206		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);207	}208209	let overrides = overrides_handle::<_, _, R>(client.clone());210211	io.merge(212		Eth::new(213			client.clone(),214			pool.clone(),215			graph,216			Some(<R as RuntimeInstance>::get_transaction_converter()),217			network.clone(),218			signers,219			overrides.clone(),220			backend.clone(),221			is_authority,222			block_data_cache.clone(),223			fee_history_cache,224			fee_history_limit,225		)226		.into_rpc(),227	)?;228229	io.merge(Unique::new(client.clone()).into_rpc())?;230231	#[cfg(not(feature = "unique-runtime"))]232	io.merge(Rmrk::new(client.clone()).into_rpc())?;233234	if let Some(filter_pool) = filter_pool {235		io.merge(236			EthFilter::new(237				client.clone(),238				backend,239				filter_pool,240				500_usize, // max stored filters241				max_past_logs,242				block_data_cache,243			)244			.into_rpc(),245		)?;246	}247248	io.merge(249		Net::new(250			client.clone(),251			network.clone(),252			// Whether to format the `peer_count` response as Hex (default) or not.253			true,254		)255		.into_rpc(),256	)?;257258	io.merge(Web3::new(client.clone()).into_rpc())?;259260	io.merge(261		EthPubSub::new(pool, client, network, subscription_task_executor, overrides).into_rpc(),262	)?;263264	Ok(io)265}