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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::{collections::VecDeque, sync::Arc, time::Duration};
use crate::{
ActorMessage, NetworkMessage,
approval::types::VotationType,
helpers::network::service::NetworkSender,
model::{
common::emit_fail,
network::{RetryNetwork, TimeOut},
},
};
use async_trait::async_trait;
use ave_actors::{
Actor, ActorContext, ActorError, ActorPath, ChildAction,
CustomIntervalStrategy, Handler, Message, NotPersistentActor, RetryActor,
RetryMessage, Strategy,
};
use ave_common::identity::{
DigestIdentifier, HashAlgorithm, PublicKey, Signed, TimeStamp,
};
use ave_network::ComunicateInfo;
use tracing::{Span, debug, error, info_span, warn};
use super::{
Approval, ApprovalMessage, request::ApprovalReq, response::ApprovalRes,
};
#[derive(Clone, Debug)]
pub struct ApprLight {
network: Arc<NetworkSender>,
node_key: PublicKey,
request_id: DigestIdentifier,
version: u64,
}
impl ApprLight {
pub const fn new(
network: Arc<NetworkSender>,
node_key: PublicKey,
request_id: DigestIdentifier,
version: u64,
) -> Self {
Self {
network,
node_key,
request_id,
version,
}
}
}
pub struct InitApprLight {
pub request_id: DigestIdentifier,
pub version: u64,
pub our_key: Arc<PublicKey>,
pub node_key: PublicKey,
pub subject_id: String,
pub pass_votation: VotationType,
pub hash: HashAlgorithm,
pub network: Arc<NetworkSender>,
}
#[derive(Debug, Clone)]
pub enum ApprLightMessage {
EndRetry,
// Lanza los retries y envía la petición a la network(exponencial)
NetworkApproval {
approval_req: Signed<ApprovalReq>,
},
// Finaliza los retries y recibe la respuesta de la network
NetworkResponse {
approval_res: Signed<ApprovalRes>,
request_id: String,
version: u64,
sender: PublicKey,
},
}
impl Message for ApprLightMessage {}
#[async_trait]
impl Actor for ApprLight {
type Event = ();
type Message = ApprLightMessage;
type Response = ();
fn get_span(id: &str, parent_span: Option<Span>) -> tracing::Span {
parent_span.map_or_else(
|| info_span!("ApprLight", id),
|parent_span| info_span!(parent: parent_span, "ApprLight", id),
)
}
}
#[async_trait]
impl Handler<Self> for ApprLight {
async fn handle_message(
&mut self,
_sender: ActorPath,
msg: ApprLightMessage,
ctx: &mut ActorContext<Self>,
) -> Result<(), ActorError> {
match msg {
ApprLightMessage::EndRetry => {
debug!(
node_key = %self.node_key,
"Retry exhausted, notifying parent and stopping"
);
match ctx.get_parent::<Approval>().await {
Ok(approval_actor) => {
if let Err(e) = approval_actor
.tell(ApprovalMessage::Response {
approval_res: ApprovalRes::TimeOut(TimeOut {
re_trys: 3,
timestamp: TimeStamp::now(),
who: self.node_key.clone(),
}),
sender: self.node_key.clone(),
signature: None,
})
.await
{
error!(
error = %e,
"Failed to send timeout response to approval actor"
);
emit_fail(ctx, e).await;
}
debug!(
request_id = %self.request_id,
version = self.version,
node_key = %self.node_key,
"Timeout response sent to approval actor"
);
}
Err(e) => {
error!(
error = %e,
path = %ctx.path().parent(),
"Approval actor not found"
);
emit_fail(ctx, e).await;
}
}
ctx.stop(None).await;
}
ApprLightMessage::NetworkApproval { approval_req } => {
// Solo admitimos eventos FACT
let subject_id = approval_req.content().subject_id.clone();
let receiver_actor = format!(
"/user/node/subject_manager/{}/approver",
subject_id
);
let message = NetworkMessage {
info: ComunicateInfo {
request_id: self.request_id.to_string(),
version: self.version,
receiver: self.node_key.clone(),
receiver_actor,
},
message: ActorMessage::ApprovalReq { req: approval_req },
};
let target = RetryNetwork::new(self.network.clone());
let strategy = Strategy::CustomIntervalStrategy(
CustomIntervalStrategy::new(VecDeque::from([
Duration::from_hours(4),
Duration::from_hours(8),
Duration::from_hours(16),
])),
);
let retry_actor = RetryActor::new_with_parent_message::<Self>(
target,
message,
strategy,
ApprLightMessage::EndRetry,
);
let retry = match ctx
.create_child::<RetryActor<RetryNetwork>, _>(
"retry",
retry_actor,
)
.await
{
Ok(retry) => retry,
Err(e) => {
error!(
msg_type = "NetworkApproval",
error = %e,
"Failed to create retry actor"
);
return Err(emit_fail(ctx, e).await);
}
};
if let Err(e) = retry.tell(RetryMessage::Retry).await {
error!(
msg_type = "NetworkApproval",
error = %e,
"Failed to send retry message"
);
return Err(emit_fail(ctx, e).await);
};
debug!(
msg_type = "NetworkApproval",
request_id = %self.request_id,
version = self.version,
"Retry actor created and started"
);
}
// Finaliza los retries
ApprLightMessage::NetworkResponse {
approval_res,
request_id,
version,
sender,
} => {
if request_id == self.request_id.to_string()
&& version == self.version
{
if self.node_key != sender
|| sender != approval_res.signature().signer
{
error!(
msg_type = "NetworkResponse",
expected_node = %self.node_key,
received_sender = %sender,
"Unexpected approval sender"
);
return Ok(());
}
if let Err(e) = approval_res.verify() {
error!(
msg_type = "NetworkResponse",
error = %e,
"Invalid approval signature"
);
return Ok(());
}
match ctx.get_parent::<Approval>().await {
Ok(approval_actor) => {
if let Err(e) = approval_actor
.tell(ApprovalMessage::Response {
approval_res: approval_res
.content()
.clone(),
sender: self.node_key.clone(),
signature: Some(
approval_res.signature().clone(),
),
})
.await
{
error!(
msg_type = "NetworkResponse",
error = %e,
"Failed to send response to approval actor"
);
return Err(emit_fail(ctx, e).await);
}
}
Err(e) => {
error!(
msg_type = "NetworkResponse",
path = %ctx.path().parent(),
"Approval actor not found"
);
return Err(emit_fail(ctx, e).await);
}
};
'retry: {
let Ok(retry) = ctx
.get_child::<RetryActor<RetryNetwork>>("retry")
.await
else {
break 'retry;
};
if let Err(e) = retry.tell(RetryMessage::End).await {
error!(
msg_type = "NetworkResponse",
error = %e,
"Failed to send end message to retry actor"
);
break 'retry;
};
}
debug!(
msg_type = "NetworkResponse",
request_id = %request_id,
version = version,
"Approval response processed successfully"
);
ctx.stop(None).await;
} else {
warn!(
msg_type = "NetworkResponse",
expected_request_id = %self.request_id,
received_request_id = %request_id,
expected_version = self.version,
received_version = version,
"Mismatched request id or version"
);
}
}
}
Ok(())
}
async fn on_child_fault(
&mut self,
error: ActorError,
ctx: &mut ActorContext<Self>,
) -> ChildAction {
error!(
request_id = %self.request_id,
version = self.version,
node_key = %self.node_key,
error = %error,
"Child fault in approval light actor"
);
emit_fail(ctx, error).await;
ChildAction::Stop
}
}
impl NotPersistentActor for ApprLight {}