#![warn(missing_docs)]
use jsonrpsee::RpcModule;
use mmr_rpc::{Mmr, MmrApiServer};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use sc_client_api::{AuxStore, BlockBackend};
use sc_consensus_subspace::archiver::{
ArchivedSegmentNotification, ObjectMappingNotification, SegmentHeadersStore,
};
use sc_consensus_subspace::notification::SubspaceNotificationStream;
use sc_consensus_subspace::slot_worker::{
NewSlotNotification, RewardSigningNotification, SubspaceSyncOracle,
};
use sc_consensus_subspace_rpc::{SubspaceRpc, SubspaceRpcApiServer, SubspaceRpcConfig};
use sc_rpc::SubscriptionTaskExecutor;
use sc_transaction_pool_api::TransactionPool;
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
use sp_consensus::SyncOracle;
use sp_consensus_subspace::SubspaceApi;
use sp_objects::ObjectsApi;
use std::sync::Arc;
use subspace_core_primitives::{BlockNumber, PublicKey};
use subspace_erasure_coding::ErasureCoding;
use subspace_kzg::Kzg;
use subspace_networking::libp2p::Multiaddr;
use subspace_runtime_primitives::opaque::Block;
use subspace_runtime_primitives::{AccountId, Balance, Nonce};
use substrate_frame_rpc_system::{System, SystemApiServer};
pub struct FullDeps<C, P, SO, AS, B>
where
SO: SyncOracle + Send + Sync + Clone,
{
pub client: Arc<C>,
pub pool: Arc<P>,
pub subscription_executor: SubscriptionTaskExecutor,
pub new_slot_notification_stream: SubspaceNotificationStream<NewSlotNotification>,
pub reward_signing_notification_stream: SubspaceNotificationStream<RewardSigningNotification>,
pub object_mapping_notification_stream: SubspaceNotificationStream<ObjectMappingNotification>,
pub archived_segment_notification_stream:
SubspaceNotificationStream<ArchivedSegmentNotification>,
pub dsn_bootstrap_nodes: Vec<Multiaddr>,
pub segment_headers_store: SegmentHeadersStore<AS>,
pub sync_oracle: SubspaceSyncOracle<SO>,
pub kzg: Kzg,
pub erasure_coding: ErasureCoding,
pub backend: Arc<B>,
}
pub fn create_full<C, P, SO, AS, B>(
deps: FullDeps<C, P, SO, AS, B>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
C: ProvideRuntimeApi<Block>
+ BlockBackend<Block>
+ HeaderBackend<Block>
+ HeaderMetadata<Block, Error = BlockChainError>
+ Send
+ Sync
+ 'static,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ BlockBuilder<Block>
+ SubspaceApi<Block, PublicKey>
+ mmr_rpc::MmrRuntimeApi<Block, <Block as sp_runtime::traits::Block>::Hash, BlockNumber>
+ ObjectsApi<Block>,
P: TransactionPool + 'static,
SO: SyncOracle + Send + Sync + Clone + 'static,
AS: AuxStore + Send + Sync + 'static,
B: sc_client_api::Backend<Block> + Send + Sync + 'static,
B::State: sc_client_api::StateBackend<sp_runtime::traits::HashingFor<Block>>,
{
let mut module = RpcModule::new(());
let FullDeps {
client,
pool,
subscription_executor,
new_slot_notification_stream,
reward_signing_notification_stream,
object_mapping_notification_stream,
archived_segment_notification_stream,
dsn_bootstrap_nodes,
segment_headers_store,
sync_oracle,
kzg,
erasure_coding,
backend,
} = deps;
module.merge(System::new(client.clone(), pool).into_rpc())?;
module.merge(TransactionPayment::new(client.clone()).into_rpc())?;
module.merge(
SubspaceRpc::new(SubspaceRpcConfig {
client: client.clone(),
subscription_executor,
new_slot_notification_stream,
reward_signing_notification_stream,
object_mapping_notification_stream,
archived_segment_notification_stream,
dsn_bootstrap_nodes,
segment_headers_store,
sync_oracle,
kzg,
erasure_coding,
})?
.into_rpc(),
)?;
module.merge(
Mmr::new(
client,
backend
.offchain_storage()
.ok_or("Backend doesn't provide the required offchain storage")?,
)
.into_rpc(),
)?;
Ok(module)
}