ave-core 0.10.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use std::{collections::{BTreeMap, BTreeSet}, sync::Arc};

use crate::{
    evaluation::{
        compiler::{CompilerResponse, error::CompilerError},
        request::{EvalWorkerContext, EvaluateData},
        response::{
            EvalRunnerError, EvaluationResult, EvaluatorError,
            EvaluatorResponse as EvalRes,
        },
        runner::types::EvaluateInfo,
    },
    governance::{data::GovernanceData, model::Schema},
    helpers::network::{NetworkMessage, service::NetworkSender},
    model::common::{
        emit_fail,
        node::{SignTypesNode, get_sign},
    },
    subject::RequestSubjectData,
    system::ConfigHelper,
};

use crate::helpers::network::ActorMessage;

use async_trait::async_trait;
use ave_common::{
    SchemaType, ValueWrapper,
    identity::{
        DigestIdentifier, HashAlgorithm, PublicKey, Signed, hash_borsh,
    },
};

use json_patch::diff;
use ave_network::ComunicateInfo;

use ave_actors::{
    Actor, ActorContext, ActorError, ActorPath, ChildAction, Handler, Message,
    NotPersistentActor,
};

use serde_json::Value;
use tracing::{Span, debug, error, info_span, warn};

use super::{
    Evaluation, EvaluationMessage,
    compiler::{TempCompiler, TempCompilerMessage},
    request::EvaluationReq,
    response::EvaluationRes,
    runner::{Runner, RunnerMessage, RunnerResponse, types::RunnerResult},
};

/// A struct representing a EvalWorker actor.
#[derive(Clone, Debug)]
pub struct EvalWorker {
    pub node_key: PublicKey,
    pub our_key: Arc<PublicKey>,
    pub governance_id: DigestIdentifier,
    pub gov_version: u64,
    pub sn: u64,
    pub context: EvalWorkerContext,
    pub init_state: Option<ValueWrapper>,
    pub hash: HashAlgorithm,
    pub network: Arc<NetworkSender>,
    pub stop: bool,
}

impl EvalWorker {
    async fn execute_contract(
        &self,
        ctx: &mut ActorContext<Self>,
        runner_data: EvaluateInfo,
        is_owner: bool,
    ) -> Result<RunnerResponse, ActorError> {
        let runner_actor = ctx.create_child("runner", Runner).await?;

        let response = runner_actor
            .ask(RunnerMessage {
                data: runner_data,
                is_owner,
            })
            .await;

        runner_actor.ask_stop().await?;

        response
    }

    async fn compile_contracts(
        &self,
        ctx: &mut ActorContext<Self>,
        ids: &[SchemaType],
        schemas: BTreeMap<SchemaType, Schema>,
    ) -> Result<Option<CompilerError>, ActorError> {
        let contracts_path = if let Some(config) =
            ctx.system().get_helper::<ConfigHelper>("config").await
        {
            config.contracts_path
        } else {
            return Err(ActorError::Helper {
                name: "config".to_owned(),
                reason: "Not found".to_owned(),
            });
        };

        let compiler = ctx
            .create_child("temp_compiler", TempCompiler::new(self.hash))
            .await?;

        for id in ids {
            let Some(schema) = schemas.get(id) else {
                return Err(ActorError::Functional { description: "There is a contract that requires compilation but its scheme could not be found".to_owned()});
            };

            let response = compiler
                .ask(TempCompilerMessage::Compile {
                    contract_name: format!("{}_{}", self.governance_id, id),
                    contract: schema.contract.clone(),
                    initial_value: schema.initial_value.0.clone(),
                    contract_path: contracts_path
                        .join("contracts")
                        .join(format!("{}_temp_{}", self.governance_id, id)),
                })
                .await?;

            if let CompilerResponse::Error(e) = response {
                compiler.ask_stop().await?;
                return Ok(Some(e));
            }
        }

        compiler.ask_stop().await?;
        Ok(None)
    }

    async fn check_governance(
        &self,
        gov_version: u64,
    ) -> Result<bool, ActorError> {
        match self.gov_version.cmp(&gov_version) {
            std::cmp::Ordering::Less => {
                warn!(
                    local_gov_version = self.gov_version,
                    request_gov_version = gov_version,
                    governance_id = %self.governance_id,
                    sender = %self.node_key,
                    "Received request with a higher governance version; ignoring request"
                );
                Err(ActorError::Functional {
                    description:
                        "Abort evaluation, request governance version is higher than local"
                            .to_owned(),
                })
            }
            std::cmp::Ordering::Equal => {
                // If it is the same it means that we have the latest version of governance, we are up to date.
                Ok(false)
            }
            std::cmp::Ordering::Greater => Ok(true),
        }
    }

    async fn evaluate(
        &self,
        ctx: &mut ActorContext<Self>,
        evaluation_req: &EvaluationReq,
    ) -> Result<RunnerResult, EvaluatorError> {
        let runner_data = evaluation_req
            .build_evaluate_info(&self.init_state, &self.context)?;

        // Mirar la parte final de execute contract.
        let response = self
            .execute_contract(ctx, runner_data, evaluation_req.signer_is_owner)
            .await
            .map_err(|e| EvaluatorError::InternalError(e.to_string()))?;

        let (result, compilations) = match response {
            RunnerResponse::Ok {
                result,
                compilations,
            } => (result, compilations),
            RunnerResponse::Error(runner_error) => {
                return Err(EvaluatorError::from(runner_error));
            }
        };

        if self.init_state.is_none() && !compilations.is_empty() {
            let governance_data = GovernanceData::try_from(
                result.final_state.clone(),
            )
            .map_err(|e| {
                let e = format!(
                    "can not convert GovernanceData from properties: {}",
                    e
                );
                EvaluatorError::InternalError(e)
            })?;

            if let Some(error) = self
                .compile_contracts(ctx, &compilations, governance_data.schemas)
                .await
                .map_err(|e| EvaluatorError::InternalError(e.to_string()))?
            {
                return Err(EvaluatorError::from(error));
            };
        }

        Ok(result)
    }

    fn generate_json_patch(
        prev_state: &Value,
        new_state: &Value,
    ) -> Result<Value, EvaluatorError> {
        let patch = diff(prev_state, new_state);
        serde_json::to_value(patch).map_err(|e| {
            EvaluatorError::InternalError(format!(
                "Can not generate json patch {}",
                e
            ))
        })
    }

    fn build_request_hashes(
        &self,
        evaluation_req: &Signed<EvaluationReq>,
    ) -> Result<(DigestIdentifier, DigestIdentifier), EvaluatorError> {
        let eval_req_hash = hash_borsh(&*self.hash.hasher(), evaluation_req)
            .map_err(|e| EvaluatorError::InternalError(e.to_string()))?;

        let EvaluationReq {
            event_request,
            governance_id,
            sn,
            gov_version,
            namespace,
            schema_id,
            signer,
            ..
        } = evaluation_req.content().clone();

        let req_subject_data_hash = hash_borsh(
            &*self.hash.hasher(),
            &RequestSubjectData {
                namespace,
                schema_id,
                subject_id: event_request.content().get_subject_id(),
                governance_id,
                sn,
                gov_version,
                signer,
            },
        )
        .map_err(|e| EvaluatorError::InternalError(e.to_string()))?;

        Ok((eval_req_hash, req_subject_data_hash))
    }

    async fn sign_result(
        &self,
        ctx: &mut ActorContext<Self>,
        result: EvaluationResult,
    ) -> Result<EvaluationRes, EvaluatorError> {
        let result_hash = hash_borsh(&*self.hash.hasher(), &result)
            .map_err(|e| EvaluatorError::InternalError(e.to_string()))?;

        let result_hash_signature = get_sign(
            ctx,
            SignTypesNode::EvaluationSignature(result_hash.clone()),
        )
        .await
        .map_err(|e| EvaluatorError::InternalError(e.to_string()))?;

        Ok(EvaluationRes::Response {
            result,
            result_hash,
            result_hash_signature,
        })
    }

    async fn build_response(
        &self,
        ctx: &mut ActorContext<Self>,
        evaluation: RunnerResult,
        evaluation_req: Signed<EvaluationReq>,
    ) -> Result<EvaluationRes, EvaluatorError> {
        let (eval_req_hash, req_subject_data_hash) =
            self.build_request_hashes(&evaluation_req)?;

        let (patch, properties_hash) = match &evaluation_req.content().data {
            EvaluateData::GovFact { state, .. } => {
                let properties_hash =
                    hash_borsh(&*self.hash.hasher(), &evaluation.final_state)
                        .map_err(|e| {
                        EvaluatorError::InternalError(e.to_string())
                    })?;

                let state = state.to_value_wrapper();
                let patch = Self::generate_json_patch(
                    &state.0,
                    &evaluation.final_state.0,
                )?;

                (ValueWrapper(patch), properties_hash)
            }
            EvaluateData::TrackerSchemasFact { state, .. } => {
                let properties_hash =
                    hash_borsh(&*self.hash.hasher(), &evaluation.final_state)
                        .map_err(|e| {
                        EvaluatorError::InternalError(e.to_string())
                    })?;

                let patch = Self::generate_json_patch(
                    &state.0,
                    &evaluation.final_state.0,
                )?;

                (ValueWrapper(patch), properties_hash)
            }
            EvaluateData::GovTransfer { state } => {
                let state = state.to_value_wrapper();
                let properties_hash = hash_borsh(&*self.hash.hasher(), &state)
                    .map_err(|e| {
                        EvaluatorError::InternalError(e.to_string())
                    })?;

                (evaluation.final_state, properties_hash)
            }
            EvaluateData::GovConfirm { state } => {
                let properties_hash =
                    hash_borsh(&*self.hash.hasher(), &evaluation.final_state)
                        .map_err(|e| {
                        EvaluatorError::InternalError(e.to_string())
                    })?;

                let state = state.to_value_wrapper();
                let patch = Self::generate_json_patch(
                    &state.0,
                    &evaluation.final_state.0,
                )?;

                (ValueWrapper(patch), properties_hash)
            }
            EvaluateData::TrackerSchemasTransfer { state, .. } => {
                let properties_hash = hash_borsh(&*self.hash.hasher(), &state)
                    .map_err(|e| {
                        EvaluatorError::InternalError(e.to_string())
                    })?;

                (evaluation.final_state, properties_hash)
            }
        };

        let result = EvaluationResult::Ok {
            response: EvalRes {
                patch,
                properties_hash,
                appr_required: evaluation.approval_required,
            },
            eval_req_hash,
            req_subject_data_hash,
        };

        self.sign_result(ctx, result).await
    }

    async fn build_response_error(
        &self,
        ctx: &mut ActorContext<Self>,
        evaluator_error: EvaluatorError,
        evaluation_req: Signed<EvaluationReq>,
    ) -> Result<EvaluationRes, EvaluatorError> {
        match &evaluator_error {
            EvaluatorError::InvalidEventSignature
            | EvaluatorError::InvalidEventRequest(..) => {
                return Ok(EvaluationRes::Abort(evaluator_error.to_string()));
            }
            EvaluatorError::Runner(EvalRunnerError::ContractNotFound(..)) => {
                return Ok(EvaluationRes::Reboot);
            }
            _ => {}
        };

        let (eval_req_hash, req_subject_data_hash) =
            self.build_request_hashes(&evaluation_req)?;

        let result = EvaluationResult::Error {
            error: evaluator_error,
            eval_req_hash,
            req_subject_data_hash,
        };

        self.sign_result(ctx, result).await
    }

    async fn create_res(
        &self,
        ctx: &mut ActorContext<Self>,
        reboot: bool,
        evaluation_req: &Signed<EvaluationReq>,
    ) -> Result<EvaluationRes, EvaluatorError> {
        let evaluation = if reboot {
            EvaluationRes::Reboot
        } else {
            match self.evaluate(ctx, evaluation_req.content()).await {
                Ok(evaluation) => {
                    self.build_response(ctx, evaluation, evaluation_req.clone())
                        .await?
                }
                Err(error) => {
                    if let EvaluatorError::InternalError(..) = &error {
                        return Err(error);
                    } else {
                        self.build_response_error(
                            ctx,
                            error,
                            evaluation_req.clone(),
                        )
                        .await?
                    }
                }
            }
        };

        Ok(evaluation)
    }

    fn check_data(
        &self,
        evaluation_req: &Signed<EvaluationReq>,
    ) -> Result<(), EvaluatorError> {
        let event_is_for_gov = evaluation_req.content().data.is_gov_event();
        match (self.init_state.is_none(), event_is_for_gov) {
            (true, false) => return Err(EvaluatorError::InvalidEventRequest(
                "Evaluator is for governance but eval request is for tracker"
                    .to_owned(),
            )),
            (false, true) => return Err(EvaluatorError::InvalidEventRequest(
                "Evaluator is for tracker but eval request is for governance"
                    .to_owned(),
            )),
            _ => {}
        };

        if evaluation_req.content().governance_id != self.governance_id {
            return Err(EvaluatorError::InvalidEventRequest(format!(
                "Evaluator governance_id {} and eval request governance_id {} are different",
                self.governance_id,
                evaluation_req.content().governance_id
            )));
        }

        if evaluation_req.verify().is_err() {
            return Err(EvaluatorError::InvalidEventSignature);
        }

        if evaluation_req.content().event_request.verify().is_err() {
            return Err(EvaluatorError::InvalidEventSignature);
        }

        if self.gov_version == evaluation_req.content().gov_version {
            let signer = evaluation_req
                .content()
                .event_request
                .signature()
                .signer
                .clone();

            if evaluation_req
                .content()
                .event_request
                .content()
                .is_fact_event()
            {
                let Some((issuers, issuer_any)) = self.context.issuers()
                else {
                    return Err(EvaluatorError::InvalidEventRequest(
                        "Fact event evaluation context does not include issuers"
                            .to_owned(),
                    ));
                };

                if !issuer_any && !issuers.contains(&signer) {
                    return Err(EvaluatorError::InvalidEventRequest(
                        "In fact events, the signer has to be an issuer"
                            .to_owned(),
                    ));
                }
            } else if signer != self.node_key {
                return Err(EvaluatorError::InvalidEventRequest(
                    "In non-fact events, the signer has to be the sender"
                        .to_owned(),
                ));
            }
        }

        Ok(())
    }
}

#[derive(Debug, Clone)]
pub enum EvalWorkerMessage {
    Update {
        gov_version: u64,
        issuers: BTreeSet<PublicKey>,
        issuer_any: bool,
    },
    LocalEvaluation {
        evaluation_req: Signed<EvaluationReq>,
    },
    NetworkRequest {
        evaluation_req: Signed<EvaluationReq>,
        sender: PublicKey,
        info: ComunicateInfo,
    },
}

impl Message for EvalWorkerMessage {}

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

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

impl NotPersistentActor for EvalWorker {}

#[async_trait]
impl Handler<Self> for EvalWorker {
    async fn handle_message(
        &mut self,
        _sender: ActorPath,
        msg: EvalWorkerMessage,
        ctx: &mut ActorContext<Self>,
    ) -> Result<(), ActorError> {
        match msg {
            EvalWorkerMessage::Update {
                gov_version,
                issuers,
                issuer_any,
            } => {
                self.gov_version = gov_version;
                self.context = EvalWorkerContext::Governance {
                    issuers,
                    issuer_any,
                };
            }
            EvalWorkerMessage::LocalEvaluation { evaluation_req } => {
                let evaluation =
                    match self.create_res(ctx, false, &evaluation_req).await {
                        Ok(eval) => eval,
                        Err(e) => {
                            error!(
                                msg_type = "LocalEvaluation",
                                error = %e,
                                "Failed to create evaluation response"
                            );
                            return Err(emit_fail(
                                ctx,
                                ActorError::FunctionalCritical {
                                    description: e.to_string(),
                                },
                            )
                            .await);
                        }
                    };

                match ctx.get_parent::<Evaluation>().await {
                    Ok(evaluation_actor) => {
                        if let Err(e) = evaluation_actor
                            .tell(EvaluationMessage::Response {
                                evaluation_res: evaluation.clone(),
                                sender: (*self.our_key).clone(),
                            })
                            .await
                        {
                            error!(
                                msg_type = "LocalEvaluation",
                                error = %e,
                                "Failed to send response to evaluation actor"
                            );
                            return Err(emit_fail(ctx, e).await);
                        }

                        debug!(
                            msg_type = "LocalEvaluation",
                            "Local evaluation completed successfully"
                        );
                    }
                    Err(e) => {
                        error!(
                            msg_type = "LocalEvaluation",
                            path = %ctx.path().parent(),
                            "Evaluation actor not found"
                        );
                        return Err(e);
                    }
                }

                ctx.stop(None).await;
            }
            EvalWorkerMessage::NetworkRequest {
                evaluation_req,
                info,
                sender,
            } => {
                if sender != evaluation_req.signature().signer
                    || sender != self.node_key
                {
                    warn!(
                        msg_type = "NetworkRequest",
                        expected_sender = %self.node_key,
                        received_sender = %sender,
                        signer = %evaluation_req.signature().signer,
                        "Unexpected sender"
                    );
                    if self.stop {
                        ctx.stop(None).await;
                    }

                    return Ok(());
                }

                let reboot = match self
                    .check_governance(evaluation_req.content().gov_version)
                    .await
                {
                    Ok(reboot) => reboot,
                    Err(e) => {
                        warn!(
                            msg_type = "NetworkRequest",
                            error = %e,
                            "Failed to check governance"
                        );
                        if let ActorError::Functional { .. } = e {
                            if self.stop {
                                ctx.stop(None).await;
                            }
                            return Err(e);
                        } else {
                            return Err(emit_fail(ctx, e).await);
                        }
                    }
                };

                let evaluation = if let Err(error) =
                    self.check_data(&evaluation_req)
                {
                    match self
                        .build_response_error(
                            ctx,
                            error,
                            evaluation_req.clone(),
                        )
                        .await
                    {
                        Ok(eval) => eval,
                        Err(e) => {
                            error!(
                                msg_type = "NetworkRequest",
                                error = %e,
                                "Failed to build error response"
                            );
                            return Err(emit_fail(
                                ctx,
                                ActorError::FunctionalCritical {
                                    description: e.to_string(),
                                },
                            )
                            .await);
                        }
                    }
                } else {
                    match self.create_res(ctx, reboot, &evaluation_req).await {
                        Ok(eval) => eval,
                        Err(e) => {
                            error!(
                                msg_type = "NetworkRequest",
                                error = %e,
                                "Internal error during evaluation"
                            );
                            return Err(emit_fail(
                                ctx,
                                ActorError::FunctionalCritical {
                                    description: e.to_string(),
                                },
                            )
                            .await);
                        }
                    }
                };

                let new_info = ComunicateInfo {
                    receiver: sender.clone(),
                    request_id: info.request_id.clone(),
                    version: info.version,
                    receiver_actor: format!(
                        "/user/request/{}/evaluation/{}",
                        evaluation_req
                            .content()
                            .event_request
                            .content()
                            .get_subject_id(),
                        self.our_key.clone()
                    ),
                };

                if let Err(e) = self
                    .network
                    .send_command(ave_network::CommandHelper::SendMessage {
                        message: NetworkMessage {
                            info: new_info,
                            message: ActorMessage::EvaluationRes {
                                res: evaluation,
                            },
                        },
                    })
                    .await
                {
                    error!(
                        msg_type = "NetworkRequest",
                        error = %e,
                        "Failed to send response to network"
                    );
                    return Err(emit_fail(ctx, e).await);
                };

                debug!(
                    msg_type = "NetworkRequest",
                    request_id = %info.request_id,
                    version = info.version,
                    sender = %sender,
                    "Network evaluation request processed successfully"
                );

                if self.stop {
                    ctx.stop(None).await;
                }
            }
        }

        Ok(())
    }

    async fn on_child_fault(
        &mut self,
        error: ActorError,
        ctx: &mut ActorContext<Self>,
    ) -> ChildAction {
        error!(
            governance_id = %self.governance_id,
            gov_version = self.gov_version,
            sn = self.sn,
            node_key = %self.node_key,
            error = %error,
            "Child fault in evaluation worker"
        );
        emit_fail(ctx, error).await;
        ChildAction::Stop
    }
}