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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use std::sync::Arc;
#[cfg(feature = "use-async-std")]
use async_std::{
channel::{unbounded as unbounded_channel, Receiver, Sender},
io::{Read, Write},
sync::Mutex,
task::{spawn, JoinHandle},
};
#[cfg(feature = "use-smol")]
use smol::{
channel::{unbounded as unbounded_channel, Receiver, Sender},
io::{AsyncRead as Read, AsyncWrite as Write},
lock::Mutex,
spawn, Task as JoinHandle,
};
use tendermint_proto::abci::{Request, Response};
#[cfg(feature = "use-tokio")]
use tokio::{
io::{AsyncRead as Read, AsyncWrite as Write},
spawn,
sync::{
mpsc::{unbounded_channel, UnboundedReceiver as Receiver, UnboundedSender as Sender},
Mutex,
},
task::JoinHandle,
};
use tracing::{debug, error, info, instrument};
use crate::{
async_api::{Consensus, Info, Mempool, Snapshot},
handler::*,
state::ConsensusStateValidator,
utils::{StreamReader, StreamWriter},
};
macro_rules! spawn {
($expr: expr) => {
cfg_if::cfg_if! {
if #[cfg(any(feature = "use-async-std", feature = "use-tokio"))] {
spawn($expr)
} else if #[cfg(feature = "use-smol")] {
spawn($expr)
.detach()
} else {
unreachable!()
}
}
};
}
#[instrument(skip(stream_reader, stream_writer, consensus))]
pub fn spawn_consensus_task<R, W, C>(
stream_reader: StreamReader<R>,
stream_writer: StreamWriter<W>,
peer_addr: String,
consensus: Arc<C>,
validator: Arc<Mutex<ConsensusStateValidator>>,
) where
R: Read + Unpin + Send + 'static,
W: Write + Unpin + Send + 'static,
C: Consensus + 'static,
{
info!(message = "Spawning consensus task");
spawn!(async move {
consensus_task(
stream_reader,
stream_writer,
peer_addr,
consensus.as_ref(),
validator,
)
.await
});
}
#[instrument(skip(stream_reader, stream_writer, consensus))]
async fn consensus_task<R, W, C>(
mut stream_reader: StreamReader<R>,
mut stream_writer: StreamWriter<W>,
peer_addr: String,
consensus: &C,
validator: Arc<Mutex<ConsensusStateValidator>>,
) where
R: Read + Unpin,
W: Write + Unpin,
C: Consensus,
{
while let Ok(request) = stream_reader.read().await {
match request {
None => debug!(message = "Received empty request"),
Some(request) => {
let request: Request = request;
let response = match request.value {
None => {
debug!(message = "Received empty value in request", ?request);
Response::default()
}
Some(request_value) => {
handle_consensus_request(consensus, validator.clone(), request_value).await
}
};
if let Err(err) = stream_writer.write(response).await {
error!(message = "Error while writing to stream", %err);
}
}
}
}
}
#[instrument(skip(stream_reader, stream_writer, mempool))]
pub fn spawn_mempool_task<R, W, M>(
stream_reader: StreamReader<R>,
stream_writer: StreamWriter<W>,
peer_addr: String,
mempool: Arc<M>,
) where
R: Read + Unpin + Send + 'static,
W: Write + Unpin + Send + 'static,
M: Mempool + 'static,
{
info!(message = "Spawning mempool tasks");
let (handle_sender, handle_receiver) = unbounded_channel();
let peer_addr_clone = peer_addr.clone();
spawn!(
async move { mempool_writer_task(stream_writer, peer_addr_clone, handle_receiver).await }
);
spawn!(
async move { mempool_reader_task(stream_reader, peer_addr, mempool, handle_sender).await }
);
}
#[instrument(skip(stream_writer, handle_receiver))]
async fn mempool_writer_task<W>(
mut stream_writer: StreamWriter<W>,
peer_addr: String,
handle_receiver: Receiver<JoinHandle<Response>>,
) where
W: Write + Unpin,
{
cfg_if::cfg_if! {
if #[cfg(any(feature = "use-async-std", feature = "use-smol"))] {
while let Ok(handle) = handle_receiver.recv().await {
let response = handle.await;
if let Err(err) = stream_writer.write(response).await {
error!(message = "Error while writing to stream", %err);
}
}
} else if #[cfg(feature = "use-tokio")] {
let mut handle_receiver = handle_receiver;
while let Some(handle) = handle_receiver.recv().await {
let response = handle.await;
match response {
Ok(response) => {
if let Err(err) = stream_writer.write(response).await {
error!(message = "Error while writing to stream", %err);
}
}
Err(err) => error!(message = "Mempool request execution not completed", ?err),
}
}
} else {
unreachable!()
}
}
}
#[instrument(skip(stream_reader, mempool, handle_sender))]
async fn mempool_reader_task<R, M>(
mut stream_reader: StreamReader<R>,
peer_addr: String,
mempool: Arc<M>,
handle_sender: Sender<JoinHandle<Response>>,
) where
R: Read + Unpin,
M: Mempool + 'static,
{
while let Ok(request) = stream_reader.read().await {
match request {
None => {
debug!(message = "Received empty request")
}
Some(request) => {
let peer_addr = peer_addr.clone();
let mempool = mempool.clone();
let handle = spawn(async move {
let request: Request = request;
match request.value {
None => {
debug!(
message = "Received empty value in request",
?peer_addr,
?request
);
Response::default()
}
Some(request_value) => {
handle_mempool_request(mempool.as_ref(), request_value).await
}
}
});
cfg_if::cfg_if! {
if #[cfg(any(feature = "use-async-std", feature = "use-smol"))] {
handle_sender
.send(handle)
.await
.expect("Channel receiver dropped");
} else if #[cfg(feature = "use-tokio")] {
handle_sender
.send(handle)
.expect("Channel receiver dropped");
} else {
unreachable!()
}
}
}
}
}
}
#[instrument(skip(stream_reader, stream_writer, info))]
pub fn spawn_info_task<R, W, I>(
stream_reader: StreamReader<R>,
stream_writer: StreamWriter<W>,
peer_addr: String,
info: Arc<I>,
validator: Arc<Mutex<ConsensusStateValidator>>,
) where
R: Read + Unpin + Send + 'static,
W: Write + Unpin + Send + 'static,
I: Info + 'static,
{
info!(message = "Spawning info task");
spawn!(async move {
info_task(
stream_reader,
stream_writer,
peer_addr,
info.as_ref(),
validator,
)
.await
});
}
#[instrument(skip(stream_reader, stream_writer, info))]
async fn info_task<R, W, I>(
mut stream_reader: StreamReader<R>,
mut stream_writer: StreamWriter<W>,
peer_addr: String,
info: &I,
validator: Arc<Mutex<ConsensusStateValidator>>,
) where
R: Read + Unpin,
W: Write + Unpin,
I: Info,
{
while let Ok(request) = stream_reader.read().await {
match request {
None => debug!(message = "Received empty request"),
Some(request) => {
let request: Request = request;
let response = match request.value {
None => {
debug!(message = "Received empty value in request", ?request);
Response::default()
}
Some(request_value) => {
handle_info_request(info, validator.clone(), request_value).await
}
};
if let Err(err) = stream_writer.write(response).await {
error!(message = "Error while writing to stream", %err);
}
}
}
}
}
#[instrument(skip(stream_reader, stream_writer, snapshot))]
pub fn spawn_snapshot_task<R, W, S>(
stream_reader: StreamReader<R>,
stream_writer: StreamWriter<W>,
peer_addr: String,
snapshot: Arc<S>,
) where
R: Read + Unpin + Send + 'static,
W: Write + Unpin + Send + 'static,
S: Snapshot + 'static,
{
info!(message = "Spawning snapshot task");
spawn!(async move {
snapshot_task(stream_reader, stream_writer, peer_addr, snapshot.as_ref()).await
});
}
#[instrument(skip(stream_reader, stream_writer, snapshot))]
async fn snapshot_task<R, W, S>(
mut stream_reader: StreamReader<R>,
mut stream_writer: StreamWriter<W>,
peer_addr: String,
snapshot: &S,
) where
R: Read + Unpin,
W: Write + Unpin,
S: Snapshot,
{
while let Ok(request) = stream_reader.read().await {
match request {
None => debug!(message = "Received empty request"),
Some(request) => {
let request: Request = request;
let response = match request.value {
None => {
debug!(message = "Received empty value in request", ?request);
Response::default()
}
Some(request_value) => handle_snapshot_request(snapshot, request_value).await,
};
if let Err(err) = stream_writer.write(response).await {
error!(message = "Error while writing to stream", %err);
}
}
}
}
}