-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathhandler.rs
98 lines (89 loc) · 3.28 KB
/
handler.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
use raiko_reqactor::Actor;
use raiko_reqpool::{
AggregationRequestEntity, AggregationRequestKey, RequestEntity, RequestKey,
SingleProofRequestKey, Status,
};
// NOTE: HTTP handlers should not check the status of the request, but just send the request to the Actor. In
// another word, Actor should be the only one guarding the status of the request.
/// Prove the request.
pub async fn prove(
actor: &Actor,
request_key: RequestKey,
request_entity: RequestEntity,
) -> Result<Status, String> {
if actor.is_paused() {
return Err("System is paused".to_string());
}
actor
.act(request_key.clone(), request_entity, chrono::Utc::now())
.await
.map(|status| status.into_status())
}
/// Cancel the request.
pub async fn cancel(_actor: &Actor, _request_key: RequestKey) -> Result<Status, String> {
unimplemented!()
}
/// Prove the aggregation request and its sub-requests.
pub async fn prove_aggregation(
actor: &Actor,
request_key: AggregationRequestKey,
request_entity_without_proofs: AggregationRequestEntity,
sub_request_keys: Vec<RequestKey>,
sub_request_entities: Vec<RequestEntity>,
) -> Result<Status, String> {
// Prove the sub-requests
let statuses = prove_many(actor, sub_request_keys, sub_request_entities).await?;
let is_all_sub_success = statuses
.iter()
.all(|status| matches!(status, Status::Success { .. }));
if !is_all_sub_success {
return Ok(Status::Registered);
}
// Prove the aggregation request
let proofs = statuses
.into_iter()
.map(|status| match status {
Status::Success { proof } => proof,
_ => unreachable!("checked above"),
})
.collect();
let request_entity = AggregationRequestEntity::new(
request_entity_without_proofs.aggregation_ids().clone(),
proofs,
request_entity_without_proofs.proof_type().clone(),
request_entity_without_proofs.prover_args().clone(),
);
prove(actor, request_key.into(), request_entity.into()).await
}
/// Prove many requests.
pub(crate) async fn prove_many(
actor: &Actor,
request_keys: Vec<RequestKey>,
request_entities: Vec<RequestEntity>,
) -> Result<Vec<Status>, String> {
let mut statuses = Vec::with_capacity(request_keys.len());
for (request_key, request_entity) in request_keys.into_iter().zip(request_entities) {
match (request_key, request_entity) {
(RequestKey::SingleProof(key), RequestEntity::SingleProof(entity)) => {
let status = prove(actor, key.into(), entity.into()).await?;
statuses.push(status);
}
(RequestKey::BatchProof(key), RequestEntity::BatchProof(entity)) => {
let status = prove(actor, key.into(), entity.into()).await?;
statuses.push(status);
}
_ => return Err("Invalid request key and entity".to_string()),
}
}
Ok(statuses)
}
pub async fn cancel_aggregation(
actor: &Actor,
request_key: AggregationRequestKey,
sub_request_keys: Vec<SingleProofRequestKey>,
) -> Result<Status, String> {
for sub_request_key in sub_request_keys {
let _discard = cancel(actor, sub_request_key.into()).await?;
}
cancel(actor, request_key.into()).await
}