p2p/network/identify/stream/
p2p_network_identify_stream_actions.rs

1use crate::{ConnectionAddr, Data, P2pAction, P2pState, PeerId, StreamId};
2use multiaddr::Multiaddr;
3use openmina_core::ActionEvent;
4use redux::EnablingCondition;
5use serde::{Deserialize, Serialize};
6
7/// Identify stream related actions.
8#[derive(Debug, Clone, Serialize, Deserialize, ActionEvent)]
9pub enum P2pNetworkIdentifyStreamAction {
10    /// Creates a new stream state.
11    New {
12        addr: ConnectionAddr,
13        peer_id: PeerId,
14        stream_id: StreamId,
15        incoming: bool,
16    },
17    /// Handles incoming data from the stream.
18    IncomingData {
19        addr: ConnectionAddr,
20        peer_id: PeerId,
21        stream_id: StreamId,
22        data: Data,
23    },
24    /// Start closing the stream (send FIN).
25    Close {
26        addr: ConnectionAddr,
27        peer_id: PeerId,
28        stream_id: StreamId,
29    },
30    /// Remote peer sent FIN to close the stream.
31    RemoteClose {
32        addr: ConnectionAddr,
33        peer_id: PeerId,
34        stream_id: StreamId,
35    },
36    /// Removes the closed stream from the state.
37    Prune {
38        addr: ConnectionAddr,
39        peer_id: PeerId,
40        stream_id: StreamId,
41    },
42    SendIdentify {
43        addr: ConnectionAddr,
44        peer_id: PeerId,
45        stream_id: StreamId,
46        addresses: Vec<Multiaddr>,
47    },
48}
49
50macro_rules! enum_field {
51    ($field:ident: $field_type:ty) => {
52        pub fn $field(&self) -> &$field_type {
53            match self {
54                P2pNetworkIdentifyStreamAction::New { $field, .. }
55                | P2pNetworkIdentifyStreamAction::IncomingData { $field, .. }
56                | P2pNetworkIdentifyStreamAction::Close { $field, .. }
57                | P2pNetworkIdentifyStreamAction::RemoteClose { $field, .. }
58                | P2pNetworkIdentifyStreamAction::SendIdentify { $field, .. }
59                | P2pNetworkIdentifyStreamAction::Prune { $field, .. } => $field,
60            }
61        }
62    };
63}
64
65impl P2pNetworkIdentifyStreamAction {
66    enum_field!(addr: ConnectionAddr);
67    enum_field!(peer_id: PeerId);
68    enum_field!(stream_id: StreamId);
69}
70
71impl EnablingCondition<P2pState> for P2pNetworkIdentifyStreamAction {
72    fn is_enabled(&self, _state: &P2pState, _time: redux::Timestamp) -> bool {
73        // TODO
74        true
75    }
76}
77
78impl From<P2pNetworkIdentifyStreamAction> for P2pAction {
79    fn from(value: P2pNetworkIdentifyStreamAction) -> Self {
80        P2pAction::Network(super::super::P2pNetworkIdentifyAction::Stream(value).into())
81    }
82}