git.delta.rocks / unique-network / refs/commits / 6090f15f03e0

difftreelog

source

node/src/rpc.rs6.7 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;7use core::marker::PhantomData;89use std::collections::BTreeMap;10use fc_rpc::RuntimeApiStorageOverride;11use fc_rpc::OverrideHandle;12use fc_rpc_core::types::{PendingTransactions, FilterPool};13use nft_runtime::{Hash, AccountId, Index, opaque::Block, BlockNumber, Balance, NftApi};14use sp_api::ProvideRuntimeApi;15use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};16use sc_client_api::{17	backend::{StorageProvider, Backend, StateBackend, AuxStore},18	client::BlockchainEvents19};20use sc_rpc::SubscriptionTaskExecutor;21use sp_runtime::traits::BlakeTwo256;22use sp_block_builder::BlockBuilder;23use sc_rpc_api::DenyUnsafe;24use sp_transaction_pool::TransactionPool;25use sc_network::NetworkService;26use jsonrpc_pubsub::manager::SubscriptionManager;27use pallet_ethereum::EthereumStorageSchema;28use fc_rpc::{StorageOverride, SchemaV1Override};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/// Full client dependencies.43pub struct FullDeps<C, P> {44	/// The client instance to use.45	pub client: Arc<C>,46	/// Transaction pool instance.47	pub pool: Arc<P>,48	/// Whether to deny unsafe calls49	pub deny_unsafe: DenyUnsafe,50	/// The Node authority flag51	pub is_authority: bool,52	/// Whether to enable dev signer53	pub enable_dev_signer: bool,54	/// Network service55	pub network: Arc<NetworkService<Block, Hash>>,56	/// Ethereum pending transactions.57	pub pending_transactions: PendingTransactions,58	/// EthFilterApi pool.59	pub filter_pool: Option<FilterPool>,60	/// Backend.61	pub backend: Arc<fc_db::Backend<Block>>,62	/// Maximum number of logs in a query.63	pub max_past_logs: u32,64}6566struct AccountCodes<C, B> {67	client: Arc<C>,68	_marker: PhantomData<B>,69}7071impl<C, Block> AccountCodes<C, Block>72where73	Block: sp_api::BlockT,74	C: ProvideRuntimeApi<Block>,75{76	fn new(client: Arc<C>) -> Self {77		Self {78			client,79			_marker: PhantomData,80		}81	}82}8384impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>85where86	Block: sp_api::BlockT,87	C: ProvideRuntimeApi<Block>,88	C::Api: pallet_nft::NftApi<Block>,89{90	fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {91		self.client.runtime_api().eth_contract_code(block, account).ok().flatten()92	}93}9495/// Instantiate all full RPC extensions.96pub fn create_full<C, P, BE>(97	deps: FullDeps<C, P>,98	subscription_task_executor: SubscriptionTaskExecutor99) -> jsonrpc_core::IoHandler<sc_rpc::Metadata> where100	BE: Backend<Block> + 'static,101	BE::State: StateBackend<BlakeTwo256>,102	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,103	C: BlockchainEvents<Block>,104	C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError>,105	C: Send + Sync + 'static,106	C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,107	C::Api: BlockBuilder<Block>,108	C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber>,109	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,110	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,111	C::Api: pallet_nft::NftApi<Block>,112	P: TransactionPool<Block=Block> + 'static,113{114	use substrate_frame_rpc_system::{FullSystem, SystemApi};115	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};116	use fc_rpc::{117		EthApi, EthApiServer, EthFilterApi, EthFilterApiServer, NetApi, NetApiServer,118		EthPubSubApi, EthPubSubApiServer, Web3Api, Web3ApiServer, EthDevSigner, EthSigner,119		HexEncodedIdProvider,120	};121122	let mut io = jsonrpc_core::IoHandler::default();123	let FullDeps {124		client,125		pool,126		deny_unsafe,127		is_authority,128		network,129		pending_transactions,130		filter_pool,131		backend,132		enable_dev_signer,133		max_past_logs,134	} = deps;135136	io.extend_with(137		SystemApi::to_delegate(FullSystem::new(client.clone(), pool.clone(), deny_unsafe))138	);139140	io.extend_with(141		TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone()))142	);143144	io.extend_with(145		pallet_contracts_rpc::ContractsApi::to_delegate(pallet_contracts_rpc::Contracts::new(client.clone()))146	);147148	let mut signers = Vec::new();149	if enable_dev_signer {150		signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);151	}152	let mut overrides_map = BTreeMap::new();153	overrides_map.insert(154		EthereumStorageSchema::V1,155		Box::new(SchemaV1Override::new_with_code_provider(156			client.clone(),157			Arc::new(AccountCodes::<C, Block>::new(client.clone()))158		)) as Box<dyn StorageOverride<_> + Send + Sync>159	);160161	let overrides = Arc::new(OverrideHandle {162		schemas: overrides_map,163		fallback: Box::new(RuntimeApiStorageOverride::new(client.clone())),164	});165166	io.extend_with(167		EthApiServer::to_delegate(EthApi::new(168			client.clone(),169			pool.clone(),170			nft_runtime::TransactionConverter,171			network.clone(),172			pending_transactions.clone(),173			signers,174			overrides.clone(),175			backend,176			is_authority,177			max_past_logs,178		))179	);180181	if let Some(filter_pool) = filter_pool {182		io.extend_with(183			EthFilterApiServer::to_delegate(EthFilterApi::new(184				client.clone(),185				filter_pool.clone(),186				500 as usize, // max stored filters187				overrides.clone(),188				max_past_logs,189			))190		);191	}192193	io.extend_with(194		NetApiServer::to_delegate(NetApi::new(195			client.clone(),196			network.clone(),197		))198	);199200	io.extend_with(201		Web3ApiServer::to_delegate(Web3Api::new(202			client.clone(),203		))204	);205206	io.extend_with(207		EthPubSubApiServer::to_delegate(EthPubSubApi::new(208			pool.clone(),209			client.clone(),210			network.clone(),211			SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(212				HexEncodedIdProvider::default(),213				Arc::new(subscription_task_executor)214			),215			overrides,216		))217	);218219	io220}221222/// Instantiate all Light RPC extensions.223pub fn create_light<C, P, M, F>(224	deps: LightDeps<C, F, P>,225) -> jsonrpc_core::IoHandler<M> where226	C: sp_blockchain::HeaderBackend<Block>,227	C: Send + Sync + 'static,228	F: sc_client_api::light::Fetcher<Block> + 'static,229	P: TransactionPool + 'static,230	M: jsonrpc_core::Metadata + Default,231{232	use substrate_frame_rpc_system::{LightSystem, SystemApi};233234	let LightDeps {235		client,236		pool,237		remote_blockchain,238		fetcher239	} = deps;240	let mut io = jsonrpc_core::IoHandler::default();241	io.extend_with(242		SystemApi::<Hash, AccountId, Index>::to_delegate(243			LightSystem::new(client, remote_blockchain, fetcher, pool)244		)245	);246247	io248}