ab_farmer/node_client/
rpc_node_client.rs1use crate::node_client::{NodeClient, NodeClientExt};
4use ab_core_primitives::pieces::{Piece, PieceIndex};
5use ab_core_primitives::segments::{SegmentIndex, SuperSegmentHeader, SuperSegmentIndex};
6use ab_farmer_rpc_primitives::{
7 BlockSealInfo, BlockSealResponse, FarmerAppInfo, FarmerShardMembershipInfo, SlotInfo,
8 SolutionResponse,
9};
10use async_lock::Semaphore;
11use async_trait::async_trait;
12use futures::{Stream, StreamExt};
13use jsonrpsee::core::client::{ClientT, Error as JsonError, SubscriptionClientT};
14use jsonrpsee::rpc_params;
15use jsonrpsee::ws_client::{WsClient, WsClientBuilder};
16use std::pin::Pin;
17use triomphe::Arc;
18
19const MAX_CONCURRENT_PIECE_REQUESTS: usize = 10;
25
26#[derive(Debug, Clone)]
31pub struct RpcNodeClient {
32 client: Arc<WsClient>,
33 piece_request_semaphore: Arc<Semaphore>,
34}
35
36impl RpcNodeClient {
37 pub async fn new(url: &str) -> Result<Self, JsonError> {
39 let client = Arc::new(
40 WsClientBuilder::default()
41 .max_request_size(20 * 1024 * 1024)
42 .build(url)
43 .await?,
44 );
45 let piece_request_semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_PIECE_REQUESTS));
46 Ok(Self {
47 client,
48 piece_request_semaphore,
49 })
50 }
51}
52
53#[async_trait]
54impl NodeClient for RpcNodeClient {
55 async fn farmer_app_info(&self) -> anyhow::Result<FarmerAppInfo> {
56 Ok(self
57 .client
58 .request("getFarmerAppInfo", rpc_params![])
59 .await?)
60 }
61
62 async fn subscribe_slot_info(
63 &self,
64 ) -> anyhow::Result<Pin<Box<dyn Stream<Item = SlotInfo> + Send + 'static>>> {
65 let subscription = self
66 .client
67 .subscribe("subscribeSlotInfo", rpc_params![], "unsubscribeSlotInfo")
68 .await?;
69
70 Ok(Box::pin(subscription.filter_map(
71 |slot_info_result| async move { slot_info_result.ok() },
72 )))
73 }
74
75 async fn submit_solution_response(
76 &self,
77 solution_response: SolutionResponse,
78 ) -> anyhow::Result<()> {
79 Ok(self
80 .client
81 .request("submitSolutionResponse", rpc_params![&solution_response])
82 .await?)
83 }
84
85 async fn subscribe_block_sealing(
86 &self,
87 ) -> anyhow::Result<Pin<Box<dyn Stream<Item = BlockSealInfo> + Send + 'static>>> {
88 let subscription = self
89 .client
90 .subscribe(
91 "subscribeBlockSealing",
92 rpc_params![],
93 "unsubscribeBlockSealing",
94 )
95 .await?;
96
97 Ok(Box::pin(subscription.filter_map(
98 |block_sealing_info_result| async move { block_sealing_info_result.ok() },
99 )))
100 }
101
102 async fn submit_block_seal(&self, block_seal: BlockSealResponse) -> anyhow::Result<()> {
103 Ok(self
104 .client
105 .request("submitBlockSeal", rpc_params![&block_seal])
106 .await?)
107 }
108
109 async fn subscribe_new_super_segment_headers(
110 &self,
111 ) -> anyhow::Result<Pin<Box<dyn Stream<Item = SuperSegmentHeader> + Send + 'static>>> {
112 let subscription = self
113 .client
114 .subscribe(
115 "subscribeNewSuperSegmentHeader",
116 rpc_params![],
117 "unsubscribeNewSuperSegmentHeader",
118 )
119 .await?;
120
121 Ok(Box::pin(subscription.filter_map(
122 |new_super_segment_header_result| async move { new_super_segment_header_result.ok() },
123 )))
124 }
125
126 async fn super_segment_headers(
127 &self,
128 super_segment_indices: Vec<SuperSegmentIndex>,
129 ) -> anyhow::Result<Vec<Option<SuperSegmentHeader>>> {
130 Ok(self
131 .client
132 .request("superSegmentHeaders", rpc_params![&super_segment_indices])
133 .await?)
134 }
135
136 async fn super_segment_header_for_segment_index(
137 &self,
138 segment_index: SegmentIndex,
139 ) -> anyhow::Result<Option<SuperSegmentHeader>> {
140 Ok(self
141 .client
142 .request(
143 "superSegmentHeaderForSegmentIndex",
144 rpc_params![&segment_index],
145 )
146 .await?)
147 }
148
149 async fn piece(&self, piece_index: PieceIndex) -> anyhow::Result<Option<Piece>> {
150 let _permit = self.piece_request_semaphore.acquire().await;
151 let client = Arc::clone(&self.client);
152 let piece_fut =
155 tokio::task::spawn(
156 async move { client.request("piece", rpc_params![&piece_index]).await },
157 );
158 Ok(piece_fut.await??)
159 }
160
161 async fn update_shard_membership_info(
162 &self,
163 info: FarmerShardMembershipInfo,
164 ) -> anyhow::Result<()> {
165 Ok(self
166 .client
167 .request("updateShardMembershipInfo", rpc_params![&info])
168 .await?)
169 }
170}
171
172#[async_trait]
173impl NodeClientExt for RpcNodeClient {
174 async fn cached_super_segment_headers(
175 &self,
176 super_segment_indices: Vec<SuperSegmentIndex>,
177 ) -> anyhow::Result<Vec<Option<SuperSegmentHeader>>> {
178 self.super_segment_headers(super_segment_indices).await
179 }
180
181 async fn last_super_segment_headers(
182 &self,
183 limit: u32,
184 ) -> anyhow::Result<Vec<Option<SuperSegmentHeader>>> {
185 Ok(self
186 .client
187 .request("lastSuperSegmentHeaders", rpc_params![limit])
188 .await?)
189 }
190}