-
Notifications
You must be signed in to change notification settings - Fork 46
/
client.rs
338 lines (288 loc) · 9.93 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use pallas_codec::Fragment;
use std::marker::PhantomData;
use thiserror::Error;
use tracing::debug;
use crate::miniprotocols::Point;
use crate::multiplexer;
use super::{BlockContent, HeaderContent, IntersectResponse, Message, State, Tip};
#[derive(Error, Debug)]
pub enum ClientError {
#[error("attempted to receive message while agency is ours")]
AgencyIsOurs,
#[error("attempted to send message while agency is theirs")]
AgencyIsTheirs,
#[error("inbound message is not valid for current state")]
InvalidInbound,
#[error("outbound message is not valid for current state")]
InvalidOutbound,
#[error("no intersection point found")]
IntersectionNotFound,
#[error("error while sending or receiving data through the channel")]
Plexer(multiplexer::Error),
}
#[derive(Debug)]
pub enum NextResponse<CONTENT> {
RollForward(CONTENT, Tip),
RollBackward(Point, Tip),
Await,
}
pub struct Client<O>(State, multiplexer::ChannelBuffer, PhantomData<O>)
where
Message<O>: Fragment;
impl<O> Client<O>
where
Message<O>: Fragment,
{
/// Constructs a new ChainSync `Client` instance.
///
/// # Arguments
///
/// * `channel` - An instance of `multiplexer::AgentChannel` to be used for
/// communication.
pub fn new(channel: multiplexer::AgentChannel) -> Self {
Self(
State::Idle,
multiplexer::ChannelBuffer::new(channel),
PhantomData {},
)
}
/// Returns the current state of the client.
pub fn state(&self) -> &State {
&self.0
}
/// Checks if the client is done.
pub fn is_done(&self) -> bool {
self.0 == State::Done
}
/// Checks if the client has agency.
pub fn has_agency(&self) -> bool {
match self.state() {
State::Idle => true,
State::CanAwait => false,
State::MustReply => false,
State::Intersect => false,
State::Done => false,
}
}
fn assert_agency_is_ours(&self) -> Result<(), ClientError> {
if !self.has_agency() {
Err(ClientError::AgencyIsTheirs)
} else {
Ok(())
}
}
fn assert_agency_is_theirs(&self) -> Result<(), ClientError> {
if self.has_agency() {
Err(ClientError::AgencyIsOurs)
} else {
Ok(())
}
}
fn assert_outbound_state(&self, msg: &Message<O>) -> Result<(), ClientError> {
match (&self.0, msg) {
(State::Idle, Message::RequestNext) => Ok(()),
(State::Idle, Message::FindIntersect(_)) => Ok(()),
(State::Idle, Message::Done) => Ok(()),
_ => Err(ClientError::InvalidOutbound),
}
}
fn assert_inbound_state(&self, msg: &Message<O>) -> Result<(), ClientError> {
match (&self.0, msg) {
(State::CanAwait, Message::RollForward(_, _)) => Ok(()),
(State::CanAwait, Message::RollBackward(_, _)) => Ok(()),
(State::CanAwait, Message::AwaitReply) => Ok(()),
(State::MustReply, Message::RollForward(_, _)) => Ok(()),
(State::MustReply, Message::RollBackward(_, _)) => Ok(()),
(State::Intersect, Message::IntersectFound(_, _)) => Ok(()),
(State::Intersect, Message::IntersectNotFound(_)) => Ok(()),
_ => Err(ClientError::InvalidInbound),
}
}
/// Sends a message to the server
///
/// # Arguments
///
/// * `msg` - A reference to the `Message` to be sent.
///
/// # Errors
///
/// Returns an error if the agency is not ours or if the outbound state is
/// invalid.
pub async fn send_message(&mut self, msg: &Message<O>) -> Result<(), ClientError> {
self.assert_agency_is_ours()?;
self.assert_outbound_state(msg)?;
self.1
.send_msg_chunks(msg)
.await
.map_err(ClientError::Plexer)?;
Ok(())
}
/// Receives the next message from the server.
///
/// # Errors
///
/// Returns an error if the agency is not theirs or if the inbound state is
/// invalid.
pub async fn recv_message(&mut self) -> Result<Message<O>, ClientError> {
self.assert_agency_is_theirs()?;
let msg = self.1.recv_full_msg().await.map_err(ClientError::Plexer)?;
self.assert_inbound_state(&msg)?;
Ok(msg)
}
/// Sends a FindIntersect message to the server.
///
/// # Arguments
///
/// * `points` - A vector of `Point` instances representing the points of
/// intersection.
///
/// # Errors
///
/// Returns an error if the message cannot be sent or if it's not valid for
/// the current state of the client.
pub async fn send_find_intersect(&mut self, points: Vec<Point>) -> Result<(), ClientError> {
let msg = Message::FindIntersect(points);
self.send_message(&msg).await?;
self.0 = State::Intersect;
debug!("send find intersect");
Ok(())
}
/// Receives an IntersectResponse message from the server.
///
/// # Errors
///
/// Returns an error if the inbound message is invalid.
pub async fn recv_intersect_response(&mut self) -> Result<IntersectResponse, ClientError> {
debug!("waiting for intersect response");
match self.recv_message().await? {
Message::IntersectFound(point, tip) => {
self.0 = State::Idle;
Ok((Some(point), tip))
}
Message::IntersectNotFound(tip) => {
self.0 = State::Idle;
Ok((None, tip))
}
_ => Err(ClientError::InvalidInbound),
}
}
/// Finds the intersection point between the client's and server's chains.
///
/// # Arguments
///
/// * `points` - A vector of `Point` instances representing the points of
/// intersection.
///
/// # Errors
///
/// Returns an error if the intersection point cannot be found or if there
/// is a communication error.
pub async fn find_intersect(
&mut self,
points: Vec<Point>,
) -> Result<IntersectResponse, ClientError> {
self.send_find_intersect(points).await?;
self.recv_intersect_response().await
}
pub async fn send_request_next(&mut self) -> Result<(), ClientError> {
let msg = Message::RequestNext;
self.send_message(&msg).await?;
self.0 = State::CanAwait;
Ok(())
}
/// Receives a response while the client is in the CanAwait state.
///
/// # Errors
///
/// Returns an error if the inbound message is invalid.
pub async fn recv_while_can_await(&mut self) -> Result<NextResponse<O>, ClientError> {
match self.recv_message().await? {
Message::AwaitReply => {
self.0 = State::MustReply;
Ok(NextResponse::Await)
}
Message::RollForward(a, b) => {
self.0 = State::Idle;
Ok(NextResponse::RollForward(a, b))
}
Message::RollBackward(a, b) => {
self.0 = State::Idle;
Ok(NextResponse::RollBackward(a, b))
}
_ => Err(ClientError::InvalidInbound),
}
}
/// Receives a response while the client is in the MustReply state.
///
/// # Errors
///
/// Returns an error if the inbound message is invalid.
pub async fn recv_while_must_reply(&mut self) -> Result<NextResponse<O>, ClientError> {
match self.recv_message().await? {
Message::RollForward(a, b) => {
self.0 = State::Idle;
Ok(NextResponse::RollForward(a, b))
}
Message::RollBackward(a, b) => {
self.0 = State::Idle;
Ok(NextResponse::RollBackward(a, b))
}
_ => Err(ClientError::InvalidInbound),
}
}
/// Sends a RequestNext message to the server.
///
/// # Errors
///
/// Returns an error if the message cannot be sent or if the state is not
/// idle.
pub async fn request_next(&mut self) -> Result<NextResponse<O>, ClientError> {
debug!("requesting next block");
self.send_request_next().await?;
self.recv_while_can_await().await
}
/// Either requests the next block, or waits for one to become available.
///
/// # Errors
///
/// Returns an error if the message cannot be sent, or if the inbound
/// message is invalid
pub async fn request_or_await_next(&mut self) -> Result<NextResponse<O>, ClientError> {
if self.has_agency() {
self.request_next().await
} else {
self.recv_while_must_reply().await
}
}
/// Attempt to intersect the chain at its origin (genesis block)
///
/// # Errors
///
/// Returns an error if the intersection point cannot be found or if there
/// is a communication error.
pub async fn intersect_origin(&mut self) -> Result<Point, ClientError> {
debug!("intersecting origin");
let (point, _) = self.find_intersect(vec![Point::Origin]).await?;
point.ok_or(ClientError::IntersectionNotFound)
}
/// Attempts to intersect the chain at the latest known tip
///
/// # Errors
///
/// Returns an error if the intersection point cannot be found or if there
/// is a communication error.
pub async fn intersect_tip(&mut self) -> Result<Point, ClientError> {
let (_, Tip(point, _)) = self.find_intersect(vec![Point::Origin]).await?;
debug!(?point, "found tip value");
let (point, _) = self.find_intersect(vec![point]).await?;
point.ok_or(ClientError::IntersectionNotFound)
}
pub async fn send_done(&mut self) -> Result<(), ClientError> {
let msg = Message::Done;
self.send_message(&msg).await?;
self.0 = State::Done;
Ok(())
}
}
pub type N2NClient = Client<HeaderContent>;
pub type N2CClient = Client<BlockContent>;