subspace_farmer/
node_client.rs

1//! Node client abstraction
2//!
3//! During farmer operation it needs to communicate with node, for example to receive slot
4//! notifications and send solutions to claim rewards.
5//!
6//! Implementation is abstracted away behind a trait to allow various implementation depending on
7//! use case. Implementation may connect to node via RPC directly, through some kind of networked
8//! middleware or even wired without network directly if node and farmer are both running in the
9//! same process.
10
11pub mod caching_proxy_node_client;
12pub mod rpc_node_client;
13
14use async_trait::async_trait;
15use futures::Stream;
16use std::fmt;
17use std::pin::Pin;
18use subspace_core_primitives::pieces::{Piece, PieceIndex};
19use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex};
20use subspace_rpc_primitives::{
21    FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
22};
23
24/// Abstraction of the Node Client
25#[async_trait]
26pub trait NodeClient: fmt::Debug + Send + Sync + 'static {
27    /// Get farmer app info
28    async fn farmer_app_info(&self) -> anyhow::Result<FarmerAppInfo>;
29
30    /// Subscribe to slot
31    async fn subscribe_slot_info(
32        &self,
33    ) -> anyhow::Result<Pin<Box<dyn Stream<Item = SlotInfo> + Send + 'static>>>;
34
35    /// Submit a slot solution
36    async fn submit_solution_response(
37        &self,
38        solution_response: SolutionResponse,
39    ) -> anyhow::Result<()>;
40
41    /// Subscribe to block signing request
42    async fn subscribe_reward_signing(
43        &self,
44    ) -> anyhow::Result<Pin<Box<dyn Stream<Item = RewardSigningInfo> + Send + 'static>>>;
45
46    /// Submit a block signature
47    async fn submit_reward_signature(
48        &self,
49        reward_signature: RewardSignatureResponse,
50    ) -> anyhow::Result<()>;
51
52    /// Subscribe to archived segment headers
53    async fn subscribe_archived_segment_headers(
54        &self,
55    ) -> anyhow::Result<Pin<Box<dyn Stream<Item = SegmentHeader> + Send + 'static>>>;
56
57    /// Get segment headers for the segments
58    async fn segment_headers(
59        &self,
60        segment_indices: Vec<SegmentIndex>,
61    ) -> anyhow::Result<Vec<Option<SegmentHeader>>>;
62
63    /// Get piece by index.
64    async fn piece(&self, piece_index: PieceIndex) -> anyhow::Result<Option<Piece>>;
65
66    /// Acknowledge segment header.
67    async fn acknowledge_archived_segment_header(
68        &self,
69        segment_index: SegmentIndex,
70    ) -> anyhow::Result<()>;
71}
72
73/// Node Client extension methods that are not necessary for farmer as a library, but might be useful for an app
74#[async_trait]
75pub trait NodeClientExt: NodeClient {
76    /// Get the last segment headers.
77    async fn last_segment_headers(&self, limit: u32) -> anyhow::Result<Vec<Option<SegmentHeader>>>;
78}