git.delta.rocks / unique-network / refs/commits / 88e36460f932

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 up_common::types::opaque::{Hash, AccountId, RuntimeInstance, Index, Block, BlockNumber, Balance};4445// RMRK46use up_data_structs::{47	RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo,48	RmrkPartType, RmrkTheme,49};5051/// Extra dependencies for GRANDPA52pub struct GrandpaDeps<B> {53	/// Voting round info.54	pub shared_voter_state: SharedVoterState,55	/// Authority set info.56	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,57	/// Receives notifications about justification events from Grandpa.58	pub justification_stream: GrandpaJustificationStream<Block>,59	/// Executor to drive the subscription manager in the Grandpa RPC handler.60	pub subscription_executor: SubscriptionTaskExecutor,61	/// Finality proof provider.62	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,63}6465/// Full client dependencies.66pub struct FullDeps<C, P, SC, CA: ChainApi> {67	/// The client instance to use.68	pub client: Arc<C>,69	/// Transaction pool instance.70	pub pool: Arc<P>,71	/// Graph pool instance.72	pub graph: Arc<Pool<CA>>,73	/// The SelectChain Strategy74	pub select_chain: SC,75	/// The Node authority flag76	pub is_authority: bool,77	/// Whether to enable dev signer78	pub enable_dev_signer: bool,79	/// Network service80	pub network: Arc<NetworkService<Block, Hash>>,81	/// Whether to deny unsafe calls82	pub deny_unsafe: DenyUnsafe,83	/// EthFilterApi pool.84	pub filter_pool: Option<FilterPool>,85	/// Backend.86	pub backend: Arc<fc_db::Backend<Block>>,87	/// Maximum number of logs in a query.88	pub max_past_logs: u32,89	/// Maximum fee history cache size.90	pub fee_history_limit: u64,91	/// Fee history cache.92	pub fee_history_cache: FeeHistoryCache,93	/// Cache for Ethereum block data.94	pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,95}9697pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>98where99	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,100	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,101	C: Send + Sync + 'static,102	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,103	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,104	BE: Backend<Block> + 'static,105	BE::State: StateBackend<BlakeTwo256>,106	R: RuntimeInstance + Send + Sync + 'static,107{108	let mut overrides_map = BTreeMap::new();109	overrides_map.insert(110		EthereumStorageSchema::V1,111		Box::new(SchemaV1Override::new(client.clone()))112			as Box<dyn StorageOverride<_> + Send + Sync>,113	);114	overrides_map.insert(115		EthereumStorageSchema::V2,116		Box::new(SchemaV2Override::new(client.clone()))117			as Box<dyn StorageOverride<_> + Send + Sync>,118	);119	overrides_map.insert(120		EthereumStorageSchema::V3,121		Box::new(SchemaV3Override::new(client.clone()))122			as Box<dyn StorageOverride<_> + Send + Sync>,123	);124125	Arc::new(OverrideHandle {126		schemas: overrides_map,127		fallback: Box::new(RuntimeApiStorageOverride::new(client)),128	})129}130131/// Instantiate all Full RPC extensions.132pub fn create_full<C, P, SC, CA, R, A, B>(133	deps: FullDeps<C, P, SC, CA>,134	subscription_task_executor: SubscriptionTaskExecutor,135) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>136where137	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,138	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,139	C: Send + Sync + 'static,140	C: BlockchainEvents<Block>,141	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,142	C::Api: BlockBuilder<Block>,143	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,144	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,145	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,146	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,147	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,148	C::Api: rmrk_rpc::RmrkApi<149		Block,150		AccountId,151		RmrkCollectionInfo<AccountId>,152		RmrkInstanceInfo<AccountId>,153		RmrkResourceInfo,154		RmrkPropertyInfo,155		RmrkBaseInfo<AccountId>,156		RmrkPartType,157		RmrkTheme,158	>,159	B: sc_client_api::Backend<Block> + Send + Sync + 'static,160	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,161	P: TransactionPool<Block = Block> + 'static,162	CA: ChainApi<Block = Block> + 'static,163	R: RuntimeInstance + Send + Sync + 'static,164	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,165	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,166{167	use fc_rpc::{168		Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,169		EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,170	};171	use uc_rpc::{UniqueApiServer, Unique};172173	#[cfg(not(feature = "unique-runtime"))]174	use uc_rpc::{RmrkApiServer, Rmrk};175176	// use pallet_contracts_rpc::{Contracts, ContractsApi};177	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};178	use substrate_frame_rpc_system::{System, SystemApiServer};179180	let mut io = RpcModule::new(());181	let FullDeps {182		client,183		pool,184		graph,185		select_chain: _,186		fee_history_limit,187		fee_history_cache,188		block_data_cache,189		enable_dev_signer,190		is_authority,191		network,192		deny_unsafe,193		filter_pool,194		backend,195		max_past_logs,196	} = deps;197198	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;199	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;200201	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));202203	let mut signers = Vec::new();204	if enable_dev_signer {205		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);206	}207208	let overrides = overrides_handle::<_, _, R>(client.clone());209210	io.merge(211		Eth::new(212			client.clone(),213			pool.clone(),214			graph,215			Some(<R as RuntimeInstance>::get_transaction_converter()),216			network.clone(),217			signers,218			overrides.clone(),219			backend.clone(),220			is_authority,221			block_data_cache.clone(),222			fee_history_cache,223			fee_history_limit,224		)225		.into_rpc(),226	)?;227228	io.merge(Unique::new(client.clone()).into_rpc())?;229230	#[cfg(not(feature = "unique-runtime"))]231	io.merge(Rmrk::new(client.clone()).into_rpc())?;232233	if let Some(filter_pool) = filter_pool {234		io.merge(235			EthFilter::new(236				client.clone(),237				backend,238				filter_pool,239				500_usize, // max stored filters240				max_past_logs,241				block_data_cache,242			)243			.into_rpc(),244		)?;245	}246247	io.merge(248		Net::new(249			client.clone(),250			network.clone(),251			// Whether to format the `peer_count` response as Hex (default) or not.252			true,253		)254		.into_rpc(),255	)?;256257	io.merge(Web3::new(client.clone()).into_rpc())?;258259	io.merge(260		EthPubSub::new(pool, client, network, subscription_task_executor, overrides).into_rpc(),261	)?;262263	Ok(io)264}