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

difftreelog

source

node/rpc/src/lib.rs7.6 KiBsourcehistory
1use nft_runtime::{Hash, AccountId, Index, opaque::Block, BlockNumber, Balance};2use fc_rpc::{3	EthBlockDataCache, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, StorageOverride,4};5use fc_rpc_core::types::FilterPool;6use jsonrpc_pubsub::manager::SubscriptionManager;7use pallet_ethereum::EthereumStorageSchema;8use sc_client_api::{9	backend::{AuxStore, StorageProvider},10	client::BlockchainEvents,11};12use pallet_nft::NftApi;13use sc_finality_grandpa::{14	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,15};16use sc_network::NetworkService;17use sc_rpc::SubscriptionTaskExecutor;18pub use sc_rpc_api::DenyUnsafe;19use sc_transaction_pool::{ChainApi, Pool};20use sp_api::ProvideRuntimeApi;21use sp_block_builder::BlockBuilder;22use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};23use sp_consensus::SelectChain;24use sc_service::TransactionPool;25use std::{collections::BTreeMap, marker::PhantomData, sync::Arc};2627/// Public io handler for exporting into other modules28pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;2930/// Light client extra dependencies.31pub struct LightDeps<C, F, P> {32	/// The client instance to use.33	pub client: Arc<C>,34	/// Transaction pool instance.35	pub pool: Arc<P>,36	/// Remote access to the blockchain (async).37	pub remote_blockchain: Arc<dyn sc_client_api::light::RemoteBlockchain<Block>>,38	/// Fetcher instance.39	pub fetcher: Arc<F>,40}4142/// Extra dependencies for GRANDPA43pub struct GrandpaDeps<B> {44	/// Voting round info.45	pub shared_voter_state: SharedVoterState,46	/// Authority set info.47	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,48	/// Receives notifications about justification events from Grandpa.49	pub justification_stream: GrandpaJustificationStream<Block>,50	/// Executor to drive the subscription manager in the Grandpa RPC handler.51	pub subscription_executor: SubscriptionTaskExecutor,52	/// Finality proof provider.53	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,54}5556/// Full client dependencies.57pub struct FullDeps<C, P, SC, CA: ChainApi> {58	/// The client instance to use.59	pub client: Arc<C>,60	/// Transaction pool instance.61	pub pool: Arc<P>,62	/// Graph pool instance.63	pub graph: Arc<Pool<CA>>,64	/// The SelectChain Strategy65	pub select_chain: SC,66	/// The Node authority flag67	pub is_authority: bool,68	/// Whether to enable dev signer69	pub enable_dev_signer: bool,70	/// Network service71	pub network: Arc<NetworkService<Block, Hash>>,72	/// Whether to deny unsafe calls73	pub deny_unsafe: DenyUnsafe,74	/// EthFilterApi pool.75	pub filter_pool: Option<FilterPool>,76	/// Backend.77	pub backend: Arc<fc_db::Backend<Block>>,78	/// Maximum number of logs in a query.79	pub max_past_logs: u32,80}8182struct AccountCodes<C, B> {83	client: Arc<C>,84	_marker: PhantomData<B>,85}8687impl<C, Block> AccountCodes<C, Block>88where89	Block: sp_api::BlockT,90	C: ProvideRuntimeApi<Block>,91{92	fn new(client: Arc<C>) -> Self {93		Self {94			client,95			_marker: PhantomData,96		}97	}98}99100impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>101where102	Block: sp_api::BlockT,103	C: ProvideRuntimeApi<Block>,104	C::Api: pallet_nft::NftApi<Block>,105{106	fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {107		self.client108			.runtime_api()109			.eth_contract_code(block, account)110			.ok()111			.flatten()112	}113}114115/// Instantiate all Full RPC extensions.116pub fn create_full<C, P, SC, CA, A, B>(117	deps: FullDeps<C, P, SC, CA>,118	subscription_task_executor: SubscriptionTaskExecutor,119) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>120where121	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,122	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,123	C: Send + Sync + 'static,124	C: BlockchainEvents<Block>,125	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,126	C::Api: BlockBuilder<Block>,127	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,128	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,129	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,130	C::Api: pallet_nft::NftApi<Block>,131	B: sc_client_api::Backend<Block> + Send + Sync + 'static,132	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,133	SC: SelectChain<Block> + 'static,134	P: TransactionPool<Block = Block> + 'static,135	CA: ChainApi<Block = Block> + 'static,136{137	use fc_rpc::{138		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,139		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,140		Web3ApiServer,141	};142	// use pallet_contracts_rpc::{Contracts, ContractsApi};143	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};144	use substrate_frame_rpc_system::{FullSystem, SystemApi};145146	let mut io = jsonrpc_core::IoHandler::default();147	let FullDeps {148		client,149		pool,150		graph,151		select_chain: _,152		enable_dev_signer,153		is_authority,154		network,155		deny_unsafe,156		filter_pool,157		backend,158		max_past_logs,159	} = deps;160161	io.extend_with(SystemApi::to_delegate(FullSystem::new(162		client.clone(),163		pool.clone(),164		deny_unsafe,165	)));166167	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(168		client.clone(),169	)));170171	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));172173	let mut signers = Vec::new();174	if enable_dev_signer {175		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);176	}177	let mut overrides_map = BTreeMap::new();178	overrides_map.insert(179		EthereumStorageSchema::V1,180		Box::new(SchemaV1Override::new_with_code_provider(181			client.clone(),182			Arc::new(AccountCodes::<C, Block>::new(client.clone())),183		)) as Box<dyn StorageOverride<_> + Send + Sync>,184	);185186	let overrides = Arc::new(OverrideHandle {187		schemas: overrides_map,188		fallback: Box::new(RuntimeApiStorageOverride::new(client.clone())),189	});190191	let block_data_cache = Arc::new(EthBlockDataCache::new(50, 50));192193	io.extend_with(EthApiServer::to_delegate(EthApi::new(194		client.clone(),195		pool.clone(),196		graph,197		nft_runtime::TransactionConverter,198		network.clone(),199		signers,200		overrides.clone(),201		backend.clone(),202		is_authority,203		max_past_logs,204		block_data_cache.clone(),205	)));206207	if let Some(filter_pool) = filter_pool {208		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(209			client.clone(),210			backend,211			filter_pool,212			500_usize, // max stored filters213			overrides.clone(),214			max_past_logs,215			block_data_cache.clone(),216		)));217	}218219	io.extend_with(NetApiServer::to_delegate(NetApi::new(220		client.clone(),221		network.clone(),222		// Whether to format the `peer_count` response as Hex (default) or not.223		true,224	)));225226	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));227228	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(229		pool,230		client,231		network,232		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(233			HexEncodedIdProvider::default(),234			Arc::new(subscription_task_executor),235		),236		overrides,237	)));238239	io240}241242/// Instantiate all Light RPC extensions.243pub fn create_light<C, P, M, F>(deps: LightDeps<C, F, P>) -> jsonrpc_core::IoHandler<M>244where245	C: sp_blockchain::HeaderBackend<Block>,246	C: Send + Sync + 'static,247	F: sc_client_api::light::Fetcher<Block> + 'static,248	P: TransactionPool + 'static,249	M: jsonrpc_core::Metadata + Default,250{251	use substrate_frame_rpc_system::{LightSystem, SystemApi};252253	let LightDeps {254		client,255		pool,256		remote_blockchain,257		fetcher,258	} = deps;259	let mut io = jsonrpc_core::IoHandler::default();260	io.extend_with(SystemApi::<Hash, AccountId, Index>::to_delegate(261		LightSystem::new(client, remote_blockchain, fetcher, pool),262	));263264	io265}