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
use std::{collections::HashSet, sync::Arc};
use ave_actors::{
Actor, ActorContext, ActorError, ActorPath, ChildAction, Handler, Message,
NotPersistentActor,
};
use async_trait::async_trait;
use ave_common::identity::{DigestIdentifier, PublicKey};
use network::ComunicateInfo;
use serde::{Deserialize, Serialize};
use tracing::{Span, debug, error, info_span};
use updater::{Updater, UpdaterMessage};
use crate::{
NetworkMessage,
helpers::network::{ActorMessage, service::NetworkSender},
model::common::emit_fail,
request::manager::{RequestManager, RequestManagerMessage},
};
pub mod updater;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum UpdateType {
Auth,
Request {
subject_id: DigestIdentifier,
id: DigestIdentifier,
},
}
pub struct UpdateNew {
pub subject_id: DigestIdentifier,
pub witnesses: HashSet<PublicKey>,
pub update_type: UpdateType,
pub network: Arc<NetworkSender>,
pub our_sn: Option<u64>,
}
#[derive(Clone, Debug)]
pub struct Update {
subject_id: DigestIdentifier,
witnesses: HashSet<PublicKey>,
better: Option<(u64, PublicKey)>,
our_sn: Option<u64>,
update_type: UpdateType,
network: Arc<NetworkSender>,
}
impl Update {
pub fn new(data: UpdateNew) -> Self {
Self {
network: data.network,
subject_id: data.subject_id,
witnesses: data.witnesses,
update_type: data.update_type,
our_sn: data.our_sn,
better: None,
}
}
pub fn update_better(&mut self, sn: u64, sender: PublicKey) {
match self.better {
Some((better_sn, ..)) => {
if sn > better_sn {
self.better = Some((sn, sender))
}
}
None => {
self.better = Some((sn, sender));
}
}
}
fn check_witness(&mut self, witness: PublicKey) -> bool {
self.witnesses.remove(&witness)
}
async fn create_updates(
&self,
ctx: &mut ActorContext<Self>,
) -> Result<(), ActorError> {
for witness in self.witnesses.clone() {
let updater = Updater::new(witness.clone(), self.network.clone());
let child = ctx.create_child(&witness.to_string(), updater).await?;
let message = UpdaterMessage::NetworkLastSn {
subject_id: self.subject_id.clone(),
node_key: witness,
};
child.tell(message).await?;
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub enum UpdateMessage {
Run,
Response { sender: PublicKey, sn: u64 },
}
impl Message for UpdateMessage {}
#[async_trait]
impl Actor for Update {
type Event = ();
type Message = UpdateMessage;
type Response = ();
fn get_span(id: &str, parent_span: Option<Span>) -> tracing::Span {
parent_span.map_or_else(
|| info_span!("Update", id),
|parent_span| info_span!(parent: parent_span, "Update", id),
)
}
}
impl NotPersistentActor for Update {}
#[async_trait]
impl Handler<Self> for Update {
async fn handle_message(
&mut self,
_sender: ActorPath,
msg: UpdateMessage,
ctx: &mut ActorContext<Self>,
) -> Result<(), ActorError> {
match msg {
UpdateMessage::Run => {
if let Err(e) = self.create_updates(ctx).await {
error!(
msg_type = "Run",
error = %e,
"Failed to create updates"
);
return Err(emit_fail(ctx, e).await);
} else {
debug!(
msg_type = "Run",
witnesses_count = self.witnesses.len(),
"Updates created successfully"
);
}
}
UpdateMessage::Response { sender, sn } => {
if self.check_witness(sender.clone()) {
self.update_better(sn, sender);
if self.witnesses.is_empty() {
if let Some((.., better_node)) = self.better.clone() {
let info = ComunicateInfo {
receiver: better_node.clone(),
request_id: String::default(),
version: 0,
receiver_actor: format!(
"/user/node/distributor_{}",
self.subject_id
),
};
if let Err(e) = self
.network
.send_command(
network::CommandHelper::SendMessage {
message: NetworkMessage {
info: info.clone(),
message: ActorMessage::DistributionLedgerReq {
actual_sn: self.our_sn,
subject_id: self.subject_id.clone(),
},
},
},
)
.await
{
error!(
msg_type = "Response",
error = %e,
node = %better_node,
"Failed to send request to network"
);
return Err(emit_fail(ctx, e).await);
} else {
debug!(
msg_type = "Response",
node = %info.receiver,
subject_id = %self.subject_id,
"Request sent to better node"
);
}
}
if let UpdateType::Request { id, subject_id } =
&self.update_type
{
let request_path = ActorPath::from(format!(
"/user/request/{}",
subject_id
));
match ctx
.system()
.get_actor::<RequestManager>(&request_path)
.await
{
Ok(request_actor) => {
let request = if self.better.is_none() {
RequestManagerMessage::FinishReboot {
request_id: id.clone(),
}
} else {
RequestManagerMessage::RebootWait {
request_id: id.clone(),
governance_id: self
.subject_id
.clone(),
}
};
if let Err(e) =
request_actor.tell(request).await
{
error!(
msg_type = "Response",
error = %e,
subject_id = %self.subject_id,
"Failed to send response to request actor"
);
return Err(emit_fail(ctx, e).await);
}
}
Err(e) => {
error!(
msg_type = "Response",
path = %request_path,
subject_id = %self.subject_id,
"Request actor not found"
);
return Err(emit_fail(ctx, e).await);
}
};
};
debug!(
msg_type = "Response",
subject_id = %self.subject_id,
has_better = self.better.is_some(),
"All witnesses responded, update complete"
);
ctx.stop(None).await;
}
}
}
};
Ok(())
}
async fn on_child_fault(
&mut self,
error: ActorError,
ctx: &mut ActorContext<Self>,
) -> ChildAction {
error!(
subject_id = %self.subject_id,
update_type = ?self.update_type,
error = %error,
"Child fault in update actor"
);
emit_fail(ctx, error).await;
ChildAction::Stop
}
}