Skip to main content

ave_core/model/common/
node.rs

1use std::sync::Arc;
2
3use ave_actors::{Actor, ActorContext, ActorError, ActorPath, Handler};
4
5use ave_common::identity::{DigestIdentifier, PublicKey, Signature};
6use network::ComunicateInfo;
7
8use crate::{
9    ActorMessage, NetworkMessage, Node, NodeMessage, NodeResponse,
10    auth::{Auth, AuthMessage},
11    helpers::network::service::NetworkSender,
12    model::event::Ledger,
13    node::SubjectData,
14};
15
16use ave_common::request::EventRequest;
17
18use crate::{
19    approval::{request::ApprovalReq, response::ApprovalRes},
20    evaluation::{request::EvaluationReq, response::EvaluationRes},
21    validation::{request::ValidationReq, response::ValidationRes},
22};
23
24use serde::{Deserialize, Serialize};
25
26#[derive(Clone, Debug, Serialize, Deserialize)]
27pub enum SignTypesNode {
28    ApprovalReq(ApprovalReq),
29    ApprovalRes(Box<ApprovalRes>),
30
31    EvaluationReq(EvaluationReq),
32    EvaluationRes(EvaluationRes),
33
34    ValidationReq(Box<ValidationReq>),
35    ValidationRes(ValidationRes),
36
37    EventRequest(EventRequest),
38    Ledger(Ledger),
39}
40
41pub async fn i_owner_new_owner<A>(
42    ctx: &mut ActorContext<A>,
43    subject_id: &DigestIdentifier,
44) -> Result<(bool, Option<bool>), ActorError>
45where
46    A: Actor + Handler<A>,
47{
48    let node_path = ActorPath::from("/user/node");
49    let node_actor = ctx.system().get_actor::<Node>(&node_path).await?;
50
51    let response = node_actor
52        .ask(NodeMessage::IOwnerNewOwnerSubject(subject_id.to_owned()))
53        .await?;
54
55    match response {
56        NodeResponse::IOwnerNewOwner {
57            i_owner,
58            i_new_owner,
59        } => Ok((i_owner, i_new_owner)),
60        _ => Err(ActorError::UnexpectedResponse {
61            path: node_path,
62            expected: "NodeResponse::IOwnerNewOwner".to_owned(),
63        }),
64    }
65}
66
67pub async fn i_can_send_last_ledger<A>(
68    ctx: &mut ActorContext<A>,
69    subject_id: &DigestIdentifier,
70) -> Result<Option<SubjectData>, ActorError>
71where
72    A: Actor + Handler<A>,
73{
74    let node_path = ActorPath::from("/user/node");
75    let node_actor = ctx.system().get_actor::<Node>(&node_path).await?;
76
77    let response = node_actor
78        .ask(NodeMessage::ICanSendLastLedger(subject_id.to_owned()))
79        .await?;
80
81    match response {
82        NodeResponse::SubjectData(data) => Ok(data),
83        _ => Err(ActorError::UnexpectedResponse {
84            path: node_path,
85            expected: "NodeResponse::SubjectData".to_owned(),
86        }),
87    }
88}
89
90pub async fn get_sign<A>(
91    ctx: &mut ActorContext<A>,
92    sign_type: SignTypesNode,
93) -> Result<Signature, ActorError>
94where
95    A: Actor + Handler<A>,
96{
97    let path = ActorPath::from("/user/node");
98    let node_actor = ctx.system().get_actor::<Node>(&path).await?;
99
100    // We obtain the validator
101    let node_response = node_actor
102        .ask(NodeMessage::SignRequest(Box::new(sign_type)))
103        .await?;
104
105    match node_response {
106        NodeResponse::SignRequest(signature) => Ok(signature),
107        _ => Err(ActorError::UnexpectedResponse {
108            path,
109            expected: "NodeResponse::SignRequest".to_owned(),
110        }),
111    }
112}
113
114pub async fn get_subject_data<A>(
115    ctx: &mut ActorContext<A>,
116    subject_id: &DigestIdentifier,
117) -> Result<Option<SubjectData>, ActorError>
118where
119    A: Actor + Handler<A>,
120{
121    let path = ActorPath::from("/user/node");
122    let node_actor = ctx.system().get_actor::<Node>(&path).await?;
123
124    let response = node_actor
125        .ask(NodeMessage::GetSubjectData(subject_id.to_owned()))
126        .await?;
127
128    match response {
129        NodeResponse::SubjectData(data) => Ok(data),
130        _ => Err(ActorError::UnexpectedResponse {
131            path,
132            expected: "NodeResponse::SubjectData".to_owned(),
133        }),
134    }
135}
136
137pub async fn try_to_update<A>(
138    ctx: &mut ActorContext<A>,
139    subject_id: DigestIdentifier,
140    objective: Option<PublicKey>,
141) -> Result<(), ActorError>
142where
143    A: Actor + Handler<A>,
144{
145    let auth_path = ActorPath::from("/user/node/auth");
146    let auth_actor = ctx.system().get_actor::<Auth>(&auth_path).await?;
147
148    auth_actor
149        .tell(AuthMessage::Update {
150            subject_id,
151            objective,
152        })
153        .await
154}
155
156pub struct UpdateData {
157    pub sn: u64,
158    pub gov_version: u64,
159    pub subject_id: DigestIdentifier,
160    pub other_node: PublicKey,
161}
162
163pub async fn update_ledger_network(
164    data: UpdateData,
165    network: Arc<NetworkSender>,
166) -> Result<(), ActorError> {
167    let subject_string = data.subject_id.to_string();
168    let request = ActorMessage::DistributionLedgerReq {
169        actual_sn: Some(data.sn),
170        subject_id: data.subject_id,
171    };
172
173    let info = ComunicateInfo {
174        receiver: data.other_node,
175        request_id: String::default(),
176        version: 0,
177        receiver_actor: format!("/user/node/distributor_{}", subject_string),
178    };
179
180    network
181        .send_command(network::CommandHelper::SendMessage {
182            message: NetworkMessage {
183                info,
184                message: request,
185            },
186        })
187        .await
188}