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

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	// RMRK52	RmrkCollectionInfo,53	RmrkInstanceInfo,54	RmrkResourceInfo,55	RmrkPropertyInfo,56	RmrkBaseInfo,57	RmrkPartType,58	RmrkTheme,59};6061/// Public io handler for exporting into other modules62pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;6364/// Extra dependencies for GRANDPA65pub struct GrandpaDeps<B> {66	/// Voting round info.67	pub shared_voter_state: SharedVoterState,68	/// Authority set info.69	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,70	/// Receives notifications about justification events from Grandpa.71	pub justification_stream: GrandpaJustificationStream<Block>,72	/// Executor to drive the subscription manager in the Grandpa RPC handler.73	pub subscription_executor: SubscriptionTaskExecutor,74	/// Finality proof provider.75	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,76}7778/// Full client dependencies.79pub struct FullDeps<C, P, SC, CA: ChainApi> {80	/// The client instance to use.81	pub client: Arc<C>,82	/// Transaction pool instance.83	pub pool: Arc<P>,84	/// Graph pool instance.85	pub graph: Arc<Pool<CA>>,86	/// The SelectChain Strategy87	pub select_chain: SC,88	/// The Node authority flag89	pub is_authority: bool,90	/// Whether to enable dev signer91	pub enable_dev_signer: bool,92	/// Network service93	pub network: Arc<NetworkService<Block, Hash>>,94	/// Whether to deny unsafe calls95	pub deny_unsafe: DenyUnsafe,96	/// EthFilterApi pool.97	pub filter_pool: Option<FilterPool>,98	/// Backend.99	pub backend: Arc<fc_db::Backend<Block>>,100	/// Maximum number of logs in a query.101	pub max_past_logs: u32,102	/// Maximum fee history cache size.103	pub fee_history_limit: u64,104	/// Fee history cache.105	pub fee_history_cache: FeeHistoryCache,106	/// Cache for Ethereum block data.107	pub block_data_cache: Arc<EthBlockDataCache<Block>>,108}109110pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>111where112	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,113	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,114	C: Send + Sync + 'static,115	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,116	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,117	BE: Backend<Block> + 'static,118	BE::State: StateBackend<BlakeTwo256>,119	R: RuntimeInstance + Send + Sync + 'static,120{121	let mut overrides_map = BTreeMap::new();122	overrides_map.insert(123		EthereumStorageSchema::V1,124		Box::new(SchemaV1Override::new(client.clone()))125			as Box<dyn StorageOverride<_> + Send + Sync>,126	);127	overrides_map.insert(128		EthereumStorageSchema::V2,129		Box::new(SchemaV2Override::new(client.clone()))130			as Box<dyn StorageOverride<_> + Send + Sync>,131	);132	overrides_map.insert(133		EthereumStorageSchema::V3,134		Box::new(SchemaV3Override::new(client.clone()))135			as Box<dyn StorageOverride<_> + Send + Sync>,136	);137138	Arc::new(OverrideHandle {139		schemas: overrides_map,140		fallback: Box::new(RuntimeApiStorageOverride::new(client)),141	})142}143144/// Instantiate all Full RPC extensions.145pub fn create_full<C, P, SC, CA, R, A, B>(146	deps: FullDeps<C, P, SC, CA>,147	subscription_task_executor: SubscriptionTaskExecutor,148) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>149where150	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,151	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,152	C: Send + Sync + 'static,153	C: BlockchainEvents<Block>,154	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,155	C::Api: BlockBuilder<Block>,156	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,157	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,158	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,159	C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,160	C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,161	C::Api: rmrk_rpc::RmrkApi<162		Block,163		AccountId,164		RmrkCollectionInfo,165		RmrkInstanceInfo,166		RmrkResourceInfo, // todo done, but for reference167		RmrkPropertyInfo,168		RmrkBaseInfo,169		RmrkPartType,170		RmrkTheme,171	>,172	B: sc_client_api::Backend<Block> + Send + Sync + 'static,173	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,174	P: TransactionPool<Block = Block> + 'static,175	CA: ChainApi<Block = Block> + 'static,176	R: RuntimeInstance + Send + Sync + 'static,177	<R as RuntimeInstance>::CrossAccountId: serde::Serialize,178	for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,179{180	use fc_rpc::{181		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,182		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,183		Web3ApiServer,184	};185	use uc_rpc::{UniqueApi, RmrkApi, Unique};186	// use pallet_contracts_rpc::{Contracts, ContractsApi};187	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};188	use substrate_frame_rpc_system::{FullSystem, SystemApi};189190	let mut io = jsonrpc_core::IoHandler::default();191	let FullDeps {192		client,193		pool,194		graph,195		select_chain: _,196		fee_history_limit,197		fee_history_cache,198		block_data_cache,199		enable_dev_signer,200		is_authority,201		network,202		deny_unsafe,203		filter_pool,204		backend,205		max_past_logs,206	} = deps;207208	io.extend_with(SystemApi::to_delegate(FullSystem::new(209		client.clone(),210		pool.clone(),211		deny_unsafe,212	)));213214	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(215		client.clone(),216	)));217218	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));219220	let mut signers = Vec::new();221	if enable_dev_signer {222		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);223	}224225	let overrides = overrides_handle::<_, _, R>(client.clone());226227	io.extend_with(EthApiServer::to_delegate(EthApi::new(228		client.clone(),229		pool.clone(),230		graph,231		Some(<R as RuntimeInstance>::get_transaction_converter()),232		network.clone(),233		signers,234		overrides.clone(),235		backend.clone(),236		is_authority,237		block_data_cache.clone(),238		fee_history_limit,239		fee_history_cache,240	)));241242	// todo243	//let unique = Unique::new(client.clone());244	io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));245	io.extend_with(RmrkApi::to_delegate(Unique::new(client.clone())));246247	if let Some(filter_pool) = filter_pool {248		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(249			client.clone(),250			backend,251			filter_pool,252			500_usize, // max stored filters253			max_past_logs,254			block_data_cache,255		)));256	}257258	io.extend_with(NetApiServer::to_delegate(NetApi::new(259		client.clone(),260		network.clone(),261		// Whether to format the `peer_count` response as Hex (default) or not.262		true,263	)));264265	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));266267	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(268		pool,269		client,270		network,271		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(272			HexEncodedIdProvider::default(),273			Arc::new(subscription_task_executor),274		),275		overrides,276	)));277278	io279}