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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use std::{sync::Arc, time::Duration};
use crate::{
helpers::network::{NetworkMessage, service::NetworkSender},
model::{common::emit_fail, network::RetryNetwork},
};
use crate::helpers::network::ActorMessage;
use async_trait::async_trait;
use ave_common::identity::{PublicKey, Signed};
use ave_network::ComunicateInfo;
use ave_actors::{
Actor, ActorContext, ActorError, ActorPath, ChildAction,
FixedIntervalStrategy, Handler, Message, NotPersistentActor, RetryActor,
RetryMessage, Strategy,
};
use tracing::{Span, debug, error, info_span, warn};
use super::{
Validation, ValidationMessage, request::ValidationReq,
response::ValidationRes,
};
/// A struct representing a ValiCoordinator actor.
#[derive(Clone, Debug)]
pub struct ValiCoordinator {
node_key: PublicKey,
request_id: String,
version: u64,
network: Arc<NetworkSender>,
}
impl ValiCoordinator {
pub const fn new(
node_key: PublicKey,
request_id: String,
version: u64,
network: Arc<NetworkSender>,
) -> Self {
Self {
node_key,
request_id,
version,
network,
}
}
}
#[derive(Debug, Clone)]
pub enum ValiCoordinatorMessage {
NetworkValidation {
validation_req: Box<Signed<ValidationReq>>,
node_key: PublicKey,
},
NetworkResponse {
validation_res: Box<Signed<ValidationRes>>,
request_id: String,
version: u64,
sender: PublicKey,
},
EndRetry,
}
impl Message for ValiCoordinatorMessage {}
#[async_trait]
impl Actor for ValiCoordinator {
type Event = ();
type Message = ValiCoordinatorMessage;
type Response = ();
fn get_span(id: &str, parent_span: Option<Span>) -> tracing::Span {
parent_span.map_or_else(
|| info_span!("ValiCoordinator", id),
|parent_span| info_span!(parent: parent_span, "ValiCoordinator", id),
)
}
}
impl NotPersistentActor for ValiCoordinator {}
#[async_trait]
impl Handler<Self> for ValiCoordinator {
async fn handle_message(
&mut self,
_sender: ActorPath,
msg: ValiCoordinatorMessage,
ctx: &mut ActorContext<Self>,
) -> Result<(), ActorError> {
match msg {
ValiCoordinatorMessage::EndRetry => {
warn!(
node_key = %self.node_key,
request_id = %self.request_id,
version = self.version,
"Retry exhausted, notifying parent and stopping"
);
match ctx.get_parent::<Validation>().await {
Ok(validation_actor) => {
if let Err(e) = validation_actor
.tell(ValidationMessage::Response {
validation_res: Box::new(
ValidationRes::TimeOut,
),
signature: None,
sender: self.node_key.clone(),
})
.await
{
error!(
error = %e,
"Failed to send timeout response to validation actor"
);
emit_fail(ctx, e).await;
} else {
debug!(
request_id = %self.request_id,
version = self.version,
"Timeout response sent to validation actor"
);
}
}
Err(e) => {
error!(
error = %e,
path = %ctx.path().parent(),
"Validation actor not found"
);
emit_fail(ctx, e).await;
}
}
ctx.stop(None).await;
}
ValiCoordinatorMessage::NetworkValidation {
validation_req,
node_key,
} => {
let schema_id = validation_req.content().get_schema_id().expect("The build process verified that the event request is valid");
let governance_id = validation_req.content().get_governance_id().expect("The build process verified that the event request is valid");
let receiver_actor = if schema_id.is_gov() {
format!(
"/user/node/subject_manager/{}/validator",
governance_id
)
} else {
format!(
"/user/node/subject_manager/{}/{}_validation",
governance_id, schema_id
)
};
// Lanzar evento donde lanzar los retrys
let message = NetworkMessage {
info: ComunicateInfo {
request_id: self.request_id.clone(),
version: self.version,
receiver: node_key.clone(),
receiver_actor,
},
message: ActorMessage::ValidationReq {
req: *validation_req,
},
};
let target = RetryNetwork::new(self.network.clone());
#[cfg(any(test, feature = "test"))]
let strategy = Strategy::FixedInterval(
FixedIntervalStrategy::new(1, Duration::from_secs(10)),
);
#[cfg(not(any(test, feature = "test")))]
let strategy = Strategy::FixedInterval(
FixedIntervalStrategy::new(3, Duration::from_secs(30)),
);
let retry_actor = RetryActor::new_with_parent_message::<Self>(
target,
message,
strategy,
ValiCoordinatorMessage::EndRetry,
);
let retry = match ctx
.create_child::<RetryActor<RetryNetwork>, _>(
"retry",
retry_actor,
)
.await
{
Ok(retry) => retry,
Err(e) => {
error!(
msg_type = "NetworkValidation",
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 = "NetworkValidation",
error = %e,
"Failed to send retry message to retry actor"
);
return Err(emit_fail(ctx, e).await);
} else {
debug!(
msg_type = "NetworkValidation",
request_id = %self.request_id,
version = self.version,
node_key = %node_key,
"Validation request sent to network with retry"
);
};
}
ValiCoordinatorMessage::NetworkResponse {
validation_res,
request_id,
version,
sender,
} => {
if request_id == self.request_id && version == self.version {
if self.node_key != sender
|| sender != validation_res.signature().signer
{
error!(
msg_type = "NetworkResponse",
expected_node = %self.node_key,
sender = %sender,
signer = %validation_res.signature().signer,
"Validation response sender mismatch"
);
return Err(ActorError::Functional {
description:
"We received a validation response from an unexpected sender"
.to_string(),
});
}
if let Err(e) = validation_res.verify() {
error!(
msg_type = "NetworkResponse",
error = %e,
"Failed to verify validation response signature"
);
return Err(ActorError::Functional {
description: format!(
"Can not verify signature: {}",
e
),
});
}
match ctx.get_parent::<Validation>().await {
Ok(validation_actor) => {
if let Err(e) = validation_actor
.tell(ValidationMessage::Response {
validation_res: Box::new(
validation_res.content().clone(),
),
sender: self.node_key.clone(),
signature: Some(
validation_res.signature().clone(),
),
})
.await
{
error!(
msg_type = "NetworkResponse",
error = %e,
"Failed to send response to validation actor"
);
return Err(emit_fail(ctx, e).await);
}
}
Err(e) => {
error!(
msg_type = "NetworkResponse",
error = %e,
path = %ctx.path().parent(),
"Validation actor not found"
);
return Err(emit_fail(ctx, e).await);
}
};
'retry: {
let Ok(retry) = ctx
.get_child::<RetryActor<RetryNetwork>>("retry")
.await
else {
debug!(
msg_type = "NetworkResponse",
sender = %sender,
"Retry actor not found while closing validation coordinator"
);
// Aquí me da igual, porque al parar este actor para el hijo
break 'retry;
};
if let Err(e) = retry.tell(RetryMessage::End).await {
warn!(
msg_type = "NetworkResponse",
error = %e,
"Failed to end retry actor"
);
// Aquí me da igual, porque al parar este actor para el hijo
break 'retry;
};
}
debug!(
msg_type = "NetworkResponse",
request_id = %self.request_id,
version = self.version,
sender = %sender,
"Validation response processed successfully"
);
ctx.stop(None).await;
} else {
warn!(
msg_type = "NetworkResponse",
expected_request_id = %self.request_id,
expected_version = self.version,
received_request_id = %request_id,
received_version = version,
"Response with mismatched request id or version"
);
}
}
}
Ok(())
}
async fn on_child_fault(
&mut self,
error: ActorError,
ctx: &mut ActorContext<Self>,
) -> ChildAction {
error!(
node_key = %self.node_key,
request_id = %self.request_id,
version = self.version,
error = %error,
"Child fault in validation coordinator"
);
emit_fail(ctx, error).await;
ChildAction::Stop
}
}