ave-core 0.11.0

Averiun Ledger core runtime and node API
Documentation
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::sync::Arc;

use async_trait::async_trait;
use ave_actors::{
    Actor, ActorContext, ActorError, ActorPath, ChildAction, Handler, Message,
    NotPersistentActor,
};
use ave_common::{
    identity::{DigestIdentifier, PublicKey},
    request::EventRequest,
    schematype::ReservedWords,
};
use serde::{Deserialize, Serialize};
use tracing::{Span, debug, error, info_span, warn};

use crate::{
    distribution::{Distribution, DistributionMessage, DistributionType},
    governance::{
        data::GovernanceData,
        model::{RoleTypes, WitnessesData},
    },
    helpers::network::service::NetworkSender,
    model::common::{
        emit_fail,
        node::i_can_send_last_ledger,
        subject::{acquire_subject, get_gov, get_last_ledger_event},
    },
    request::types::{DistributionPlanEntry, DistributionPlanMode},
};

pub struct ManualDistribution {
    our_key: Arc<PublicKey>,
}

impl ManualDistribution {
    pub const fn new(our_key: Arc<PublicKey>) -> Self {
        Self { our_key }
    }

    fn tracker_fact_mode_for_creator(
        governance_data: &GovernanceData,
        schema_id: &ave_common::SchemaType,
        namespace: &ave_common::Namespace,
        creator: &PublicKey,
        witness: &PublicKey,
        viewpoints: &std::collections::BTreeSet<String>,
    ) -> DistributionPlanMode {
        let Some(witness_name) = governance_data
            .members
            .iter()
            .find(|(_, key)| *key == witness)
            .map(|(name, _)| name.clone())
        else {
            return DistributionPlanMode::Opaque;
        };

        let Some(creator_name) = governance_data
            .members
            .iter()
            .find(|(_, key)| *key == creator)
            .map(|(name, _)| name.clone())
        else {
            return DistributionPlanMode::Opaque;
        };

        let Some(roles_schema) = governance_data.roles_schema.get(schema_id)
        else {
            return DistributionPlanMode::Opaque;
        };

        let Some(role_creator) = roles_schema.creator.get(
            &ave_common::governance::RoleCreator::create(
                &creator_name,
                namespace.clone(),
            ),
        ) else {
            return DistributionPlanMode::Opaque;
        };

        let is_generic_witness =
            roles_schema.hash_this_rol(
                RoleTypes::Witness,
                namespace.clone(),
                &witness_name,
            ) || governance_data.roles_tracker_schemas.hash_this_rol(
                RoleTypes::Witness,
                namespace.clone(),
                &witness_name,
            );

        let allows_clear =
            role_creator.witnesses.iter().any(|creator_witness| {
                let applies = creator_witness.name == witness_name
                    || (creator_witness.name
                        == ReservedWords::Witnesses.to_string()
                        && is_generic_witness);

                if !applies {
                    return false;
                }

                creator_witness
                    .viewpoints
                    .contains(&ReservedWords::AllViewpoints.to_string())
                    || viewpoints.is_empty()
                    || viewpoints.is_subset(&creator_witness.viewpoints)
            });

        if allows_clear {
            DistributionPlanMode::Clear
        } else {
            DistributionPlanMode::Opaque
        }
    }

    fn build_tracker_manual_plan(
        governance_data: &GovernanceData,
        schema_id: ave_common::SchemaType,
        namespace: ave_common::Namespace,
        event_request: &EventRequest,
        signer: &PublicKey,
    ) -> Result<Vec<DistributionPlanEntry>, ActorError> {
        let witnesses = governance_data
            .get_witnesses(WitnessesData::Schema {
                creator: signer.clone(),
                schema_id: schema_id.clone(),
                namespace: namespace.clone(),
            })
            .map_err(|e| ActorError::Functional {
                description: e.to_string(),
            })?;

        Ok(witnesses
            .into_iter()
            .map(|node| {
                let mode = match event_request {
                    EventRequest::Fact(fact_request) => {
                        Self::tracker_fact_mode_for_creator(
                            governance_data,
                            &schema_id,
                            &namespace,
                            signer,
                            &node,
                            &fact_request.viewpoints,
                        )
                    }
                    _ => DistributionPlanMode::Clear,
                };

                DistributionPlanEntry { node, mode }
            })
            .collect())
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ManualDistributionMessage {
    Update(DigestIdentifier),
}

impl Message for ManualDistributionMessage {}

impl NotPersistentActor for ManualDistribution {}

#[async_trait]
impl Actor for ManualDistribution {
    type Message = ManualDistributionMessage;
    type Event = ();
    type Response = ();

    fn get_span(_id: &str, parent_span: Option<Span>) -> tracing::Span {
        parent_span.map_or_else(
            || info_span!("ManualDistribution"),
            |parent_span| info_span!(parent: parent_span, "ManualDistribution"),
        )
    }
}

#[async_trait]
impl Handler<Self> for ManualDistribution {
    async fn handle_message(
        &mut self,
        _sender: ActorPath,
        msg: ManualDistributionMessage,
        ctx: &mut ave_actors::ActorContext<Self>,
    ) -> Result<(), ActorError> {
        match msg {
            ManualDistributionMessage::Update(subject_id) => {
                let data = i_can_send_last_ledger(ctx, &subject_id)
                    .await
                    .map_err(|e| {
                        error!(
                            msg_type = "Update",
                            subject_id = %subject_id,
                            error = %e,
                            "Failed to check if we can send last ledger"
                        );
                        e
                    })?;

                let Some(data) = data else {
                    warn!(
                        msg_type = "Update",
                        subject_id = %subject_id,
                        "Not the owner of the subject nor rejected transfer"
                    );
                    return Err(ActorError::Functional {
                        description: "Not the owner of the subject, nor have I refused the transfer".to_owned(),
                    });
                };

                let is_gov = data.get_schema_id().is_gov();

                let ledger = if is_gov {
                    get_last_ledger_event(ctx, &subject_id).await
                } else {
                    let lease = acquire_subject(
                        ctx,
                        &subject_id,
                        format!("manual_distribution:{}", subject_id),
                        None,
                        true,
                    )
                    .await?;

                    let ledger = get_last_ledger_event(ctx, &subject_id).await;
                    lease.finish(ctx).await?;
                    ledger
                };

                let ledger = ledger.map_err(|e| {
                    error!(
                        msg_type = "Update",
                        subject_id = %subject_id,
                        error = %e,
                        "Failed to get last ledger event"
                    );
                    e
                })?;

                let Some(ledger) = ledger else {
                    error!(
                        msg_type = "Update",
                        subject_id = %subject_id,
                        "No ledger event found for subject"
                    );
                    return Err(ActorError::Functional {
                        description: "Cannot obtain last ledger event"
                            .to_string(),
                    });
                };

                let governance_id =
                    data.get_governance_id().as_ref().map_or_else(
                        || subject_id.clone(),
                        |governance_id| governance_id.clone(),
                    );

                let schema_id = data.get_schema_id();
                let recipients = if is_gov {
                    let gov =
                        get_gov(ctx, &governance_id).await.map_err(|e| {
                            error!(
                                msg_type = "Update",
                                subject_id = %subject_id,
                                governance_id = %governance_id,
                                error = %e,
                                "Failed to get governance"
                            );
                            e
                        })?;

                    let mut witnesses =
                        gov.get_witnesses(WitnessesData::Gov).map_err(|e| {
                            error!(
                                msg_type = "Update",
                                subject_id = %subject_id,
                                is_gov = is_gov,
                                error = %e,
                                "Failed to get witnesses from governance"
                            );
                            ActorError::Functional {
                                description: e.to_string(),
                            }
                        })?;
                    witnesses.remove(&*self.our_key);
                    witnesses
                        .into_iter()
                        .map(|node| DistributionPlanEntry {
                            node,
                            mode: DistributionPlanMode::Clear,
                        })
                        .collect::<Vec<_>>()
                } else {
                    let gov =
                        get_gov(ctx, &governance_id).await.map_err(|e| {
                            error!(
                                msg_type = "Update",
                                subject_id = %subject_id,
                                governance_id = %governance_id,
                                error = %e,
                                "Failed to get governance"
                            );
                            e
                        })?;

                    let Some(event_request) = ledger.get_event_request() else {
                        return Err(ActorError::Functional {
                            description:
                                "Missing event request in tracker ledger"
                                    .to_owned(),
                        });
                    };

                    Self::build_tracker_manual_plan(
                        &gov,
                        schema_id.clone(),
                        ave_common::Namespace::from(data.get_namespace()),
                        &event_request,
                        &ledger.ledger_seal_signature.signer,
                    )?
                    .into_iter()
                    .filter(|entry| entry.node != *self.our_key)
                    .collect::<Vec<_>>()
                };

                if recipients.is_empty() {
                    warn!(
                        msg_type = "Update",
                        subject_id = %subject_id,
                        "No witnesses available for manual distribution"
                    );
                    return Err(ActorError::Functional {
                        description: "No witnesses available to manually send the last ledger event".to_string()
                    });
                }

                let witnesses_count = recipients.len();

                let Some(network) = ctx
                    .system()
                    .get_helper::<Arc<NetworkSender>>("network")
                    .await
                else {
                    error!(
                        msg_type = "Update",
                        subject_id = %subject_id,
                        "Network helper not found"
                    );
                    return Err(ActorError::Helper {
                        name: "network".to_owned(),
                        reason: "Not found".to_owned(),
                    });
                };

                let distribution = Distribution::new(
                    network,
                    DistributionType::Manual,
                    DigestIdentifier::default(),
                );

                let distribution_actor = ctx.create_child(&subject_id.to_string(), distribution).await.map_err(|e| {
                    warn!(
                        msg_type = "Update",
                        subject_id = %subject_id,
                        error = %e,
                        "Manual distribution already in progress"
                    );
                    ActorError::Functional {
                        description: "Manual distribution already in progress for this subject".to_owned()
                    }
                })?;

                if let Err(e) = distribution_actor
                    .tell(DistributionMessage::Create {
                        distribution_plan: recipients,
                        ledger: Box::new(ledger),
                    })
                    .await
                {
                    error!(
                        msg_type = "Update",
                        subject_id = %subject_id,
                        witnesses_count = witnesses_count,
                        error = %e,
                        "Failed to start manual distribution"
                    );
                    return Err(ActorError::Functional {
                        description: format!(
                            "Failed to start manual distribution: {}",
                            e
                        ),
                    });
                };

                debug!(
                    msg_type = "Update",
                    subject_id = %subject_id,
                    witnesses_count = witnesses_count,
                    is_gov = is_gov,
                    "Manual distribution started successfully"
                );

                Ok(())
            }
        }
    }

    async fn on_child_fault(
        &mut self,
        error: ActorError,
        ctx: &mut ActorContext<Self>,
    ) -> ChildAction {
        error!(
            error = %error,
            "Child actor fault in manual distribution"
        );
        emit_fail(ctx, error).await;
        ChildAction::Stop
    }
}