-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathbatch.rs
More file actions
80 lines (71 loc) · 2.57 KB
/
batch.rs
File metadata and controls
80 lines (71 loc) · 2.57 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
use log::debug;
use crate::{
common::{
errors,
types::{
AlignedVerificationData, BatchInclusionData, Network, VerificationCommitmentBatch,
VerificationDataCommitment,
},
},
verification_layer::is_proof_verified,
};
const RETRIES: u64 = 10;
const TIME_BETWEEN_RETRIES: u64 = 10;
pub fn process_batcher_response(
batch_inclusion_data: &BatchInclusionData,
verification_data_commitment: &VerificationDataCommitment,
) -> Result<AlignedVerificationData, errors::SubmitError> {
debug!("Received response from batcher");
debug!(
"Batch merkle root: {}",
hex::encode(batch_inclusion_data.batch_merkle_root)
);
debug!("Index in batch: {}", batch_inclusion_data.index_in_batch);
if verify_proof_inclusion(verification_data_commitment, batch_inclusion_data) {
Ok(AlignedVerificationData::new(
verification_data_commitment,
batch_inclusion_data,
))
} else {
Err(errors::SubmitError::InvalidProofInclusionData)
}
}
pub async fn await_batch_verification(
aligned_verification_data: &AlignedVerificationData,
rpc_url: &str,
network: Network,
) -> Result<(), errors::SubmitError> {
for _ in 0..RETRIES {
if is_proof_verified(aligned_verification_data, network.clone(), rpc_url)
.await
.is_ok_and(|r| r)
{
return Ok(());
}
debug!(
"Proof not verified yet. Waiting {} seconds before checking again...",
TIME_BETWEEN_RETRIES
);
tokio::time::sleep(tokio::time::Duration::from_secs(TIME_BETWEEN_RETRIES)).await;
}
Err(errors::SubmitError::BatchVerificationTimeout {
timeout_seconds: (TIME_BETWEEN_RETRIES * RETRIES),
})
}
fn verify_proof_inclusion(
verification_data_commitment: &VerificationDataCommitment,
batch_inclusion_data: &BatchInclusionData,
) -> bool {
debug!("Verifying response data matches sent proof data ...");
let batch_inclusion_proof = batch_inclusion_data.batch_inclusion_proof.clone();
if batch_inclusion_proof.verify::<VerificationCommitmentBatch>(
&batch_inclusion_data.batch_merkle_root,
batch_inclusion_data.index_in_batch,
verification_data_commitment,
) {
debug!("Done. Data sent matches batcher answer");
return true;
}
debug!("Verification data commitments and batcher response with merkle root {} and index in batch {} don't match", hex::encode(batch_inclusion_data.batch_merkle_root), batch_inclusion_data.index_in_batch);
false
}