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

difftreelog

source

node/rpc/src/lib.rs8.6 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	EthBlockDataCache, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override,20	StorageOverride, SchemaV2Override, SchemaV3Override,21};22use fc_rpc_core::types::{FilterPool, FeeHistoryCache};23use jsonrpc_pubsub::manager::SubscriptionManager;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// RMRK47/* TODO free RMRK! use up_data_structs::{48	RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo,49	RmrkPartType, RmrkTheme,50};*/5152/// Public io handler for exporting into other modules53pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;5455/// Extra dependencies for GRANDPA56pub struct GrandpaDeps<B> {57	/// Voting round info.58	pub shared_voter_state: SharedVoterState,59	/// Authority set info.60	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,61	/// Receives notifications about justification events from Grandpa.62	pub justification_stream: GrandpaJustificationStream<Block>,63	/// Executor to drive the subscription manager in the Grandpa RPC handler.64	pub subscription_executor: SubscriptionTaskExecutor,65	/// Finality proof provider.66	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,67}6869/// Full client dependencies.70pub struct FullDeps<C, P, SC, CA: ChainApi> {71	/// The client instance to use.72	pub client: Arc<C>,73	/// Transaction pool instance.74	pub pool: Arc<P>,75	/// Graph pool instance.76	pub graph: Arc<Pool<CA>>,77	/// The SelectChain Strategy78	pub select_chain: SC,79	/// The Node authority flag80	pub is_authority: bool,81	/// Whether to enable dev signer82	pub enable_dev_signer: bool,83	/// Network service84	pub network: Arc<NetworkService<Block, Hash>>,85	/// Whether to deny unsafe calls86	pub deny_unsafe: DenyUnsafe,87	/// EthFilterApi pool.88	pub filter_pool: Option<FilterPool>,89	/// Backend.90	pub backend: Arc<fc_db::Backend<Block>>,91	/// Maximum number of logs in a query.92	pub max_past_logs: u32,93	/// Maximum fee history cache size.94	pub fee_history_limit: u64,95	/// Fee history cache.96	pub fee_history_cache: FeeHistoryCache,97	/// Cache for Ethereum block data.98	pub block_data_cache: Arc<EthBlockDataCache<Block>>,99}100101pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>102where103	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,104	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,105	C: Send + Sync + 'static,106	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,107	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,108	BE: Backend<Block> + 'static,109	BE::State: StateBackend<BlakeTwo256>,110	R: RuntimeInstance + Send + Sync + 'static,111{112	let mut overrides_map = BTreeMap::new();113	overrides_map.insert(114		EthereumStorageSchema::V1,115		Box::new(SchemaV1Override::new(client.clone()))116			as Box<dyn StorageOverride<_> + Send + Sync>,117	);118	overrides_map.insert(119		EthereumStorageSchema::V2,120		Box::new(SchemaV2Override::new(client.clone()))121			as Box<dyn StorageOverride<_> + Send + Sync>,122	);123	overrides_map.insert(124		EthereumStorageSchema::V3,125		Box::new(SchemaV3Override::new(client.clone()))126			as Box<dyn StorageOverride<_> + Send + Sync>,127	);128129	Arc::new(OverrideHandle {130		schemas: overrides_map,131		fallback: Box::new(RuntimeApiStorageOverride::new(client)),132	})133}134135/// Instantiate all Full RPC extensions.136pub fn create_full<C, P, SC, CA, R, A, B>(137	deps: FullDeps<C, P, SC, CA>,138	subscription_task_executor: SubscriptionTaskExecutor,139) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>140where141	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,142	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,143	C: Send + Sync + 'static,144	C: BlockchainEvents<Block>,145	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,146	C::Api: BlockBuilder<Block>,147	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,148	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,149	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,150	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,151	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,152	/* TODO free RMRK!153	C::Api: rmrk_rpc::RmrkApi<154		Block,155		AccountId,156		RmrkCollectionInfo<AccountId>,157		RmrkInstanceInfo<AccountId>,158		RmrkResourceInfo,159		RmrkPropertyInfo,160		RmrkBaseInfo<AccountId>,161		RmrkPartType,162		RmrkTheme,163	>,*/164	B: sc_client_api::Backend<Block> + Send + Sync + 'static,165	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,166	P: TransactionPool<Block = Block> + 'static,167	CA: ChainApi<Block = Block> + 'static,168	R: RuntimeInstance + Send + Sync + 'static,169	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,170	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,171{172	use fc_rpc::{173		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,174		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,175		Web3ApiServer,176	};177	use uc_rpc::{UniqueApi, RmrkApi, Unique};178	// use pallet_contracts_rpc::{Contracts, ContractsApi};179	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};180	use substrate_frame_rpc_system::{FullSystem, SystemApi};181182	let mut io = jsonrpc_core::IoHandler::default();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.extend_with(SystemApi::to_delegate(FullSystem::new(201		client.clone(),202		pool.clone(),203		deny_unsafe,204	)));205206	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(207		client.clone(),208	)));209210	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));211212	let mut signers = Vec::new();213	if enable_dev_signer {214		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);215	}216217	let overrides = overrides_handle::<_, _, R>(client.clone());218219	io.extend_with(EthApiServer::to_delegate(EthApi::new(220		client.clone(),221		pool.clone(),222		graph,223		Some(<R as RuntimeInstance>::get_transaction_converter()),224		network.clone(),225		signers,226		overrides.clone(),227		backend.clone(),228		is_authority,229		block_data_cache.clone(),230		fee_history_limit,231		fee_history_cache,232	)));233234	// todo look into235	//let unique = Unique::new(client.clone());236	io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));237	// TODO free RMRK! io.extend_with(RmrkApi::to_delegate(Unique::new(client.clone())));238239	if let Some(filter_pool) = filter_pool {240		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(241			client.clone(),242			backend,243			filter_pool,244			500_usize, // max stored filters245			max_past_logs,246			block_data_cache,247		)));248	}249250	io.extend_with(NetApiServer::to_delegate(NetApi::new(251		client.clone(),252		network.clone(),253		// Whether to format the `peer_count` response as Hex (default) or not.254		true,255	)));256257	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));258259	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(260		pool,261		client,262		network,263		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(264			HexEncodedIdProvider::default(),265			Arc::new(subscription_task_executor),266		),267		overrides,268	)));269270	io271}