File size: 10,455 Bytes
f0f4f2b |
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 |
use crate::future::{BoxFuture, Either, FutureExt};
use futures::{future, AsyncRead, AsyncWrite};
use futures::{AsyncReadExt, Stream};
use futures::{AsyncWriteExt, StreamExt};
use libp2p_core::muxing::StreamMuxerExt;
use libp2p_core::upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade};
use libp2p_core::{StreamMuxer, UpgradeInfo};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use std::{fmt, mem};
pub async fn connected_muxers_on_memory_ring_buffer<MC, M, E>() -> (M, M)
where
MC: InboundConnectionUpgrade<futures_ringbuf::Endpoint, Error = E, Output = M>
+ OutboundConnectionUpgrade<futures_ringbuf::Endpoint, Error = E, Output = M>
+ Send
+ 'static
+ Default,
<MC as UpgradeInfo>::Info: Send,
<<MC as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send,
<MC as InboundConnectionUpgrade<futures_ringbuf::Endpoint>>::Future: Send,
<MC as OutboundConnectionUpgrade<futures_ringbuf::Endpoint>>::Future: Send,
E: std::error::Error + Send + Sync + 'static,
{
let (alice, bob) = futures_ringbuf::Endpoint::pair(100, 100);
let alice_upgrade = MC::default().upgrade_inbound(
alice,
MC::default().protocol_info().into_iter().next().unwrap(),
);
let bob_upgrade = MC::default().upgrade_outbound(
bob,
MC::default().protocol_info().into_iter().next().unwrap(),
);
futures::future::try_join(alice_upgrade, bob_upgrade)
.await
.unwrap()
}
/// Verifies that Alice can send a message and immediately close the stream afterwards and Bob can use `read_to_end` to read the entire message.
pub async fn close_implies_flush<A, B, S, E>(alice: A, bob: B)
where
A: StreamMuxer<Substream = S, Error = E> + Unpin,
B: StreamMuxer<Substream = S, Error = E> + Unpin,
S: AsyncRead + AsyncWrite + Send + Unpin + 'static,
E: fmt::Debug,
{
run_commutative(
alice,
bob,
|mut stream| async move {
stream.write_all(b"PING").await.unwrap();
stream.close().await.unwrap();
},
|mut stream| async move {
let mut buf = Vec::new();
stream.read_to_end(&mut buf).await.unwrap();
assert_eq!(buf, b"PING");
},
)
.await;
}
/// Verifies that we can "half-close" a substream.
pub async fn read_after_close<A, B, S, E>(alice: A, bob: B)
where
A: StreamMuxer<Substream = S, Error = E> + Unpin,
B: StreamMuxer<Substream = S, Error = E> + Unpin,
S: AsyncRead + AsyncWrite + Send + Unpin + 'static,
E: fmt::Debug,
{
run_commutative(
alice,
bob,
|mut stream| async move {
stream.write_all(b"PING").await.unwrap();
stream.close().await.unwrap();
let mut buf = Vec::new();
stream.read_to_end(&mut buf).await.unwrap();
assert_eq!(buf, b"PONG");
},
|mut stream| async move {
let mut buf = [0u8; 4];
stream.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"PING");
stream.write_all(b"PONG").await.unwrap();
stream.close().await.unwrap();
},
)
.await;
}
/// Runs the given protocol between the two parties, ensuring commutativity, i.e. either party can be the dialer and listener.
async fn run_commutative<A, B, S, E, F1, F2>(
mut alice: A,
mut bob: B,
alice_proto: impl Fn(S) -> F1 + Clone + 'static,
bob_proto: impl Fn(S) -> F2 + Clone + 'static,
) where
A: StreamMuxer<Substream = S, Error = E> + Unpin,
B: StreamMuxer<Substream = S, Error = E> + Unpin,
S: AsyncRead + AsyncWrite + Send + Unpin + 'static,
E: fmt::Debug,
F1: Future<Output = ()> + Send + 'static,
F2: Future<Output = ()> + Send + 'static,
{
run(&mut alice, &mut bob, alice_proto.clone(), bob_proto.clone()).await;
run(&mut bob, &mut alice, alice_proto, bob_proto).await;
}
/// Runs a given protocol between the two parties.
///
/// The first party will open a new substream and the second party will wait for this.
/// The [`StreamMuxer`] is polled until both parties have completed the protocol to ensure that the underlying connection can make progress at all times.
async fn run<A, B, S, E, F1, F2>(
dialer: &mut A,
listener: &mut B,
alice_proto: impl Fn(S) -> F1 + 'static,
bob_proto: impl Fn(S) -> F2 + 'static,
) where
A: StreamMuxer<Substream = S, Error = E> + Unpin,
B: StreamMuxer<Substream = S, Error = E> + Unpin,
S: AsyncRead + AsyncWrite + Send + Unpin + 'static,
E: fmt::Debug,
F1: Future<Output = ()> + Send + 'static,
F2: Future<Output = ()> + Send + 'static,
{
let mut dialer = Harness::OutboundSetup {
muxer: dialer,
proto_fn: Box::new(move |s| alice_proto(s).boxed()),
};
let mut listener = Harness::InboundSetup {
muxer: listener,
proto_fn: Box::new(move |s| bob_proto(s).boxed()),
};
let mut dialer_complete = false;
let mut listener_complete = false;
loop {
match futures::future::select(dialer.next(), listener.next()).await {
Either::Left((Some(Event::SetupComplete), _)) => {
tracing::info!("Dialer opened outbound stream");
}
Either::Left((Some(Event::ProtocolComplete), _)) => {
tracing::info!("Dialer completed protocol");
dialer_complete = true
}
Either::Left((Some(Event::Timeout), _)) => {
panic!("Dialer protocol timed out");
}
Either::Right((Some(Event::SetupComplete), _)) => {
tracing::info!("Listener received inbound stream");
}
Either::Right((Some(Event::ProtocolComplete), _)) => {
tracing::info!("Listener completed protocol");
listener_complete = true
}
Either::Right((Some(Event::Timeout), _)) => {
panic!("Listener protocol timed out");
}
_ => unreachable!(),
}
if dialer_complete && listener_complete {
break;
}
}
}
enum Harness<'m, M>
where
M: StreamMuxer,
{
InboundSetup {
muxer: &'m mut M,
proto_fn: Box<dyn FnOnce(M::Substream) -> BoxFuture<'static, ()>>,
},
OutboundSetup {
muxer: &'m mut M,
proto_fn: Box<dyn FnOnce(M::Substream) -> BoxFuture<'static, ()>>,
},
Running {
muxer: &'m mut M,
timeout: futures_timer::Delay,
proto: BoxFuture<'static, ()>,
},
Complete {
muxer: &'m mut M,
},
Poisoned,
}
enum Event {
SetupComplete,
Timeout,
ProtocolComplete,
}
impl<'m, M> Stream for Harness<'m, M>
where
M: StreamMuxer + Unpin,
{
type Item = Event;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
loop {
match mem::replace(this, Self::Poisoned) {
Harness::InboundSetup { muxer, proto_fn } => {
if let Poll::Ready(stream) = muxer.poll_inbound_unpin(cx) {
*this = Harness::Running {
muxer,
timeout: futures_timer::Delay::new(Duration::from_secs(10)),
proto: proto_fn(stream.unwrap()),
};
return Poll::Ready(Some(Event::SetupComplete));
}
if let Poll::Ready(event) = muxer.poll_unpin(cx) {
event.unwrap();
*this = Harness::InboundSetup { muxer, proto_fn };
continue;
}
*this = Harness::InboundSetup { muxer, proto_fn };
return Poll::Pending;
}
Harness::OutboundSetup { muxer, proto_fn } => {
if let Poll::Ready(stream) = muxer.poll_outbound_unpin(cx) {
*this = Harness::Running {
muxer,
timeout: futures_timer::Delay::new(Duration::from_secs(10)),
proto: proto_fn(stream.unwrap()),
};
return Poll::Ready(Some(Event::SetupComplete));
}
if let Poll::Ready(event) = muxer.poll_unpin(cx) {
event.unwrap();
*this = Harness::OutboundSetup { muxer, proto_fn };
continue;
}
*this = Harness::OutboundSetup { muxer, proto_fn };
return Poll::Pending;
}
Harness::Running {
muxer,
mut proto,
mut timeout,
} => {
if let Poll::Ready(event) = muxer.poll_unpin(cx) {
event.unwrap();
*this = Harness::Running {
muxer,
proto,
timeout,
};
continue;
}
if let Poll::Ready(()) = proto.poll_unpin(cx) {
*this = Harness::Complete { muxer };
return Poll::Ready(Some(Event::ProtocolComplete));
}
if let Poll::Ready(()) = timeout.poll_unpin(cx) {
return Poll::Ready(Some(Event::Timeout));
}
*this = Harness::Running {
muxer,
proto,
timeout,
};
return Poll::Pending;
}
Harness::Complete { muxer } => {
if let Poll::Ready(event) = muxer.poll_unpin(cx) {
event.unwrap();
*this = Harness::Complete { muxer };
continue;
}
*this = Harness::Complete { muxer };
return Poll::Pending;
}
Harness::Poisoned => {
unreachable!()
}
}
}
}
}
|