git.delta.rocks / unique-network / refs/commits / 8a2ef94dc142

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 common_types::opaque::{44	Hash, AccountId, RuntimeInstance, Index, Block, BlockNumber, Balance,45};4647// RMRK48use up_data_structs::{49	RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo,50	RmrkPartType, RmrkTheme,51};5253/// Extra dependencies for GRANDPA54pub struct GrandpaDeps<B> {55	/// Voting round info.56	pub shared_voter_state: SharedVoterState,57	/// Authority set info.58	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,59	/// Receives notifications about justification events from Grandpa.60	pub justification_stream: GrandpaJustificationStream<Block>,61	/// Executor to drive the subscription manager in the Grandpa RPC handler.62	pub subscription_executor: SubscriptionTaskExecutor,63	/// Finality proof provider.64	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,65}6667/// Full client dependencies.68pub struct FullDeps<C, P, SC, CA: ChainApi> {69	/// The client instance to use.70	pub client: Arc<C>,71	/// Transaction pool instance.72	pub pool: Arc<P>,73	/// Graph pool instance.74	pub graph: Arc<Pool<CA>>,75	/// The SelectChain Strategy76	pub select_chain: SC,77	/// The Node authority flag78	pub is_authority: bool,79	/// Whether to enable dev signer80	pub enable_dev_signer: bool,81	/// Network service82	pub network: Arc<NetworkService<Block, Hash>>,83	/// Whether to deny unsafe calls84	pub deny_unsafe: DenyUnsafe,85	/// EthFilterApi pool.86	pub filter_pool: Option<FilterPool>,87	/// Backend.88	pub backend: Arc<fc_db::Backend<Block>>,89	/// Maximum number of logs in a query.90	pub max_past_logs: u32,91	/// Maximum fee history cache size.92	pub fee_history_limit: u64,93	/// Fee history cache.94	pub fee_history_cache: FeeHistoryCache,95	/// Cache for Ethereum block data.96	pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,97}9899pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>100where101	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,102	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,103	C: Send + Sync + 'static,104	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,105	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,106	BE: Backend<Block> + 'static,107	BE::State: StateBackend<BlakeTwo256>,108	R: RuntimeInstance + Send + Sync + 'static,109{110	let mut overrides_map = BTreeMap::new();111	overrides_map.insert(112		EthereumStorageSchema::V1,113		Box::new(SchemaV1Override::new(client.clone()))114			as Box<dyn StorageOverride<_> + Send + Sync>,115	);116	overrides_map.insert(117		EthereumStorageSchema::V2,118		Box::new(SchemaV2Override::new(client.clone()))119			as Box<dyn StorageOverride<_> + Send + Sync>,120	);121	overrides_map.insert(122		EthereumStorageSchema::V3,123		Box::new(SchemaV3Override::new(client.clone()))124			as Box<dyn StorageOverride<_> + Send + Sync>,125	);126127	Arc::new(OverrideHandle {128		schemas: overrides_map,129		fallback: Box::new(RuntimeApiStorageOverride::new(client)),130	})131}132133/// Instantiate all Full RPC extensions.134pub fn create_full<C, P, SC, CA, R, A, B>(135	deps: FullDeps<C, P, SC, CA>,136	subscription_task_executor: SubscriptionTaskExecutor,137) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>138where139	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,140	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,141	C: Send + Sync + 'static,142	C: BlockchainEvents<Block>,143	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,144	C::Api: BlockBuilder<Block>,145	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,146	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,147	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,148	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,149	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,150	C::Api: rmrk_rpc::RmrkApi<151		Block,152		AccountId,153		RmrkCollectionInfo<AccountId>,154		RmrkInstanceInfo<AccountId>,155		RmrkResourceInfo,156		RmrkPropertyInfo,157		RmrkBaseInfo<AccountId>,158		RmrkPartType,159		RmrkTheme,160	>,161	B: sc_client_api::Backend<Block> + Send + Sync + 'static,162	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,163	P: TransactionPool<Block = Block> + 'static,164	CA: ChainApi<Block = Block> + 'static,165	R: RuntimeInstance + Send + Sync + 'static,166	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,167	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,168{169	use fc_rpc::{170		Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,171		EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,172	};173	use uc_rpc::{UniqueApiServer, Unique};174175	#[cfg(not(feature = "unique-runtime"))]176	use uc_rpc::{RmrkApiServer, Rmrk};177178	// use pallet_contracts_rpc::{Contracts, ContractsApi};179	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};180	use substrate_frame_rpc_system::{System, SystemApiServer};181182	let mut io = RpcModule::new(());183	let FullDeps {184		client,185		pool,186		graph,187		select_chain: _,188		fee_history_limit,189		fee_history_cache,190		block_data_cache,191		enable_dev_signer,192		is_authority,193		network,194		deny_unsafe,195		filter_pool,196		backend,197		max_past_logs,198	} = deps;199200	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;201	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;202203	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));204205	let mut signers = Vec::new();206	if enable_dev_signer {207		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);208	}209210	let overrides = overrides_handle::<_, _, R>(client.clone());211212	io.merge(213		Eth::new(214			client.clone(),215			pool.clone(),216			graph,217			Some(<R as RuntimeInstance>::get_transaction_converter()),218			network.clone(),219			signers,220			overrides.clone(),221			backend.clone(),222			is_authority,223			block_data_cache.clone(),224			fee_history_cache,225			fee_history_limit,226		)227		.into_rpc(),228	)?;229230	io.merge(Unique::new(client.clone()).into_rpc())?;231232	#[cfg(not(feature = "unique-runtime"))]233	io.merge(Rmrk::new(client.clone()).into_rpc())?;234235	if let Some(filter_pool) = filter_pool {236		io.merge(237			EthFilter::new(238				client.clone(),239				backend,240				filter_pool,241				500_usize, // max stored filters242				max_past_logs,243				block_data_cache,244			)245			.into_rpc(),246		)?;247	}248249	io.merge(250		Net::new(251			client.clone(),252			network.clone(),253			// Whether to format the `peer_count` response as Hex (default) or not.254			true,255		)256		.into_rpc(),257	)?;258259	io.merge(Web3::new(client.clone()).into_rpc())?;260261	io.merge(262		EthPubSub::new(pool, client, network, subscription_task_executor, overrides).into_rpc(),263	)?;264265	Ok(io)266}