-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmessaging.rs
More file actions
326 lines (301 loc) · 13.1 KB
/
messaging.rs
File metadata and controls
326 lines (301 loc) · 13.1 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
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
use ethers::signers::Signer;
use ethers::types::Address;
use futures_util::{stream::SplitStream, SinkExt, StreamExt};
use log::{debug, error, info, warn};
use std::sync::Arc;
use tokio::{net::TcpStream, sync::Mutex};
use ethers::{core::k256::ecdsa::SigningKey, signers::Wallet, types::U256};
use futures_util::future::Ready;
use futures_util::stream::{SplitSink, TryFilter};
use tokio_tungstenite::{tungstenite::Message, MaybeTlsStream, WebSocketStream};
use crate::communication::serialization::{cbor_deserialize, cbor_serialize};
use crate::core::types::{BatchInclusionData, SubmitProofMessage};
use crate::{
communication::batch::process_batcher_response,
core::{
errors::SubmitError,
types::{
AlignedVerificationData, ClientMessage, NoncedVerificationData,
SubmitProofResponseMessage, VerificationData, VerificationDataCommitment,
},
},
};
pub type ResponseStream = TryFilter<
SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
Ready<bool>,
fn(&Message) -> Ready<bool>,
>;
// Sends the proofs to the batcher via WS
// Stores the proofs sent in an array
// Returns the array
pub async fn send_messages(
ws_write: Arc<Mutex<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>>,
payment_service_addr: Address,
verification_data: &[VerificationData],
max_fee: U256,
wallet: Wallet<SigningKey>,
mut nonce: U256,
) -> Vec<Result<NoncedVerificationData, SubmitError>> {
let chain_id = U256::from(wallet.chain_id());
let mut ws_write = ws_write.lock().await;
let mut sent_verification_data: Vec<Result<NoncedVerificationData, SubmitError>> = Vec::new();
for (idx, verification_data_i) in verification_data.iter().enumerate() {
// Build each message to send
let verification_data = NoncedVerificationData::new(
verification_data_i.clone(),
nonce,
max_fee,
chain_id,
payment_service_addr,
);
nonce += U256::one();
let data = SubmitProofMessage::new(verification_data.clone(), wallet.clone()).await;
let msg = ClientMessage::SubmitProof(Box::new(data));
let msg_bin = match cbor_serialize(&msg) {
Ok(bin) => bin,
Err(e) => {
error!("Error while serializing message: {:?}", e);
sent_verification_data.push(Err(SubmitError::SerializationError(e)));
return sent_verification_data;
}
};
// Send the message
if let Err(e) = ws_write.send(Message::Binary(msg_bin.clone())).await {
error!("Error while sending message: {:?}", e);
sent_verification_data.push(Err(SubmitError::WebSocketConnectionError(e)));
return sent_verification_data;
}
debug!("{:?} Message sent", idx);
// Save the verification data commitment to read its response later
sent_verification_data.push(Ok(verification_data));
}
info!("All proofs sent");
// This vector is reversed so that while responses are received, removing from the end is cheaper.
let sent_verification_data_rev: Vec<Result<NoncedVerificationData, SubmitError>> =
sent_verification_data.into_iter().rev().collect();
sent_verification_data_rev
}
// Receives the array of proofs sent
// Reads the WS responses
// Matches each response with the corresponding proof sent
// finishes when the last proof sent receives its response
// finishes early if the batcher replies with a SubmitError
pub async fn receive(
response_stream: Arc<Mutex<ResponseStream>>,
mut sent_verification_data_rev: Vec<Result<NoncedVerificationData, SubmitError>>,
) -> Vec<Result<AlignedVerificationData, SubmitError>> {
// Responses are filtered to only admit binary or close messages.
let mut response_stream = response_stream.lock().await;
let mut aligned_submitted_data: Vec<Result<AlignedVerificationData, SubmitError>> = Vec::new();
let last_proof_nonce = get_biggest_nonce(&sent_verification_data_rev);
// read from WS
while let Some(Ok(msg)) = response_stream.next().await {
// unexpected WS close:
if let Message::Close(close_frame) = msg {
warn!("Unexpected WS close");
if let Some(close_msg) = close_frame {
aligned_submitted_data.push(Err(SubmitError::WebSocketClosedUnexpectedlyError(
close_msg.to_owned(),
)));
break;
}
aligned_submitted_data.push(Err(SubmitError::GenericError(
"Connection was closed before receive() processed all sent messages ".to_string(),
)));
break;
}
// first error msg from batcher will drop the rest of the messages in the burst
let batch_inclusion_data_message = match handle_batcher_response(msg).await {
Ok(data) => data,
Err(e) => {
warn!("Error while handling batcher response: {:?}", e);
aligned_submitted_data.push(Err(e));
break;
}
};
let related_verification_data = match match_batcher_response_with_stored_verification_data(
&batch_inclusion_data_message,
&mut sent_verification_data_rev,
) {
Ok(data) => data,
Err(e) => {
warn!(
"Error while matching batcher response with sent data: {:?}",
e
);
aligned_submitted_data.push(Err(e));
break;
}
};
let aligned_verification_data = match process_batcher_response(
&batch_inclusion_data_message,
&related_verification_data,
) {
Ok(data) => data,
Err(e) => {
warn!("Error while processing batcher response: {:?}", e);
aligned_submitted_data.push(Err(e));
break;
}
};
aligned_submitted_data.push(Ok(aligned_verification_data));
debug!("Message response handled successfully");
if batch_inclusion_data_message.user_nonce == last_proof_nonce {
break;
}
}
aligned_submitted_data
}
async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, SubmitError> {
let data = msg.into_data();
match cbor_deserialize(data.as_slice()) {
Ok(SubmitProofResponseMessage::BatchInclusionData(batch_inclusion_data)) => {
//OK case. Proofs was valid and it was included in this batch.
Ok(batch_inclusion_data)
}
Ok(SubmitProofResponseMessage::InvalidNonce) => {
error!("Batcher responded with invalid nonce. Funds have not been spent.");
Err(SubmitError::InvalidNonce)
}
Ok(SubmitProofResponseMessage::InvalidSignature) => {
error!("Batcher responded with invalid signature. Funds have not been spent.");
Err(SubmitError::InvalidSignature)
}
Ok(SubmitProofResponseMessage::ProofTooLarge) => {
error!("Batcher responded with proof too large. Funds have not been spent.");
Err(SubmitError::ProofTooLarge)
}
Ok(SubmitProofResponseMessage::InvalidMaxFee) => {
error!("Batcher responded with invalid max fee. Funds have not been spent.");
Err(SubmitError::InvalidMaxFee)
}
Ok(SubmitProofResponseMessage::InsufficientBalance(addr)) => {
error!("Batcher responded with insufficient balance. Funds have not been spent for submittions which had insufficient balance.");
Err(SubmitError::InsufficientBalance(addr))
}
Ok(SubmitProofResponseMessage::InvalidChainId) => {
error!("Batcher responded with invalid chain id. Funds have not been spent.");
Err(SubmitError::InvalidChainId)
}
Ok(SubmitProofResponseMessage::InvalidReplacementMessage) => {
error!(
"Batcher responded with invalid replacement message. Funds have not been spent."
);
Err(SubmitError::InvalidReplacementMessage)
}
Ok(SubmitProofResponseMessage::AddToBatchError) => {
error!("Batcher responded with add to batch error. Funds have not been spent.");
Err(SubmitError::AddToBatchError)
}
Ok(SubmitProofResponseMessage::EthRpcError) => {
error!("Batcher experienced Eth RPC connection error. Funds have not been spent.");
Err(SubmitError::EthereumProviderError(
"Batcher experienced Eth RPC connection error. Funds have not been spent."
.to_string(),
))
}
Ok(SubmitProofResponseMessage::InvalidPaymentServiceAddress(
received_addr,
expected_addr,
)) => {
error!(
"Batcher responded with invalid payment service address: {:?}, expected: {:?}. Funds have not been spent.",
received_addr, expected_addr
);
Err(SubmitError::InvalidPaymentServiceAddress(
received_addr,
expected_addr,
))
}
Ok(SubmitProofResponseMessage::InvalidProof(reason)) => {
error!(
"Batcher responded with invalid proof: {}. Funds have not been spent.",
reason
);
Err(SubmitError::InvalidProof(reason))
}
Ok(SubmitProofResponseMessage::CreateNewTaskError(merkle_root, error)) => {
error!(
"Batcher responded with create new task error: {}. Funds have not been spent.",
error
);
Err(SubmitError::BatchSubmissionFailed(
"Could not create task with merkle root ".to_owned()
+ &merkle_root
+ ", failed with error: "
+ &error,
))
}
Ok(SubmitProofResponseMessage::ProtocolVersion(_)) => {
error!("Batcher responded with protocol version instead of batch inclusion data. Funds have not been spent.");
Err(SubmitError::UnexpectedBatcherResponse(
"Batcher responded with protocol version instead of batch inclusion data. Funds have not been spent."
.to_string(),
))
}
Ok(SubmitProofResponseMessage::BatchReset) => {
error!("Batcher responded with batch reset. Funds have not been spent.");
Err(SubmitError::ProofQueueFlushed)
}
Ok(SubmitProofResponseMessage::Error(e)) => {
error!(
"Batcher responded with error: {}. Funds have not been spent.",
e
);
Err(SubmitError::GenericError(e))
}
Ok(SubmitProofResponseMessage::BatchQueueLimitExceededError) => {
error!("Batcher responded with error: queue limit has been exeeded. Funds have not been spent.");
Err(SubmitError::BatchQueueLimitExceededError)
}
Err(e) => {
error!(
"Error while deserializing batch inclusion data: {}. Funds have not been spent.",
e
);
Err(SubmitError::SerializationError(e))
}
}
}
// Used to match the message received from the batcher,
// with the NoncedVerificationData you sent
// This is used to verify the proof you sent was indeed included in the batch
fn match_batcher_response_with_stored_verification_data(
batch_inclusion_data: &BatchInclusionData,
sent_verification_data_rev: &mut Vec<Result<NoncedVerificationData, SubmitError>>,
) -> Result<VerificationDataCommitment, SubmitError> {
debug!("Matching verification data with batcher response ...");
let mut index = None;
for (i, sent_nonced_verification_data) in
sent_verification_data_rev.iter_mut().enumerate().rev()
{
// iterate in reverse since the last element is the most probable to match
if let Ok(sent_nonced_verification_data) = sent_nonced_verification_data {
if sent_nonced_verification_data.nonce == batch_inclusion_data.user_nonce {
debug!("local nonced verification data matched with batcher response");
index = Some(i);
break;
}
}
}
// cant remove an element while iterating, so we remove it here
if let Some(i) = index {
let verification_data = sent_verification_data_rev.remove(i).unwrap();
return Ok(verification_data.verification_data.clone().into());
}
Err(SubmitError::InvalidProofInclusionData)
}
// Returns the biggest nonce from the sent verification data
// Used to know which is the last proof sent to the Batcher,
// to know when to stop reading the WS for responses
fn get_biggest_nonce(
sent_verification_data: &[Result<NoncedVerificationData, SubmitError>],
) -> U256 {
let mut biggest_nonce = U256::zero();
for verification_data in sent_verification_data.iter().flatten() {
if verification_data.nonce > biggest_nonce {
biggest_nonce = verification_data.nonce;
}
}
biggest_nonce
}