Skip to content

Commit

Permalink
Polish RPC & service (#36)
Browse files Browse the repository at this point in the history
* Use full path

* Abstract APIs & types

* Format
  • Loading branch information
aurexav authored Nov 14, 2022
1 parent e5dfcd6 commit 56b31f8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 107 deletions.
69 changes: 25 additions & 44 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,87 +27,71 @@ pub use sc_rpc::{DenyUnsafe, SubscriptionTaskExecutor};
use std::sync::Arc;
// darwinia
use dc_primitives::*;
// frontier
use fc_rpc::{EthBlockDataCacheTask, OverrideHandle};
use fc_rpc_core::types::{FeeHistoryCache, FeeHistoryCacheLimit, FilterPool};
use fp_rpc::NoTransactionConverter;
// substrate
use sc_client_api::{
backend::{AuxStore, Backend, StateBackend, StorageProvider},
BlockchainEvents,
};
use sc_network::NetworkService;
use sc_transaction_pool::{ChainApi, Pool};
use sc_transaction_pool_api::TransactionPool;
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};

/// A type representing all RPC extensions.
pub type RpcExtension = jsonrpsee::RpcModule<()>;

/// Full client dependencies
pub struct FullDeps<C, P, A: ChainApi> {
pub struct FullDeps<C, P, A: sc_transaction_pool::ChainApi> {
/// The client instance to use.
pub client: Arc<C>,
/// Transaction pool instance.
pub pool: Arc<P>,
/// Graph pool instance.
pub graph: Arc<Pool<A>>,
pub graph: Arc<sc_transaction_pool::Pool<A>>,
/// Whether to deny unsafe calls
pub deny_unsafe: DenyUnsafe,
pub deny_unsafe: sc_rpc::DenyUnsafe,
/// The Node authority flag
pub is_authority: bool,
/// Network service
pub network: Arc<NetworkService<Block, Hash>>,
pub network: Arc<sc_network::NetworkService<Block, Hash>>,
/// EthFilterApi pool.
pub filter_pool: Option<FilterPool>,
pub filter_pool: Option<fc_rpc_core::types::FilterPool>,
/// Backend.
pub backend: Arc<fc_db::Backend<Block>>,
/// Maximum number of logs in a query.
pub max_past_logs: u32,
/// Fee history cache.
pub fee_history_cache: FeeHistoryCache,
pub fee_history_cache: fc_rpc_core::types::FeeHistoryCache,
/// Maximum fee history cache size.
pub fee_history_cache_limit: FeeHistoryCacheLimit,
pub fee_history_cache_limit: fc_rpc_core::types::FeeHistoryCacheLimit,
/// Ethereum data access overrides.
pub overrides: Arc<OverrideHandle<Block>>,
pub overrides: Arc<fc_rpc::OverrideHandle<Block>>,
/// Cache for Ethereum block data.
pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,
pub block_data_cache: Arc<fc_rpc::EthBlockDataCacheTask<Block>>,
}

/// Instantiate all RPC extensions.
pub fn create_full<C, P, BE, A>(
deps: FullDeps<C, P, A>,
subscription_task_executor: SubscriptionTaskExecutor,
subscription_task_executor: sc_rpc::SubscriptionTaskExecutor,
) -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>>
where
BE: 'static + Backend<Block>,
BE::State: StateBackend<Hashing>,
BE: 'static + sc_client_api::backend::Backend<Block>,
BE::State: sc_client_api::backend::StateBackend<Hashing>,
C: 'static
+ Send
+ Sync
+ ProvideRuntimeApi<Block>
+ HeaderBackend<Block>
+ StorageProvider<Block, BE>
+ BlockchainEvents<Block>
+ AuxStore
+ HeaderMetadata<Block, Error = BlockChainError>
+ ProvideRuntimeApi<Block>
+ AuxStore,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ sc_client_api::backend::StorageProvider<Block, BE>
+ sc_client_api::BlockchainEvents<Block>
+ sc_client_api::backend::AuxStore
+ sp_api::ProvideRuntimeApi<Block>
+ sp_blockchain::HeaderBackend<Block>
+ sp_blockchain::HeaderMetadata<Block, Error = sp_blockchain::Error>,
C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ BlockBuilder<Block>,
P: 'static + Sync + Send + TransactionPool<Block = Block>,
A: 'static + ChainApi<Block = Block>,
+ sp_block_builder::BlockBuilder<Block>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
P: 'static + Sync + Send + sc_transaction_pool_api::TransactionPool<Block = Block>,
A: 'static + sc_transaction_pool::ChainApi<Block = Block>,
{
// frontier
use fc_rpc::{
Eth, EthApiServer, EthFilter, EthFilterApiServer, EthPubSub, EthPubSubApiServer, Net,
NetApiServer, Web3, Web3ApiServer,
};
use fp_rpc::NoTransactionConverter;
// substrate
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use substrate_frame_rpc_system::{System, SystemApiServer};
Expand All @@ -131,7 +115,6 @@ where

module.merge(System::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?;
module.merge(TransactionPayment::new(client.clone()).into_rpc())?;

module.merge(
Eth::new(
client.clone(),
Expand Down Expand Up @@ -175,7 +158,6 @@ where
)
.into_rpc(),
)?;

module.merge(
Net::new(
client.clone(),
Expand All @@ -185,7 +167,6 @@ where
)
.into_rpc(),
)?;

module.merge(Web3::new(client).into_rpc())?;

Ok(module)
Expand Down
118 changes: 55 additions & 63 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,46 @@ use polkadot_service::CollatorPair;
use sc_executor::NativeElseWasmExecutor;
use sc_network::NetworkService;
use sc_network_common::service::NetworkBlock;
use sc_service::{Configuration, PartialComponents, TFullBackend, TFullClient, TaskManager};
use sc_service::{Configuration, PartialComponents, TFullClient, TaskManager};
use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};
use sp_api::ConstructRuntimeApi;
use sp_keystore::SyncCryptoStorePtr;
use substrate_prometheus_endpoint::Registry;

type FullBackend = sc_service::TFullBackend<Block>;
type FullClient<RuntimeApi, Executor> =
sc_service::TFullClient<Block, RuntimeApi, sc_executor::NativeElseWasmExecutor<Executor>>;

/// A set of APIs that darwinia-like runtimes must implement.
pub trait RuntimeApiCollection:
cumulus_primitives_core::CollectCollationInfo<Block>
+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>
+ sp_api::Metadata<Block>
+ sp_block_builder::BlockBuilder<Block>
+ sp_offchain::OffchainWorkerApi<Block>
+ sp_session::SessionKeys<Block>
+ sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
{
}
impl<Api> RuntimeApiCollection for Api where
Api: cumulus_primitives_core::CollectCollationInfo<Block>
+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>
+ sp_api::Metadata<Block>
+ sp_block_builder::BlockBuilder<Block>
+ sp_offchain::OffchainWorkerApi<Block>
+ sp_session::SessionKeys<Block>
+ sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
{
}

/// Native executor instance.
pub struct DarwiniaRuntimeExecutor;
impl sc_executor::NativeExecutionDispatch for DarwiniaRuntimeExecutor {
Expand All @@ -82,46 +117,28 @@ pub fn new_partial<RuntimeApi, Executor, BIQ>(
build_import_queue: BIQ,
) -> Result<
PartialComponents<
TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>,
TFullBackend<Block>,
FullClient<RuntimeApi, Executor>,
FullBackend,
(),
sc_consensus::DefaultImportQueue<
Block,
TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>,
>,
sc_transaction_pool::FullPool<
Block,
TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>,
>,
sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, Executor>>,
sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, Executor>>,
(Option<Telemetry>, Option<TelemetryWorkerHandle>),
>,
sc_service::Error,
>
where
RuntimeApi: 'static
+ Send
+ Sync
+ ConstructRuntimeApi<Block, TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>>,
RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
+ sp_api::Metadata<Block>
+ sp_session::SessionKeys<Block>
+ sp_api::ApiExt<
Block,
StateBackend = sc_client_api::StateBackendFor<TFullBackend<Block>, Block>,
> + sp_offchain::OffchainWorkerApi<Block>
+ sp_block_builder::BlockBuilder<Block>,
sc_client_api::StateBackendFor<TFullBackend<Block>, Block>: sp_api::StateBackend<Hashing>,
RuntimeApi:
'static + Send + Sync + ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>,
RuntimeApi::RuntimeApi: RuntimeApiCollection,
sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<Hashing>,
Executor: 'static + sc_executor::NativeExecutionDispatch,
BIQ: FnOnce(
Arc<TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>>,
Arc<FullClient<RuntimeApi, Executor>>,
&Configuration,
Option<TelemetryHandle>,
&TaskManager,
) -> Result<
sc_consensus::DefaultImportQueue<
Block,
TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>,
>,
sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, Executor>>,
sc_service::Error,
>,
{
Expand Down Expand Up @@ -217,29 +234,12 @@ async fn start_node_impl<RuntimeApi, Executor, RB, BIQ, BIC>(
build_consensus: BIC,
hwbench: Option<sc_sysinfo::HwBench>,
eth_rpc_config: EthRpcConfig,
) -> sc_service::error::Result<(
TaskManager,
Arc<TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>>,
)>
) -> sc_service::error::Result<(TaskManager, Arc<FullClient<RuntimeApi, Executor>>)>
where
RuntimeApi: 'static
+ Send
+ Sync
+ ConstructRuntimeApi<Block, TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>>,
RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
+ sp_api::Metadata<Block>
+ sp_session::SessionKeys<Block>
+ sp_api::ApiExt<
Block,
StateBackend = sc_client_api::StateBackendFor<TFullBackend<Block>, Block>,
> + sp_offchain::OffchainWorkerApi<Block>
+ sp_block_builder::BlockBuilder<Block>
+ cumulus_primitives_core::CollectCollationInfo<Block>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
sc_client_api::StateBackendFor<TFullBackend<Block>, Block>: sp_api::StateBackend<Hashing>,
RuntimeApi:
'static + Send + Sync + ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>,
RuntimeApi::RuntimeApi: RuntimeApiCollection,
sc_client_api::StateBackendFor<FullBackend, Block>: sp_api::StateBackend<Hashing>,
Executor: 'static + sc_executor::NativeExecutionDispatch,
RB: 'static
+ Send
Expand All @@ -248,29 +248,21 @@ where
) -> Result<RpcModule<()>, sc_service::Error>,
BIQ: 'static
+ FnOnce(
Arc<TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>>,
Arc<FullClient<RuntimeApi, Executor>>,
&Configuration,
Option<TelemetryHandle>,
&TaskManager,
) -> Result<
sc_consensus::DefaultImportQueue<
Block,
TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>,
>,
sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, Executor>>,
sc_service::Error,
>,
BIC: FnOnce(
Arc<TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>>,
Arc<FullClient<RuntimeApi, Executor>>,
Option<&Registry>,
Option<TelemetryHandle>,
&TaskManager,
Arc<dyn RelayChainInterface>,
Arc<
sc_transaction_pool::FullPool<
Block,
TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>,
>,
>,
Arc<sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, Executor>>>,
Arc<NetworkService<Block, Hash>>,
SyncCryptoStorePtr,
bool,
Expand Down

0 comments on commit 56b31f8

Please sign in to comment.