git.delta.rocks / unique-network / refs/commits / 554134b0f679

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// RMRK47use 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	C::Api: rmrk_rpc::RmrkApi<153		Block,154		AccountId,155		RmrkCollectionInfo<AccountId>,156		RmrkInstanceInfo<AccountId>,157		RmrkResourceInfo,158		RmrkPropertyInfo,159		RmrkBaseInfo<AccountId>,160		RmrkPartType,161		RmrkTheme,162	>,163	B: sc_client_api::Backend<Block> + Send + Sync + 'static,164	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,165	P: TransactionPool<Block = Block> + 'static,166	CA: ChainApi<Block = Block> + 'static,167	R: RuntimeInstance + Send + Sync + 'static,168	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,169	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,170{171	use fc_rpc::{172		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,173		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,174		Web3ApiServer,175	};176	use uc_rpc::{UniqueApi, RmrkApi, Unique};177	// use pallet_contracts_rpc::{Contracts, ContractsApi};178	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};179	use substrate_frame_rpc_system::{FullSystem, SystemApi};180181	let mut io = jsonrpc_core::IoHandler::default();182	let FullDeps {183		client,184		pool,185		graph,186		select_chain: _,187		fee_history_limit,188		fee_history_cache,189		block_data_cache,190		enable_dev_signer,191		is_authority,192		network,193		deny_unsafe,194		filter_pool,195		backend,196		max_past_logs,197	} = deps;198199	io.extend_with(SystemApi::to_delegate(FullSystem::new(200		client.clone(),201		pool.clone(),202		deny_unsafe,203	)));204205	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(206		client.clone(),207	)));208209	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));210211	let mut signers = Vec::new();212	if enable_dev_signer {213		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);214	}215216	let overrides = overrides_handle::<_, _, R>(client.clone());217218	io.extend_with(EthApiServer::to_delegate(EthApi::new(219		client.clone(),220		pool.clone(),221		graph,222		Some(<R as RuntimeInstance>::get_transaction_converter()),223		network.clone(),224		signers,225		overrides.clone(),226		backend.clone(),227		is_authority,228		block_data_cache.clone(),229		fee_history_limit,230		fee_history_cache,231	)));232233	// todo look into234	//let unique = Unique::new(client.clone());235	io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));236	io.extend_with(RmrkApi::to_delegate(Unique::new(client.clone())));237238	if let Some(filter_pool) = filter_pool {239		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(240			client.clone(),241			backend,242			filter_pool,243			500_usize, // max stored filters244			max_past_logs,245			block_data_cache,246		)));247	}248249	io.extend_with(NetApiServer::to_delegate(NetApi::new(250		client.clone(),251		network.clone(),252		// Whether to format the `peer_count` response as Hex (default) or not.253		true,254	)));255256	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));257258	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(259		pool,260		client,261		network,262		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(263			HexEncodedIdProvider::default(),264			Arc::new(subscription_task_executor),265		),266		overrides,267	)));268269	io270}