node/p2p/callbacks/
p2p_callbacks_actions.rs

1use openmina_core::ActionEvent;
2use p2p::{
3    channels::{
4        rpc::{P2pRpcId, P2pRpcRequest, P2pRpcResponse},
5        streaming_rpc::P2pStreamingRpcResponseFull,
6    },
7    P2pNetworkPubsubMessageCacheId, PeerId,
8};
9use serde::{Deserialize, Serialize};
10
11#[derive(Serialize, Deserialize, Debug, Clone, ActionEvent)]
12#[action_event(level = debug)]
13pub enum P2pCallbacksAction {
14    P2pChannelsRpcReady {
15        peer_id: PeerId,
16    },
17    P2pChannelsRpcTimeout {
18        peer_id: PeerId,
19        id: P2pRpcId,
20    },
21    P2pChannelsRpcResponseReceived {
22        peer_id: PeerId,
23        id: P2pRpcId,
24        response: Option<Box<P2pRpcResponse>>,
25    },
26    P2pChannelsRpcRequestReceived {
27        peer_id: PeerId,
28        id: P2pRpcId,
29        request: Box<P2pRpcRequest>,
30    },
31
32    P2pChannelsStreamingRpcReady,
33    P2pChannelsStreamingRpcTimeout {
34        peer_id: PeerId,
35        id: P2pRpcId,
36    },
37    P2pChannelsStreamingRpcResponseReceived {
38        peer_id: PeerId,
39        id: P2pRpcId,
40        response: Option<P2pStreamingRpcResponseFull>,
41    },
42
43    P2pDisconnection {
44        peer_id: PeerId,
45    },
46    RpcRespondBestTip {
47        peer_id: PeerId,
48    },
49    P2pPubsubValidateMessage {
50        message_id: P2pNetworkPubsubMessageCacheId,
51    },
52}
53
54impl redux::EnablingCondition<crate::State> for P2pCallbacksAction {
55    fn is_enabled(&self, state: &crate::State, _time: redux::Timestamp) -> bool {
56        match self {
57            P2pCallbacksAction::P2pChannelsRpcReady { .. } => true,
58            P2pCallbacksAction::P2pChannelsRpcTimeout { .. } => true,
59            P2pCallbacksAction::P2pChannelsRpcResponseReceived { .. } => true,
60            P2pCallbacksAction::P2pChannelsRpcRequestReceived { .. } => true,
61            P2pCallbacksAction::P2pChannelsStreamingRpcReady => true,
62            P2pCallbacksAction::P2pChannelsStreamingRpcTimeout { .. } => true,
63            P2pCallbacksAction::P2pChannelsStreamingRpcResponseReceived { .. } => true,
64            P2pCallbacksAction::P2pDisconnection { .. } => true,
65            // TODO: what if we don't have best tip?
66            P2pCallbacksAction::RpcRespondBestTip { .. } => {
67                state.transition_frontier.best_tip().is_some()
68            }
69            P2pCallbacksAction::P2pPubsubValidateMessage { .. } => true,
70        }
71    }
72}