git.delta.rocks / unique-network / refs/commits / d9c46d549557

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}9596pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>97where98	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,99	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,100	C: Send + Sync + 'static,101	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,102	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,103	BE: Backend<Block> + 'static,104	BE::State: StateBackend<BlakeTwo256>,105	R: RuntimeInstance + Send + Sync + 'static,106{107	let mut overrides_map = BTreeMap::new();108	overrides_map.insert(109		EthereumStorageSchema::V1,110		Box::new(SchemaV1Override::new(client.clone()))111			as Box<dyn StorageOverride<_> + Send + Sync>,112	);113	overrides_map.insert(114		EthereumStorageSchema::V2,115		Box::new(SchemaV2Override::new(client.clone()))116			as Box<dyn StorageOverride<_> + Send + Sync>,117	);118	overrides_map.insert(119		EthereumStorageSchema::V3,120		Box::new(SchemaV3Override::new(client.clone()))121			as Box<dyn StorageOverride<_> + Send + Sync>,122	);123124	Arc::new(OverrideHandle {125		schemas: overrides_map,126		fallback: Box::new(RuntimeApiStorageOverride::new(client)),127	})128}129130/// Instantiate all Full RPC extensions.131pub fn create_full<C, P, SC, CA, R, A, B>(132	deps: FullDeps<C, P, SC, CA>,133	subscription_task_executor: SubscriptionTaskExecutor,134) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>135where136	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,137	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,138	C: Send + Sync + 'static,139	C: BlockchainEvents<Block>,140	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,141	C::Api: BlockBuilder<Block>,142	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,143	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,144	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,145	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,146	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,147	B: sc_client_api::Backend<Block> + Send + Sync + 'static,148	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,149	P: TransactionPool<Block = Block> + 'static,150	CA: ChainApi<Block = Block> + 'static,151	R: RuntimeInstance + Send + Sync + 'static,152	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,153	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,154{155	use fc_rpc::{156		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,157		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,158		Web3ApiServer,159	};160	use uc_rpc::{UniqueApi, Unique};161	// use pallet_contracts_rpc::{Contracts, ContractsApi};162	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};163	use substrate_frame_rpc_system::{FullSystem, SystemApi};164165	let mut io = jsonrpc_core::IoHandler::default();166	let FullDeps {167		client,168		pool,169		graph,170		select_chain: _,171		fee_history_limit,172		fee_history_cache,173		block_data_cache,174		enable_dev_signer,175		is_authority,176		network,177		deny_unsafe,178		filter_pool,179		backend,180		max_past_logs,181	} = deps;182183	io.extend_with(SystemApi::to_delegate(FullSystem::new(184		client.clone(),185		pool.clone(),186		deny_unsafe,187	)));188189	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(190		client.clone(),191	)));192193	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));194195	let mut signers = Vec::new();196	if enable_dev_signer {197		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);198	}199200	let overrides = overrides_handle::<_, _, R>(client.clone());201202	io.extend_with(EthApiServer::to_delegate(EthApi::new(203		client.clone(),204		pool.clone(),205		graph,206		Some(<R as RuntimeInstance>::get_transaction_converter()),207		network.clone(),208		signers,209		overrides.clone(),210		backend.clone(),211		is_authority,212		block_data_cache.clone(),213		fee_history_limit,214		fee_history_cache,215	)));216	io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));217218	if let Some(filter_pool) = filter_pool {219		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(220			client.clone(),221			backend,222			filter_pool,223			500_usize, // max stored filters224			max_past_logs,225			block_data_cache,226		)));227	}228229	io.extend_with(NetApiServer::to_delegate(NetApi::new(230		client.clone(),231		network.clone(),232		// Whether to format the `peer_count` response as Hex (default) or not.233		true,234	)));235236	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));237238	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(239		pool,240		client,241		network,242		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(243			HexEncodedIdProvider::default(),244			Arc::new(subscription_task_executor),245		),246		overrides,247	)));248249	io250}