1use std::sync::Arc;
2
3use ave_actors::{Actor, ActorContext, ActorError, ActorPath, Handler};
4
5use ave_common::identity::{DigestIdentifier, PublicKey, Signature};
6use ave_network::ComunicateInfo;
7
8use crate::{
9 ActorMessage, NetworkMessage, Node, NodeMessage, NodeResponse,
10 auth::{Auth, AuthMessage},
11 helpers::network::service::NetworkSender,
12 model::event::LedgerSeal,
13 node::SubjectData,
14};
15
16use ave_common::request::EventRequest;
17
18use crate::{
19 approval::{request::ApprovalReq, response::ApprovalRes},
20 evaluation::request::EvaluationReq,
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(Box<EvaluationReq>),
32 EvaluationSignature(DigestIdentifier),
33
34 ValidationReq(Box<ValidationReq>),
35 ValidationRes(ValidationRes),
36
37 EventRequest(EventRequest),
38 LedgerSeal(LedgerSeal),
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 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 strict: false,
153 })
154 .await
155}
156
157pub struct UpdateData {
158 pub sn: u64,
159 pub gov_version: u64,
160 pub subject_id: DigestIdentifier,
161 pub other_node: PublicKey,
162}
163
164pub async fn update_ledger_network(
165 data: UpdateData,
166 network: Arc<NetworkSender>,
167) -> Result<(), ActorError> {
168 let subject_string = data.subject_id.to_string();
169 let request = ActorMessage::DistributionLedgerReq {
170 actual_sn: Some(data.sn),
171 target_sn: None,
172 subject_id: data.subject_id,
173 };
174
175 let info = ComunicateInfo {
176 receiver: data.other_node,
177 request_id: String::default(),
178 version: 0,
179 receiver_actor: format!("/user/node/distributor_{}", subject_string),
180 };
181
182 network
183 .send_command(ave_network::CommandHelper::SendMessage {
184 message: NetworkMessage {
185 info,
186 message: request,
187 },
188 })
189 .await
190}