Skip to main content

ave_core/model/
network.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use ave_actors::{
5    Actor, ActorContext, ActorError, ActorPath, Handler, NotPersistentActor,
6};
7use ave_common::identity::{PublicKey, TimeStamp};
8use borsh::{BorshDeserialize, BorshSerialize};
9use serde::{Deserialize, Serialize};
10use tracing::{Span, error, info_span};
11
12use crate::{NetworkMessage, helpers::network::service::NetworkSender};
13
14use super::common::emit_fail;
15
16#[derive(
17    Debug,
18    Clone,
19    Serialize,
20    Deserialize,
21    PartialEq,
22    Eq,
23    Hash,
24    BorshSerialize,
25    BorshDeserialize,
26    Ord,
27    PartialOrd,
28)]
29pub struct TimeOut {
30    pub who: PublicKey,
31    pub re_trys: u32,
32    pub timestamp: TimeStamp,
33}
34
35#[derive(Clone, Debug)]
36pub struct RetryNetwork {
37    network: Arc<NetworkSender>,
38}
39
40impl RetryNetwork {
41    pub const fn new(network: Arc<NetworkSender>) -> Self {
42        Self { network }
43    }
44}
45
46#[async_trait]
47impl Actor for RetryNetwork {
48    type Event = ();
49    type Message = NetworkMessage;
50    type Response = ();
51
52    fn get_span(_id: &str, parent_span: Option<Span>) -> tracing::Span {
53        parent_span.map_or_else(
54            || info_span!("RetryNetwork"),
55            |parent_span| info_span!(parent: parent_span, "RetryNetwork"),
56        )
57    }
58}
59
60impl NotPersistentActor for RetryNetwork {}
61
62#[async_trait]
63impl Handler<Self> for RetryNetwork {
64    async fn handle_message(
65        &mut self,
66        _sender: ActorPath,
67        msg: NetworkMessage,
68        ctx: &mut ActorContext<Self>,
69    ) -> Result<(), ActorError> {
70        if let Err(e) = self
71            .network
72            .send_command(network::CommandHelper::SendMessage { message: msg })
73            .await
74        {
75            error!(
76                error = %e,
77                "Failed to send message to network helper"
78            );
79            return Err(emit_fail(ctx, e).await);
80        };
81        Ok(())
82    }
83}