git.delta.rocks / unique-network / refs/commits / 5b6b6125c041

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};5051type FullBackend = sc_service::TFullBackend<Block>;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	88	#[cfg(feature = "pov-estimate")]89	pub runtime_id: RuntimeId,90	/// Executor params for PoV estimating91	#[cfg(feature = "pov-estimate")]92	pub exec_params: uc_rpc::pov_estimate::ExecutorParams,93	/// Substrate Backend.94	#[cfg(feature = "pov-estimate")]95	pub backend: Arc<FullBackend>,9697	/// Ethereum Backend.98	pub eth_backend: Arc<fc_db::Backend<Block>>,99	/// Maximum number of logs in a query.100	pub max_past_logs: u32,101	/// Maximum fee history cache size.102	pub fee_history_limit: u64,103	/// Fee history cache.104	pub fee_history_cache: FeeHistoryCache,105	/// Cache for Ethereum block data.106	pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,107}108109pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>110where111	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,112	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,113	C: Send + Sync + 'static,114	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,115	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,116	BE: Backend<Block> + 'static,117	BE::State: StateBackend<BlakeTwo256>,118	R: RuntimeInstance + Send + Sync + 'static,119{120	let mut overrides_map = BTreeMap::new();121	overrides_map.insert(122		EthereumStorageSchema::V1,123		Box::new(SchemaV1Override::new(client.clone()))124			as Box<dyn StorageOverride<_> + Send + Sync>,125	);126	overrides_map.insert(127		EthereumStorageSchema::V2,128		Box::new(SchemaV2Override::new(client.clone()))129			as Box<dyn StorageOverride<_> + Send + Sync>,130	);131	overrides_map.insert(132		EthereumStorageSchema::V3,133		Box::new(SchemaV3Override::new(client.clone()))134			as Box<dyn StorageOverride<_> + Send + Sync>,135	);136137	Arc::new(OverrideHandle {138		schemas: overrides_map,139		fallback: Box::new(RuntimeApiStorageOverride::new(client)),140	})141}142143/// Instantiate all Full RPC extensions.144pub fn create_full<C, P, SC, CA, R, A, B>(145	deps: FullDeps<C, P, SC, CA>,146	subscription_task_executor: SubscriptionTaskExecutor,147) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>148where149	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,150	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,151	C: Send + Sync + 'static,152	C: BlockchainEvents<Block>,153	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,154	C::Api: BlockBuilder<Block>,155	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,156	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,157	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,158	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,159	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,160	C::Api: app_promotion_rpc::AppPromotionApi<161		Block,162		BlockNumber,163		<R as RuntimeInstance>::CrossAccountId,164		AccountId,165	>,166	C::Api: rmrk_rpc::RmrkApi<167		Block,168		AccountId,169		RmrkCollectionInfo<AccountId>,170		RmrkInstanceInfo<AccountId>,171		RmrkResourceInfo,172		RmrkPropertyInfo,173		RmrkBaseInfo<AccountId>,174		RmrkPartType,175		RmrkTheme,176	>,177	C::Api: up_pov_estimate_rpc::PovEstimateApi<Block>,178	B: sc_client_api::Backend<Block> + Send + Sync + 'static,179	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,180	P: TransactionPool<Block = Block> + 'static,181	CA: ChainApi<Block = Block> + 'static,182	R: RuntimeInstance + Send + Sync + 'static,183	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,184	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,185{186	use fc_rpc::{187		Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,188		EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,189	};190	use uc_rpc::{UniqueApiServer, Unique};191192	#[cfg(not(feature = "unique-runtime"))]193	use uc_rpc::{AppPromotionApiServer, AppPromotion};194195	#[cfg(not(feature = "unique-runtime"))]196	use uc_rpc::{RmrkApiServer, Rmrk};197198	#[cfg(feature = "pov-estimate")]199	use uc_rpc::pov_estimate::{PovEstimateApiServer, PovEstimate};200201	// use pallet_contracts_rpc::{Contracts, ContractsApi};202	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};203	use substrate_frame_rpc_system::{System, SystemApiServer};204205	let mut io = RpcModule::new(());206	let FullDeps {207		client,208		pool,209		graph,210		select_chain: _,211		fee_history_limit,212		fee_history_cache,213		block_data_cache,214		enable_dev_signer,215		is_authority,216		network,217		deny_unsafe,218		filter_pool,219220		#[cfg(feature = "pov-estimate")]221		runtime_id,222223		#[cfg(feature = "pov-estimate")]224		exec_params,225226		#[cfg(feature = "pov-estimate")]227		backend,228229		eth_backend,230		max_past_logs,231	} = deps;232233	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;234	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;235236	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));237238	let mut signers = Vec::new();239	if enable_dev_signer {240		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);241	}242243	let overrides = overrides_handle::<_, _, R>(client.clone());244245	let execute_gas_limit_multiplier = 10;246	io.merge(247		Eth::new(248			client.clone(),249			pool.clone(),250			graph,251			Some(<R as RuntimeInstance>::get_transaction_converter()),252			network.clone(),253			signers,254			overrides.clone(),255			eth_backend.clone(),256			is_authority,257			block_data_cache.clone(),258			fee_history_cache,259			fee_history_limit,260			execute_gas_limit_multiplier,261		)262		.into_rpc(),263	)?;264265	io.merge(Unique::new(client.clone()).into_rpc())?;266267	#[cfg(not(feature = "unique-runtime"))]268	io.merge(AppPromotion::new(client.clone()).into_rpc())?;269270	#[cfg(not(feature = "unique-runtime"))]271	io.merge(Rmrk::new(client.clone()).into_rpc())?;272273	#[cfg(feature = "pov-estimate")]274	io.merge(PovEstimate::new(client.clone(), backend, deny_unsafe, exec_params, runtime_id).into_rpc())?;275276	if let Some(filter_pool) = filter_pool {277		io.merge(278			EthFilter::new(279				client.clone(),280				eth_backend,281				filter_pool,282				500_usize, // max stored filters283				max_past_logs,284				block_data_cache,285			)286			.into_rpc(),287		)?;288	}289290	io.merge(291		Net::new(292			client.clone(),293			network.clone(),294			// Whether to format the `peer_count` response as Hex (default) or not.295			true,296		)297		.into_rpc(),298	)?;299300	io.merge(Web3::new(client.clone()).into_rpc())?;301302	io.merge(303		EthPubSub::new(pool, client, network, subscription_task_executor, overrides).into_rpc(),304	)?;305306	Ok(io)307}