-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathprotocol.rs
More file actions
38 lines (34 loc) · 1.4 KB
/
protocol.rs
File metadata and controls
38 lines (34 loc) · 1.4 KB
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
use futures_util::{stream::SplitStream, StreamExt};
use tokio::net::TcpStream;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use crate::common::{errors::SubmitError, types::SubmitProofResponseMessage};
use super::serialization::cbor_deserialize;
pub const EXPECTED_PROTOCOL_VERSION: u16 = 4;
pub async fn check_protocol_version(
ws_read: &mut SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
) -> Result<(), SubmitError> {
if let Some(Ok(msg)) = ws_read.next().await {
match cbor_deserialize(msg.into_data().as_slice()) {
Ok(SubmitProofResponseMessage::ProtocolVersion(protocol_version)) => {
if protocol_version > EXPECTED_PROTOCOL_VERSION {
return Err(SubmitError::ProtocolVersionMismatch {
current: protocol_version,
expected: EXPECTED_PROTOCOL_VERSION,
});
}
return Ok(());
}
Ok(_) => {
return Err(SubmitError::UnexpectedBatcherResponse(
"Batcher did not respond with the protocol version".to_string(),
));
}
Err(e) => {
return Err(SubmitError::SerializationError(e));
}
}
}
Err(SubmitError::UnexpectedBatcherResponse(
"Batcher did not respond with the protocol version".to_string(),
))
}