git.delta.rocks / unique-network / refs/commits / 733eebb466ef

difftreelog

source

node/src/rpc.rs5.5 KiBsourcehistory
1//! A collection of node-specific RPC methods.2//! Substrate provides the `sc-rpc` crate, which defines the core RPC layer3//! used by Substrate nodes. This file extends those RPC definitions with4//! capabilities that are specific to this project's runtime configuration.56use std::sync::Arc;78use std::collections::BTreeMap;9use fc_rpc_core::types::{PendingTransactions, FilterPool};10use nft_runtime::{Hash, AccountId, Index, opaque::Block, BlockNumber, Balance};11use sp_api::ProvideRuntimeApi;12use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};13use sc_client_api::{14	backend::{StorageProvider, Backend, StateBackend, AuxStore},15	client::BlockchainEvents16};17use sc_rpc::SubscriptionTaskExecutor;18use sp_runtime::traits::BlakeTwo256;19use sp_block_builder::BlockBuilder;20use sc_rpc_api::DenyUnsafe;21use sp_transaction_pool::TransactionPool;22use sc_network::NetworkService;23use jsonrpc_pubsub::manager::SubscriptionManager;24use pallet_ethereum::EthereumStorageSchema;25use fc_rpc::{StorageOverride, SchemaV1Override};2627/// Light client extra dependencies.28pub struct LightDeps<C, F, P> {29	/// The client instance to use.30	pub client: Arc<C>,31	/// Transaction pool instance.32	pub pool: Arc<P>,33	/// Remote access to the blockchain (async).34	pub remote_blockchain: Arc<dyn sc_client_api::light::RemoteBlockchain<Block>>,35	/// Fetcher instance.36	pub fetcher: Arc<F>,37}3839/// Full client dependencies.40pub struct FullDeps<C, P> {41	/// The client instance to use.42	pub client: Arc<C>,43	/// Transaction pool instance.44	pub pool: Arc<P>,45	/// Whether to deny unsafe calls46	pub deny_unsafe: DenyUnsafe,47	/// The Node authority flag48	pub is_authority: bool,49	/// Whether to enable dev signer50	pub enable_dev_signer: bool,51	/// Network service52	pub network: Arc<NetworkService<Block, Hash>>,53	/// Ethereum pending transactions.54	pub pending_transactions: PendingTransactions,55	/// EthFilterApi pool.56	pub filter_pool: Option<FilterPool>,57	/// Backend.58	pub backend: Arc<fc_db::Backend<Block>>,59}6061/// Instantiate all full RPC extensions.62pub fn create_full<C, P, BE>(63	deps: FullDeps<C, P>,64	subscription_task_executor: SubscriptionTaskExecutor65) -> jsonrpc_core::IoHandler<sc_rpc::Metadata> where66	BE: Backend<Block> + 'static,67	BE::State: StateBackend<BlakeTwo256>,68	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,69	C: BlockchainEvents<Block>,70	C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError>,71	C: Send + Sync + 'static,72	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,73	C::Api: BlockBuilder<Block>,74	C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber>,75	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,76	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,77	P: TransactionPool<Block=Block> + 'static,78{79	use substrate_frame_rpc_system::{FullSystem, SystemApi};80	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};81	use fc_rpc::{82		EthApi, EthApiServer, EthFilterApi, EthFilterApiServer, NetApi, NetApiServer,83		EthPubSubApi, EthPubSubApiServer, Web3Api, Web3ApiServer, EthDevSigner, EthSigner,84		HexEncodedIdProvider,85	};8687	let mut io = jsonrpc_core::IoHandler::default();88	let FullDeps {89		client,90		pool,91		deny_unsafe,92		is_authority,93		network,94		pending_transactions,95		filter_pool,96		backend,97		enable_dev_signer,98	} = deps;99100	io.extend_with(101		SystemApi::to_delegate(FullSystem::new(client.clone(), pool.clone(), deny_unsafe))102	);103104	io.extend_with(105		TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone()))106	);107108	io.extend_with(109		pallet_contracts_rpc::ContractsApi::to_delegate(pallet_contracts_rpc::Contracts::new(client.clone()))110	);111112	let mut signers = Vec::new();113	if enable_dev_signer {114		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);115	}116	let mut overrides = BTreeMap::new();117	overrides.insert(118		EthereumStorageSchema::V1,119		Box::new(SchemaV1Override::new(client.clone())) as Box<dyn StorageOverride<_> + Send + Sync>120	);121	io.extend_with(122		EthApiServer::to_delegate(EthApi::new(123			client.clone(),124			pool.clone(),125			nft_runtime::TransactionConverter,126			network.clone(),127			pending_transactions.clone(),128			signers,129			overrides,130			backend,131			is_authority,132		))133	);134135	if let Some(filter_pool) = filter_pool {136		io.extend_with(137			EthFilterApiServer::to_delegate(EthFilterApi::new(138				client.clone(),139				filter_pool.clone(),140				500 as usize, // max stored filters141			))142		);143	}144145	io.extend_with(146		NetApiServer::to_delegate(NetApi::new(147			client.clone(),148			network.clone(),149		))150	);151152	io.extend_with(153		Web3ApiServer::to_delegate(Web3Api::new(154			client.clone(),155		))156	);157158	io.extend_with(159		EthPubSubApiServer::to_delegate(EthPubSubApi::new(160			pool.clone(),161			client.clone(),162			network.clone(),163			SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(164				HexEncodedIdProvider::default(),165				Arc::new(subscription_task_executor)166			),167		))168	);169170	io171}172173/// Instantiate all Light RPC extensions.174pub fn create_light<C, P, M, F>(175	deps: LightDeps<C, F, P>,176) -> jsonrpc_core::IoHandler<M> where177	C: sp_blockchain::HeaderBackend<Block>,178	C: Send + Sync + 'static,179	F: sc_client_api::light::Fetcher<Block> + 'static,180	P: TransactionPool + 'static,181	M: jsonrpc_core::Metadata + Default,182{183	use substrate_frame_rpc_system::{LightSystem, SystemApi};184185	let LightDeps {186		client,187		pool,188		remote_blockchain,189		fetcher190	} = deps;191	let mut io = jsonrpc_core::IoHandler::default();192	io.extend_with(193		SystemApi::<Hash, AccountId, Index>::to_delegate(194			LightSystem::new(client, remote_blockchain, fetcher, pool)195		)196	);197198	io199}