git.delta.rocks / unique-network / refs/commits / 30f58ba5fa05

difftreelog

source

node/rpc/src/lib.rs6.7 KiBsourcehistory
1use unique_runtime::{Hash, AccountId, CrossAccountId, 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 sc_finality_grandpa::{13	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,14};15use sc_network::NetworkService;16use sc_rpc::SubscriptionTaskExecutor;17pub use sc_rpc_api::DenyUnsafe;18use sc_transaction_pool::{ChainApi, Pool};19use sp_api::ProvideRuntimeApi;20use sp_block_builder::BlockBuilder;21use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};22use sp_consensus::SelectChain;23use sc_service::TransactionPool;24use std::{collections::BTreeMap, marker::PhantomData, sync::Arc};2526/// Public io handler for exporting into other modules27pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;2829/// Extra dependencies for GRANDPA30pub struct GrandpaDeps<B> {31	/// Voting round info.32	pub shared_voter_state: SharedVoterState,33	/// Authority set info.34	pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,35	/// Receives notifications about justification events from Grandpa.36	pub justification_stream: GrandpaJustificationStream<Block>,37	/// Executor to drive the subscription manager in the Grandpa RPC handler.38	pub subscription_executor: SubscriptionTaskExecutor,39	/// Finality proof provider.40	pub finality_provider: Arc<FinalityProofProvider<B, Block>>,41}4243/// Full client dependencies.44pub struct FullDeps<C, P, SC, CA: ChainApi> {45	/// The client instance to use.46	pub client: Arc<C>,47	/// Transaction pool instance.48	pub pool: Arc<P>,49	/// Graph pool instance.50	pub graph: Arc<Pool<CA>>,51	/// The SelectChain Strategy52	pub select_chain: SC,53	/// The Node authority flag54	pub is_authority: bool,55	/// Whether to enable dev signer56	pub enable_dev_signer: bool,57	/// Network service58	pub network: Arc<NetworkService<Block, Hash>>,59	/// Whether to deny unsafe calls60	pub deny_unsafe: DenyUnsafe,61	/// EthFilterApi pool.62	pub filter_pool: Option<FilterPool>,63	/// Backend.64	pub backend: Arc<fc_db::Backend<Block>>,65	/// Maximum number of logs in a query.66	pub max_past_logs: u32,67}6869struct AccountCodes<C, B> {70	client: Arc<C>,71	_marker: PhantomData<B>,72}7374impl<C, Block> AccountCodes<C, Block>75where76	Block: sp_api::BlockT,77	C: ProvideRuntimeApi<Block>,78{79	fn new(client: Arc<C>) -> Self {80		Self {81			client,82			_marker: PhantomData,83		}84	}85}8687impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>88where89	Block: sp_api::BlockT,90	C: ProvideRuntimeApi<Block>,91	C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,92{93	fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {94		use up_rpc::UniqueApi;95		self.client96			.runtime_api()97			.eth_contract_code(block, account)98			.ok()99			.flatten()100	}101}102103/// Instantiate all Full RPC extensions.104pub fn create_full<C, P, SC, CA, A, B>(105	deps: FullDeps<C, P, SC, CA>,106	subscription_task_executor: SubscriptionTaskExecutor,107) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>108where109	C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,110	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,111	C: Send + Sync + 'static,112	C: BlockchainEvents<Block>,113	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,114	C::Api: BlockBuilder<Block>,115	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,116	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,117	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,118	C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,119	B: sc_client_api::Backend<Block> + Send + Sync + 'static,120	B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,121	SC: SelectChain<Block> + 'static,122	P: TransactionPool<Block = Block> + 'static,123	CA: ChainApi<Block = Block> + 'static,124{125	use fc_rpc::{126		EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,127		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,128		Web3ApiServer,129	};130	use uc_rpc::{UniqueApi, Unique};131	// use pallet_contracts_rpc::{Contracts, ContractsApi};132	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};133	use substrate_frame_rpc_system::{FullSystem, SystemApi};134135	let mut io = jsonrpc_core::IoHandler::default();136	let FullDeps {137		client,138		pool,139		graph,140		select_chain: _,141		enable_dev_signer,142		is_authority,143		network,144		deny_unsafe,145		filter_pool,146		backend,147		max_past_logs,148	} = deps;149150	io.extend_with(SystemApi::to_delegate(FullSystem::new(151		client.clone(),152		pool.clone(),153		deny_unsafe,154	)));155156	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(157		client.clone(),158	)));159160	// io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));161162	let mut signers = Vec::new();163	if enable_dev_signer {164		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);165	}166	let mut overrides_map = BTreeMap::new();167	overrides_map.insert(168		EthereumStorageSchema::V1,169		Box::new(SchemaV1Override::new_with_code_provider(170			client.clone(),171			Arc::new(AccountCodes::<C, Block>::new(client.clone())),172		)) as Box<dyn StorageOverride<_> + Send + Sync>,173	);174175	let overrides = Arc::new(OverrideHandle {176		schemas: overrides_map,177		fallback: Box::new(RuntimeApiStorageOverride::new(client.clone())),178	});179180	let block_data_cache = Arc::new(EthBlockDataCache::new(50, 50));181182	io.extend_with(EthApiServer::to_delegate(EthApi::new(183		client.clone(),184		pool.clone(),185		graph,186		unique_runtime::TransactionConverter,187		network.clone(),188		signers,189		overrides.clone(),190		backend.clone(),191		is_authority,192		max_past_logs,193		block_data_cache.clone(),194	)));195	io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));196197	if let Some(filter_pool) = filter_pool {198		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(199			client.clone(),200			backend,201			filter_pool,202			500_usize, // max stored filters203			overrides.clone(),204			max_past_logs,205			block_data_cache,206		)));207	}208209	io.extend_with(NetApiServer::to_delegate(NetApi::new(210		client.clone(),211		network.clone(),212		// Whether to format the `peer_count` response as Hex (default) or not.213		true,214	)));215216	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));217218	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(219		pool,220		client,221		network,222		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(223			HexEncodedIdProvider::default(),224			Arc::new(subscription_task_executor),225		),226		overrides,227	)));228229	io230}