git.delta.rocks / unique-network / refs/commits / 2f9c8d434e2f

difftreelog

source

node/rpc/src/lib.rs8.1 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};4647/// Public io handler for exporting into other modules48pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;4950/// Extra dependencies for GRANDPA51pub struct GrandpaDeps<B> {52	/// Voting round info.53	pub shared_voter_state: SharedVoterState,54	/// Authority set info.55	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,56	/// Receives notifications about justification events from Grandpa.57	pub justification_stream: GrandpaJustificationStream<Block>,58	/// Executor to drive the subscription manager in the Grandpa RPC handler.59	pub subscription_executor: SubscriptionTaskExecutor,60	/// Finality proof provider.61	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,62}6364/// Full client dependencies.65pub struct FullDeps<C, P, SC, CA: ChainApi> {66	/// The client instance to use.67	pub client: Arc<C>,68	/// Transaction pool instance.69	pub pool: Arc<P>,70	/// Graph pool instance.71	pub graph: Arc<Pool<CA>>,72	/// The SelectChain Strategy73	pub select_chain: SC,74	/// The Node authority flag75	pub is_authority: bool,76	/// Whether to enable dev signer77	pub enable_dev_signer: bool,78	/// Network service79	pub network: Arc<NetworkService<Block, Hash>>,80	/// Whether to deny unsafe calls81	pub deny_unsafe: DenyUnsafe,82	/// EthFilterApi pool.83	pub filter_pool: Option<FilterPool>,84	/// Backend.85	pub backend: Arc<fc_db::Backend<Block>>,86	/// Maximum number of logs in a query.87	pub max_past_logs: u32,88	/// Maximum fee history cache size.89	pub fee_history_limit: u64,90	/// Fee history cache.91	pub fee_history_cache: FeeHistoryCache,92	/// Cache for Ethereum block data.93	pub block_data_cache: Arc<EthBlockDataCache<Block>>,94}959697pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>98where99	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,100	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,101	C: Send + Sync + 'static,102	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,103	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,104	BE: Backend<Block> + 'static,105	BE::State: StateBackend<BlakeTwo256>,106	R: RuntimeInstance + Send + Sync + 'static,107{108	let mut overrides_map = BTreeMap::new();109	overrides_map.insert(110		EthereumStorageSchema::V1,111		Box::new(SchemaV1Override::new(client.clone()))112			as Box<dyn StorageOverride<_> + Send + Sync>,113	);114	overrides_map.insert(115		EthereumStorageSchema::V2,116		Box::new(SchemaV2Override::new(client.clone()))117			as Box<dyn StorageOverride<_> + Send + Sync>,118	);119	overrides_map.insert(120		EthereumStorageSchema::V3,121		Box::new(SchemaV3Override::new(client.clone()))122			as Box<dyn StorageOverride<_> + Send + Sync>,123	);124125	Arc::new(OverrideHandle {126		schemas: overrides_map,127		fallback: Box::new(RuntimeApiStorageOverride::new(client)),128	})129}130131/// Instantiate all Full RPC extensions.132pub fn create_full<C, P, SC, CA, R, A, B>(133	deps: FullDeps<C, P, SC, CA>,134	subscription_task_executor: SubscriptionTaskExecutor,135) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>136where137	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,138	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,139	C: Send + Sync + 'static,140	C: BlockchainEvents<Block>,141	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,142	C::Api: BlockBuilder<Block>,143	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,144	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,145	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,146	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,147	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,148	B: sc_client_api::Backend<Block> + Send + Sync + 'static,149	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,150	P: TransactionPool<Block = Block> + 'static,151	CA: ChainApi<Block = Block> + 'static,152	R: RuntimeInstance + Send + Sync + 'static,153	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,154	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,155{156	use fc_rpc::{157		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,158		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,159		Web3ApiServer,160	};161	use uc_rpc::{UniqueApi, Unique};162	// use pallet_contracts_rpc::{Contracts, ContractsApi};163	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};164	use substrate_frame_rpc_system::{FullSystem, SystemApi};165166	let mut io = jsonrpc_core::IoHandler::default();167	let FullDeps {168		client,169		pool,170		graph,171		select_chain: _,172		fee_history_limit,173		fee_history_cache,174		block_data_cache,175		enable_dev_signer,176		is_authority,177		network,178		deny_unsafe,179		filter_pool,180		backend,181		max_past_logs,182	} = deps;183184	io.extend_with(SystemApi::to_delegate(FullSystem::new(185		client.clone(),186		pool.clone(),187		deny_unsafe,188	)));189190	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(191		client.clone(),192	)));193194	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));195196	let mut signers = Vec::new();197	if enable_dev_signer {198		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);199	}200201	let overrides = overrides_handle::<_, _, R>(client.clone());202203	io.extend_with(EthApiServer::to_delegate(EthApi::new(204		client.clone(),205		pool.clone(),206		graph,207		Some(<R as RuntimeInstance>::get_transaction_converter()),208		network.clone(),209		signers,210		overrides.clone(),211		backend.clone(),212		is_authority,213		block_data_cache.clone(),214		fee_history_limit,215		fee_history_cache,216	)));217	io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));218219	if let Some(filter_pool) = filter_pool {220		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(221			client.clone(),222			backend,223			filter_pool,224			500_usize, // max stored filters225			max_past_logs,226			block_data_cache,227		)));228	}229230	io.extend_with(NetApiServer::to_delegate(NetApi::new(231		client.clone(),232		network.clone(),233		// Whether to format the `peer_count` response as Hex (default) or not.234		true,235	)));236237	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));238239	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(240		pool,241		client,242		network,243		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(244			HexEncodedIdProvider::default(),245			Arc::new(subscription_task_executor),246		),247		overrides,248	)));249250	io251}