p2p/network/noise/
p2p_network_noise_actions.rs

1use super::p2p_network_noise_state::Sk;
2use crate::{ConnectionAddr, Data, P2pNetworkAction, P2pState, PeerId};
3use openmina_core::ActionEvent;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Debug, Clone, ActionEvent)]
7#[action_event(level = debug, fields(display(addr), incoming, debug(data), display(peer_id)))]
8pub enum P2pNetworkNoiseAction {
9    Init {
10        addr: ConnectionAddr,
11        incoming: bool,
12        ephemeral_sk: Sk,
13        static_sk: Sk,
14        signature: Data,
15    },
16    /// remote peer sends the data to the noise
17    IncomingData {
18        addr: ConnectionAddr,
19        data: Data,
20    },
21    IncomingChunk {
22        addr: ConnectionAddr,
23    },
24    OutgoingChunk {
25        addr: ConnectionAddr,
26        data: Vec<Data>,
27    },
28    OutgoingChunkSelectMux {
29        addr: ConnectionAddr,
30        data: Vec<Data>,
31    },
32    // internals sends the data to the remote peer thru noise
33    OutgoingData {
34        addr: ConnectionAddr,
35        data: Data,
36    },
37    OutgoingDataSelectMux {
38        addr: ConnectionAddr,
39        data: Data,
40    },
41    // the remote peer sends the data to internals thru noise
42    #[action_event(fields(display(addr), debug(data), debug(peer_id)))]
43    DecryptedData {
44        addr: ConnectionAddr,
45        peer_id: Option<PeerId>,
46        data: Data,
47    },
48    HandshakeDone {
49        addr: ConnectionAddr,
50        peer_id: PeerId,
51        incoming: bool,
52    },
53}
54
55impl P2pNetworkNoiseAction {
56    pub fn addr(&self) -> &ConnectionAddr {
57        match self {
58            Self::Init { addr, .. } => addr,
59            Self::IncomingData { addr, .. } => addr,
60            Self::IncomingChunk { addr, .. } => addr,
61            Self::OutgoingChunk { addr, .. } => addr,
62            Self::OutgoingChunkSelectMux { addr, .. } => addr,
63            Self::OutgoingData { addr, .. } => addr,
64            Self::OutgoingDataSelectMux { addr, .. } => addr,
65            Self::DecryptedData { addr, .. } => addr,
66            Self::HandshakeDone { addr, .. } => addr,
67        }
68    }
69}
70
71impl From<P2pNetworkNoiseAction> for crate::P2pAction {
72    fn from(a: P2pNetworkNoiseAction) -> Self {
73        Self::Network(P2pNetworkAction::Noise(a))
74    }
75}
76
77impl redux::EnablingCondition<P2pState> for P2pNetworkNoiseAction {
78    fn is_enabled(&self, state: &P2pState, _time: redux::Timestamp) -> bool {
79        if state
80            .network
81            .scheduler
82            .connection_state(self.addr())
83            .and_then(|state| state.noise_state())
84            .is_none()
85        {
86            return false;
87        };
88
89        match self {
90            Self::Init { .. } => true,
91            Self::IncomingData { .. } => true,
92            Self::IncomingChunk { .. } => true,
93            Self::OutgoingChunk { .. } => true,
94            Self::OutgoingChunkSelectMux { .. } => true,
95            Self::OutgoingData { .. } => true,
96            Self::OutgoingDataSelectMux { .. } => true,
97            Self::DecryptedData { .. } => true,
98            Self::HandshakeDone { .. } => true,
99        }
100    }
101}