git.delta.rocks / unique-network / refs/commits / 8484aa77f543

difftreelog

source

node/rpc/src/lib.rs7.5 KiBsourcehistory
1use nft_runtime::{AccountId, Balance, BlockNumber, CrossAccountId, Hash, Index, opaque::Block};2use fc_rpc::{OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, StorageOverride};3use fc_rpc_core::types::{FilterPool, PendingTransactions};4use jsonrpc_pubsub::manager::SubscriptionManager;5use pallet_ethereum::EthereumStorageSchema;6use sc_client_api::{7	backend::{AuxStore, StorageProvider},8	client::BlockchainEvents,9};10use sc_finality_grandpa::{11	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,12};13use sc_network::NetworkService;14use sc_rpc::SubscriptionTaskExecutor;15pub use sc_rpc_api::DenyUnsafe;16use sp_api::ProvideRuntimeApi;17use sp_block_builder::BlockBuilder;18use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};19use sp_consensus::SelectChain;20use sc_service::TransactionPool;21use std::{collections::BTreeMap, marker::PhantomData, sync::Arc};2223/// Public io handler for exporting into other modules24pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;2526/// Light client extra dependencies.27pub struct LightDeps<C, F, P> {28	/// The client instance to use.29	pub client: Arc<C>,30	/// Transaction pool instance.31	pub pool: Arc<P>,32	/// Remote access to the blockchain (async).33	pub remote_blockchain: Arc<dyn sc_client_api::light::RemoteBlockchain<Block>>,34	/// Fetcher instance.35	pub fetcher: Arc<F>,36}3738/// Extra dependencies for GRANDPA39pub struct GrandpaDeps<B> {40	/// Voting round info.41	pub shared_voter_state: SharedVoterState,42	/// Authority set info.43	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,44	/// Receives notifications about justification events from Grandpa.45	pub justification_stream: GrandpaJustificationStream<Block>,46	/// Executor to drive the subscription manager in the Grandpa RPC handler.47	pub subscription_executor: SubscriptionTaskExecutor,48	/// Finality proof provider.49	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,50}5152/// Full client dependencies.53pub struct FullDeps<C, P, SC> {54	/// The client instance to use.55	pub client: Arc<C>,56	/// Transaction pool instance.57	pub pool: Arc<P>,58	/// The SelectChain Strategy59	pub select_chain: SC,60	/// The Node authority flag61	pub is_authority: bool,62	/// Whether to enable dev signer63	pub enable_dev_signer: bool,64	/// Network service65	pub network: Arc<NetworkService<Block, Hash>>,66	/// Whether to deny unsafe calls67	pub deny_unsafe: DenyUnsafe,68	/// Ethereum pending transactions.69	pub pending_transactions: PendingTransactions,70	/// EthFilterApi pool.71	pub filter_pool: Option<FilterPool>,72	/// Backend.73	pub backend: Arc<fc_db::Backend<Block>>,74	/// Maximum number of logs in a query.75	pub max_past_logs: u32,76}7778struct AccountCodes<C, B> {79	client: Arc<C>,80	_marker: PhantomData<B>,81}8283impl<C, Block> AccountCodes<C, Block>84where85	Block: sp_api::BlockT,86	C: ProvideRuntimeApi<Block>,87{88	fn new(client: Arc<C>) -> Self {89		Self {90			client,91			_marker: PhantomData,92		}93	}94}9596impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>97where98	Block: sp_api::BlockT,99	C: ProvideRuntimeApi<Block>,100	C::Api: up_rpc::NftApi<Block, CrossAccountId, AccountId>,101{102	fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {103		use up_rpc::NftApi;104		self.client105			.runtime_api()106			.eth_contract_code(block, account)107			.ok()108			.flatten()109	}110}111112/// Instantiate all Full RPC extensions.113pub fn create_full<C, P, SC, A, B>(114	deps: FullDeps<C, P, SC>,115	subscription_task_executor: SubscriptionTaskExecutor,116) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>117where118	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,119	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,120	C: Send + Sync + 'static,121	C: BlockchainEvents<Block>,122	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,123	C::Api: BlockBuilder<Block>,124	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,125	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,126	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,127	C::Api: up_rpc::NftApi<Block, CrossAccountId, AccountId>,128	P: TransactionPool<Block = Block> + 'static,129	SC: SelectChain<Block> + 'static,130	B: sc_client_api::Backend<Block> + Send + Sync + 'static,131	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,132{133	use fc_rpc::{134		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,135		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,136		Web3ApiServer,137	};138	use uc_rpc::{NftApi, Nft};139	// use pallet_contracts_rpc::{Contracts, ContractsApi};140	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};141	use substrate_frame_rpc_system::{FullSystem, SystemApi};142143	let mut io = jsonrpc_core::IoHandler::default();144	let FullDeps {145		client,146		pool,147		select_chain: _,148		enable_dev_signer,149		is_authority,150		network,151		deny_unsafe,152		pending_transactions,153		filter_pool,154		backend,155		max_past_logs,156	} = deps;157158	io.extend_with(SystemApi::to_delegate(FullSystem::new(159		client.clone(),160		pool.clone(),161		deny_unsafe,162	)));163164	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(165		client.clone(),166	)));167168	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));169170	let mut signers = Vec::new();171	if enable_dev_signer {172		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);173	}174	let mut overrides_map = BTreeMap::new();175	overrides_map.insert(176		EthereumStorageSchema::V1,177		Box::new(SchemaV1Override::new_with_code_provider(178			client.clone(),179			Arc::new(AccountCodes::<C, Block>::new(client.clone())),180		)) as Box<dyn StorageOverride<_> + Send + Sync>,181	);182183	let overrides = Arc::new(OverrideHandle {184		schemas: overrides_map,185		fallback: Box::new(RuntimeApiStorageOverride::new(client.clone())),186	});187188	io.extend_with(EthApiServer::to_delegate(EthApi::new(189		client.clone(),190		pool.clone(),191		nft_runtime::TransactionConverter,192		network.clone(),193		pending_transactions,194		signers,195		overrides.clone(),196		backend.clone(),197		is_authority,198		max_past_logs,199	)));200	io.extend_with(NftApi::to_delegate(Nft::new(client.clone())));201202	if let Some(filter_pool) = filter_pool {203		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(204			client.clone(),205			backend,206			filter_pool,207			500_usize, // max stored filters208			overrides.clone(),209			max_past_logs,210		)));211	}212213	io.extend_with(NetApiServer::to_delegate(NetApi::new(214		client.clone(),215		network.clone(),216		// Whether to format the `peer_count` response as Hex (default) or not.217		true,218	)));219220	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));221222	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(223		pool,224		client,225		network,226		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(227			HexEncodedIdProvider::default(),228			Arc::new(subscription_task_executor),229		),230		overrides,231	)));232233	io234}235236/// Instantiate all Light RPC extensions.237pub fn create_light<C, P, M, F>(deps: LightDeps<C, F, P>) -> jsonrpc_core::IoHandler<M>238where239	C: sp_blockchain::HeaderBackend<Block>,240	C: Send + Sync + 'static,241	F: sc_client_api::light::Fetcher<Block> + 'static,242	P: TransactionPool + 'static,243	M: jsonrpc_core::Metadata + Default,244{245	use substrate_frame_rpc_system::{LightSystem, SystemApi};246247	let LightDeps {248		client,249		pool,250		remote_blockchain,251		fetcher,252	} = deps;253	let mut io = jsonrpc_core::IoHandler::default();254	io.extend_with(SystemApi::<Hash, AccountId, Index>::to_delegate(255		LightSystem::new(client, remote_blockchain, fetcher, pool),256	));257258	io259}