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

difftreelog

source

node/rpc/src/lib.rs9.3 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::*;4445// RMRK46use up_data_structs::{47	RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo,48	RmrkPartType, RmrkTheme,49};5051#[cfg(feature = "pov-estimate")]52type FullBackend = sc_service::TFullBackend<Block>;5354/// Extra dependencies for GRANDPA55pub struct GrandpaDeps<B> {56	/// Voting round info.57	pub shared_voter_state: SharedVoterState,58	/// Authority set info.59	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,60	/// Receives notifications about justification events from Grandpa.61	pub justification_stream: GrandpaJustificationStream<Block>,62	/// Executor to drive the subscription manager in the Grandpa RPC handler.63	pub subscription_executor: SubscriptionTaskExecutor,64	/// Finality proof provider.65	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,66}6768/// Full client dependencies.69pub struct FullDeps<C, P, SC, CA: ChainApi> {70	/// The client instance to use.71	pub client: Arc<C>,72	/// Transaction pool instance.73	pub pool: Arc<P>,74	/// Graph pool instance.75	pub graph: Arc<Pool<CA>>,76	/// The SelectChain Strategy77	pub select_chain: SC,78	/// The Node authority flag79	pub is_authority: bool,80	/// Whether to enable dev signer81	pub enable_dev_signer: bool,82	/// Network service83	pub network: Arc<NetworkService<Block, Hash>>,84	/// Whether to deny unsafe calls85	pub deny_unsafe: DenyUnsafe,86	/// EthFilterApi pool.87	pub filter_pool: Option<FilterPool>,8889	#[cfg(feature = "pov-estimate")]90	pub runtime_id: RuntimeId,91	/// Executor params for PoV estimating92	#[cfg(feature = "pov-estimate")]93	pub exec_params: uc_rpc::pov_estimate::ExecutorParams,94	/// Substrate Backend.95	#[cfg(feature = "pov-estimate")]96	pub backend: Arc<FullBackend>,9798	/// Ethereum Backend.99	pub eth_backend: Arc<fc_db::Backend<Block>>,100	/// Maximum number of logs in a query.101	pub max_past_logs: u32,102	/// Maximum fee history cache size.103	pub fee_history_limit: u64,104	/// Fee history cache.105	pub fee_history_cache: FeeHistoryCache,106	/// Cache for Ethereum block data.107	pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,108}109110pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>111where112	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,113	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,114	C: Send + Sync + 'static,115	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,116	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,117	BE: Backend<Block> + 'static,118	BE::State: StateBackend<BlakeTwo256>,119	R: RuntimeInstance + Send + Sync + 'static,120{121	let mut overrides_map = BTreeMap::new();122	overrides_map.insert(123		EthereumStorageSchema::V1,124		Box::new(SchemaV1Override::new(client.clone()))125			as Box<dyn StorageOverride<_> + Send + Sync>,126	);127	overrides_map.insert(128		EthereumStorageSchema::V2,129		Box::new(SchemaV2Override::new(client.clone()))130			as Box<dyn StorageOverride<_> + Send + Sync>,131	);132	overrides_map.insert(133		EthereumStorageSchema::V3,134		Box::new(SchemaV3Override::new(client.clone()))135			as Box<dyn StorageOverride<_> + Send + Sync>,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: rmrk_rpc::RmrkApi<168		Block,169		AccountId,170		RmrkCollectionInfo<AccountId>,171		RmrkInstanceInfo<AccountId>,172		RmrkResourceInfo,173		RmrkPropertyInfo,174		RmrkBaseInfo<AccountId>,175		RmrkPartType,176		RmrkTheme,177	>,178	C::Api: up_pov_estimate_rpc::PovEstimateApi<Block>,179	B: sc_client_api::Backend<Block> + Send + Sync + 'static,180	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,181	P: TransactionPool<Block = Block> + 'static,182	CA: ChainApi<Block = Block> + 'static,183	R: RuntimeInstance + Send + Sync + 'static,184	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,185	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,186{187	use fc_rpc::{188		Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,189		EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,190	};191	use uc_rpc::{UniqueApiServer, Unique};192193	#[cfg(not(feature = "unique-runtime"))]194	use uc_rpc::{AppPromotionApiServer, AppPromotion};195196	#[cfg(not(feature = "unique-runtime"))]197	use uc_rpc::{RmrkApiServer, Rmrk};198199	#[cfg(feature = "pov-estimate")]200	use uc_rpc::pov_estimate::{PovEstimateApiServer, PovEstimate};201202	// use pallet_contracts_rpc::{Contracts, ContractsApi};203	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};204	use substrate_frame_rpc_system::{System, SystemApiServer};205206	let mut io = RpcModule::new(());207	let FullDeps {208		client,209		pool,210		graph,211		select_chain: _,212		fee_history_limit,213		fee_history_cache,214		block_data_cache,215		enable_dev_signer,216		is_authority,217		network,218		deny_unsafe,219		filter_pool,220221		#[cfg(feature = "pov-estimate")]222		runtime_id,223224		#[cfg(feature = "pov-estimate")]225		exec_params,226227		#[cfg(feature = "pov-estimate")]228		backend,229230		eth_backend,231		max_past_logs,232	} = deps;233234	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;235	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;236237	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));238239	let mut signers = Vec::new();240	if enable_dev_signer {241		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);242	}243244	let overrides = overrides_handle::<_, _, R>(client.clone());245246	let execute_gas_limit_multiplier = 10;247	io.merge(248		Eth::new(249			client.clone(),250			pool.clone(),251			graph,252			Some(<R as RuntimeInstance>::get_transaction_converter()),253			network.clone(),254			signers,255			overrides.clone(),256			eth_backend.clone(),257			is_authority,258			block_data_cache.clone(),259			fee_history_cache,260			fee_history_limit,261			execute_gas_limit_multiplier,262		)263		.into_rpc(),264	)?;265266	io.merge(Unique::new(client.clone()).into_rpc())?;267268	#[cfg(not(feature = "unique-runtime"))]269	io.merge(AppPromotion::new(client.clone()).into_rpc())?;270271	#[cfg(not(feature = "unique-runtime"))]272	io.merge(Rmrk::new(client.clone()).into_rpc())?;273274	#[cfg(feature = "pov-estimate")]275	io.merge(276		PovEstimate::new(277			client.clone(),278			backend,279			deny_unsafe,280			exec_params,281			runtime_id,282		)283		.into_rpc(),284	)?;285286	if let Some(filter_pool) = filter_pool {287		io.merge(288			EthFilter::new(289				client.clone(),290				eth_backend,291				filter_pool,292				500_usize, // max stored filters293				max_past_logs,294				block_data_cache,295			)296			.into_rpc(),297		)?;298	}299300	io.merge(301		Net::new(302			client.clone(),303			network.clone(),304			// Whether to format the `peer_count` response as Hex (default) or not.305			true,306		)307		.into_rpc(),308	)?;309310	io.merge(Web3::new(client.clone()).into_rpc())?;311312	io.merge(313		EthPubSub::new(pool, client, network, subscription_task_executor, overrides).into_rpc(),314	)?;315316	Ok(io)317}