-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathconnection.rs
More file actions
75 lines (65 loc) · 2.59 KB
/
connection.rs
File metadata and controls
75 lines (65 loc) · 2.59 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
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
use std::sync::Arc;
use crate::types::{batch_queue::BatchQueueEntry, errors::BatcherError};
use aligned_sdk::{
communication::serialization::cbor_serialize,
core::types::{BatchInclusionData, SubmitProofResponseMessage, VerificationCommitmentBatch},
};
use futures_util::{stream::SplitSink, SinkExt};
use lambdaworks_crypto::merkle_tree::merkle::MerkleTree;
use log::{debug, error};
use serde::Serialize;
use tokio::{net::TcpStream, sync::RwLock};
use tokio_rustls::server::TlsStream;
use tokio_tungstenite::{
tungstenite::{Error, Message},
WebSocketStream,
};
pub(crate) type WsMessageSink =
Arc<RwLock<SplitSink<WebSocketStream<TlsStream<TcpStream>>, Message>>>;
pub(crate) async fn send_batch_inclusion_data_responses(
finalized_batch: Vec<BatchQueueEntry>,
batch_merkle_tree: &MerkleTree<VerificationCommitmentBatch>,
) -> Result<(), BatcherError> {
// Finalized_batch is ordered as the PriorityQueue, ordered by: ascending max_fee && if max_fee is equal, by descending nonce.
// We iter it in reverse because each sender wants to receive responses in ascending nonce order
for (vd_batch_idx, entry) in finalized_batch.iter().enumerate().rev() {
let batch_inclusion_data = BatchInclusionData::new(
vd_batch_idx,
batch_merkle_tree,
entry.nonced_verification_data.nonce,
);
let response = SubmitProofResponseMessage::BatchInclusionData(batch_inclusion_data);
let serialized_response = cbor_serialize(&response)
.map_err(|e| BatcherError::SerializationError(e.to_string()))?;
let Some(ws_sink) = entry.messaging_sink.as_ref() else {
return Err(BatcherError::WsSinkEmpty);
};
let sending_result = ws_sink
.write()
.await
.send(Message::binary(serialized_response))
.await;
match sending_result {
Err(Error::AlreadyClosed) => (),
Err(e) => error!("Error while sending batch inclusion data response: {}", e),
Ok(_) => (),
}
debug!("Response sent");
}
Ok(())
}
pub(crate) async fn send_message<T: Serialize>(ws_conn_sink: WsMessageSink, message: T) {
match cbor_serialize(&message) {
Ok(serialized_response) => {
if let Err(err) = ws_conn_sink
.write()
.await
.send(Message::binary(serialized_response))
.await
{
error!("Error while sending message: {}", err)
}
}
Err(e) => error!("Error while serializing message: {}", e),
}
}