ab_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 seal blocks.
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 ab_core_primitives::pieces::{Piece, PieceIndex};
15use ab_core_primitives::segments::{SegmentHeader, SegmentIndex};
16use ab_farmer_rpc_primitives::{
17    BlockSealInfo, BlockSealResponse, FarmerAppInfo, SlotInfo, SolutionResponse,
18};
19use async_trait::async_trait;
20use futures::Stream;
21use std::fmt;
22use std::pin::Pin;
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 sealing requests
42    async fn subscribe_block_sealing(
43        &self,
44    ) -> anyhow::Result<Pin<Box<dyn Stream<Item = BlockSealInfo> + Send + 'static>>>;
45
46    /// Submit a block seal
47    async fn submit_block_seal(&self, block_seal: BlockSealResponse) -> anyhow::Result<()>;
48
49    /// Subscribe to archived segment headers
50    async fn subscribe_archived_segment_headers(
51        &self,
52    ) -> anyhow::Result<Pin<Box<dyn Stream<Item = SegmentHeader> + Send + 'static>>>;
53
54    /// Get segment headers for the segments
55    async fn segment_headers(
56        &self,
57        segment_indices: Vec<SegmentIndex>,
58    ) -> anyhow::Result<Vec<Option<SegmentHeader>>>;
59
60    /// Get piece by index.
61    async fn piece(&self, piece_index: PieceIndex) -> anyhow::Result<Option<Piece>>;
62
63    /// Acknowledge segment header.
64    async fn acknowledge_archived_segment_header(
65        &self,
66        segment_index: SegmentIndex,
67    ) -> anyhow::Result<()>;
68}
69
70/// Node Client extension methods that are not necessary for farmer as a library, but might be useful for an app
71#[async_trait]
72pub trait NodeClientExt: NodeClient {
73    /// Get the cached segment headers for the given segment indices.
74    /// If there is a cache, it is not updated, to avoid remote denial of service.
75    ///
76    /// Returns `None` for segment indices that are not in the cache.
77    async fn cached_segment_headers(
78        &self,
79        segment_indices: Vec<SegmentIndex>,
80    ) -> anyhow::Result<Vec<Option<SegmentHeader>>>;
81
82    /// Get up to `limit` most recent segment headers.
83    /// If there is a cache, it is not updated, to avoid remote denial of service.
84    ///
85    /// If the node or cache has less than `limit` segment headers, the returned vector will be
86    /// shorter. Each returned segment header is wrapped in `Some`.
87    async fn last_segment_headers(&self, limit: u32) -> anyhow::Result<Vec<Option<SegmentHeader>>>;
88}