-
Notifications
You must be signed in to change notification settings - Fork 7
/
p2p_channels_rpc_actions.rs
157 lines (147 loc) · 5.78 KB
/
p2p_channels_rpc_actions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use serde::{Deserialize, Serialize};
use crate::{P2pState, PeerId};
use super::{P2pChannelsRpcState, P2pRpcId, P2pRpcLocalState, P2pRpcRequest, P2pRpcResponse};
pub type P2pChannelsRpcActionWithMetaRef<'a> = redux::ActionWithMeta<&'a P2pChannelsRpcAction>;
pub const MAX_P2P_RPC_REMOTE_CONCURRENT_REQUESTS: usize = 5;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum P2pChannelsRpcAction {
Init {
peer_id: PeerId,
},
Pending {
peer_id: PeerId,
},
Ready {
peer_id: PeerId,
},
RequestSend {
peer_id: PeerId,
id: P2pRpcId,
request: P2pRpcRequest,
},
Timeout {
peer_id: PeerId,
id: P2pRpcId,
},
ResponseReceived {
peer_id: PeerId,
id: P2pRpcId,
response: Option<P2pRpcResponse>,
},
RequestReceived {
peer_id: PeerId,
id: P2pRpcId,
request: P2pRpcRequest,
},
ResponseSend {
peer_id: PeerId,
id: P2pRpcId,
response: Option<P2pRpcResponse>,
},
}
impl P2pChannelsRpcAction {
pub fn peer_id(&self) -> &PeerId {
match self {
Self::Init { peer_id }
| Self::Pending { peer_id }
| Self::Ready { peer_id }
| Self::RequestSend { peer_id, .. }
| Self::Timeout { peer_id, .. }
| Self::ResponseReceived { peer_id, .. }
| Self::RequestReceived { peer_id, .. }
| Self::ResponseSend { peer_id, .. } => peer_id,
}
}
}
impl redux::EnablingCondition<P2pState> for P2pChannelsRpcAction {
fn is_enabled(&self, state: &P2pState) -> bool {
match self {
P2pChannelsRpcAction::Init { peer_id } => {
state.get_ready_peer(peer_id).map_or(false, |p| {
matches!(p.channels.rpc, P2pChannelsRpcState::Enabled)
})
},
P2pChannelsRpcAction::Pending { peer_id } => {
state.get_ready_peer(peer_id).map_or(false, |p| {
matches!(p.channels.rpc, P2pChannelsRpcState::Init { .. })
})
},
P2pChannelsRpcAction::Ready { peer_id } => {
state.get_ready_peer(peer_id).map_or(false, |p| {
matches!(p.channels.rpc, P2pChannelsRpcState::Pending { .. })
})
},
P2pChannelsRpcAction::RequestSend { peer_id, id, request } => {
if !cfg!(feature = "p2p-libp2p") {
return if !request.kind().supported_by_libp2p() {
false
} else if let Some(streams) = state
.network
.scheduler
.rpc_outgoing_streams
.get(peer_id)
{
!streams.is_empty()
} else {
false
};
}
state.peers.get(peer_id)
.filter(|p| !p.is_libp2p() || request.kind().supported_by_libp2p())
.and_then(|p| p.status.as_ready())
.map_or(false, |p| match &p.channels.rpc {
P2pChannelsRpcState::Ready { local, next_local_rpc_id, .. } => {
next_local_rpc_id == id && matches!(
local,
P2pRpcLocalState::WaitingForRequest { .. } | P2pRpcLocalState::Responded { .. }
)
},
_ => false,
})
},
P2pChannelsRpcAction::Timeout { peer_id, id } => {
state.get_ready_peer(peer_id).map_or(false, |p| match &p.channels.rpc {
P2pChannelsRpcState::Ready { local, .. } => {
matches!(local, P2pRpcLocalState::Requested { id: rpc_id, .. } if rpc_id == id)
},
_ => false,
})
},
P2pChannelsRpcAction::ResponseReceived { peer_id, id, .. } => {
// TODO(binier): use consensus to enforce that peer doesn't send
// us inferior block than it has in the past.
state.get_ready_peer(peer_id).map_or(false, |p| match &p.channels.rpc {
P2pChannelsRpcState::Ready { local, .. } => {
// TODO(binier): validate that response corresponds to request.
matches!(local, P2pRpcLocalState::Requested { id: rpc_id, .. } if rpc_id == id)
},
_ => false,
})
},
P2pChannelsRpcAction::RequestReceived { peer_id, id, .. } => {
state.get_ready_peer(peer_id).map_or(false, |p| match &p.channels.rpc {
P2pChannelsRpcState::Ready { remote, .. } => {
remote.pending_requests.len() < MAX_P2P_RPC_REMOTE_CONCURRENT_REQUESTS &&
remote.pending_requests.iter().all(|v| v.id != *id)
},
_ => false,
})
},
P2pChannelsRpcAction::ResponseSend { peer_id, id, .. } => {
state.get_ready_peer(peer_id).map_or(false, |p| match &p.channels.rpc {
P2pChannelsRpcState::Ready { remote, .. } => {
// TODO(binier): validate that response corresponds to request.
remote.pending_requests.iter().any(|v| v.id == *id)
},
_ => false,
})
},
}
}
}
use crate::channels::P2pChannelsAction;
impl From<P2pChannelsRpcAction> for crate::P2pAction {
fn from(a: P2pChannelsRpcAction) -> Self {
Self::Channels(P2pChannelsAction::Rpc(a))
}
}