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 let node_response =
102 node_actor.ask(NodeMessage::SignRequest(sign_type)).await?;
103
104 match node_response {
105 NodeResponse::SignRequest(signature) => Ok(signature),
106 _ => Err(ActorError::UnexpectedResponse {
107 path,
108 expected: "NodeResponse::SignRequest".to_owned(),
109 }),
110 }
111}
112
113pub async fn get_subject_data<A>(
114 ctx: &mut ActorContext<A>,
115 subject_id: &DigestIdentifier,
116) -> Result<Option<SubjectData>, ActorError>
117where
118 A: Actor + Handler<A>,
119{
120 let path = ActorPath::from("/user/node");
121 let node_actor = ctx.system().get_actor::<Node>(&path).await?;
122
123 let response = node_actor
124 .ask(NodeMessage::GetSubjectData(subject_id.to_owned()))
125 .await?;
126
127 match response {
128 NodeResponse::SubjectData(data) => Ok(data),
129 _ => Err(ActorError::UnexpectedResponse {
130 path,
131 expected: "NodeResponse::SubjectData".to_owned(),
132 }),
133 }
134}
135
136pub async fn try_to_update<A>(
137 ctx: &mut ActorContext<A>,
138 subject_id: DigestIdentifier,
139 objective: Option<PublicKey>,
140) -> Result<(), ActorError>
141where
142 A: Actor + Handler<A>,
143{
144 let auth_path = ActorPath::from("/user/node/auth");
145 let auth_actor = ctx.system().get_actor::<Auth>(&auth_path).await?;
146
147 auth_actor
148 .tell(AuthMessage::Update {
149 subject_id,
150 objective,
151 })
152 .await
153}
154
155pub struct UpdateData {
156 pub sn: u64,
157 pub gov_version: u64,
158 pub subject_id: DigestIdentifier,
159 pub other_node: PublicKey,
160}
161
162pub async fn update_ledger_network(
163 data: UpdateData,
164 network: Arc<NetworkSender>,
165) -> Result<(), ActorError> {
166 let subject_string = data.subject_id.to_string();
167 let request = ActorMessage::DistributionLedgerReq {
168 actual_sn: Some(data.sn),
169 subject_id: data.subject_id,
170 };
171
172 let info = ComunicateInfo {
173 receiver: data.other_node,
174 request_id: String::default(),
175 version: 0,
176 receiver_actor: format!("/user/node/distributor_{}", subject_string),
177 };
178
179 network
180 .send_command(network::CommandHelper::SendMessage {
181 message: NetworkMessage {
182 info,
183 message: request,
184 },
185 })
186 .await
187}