Skip to main content

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::{
16    SegmentHeader, SegmentIndex, SuperSegmentHeader, SuperSegmentIndex,
17};
18use ab_farmer_rpc_primitives::{
19    BlockSealInfo, BlockSealResponse, FarmerAppInfo, FarmerShardMembershipInfo, SlotInfo,
20    SolutionResponse,
21};
22use async_trait::async_trait;
23use futures::Stream;
24use std::fmt;
25use std::pin::Pin;
26
27/// Abstraction of the Node Client
28#[async_trait]
29pub trait NodeClient: fmt::Debug + Send + Sync + 'static {
30    /// Get farmer app info
31    async fn farmer_app_info(&self) -> anyhow::Result<FarmerAppInfo>;
32
33    /// Subscribe to slot
34    async fn subscribe_slot_info(
35        &self,
36    ) -> anyhow::Result<Pin<Box<dyn Stream<Item = SlotInfo> + Send + 'static>>>;
37
38    /// Submit a slot solution
39    async fn submit_solution_response(
40        &self,
41        solution_response: SolutionResponse,
42    ) -> anyhow::Result<()>;
43
44    /// Subscribe to block sealing requests
45    async fn subscribe_block_sealing(
46        &self,
47    ) -> anyhow::Result<Pin<Box<dyn Stream<Item = BlockSealInfo> + Send + 'static>>>;
48
49    /// Submit a block seal
50    async fn submit_block_seal(&self, block_seal: BlockSealResponse) -> 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 super segment headers
58    async fn super_segment_headers(
59        &self,
60        super_segment_indices: Vec<SuperSegmentIndex>,
61    ) -> anyhow::Result<Vec<Option<SuperSegmentHeader>>>;
62
63    /// Get segment headers
64    async fn segment_headers(
65        &self,
66        segment_indices: Vec<SegmentIndex>,
67    ) -> anyhow::Result<Vec<Option<SegmentHeader>>>;
68
69    /// Get piece by index.
70    async fn piece(&self, piece_index: PieceIndex) -> anyhow::Result<Option<Piece>>;
71
72    /// Acknowledge segment header.
73    async fn acknowledge_archived_segment_header(
74        &self,
75        segment_index: SegmentIndex,
76    ) -> anyhow::Result<()>;
77
78    // TODO: Move into `NodeClientExt`?
79    /// Must be called while there is an active `shard_membership_entropy_update` subscription
80    async fn update_shard_membership_info(
81        &self,
82        info: FarmerShardMembershipInfo,
83    ) -> anyhow::Result<()>;
84}
85
86/// Node Client extension methods that are not necessary for a farmer as a library but might be
87/// useful for an app
88#[async_trait]
89pub trait NodeClientExt: NodeClient {
90    /// Get the cached segment headers for the given segment indices.
91    /// If there is a cache, it is not updated, to avoid remote denial of service.
92    ///
93    /// Returns `None` for segment indices that are not in the cache.
94    async fn cached_segment_headers(
95        &self,
96        segment_indices: Vec<SegmentIndex>,
97    ) -> anyhow::Result<Vec<Option<SegmentHeader>>>;
98
99    /// Get up to `limit` most recent segment headers.
100    /// If there is a cache, it is not updated, to avoid remote denial of service.
101    ///
102    /// If the node or cache has less than `limit` segment headers, the returned vector will be
103    /// shorter. Each returned segment header is wrapped in `Some`.
104    async fn last_segment_headers(&self, limit: u32) -> anyhow::Result<Vec<Option<SegmentHeader>>>;
105}