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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use futures::{channel::oneshot, prelude::Future};
use parity_scale_codec::{Decode, Encode, Error as DecodingError};
use sc_network as network;
use sc_network::PeerId;
use polkadot_primitives::v2::AuthorityDiscoveryId;
use super::{v1, IsRequest, Protocol};
#[derive(Debug)]
pub enum Requests {
ChunkFetchingV1(OutgoingRequest<v1::ChunkFetchingRequest>),
CollationFetchingV1(OutgoingRequest<v1::CollationFetchingRequest>),
PoVFetchingV1(OutgoingRequest<v1::PoVFetchingRequest>),
AvailableDataFetchingV1(OutgoingRequest<v1::AvailableDataFetchingRequest>),
StatementFetchingV1(OutgoingRequest<v1::StatementFetchingRequest>),
DisputeSendingV1(OutgoingRequest<v1::DisputeRequest>),
}
impl Requests {
pub fn get_protocol(&self) -> Protocol {
match self {
Self::ChunkFetchingV1(_) => Protocol::ChunkFetchingV1,
Self::CollationFetchingV1(_) => Protocol::CollationFetchingV1,
Self::PoVFetchingV1(_) => Protocol::PoVFetchingV1,
Self::AvailableDataFetchingV1(_) => Protocol::AvailableDataFetchingV1,
Self::StatementFetchingV1(_) => Protocol::StatementFetchingV1,
Self::DisputeSendingV1(_) => Protocol::DisputeSendingV1,
}
}
pub fn encode_request(self) -> (Protocol, OutgoingRequest<Vec<u8>>) {
match self {
Self::ChunkFetchingV1(r) => r.encode_request(),
Self::CollationFetchingV1(r) => r.encode_request(),
Self::PoVFetchingV1(r) => r.encode_request(),
Self::AvailableDataFetchingV1(r) => r.encode_request(),
Self::StatementFetchingV1(r) => r.encode_request(),
Self::DisputeSendingV1(r) => r.encode_request(),
}
}
}
pub type ResponseSender = oneshot::Sender<Result<Vec<u8>, network::RequestFailure>>;
#[derive(Debug, thiserror::Error)]
pub enum RequestError {
#[error("Response could not be decoded: {0}")]
InvalidResponse(#[from] DecodingError),
#[error("{0}")]
NetworkError(#[from] network::RequestFailure),
#[error("Response channel got canceled")]
Canceled(#[from] oneshot::Canceled),
}
impl RequestError {
pub fn is_timed_out(&self) -> bool {
match self {
Self::Canceled(_) |
Self::NetworkError(network::RequestFailure::Obsolete) |
Self::NetworkError(network::RequestFailure::Network(
network::OutboundFailure::Timeout,
)) => true,
_ => false,
}
}
}
#[derive(Debug)]
pub struct OutgoingRequest<Req> {
pub peer: Recipient,
pub payload: Req,
pub pending_response: ResponseSender,
}
#[derive(Debug, Eq, Hash, PartialEq, Clone)]
pub enum Recipient {
Peer(PeerId),
Authority(AuthorityDiscoveryId),
}
pub type OutgoingResult<Res> = Result<Res, RequestError>;
impl<Req> OutgoingRequest<Req>
where
Req: IsRequest + Encode,
Req::Response: Decode,
{
pub fn new(
peer: Recipient,
payload: Req,
) -> (Self, impl Future<Output = OutgoingResult<Req::Response>>) {
let (tx, rx) = oneshot::channel();
let r = Self { peer, payload, pending_response: tx };
(r, receive_response::<Req>(rx))
}
pub fn encode_request(self) -> (Protocol, OutgoingRequest<Vec<u8>>) {
let OutgoingRequest { peer, payload, pending_response } = self;
let encoded = OutgoingRequest { peer, payload: payload.encode(), pending_response };
(Req::PROTOCOL, encoded)
}
}
async fn receive_response<Req>(
rec: oneshot::Receiver<Result<Vec<u8>, network::RequestFailure>>,
) -> OutgoingResult<Req::Response>
where
Req: IsRequest,
Req::Response: Decode,
{
let raw = rec.await??;
Ok(Decode::decode(&mut raw.as_ref())?)
}