git.delta.rocks / unique-network / refs/commits / 547ee7a8f073

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,45	AccountId,46	RuntimeInstance,47	Index,48	Block,49	BlockNumber,50	Balance,51};52// RMRK53use up_data_structs::{54	RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo, RmrkPartType, RmrkTheme, 55};5657/// Public io handler for exporting into other modules58pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;5960/// Extra dependencies for GRANDPA61pub struct GrandpaDeps<B> {62	/// Voting round info.63	pub shared_voter_state: SharedVoterState,64	/// Authority set info.65	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,66	/// Receives notifications about justification events from Grandpa.67	pub justification_stream: GrandpaJustificationStream<Block>,68	/// Executor to drive the subscription manager in the Grandpa RPC handler.69	pub subscription_executor: SubscriptionTaskExecutor,70	/// Finality proof provider.71	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,72}7374/// Full client dependencies.75pub struct FullDeps<C, P, SC, CA: ChainApi> {76	/// The client instance to use.77	pub client: Arc<C>,78	/// Transaction pool instance.79	pub pool: Arc<P>,80	/// Graph pool instance.81	pub graph: Arc<Pool<CA>>,82	/// The SelectChain Strategy83	pub select_chain: SC,84	/// The Node authority flag85	pub is_authority: bool,86	/// Whether to enable dev signer87	pub enable_dev_signer: bool,88	/// Network service89	pub network: Arc<NetworkService<Block, Hash>>,90	/// Whether to deny unsafe calls91	pub deny_unsafe: DenyUnsafe,92	/// EthFilterApi pool.93	pub filter_pool: Option<FilterPool>,94	/// Backend.95	pub backend: Arc<fc_db::Backend<Block>>,96	/// Maximum number of logs in a query.97	pub max_past_logs: u32,98	/// Maximum fee history cache size.99	pub fee_history_limit: u64,100	/// Fee history cache.101	pub fee_history_cache: FeeHistoryCache,102	/// Cache for Ethereum block data.103	pub block_data_cache: Arc<EthBlockDataCache<Block>>,104}105106pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>107where108	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,109	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,110	C: Send + Sync + 'static,111	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,112	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,113	BE: Backend<Block> + 'static,114	BE::State: StateBackend<BlakeTwo256>,115	R: RuntimeInstance + Send + Sync + 'static,116{117	let mut overrides_map = BTreeMap::new();118	overrides_map.insert(119		EthereumStorageSchema::V1,120		Box::new(SchemaV1Override::new(client.clone()))121			as Box<dyn StorageOverride<_> + Send + Sync>,122	);123	overrides_map.insert(124		EthereumStorageSchema::V2,125		Box::new(SchemaV2Override::new(client.clone()))126			as Box<dyn StorageOverride<_> + Send + Sync>,127	);128	overrides_map.insert(129		EthereumStorageSchema::V3,130		Box::new(SchemaV3Override::new(client.clone()))131			as Box<dyn StorageOverride<_> + Send + Sync>,132	);133134	Arc::new(OverrideHandle {135		schemas: overrides_map,136		fallback: Box::new(RuntimeApiStorageOverride::new(client)),137	})138}139140/// Instantiate all Full RPC extensions.141pub fn create_full<C, P, SC, CA, R, A, B>(142	deps: FullDeps<C, P, SC, CA>,143	subscription_task_executor: SubscriptionTaskExecutor,144) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>145where146	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,147	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,148	C: Send + Sync + 'static,149	C: BlockchainEvents<Block>,150	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,151	C::Api: BlockBuilder<Block>,152	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,153	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,154	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,155	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,156	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,157	C::Api: rmrk_rpc::RmrkApi<158		Block,159		AccountId,160		RmrkCollectionInfo<AccountId>,161		RmrkInstanceInfo<AccountId>,162		RmrkResourceInfo,163		RmrkPropertyInfo,164		RmrkBaseInfo<AccountId>,165		RmrkPartType,166		RmrkTheme,167	>,168	B: sc_client_api::Backend<Block> + Send + Sync + 'static,169	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,170	P: TransactionPool<Block = Block> + 'static,171	CA: ChainApi<Block = Block> + 'static,172	R: RuntimeInstance + Send + Sync + 'static,173	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,174	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,175{176	use fc_rpc::{177		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,178		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,179		Web3ApiServer,180	};181	use uc_rpc::{UniqueApi, RmrkApi, Unique};182	// use pallet_contracts_rpc::{Contracts, ContractsApi};183	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};184	use substrate_frame_rpc_system::{FullSystem, SystemApi};185186	let mut io = jsonrpc_core::IoHandler::default();187	let FullDeps {188		client,189		pool,190		graph,191		select_chain: _,192		fee_history_limit,193		fee_history_cache,194		block_data_cache,195		enable_dev_signer,196		is_authority,197		network,198		deny_unsafe,199		filter_pool,200		backend,201		max_past_logs,202	} = deps;203204	io.extend_with(SystemApi::to_delegate(FullSystem::new(205		client.clone(),206		pool.clone(),207		deny_unsafe,208	)));209210	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(211		client.clone(),212	)));213214	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));215216	let mut signers = Vec::new();217	if enable_dev_signer {218		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);219	}220221	let overrides = overrides_handle::<_, _, R>(client.clone());222223	io.extend_with(EthApiServer::to_delegate(EthApi::new(224		client.clone(),225		pool.clone(),226		graph,227		Some(<R as RuntimeInstance>::get_transaction_converter()),228		network.clone(),229		signers,230		overrides.clone(),231		backend.clone(),232		is_authority,233		block_data_cache.clone(),234		fee_history_limit,235		fee_history_cache,236	)));237238	// todo look into239	//let unique = Unique::new(client.clone());240	io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));241	io.extend_with(RmrkApi::to_delegate(Unique::new(client.clone())));242243	if let Some(filter_pool) = filter_pool {244		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(245			client.clone(),246			backend,247			filter_pool,248			500_usize, // max stored filters249			max_past_logs,250			block_data_cache,251		)));252	}253254	io.extend_with(NetApiServer::to_delegate(NetApi::new(255		client.clone(),256		network.clone(),257		// Whether to format the `peer_count` response as Hex (default) or not.258		true,259	)));260261	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));262263	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(264		pool,265		client,266		network,267		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(268			HexEncodedIdProvider::default(),269			Arc::new(subscription_task_executor),270		),271		overrides,272	)));273274	io275}