Skip to content

Commit 42dbb9c

Browse files
authored
yamux: Switch to upstream implementation while keeping the controller API (#320)
This PR relies on the libp2p-yamux crate for the core functionality of our multiplexer. The main goal is to bring complete compatibility between libp2p and litep2p on the yamux layer, and remove 90% of the yamux code in favor of the upstream implementation while keeping the controller API in place. The upstream crate brings in multiple fixes for substreams while minimally impacting our dependency tree. The downside of the upstream implementation is that the controller API has been removed. Adjusting to the new API would be a massive breaking change for all transport layers. Therefore, we keep the controller API which integrates seamlessly with the upstream yamux. No other changes were present to the controller API in the upstream implementation. ### Yamux changelog The changelog includes the fixes from the upstream since the moment we have inlined the crate in litep2p: ``` # 0.13.4 - Fix sending pending frames after closing. See [PR 194](libp2p/rust-yamux#194). # 0.13.3 - Wake up readers after setting the state to RecvClosed to not miss EOF. See [PR 190](libp2p/rust-yamux#190). - Use `web-time` instead of `instant`. See [PR 191](libp2p/rust-yamux#191). # 0.13.2 - Bound `Active`'s `pending_frames` to enforce backpressure. See [460baf2](libp2p/rust-yamux@460baf2) # 0.13.1 - Fix WASM support using `instant::{Duration, Instant}` instead of `std::time::{Duration, Instant}`. See [PR 179](libp2p/rust-yamux#179). # 0.13.0 - Introduce dynamic stream receive window auto-tuning. While low-resourced deployments maintain the benefit of small buffers, high resource deployments eventually end-up with a window of roughly the bandwidth-delay-product (ideal) and are thus able to use the entire available bandwidth. See [PR 176](libp2p/rust-yamux#176) for performance results and details on the implementation. - Remove `WindowUpdateMode`. Behavior will always be `WindowUpdateMode::OnRead`, thus enabling flow-control and enforcing backpressure. See [PR 178](libp2p/rust-yamux#178). # 0.12.1 - Deprecate `WindowUpdateMode::OnReceive`. It does not enforce flow-control, i.e. breaks backpressure. Use `WindowUpdateMode::OnRead` instead. See [PR #177](libp2p/rust-yamux#177). # 0.12.0 - Remove `Control` and `ControlledConnection`. Users have to move to the `poll_` functions of `Connection`. See [PR #164](libp2p/rust-yamux#164). - Fix a bug where `Stream`s would not be dropped until their corresponding `Connection` was dropped. See [PR #167](libp2p/rust-yamux#167). ``` ### Next Steps - [x] deployment in versi-net and monitor metrics / CPU impact (extensively test this) cc @paritytech/networking --------- Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
1 parent b7511c8 commit 42dbb9c

15 files changed

+109
-3061
lines changed

Cargo.lock

Lines changed: 28 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ x25519-dalek = "2.0.0"
5252
x509-parser = "0.16.0"
5353
yasna = "0.5.0"
5454
zeroize = "1.8.1"
55-
nohash-hasher = "0.2.0"
56-
static_assertions = "1.1.0"
55+
yamux = "0.13.4"
5756

5857
# Exposed dependencies. Breaking changes to these are breaking changes to us.
5958
[dependencies.rustls]

src/error.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub enum ParseError {
182182
InvalidData,
183183
}
184184

185-
#[derive(Debug, thiserror::Error, PartialEq)]
185+
#[derive(Debug, thiserror::Error)]
186186
pub enum SubstreamError {
187187
#[error("Connection closed")]
188188
ConnectionClosed,
@@ -202,6 +202,76 @@ pub enum SubstreamError {
202202
NegotiationError(#[from] NegotiationError),
203203
}
204204

205+
// Libp2p yamux does not implement PartialEq for ConnectionError.
206+
impl PartialEq for SubstreamError {
207+
fn eq(&self, other: &Self) -> bool {
208+
match (self, other) {
209+
(Self::ConnectionClosed, Self::ConnectionClosed) => true,
210+
(Self::ChannelClogged, Self::ChannelClogged) => true,
211+
(Self::PeerDoesNotExist(lhs), Self::PeerDoesNotExist(rhs)) => lhs == rhs,
212+
(Self::IoError(lhs), Self::IoError(rhs)) => lhs == rhs,
213+
(Self::YamuxError(lhs, lhs_1), Self::YamuxError(rhs, rhs_1)) => {
214+
if lhs_1 != rhs_1 {
215+
return false;
216+
}
217+
218+
match (lhs, rhs) {
219+
(
220+
crate::yamux::ConnectionError::Io(lhs),
221+
crate::yamux::ConnectionError::Io(rhs),
222+
) => lhs.kind() == rhs.kind(),
223+
(
224+
crate::yamux::ConnectionError::Decode(lhs),
225+
crate::yamux::ConnectionError::Decode(rhs),
226+
) => match (lhs, rhs) {
227+
(
228+
crate::yamux::FrameDecodeError::Io(lhs),
229+
crate::yamux::FrameDecodeError::Io(rhs),
230+
) => lhs.kind() == rhs.kind(),
231+
(
232+
crate::yamux::FrameDecodeError::FrameTooLarge(lhs),
233+
crate::yamux::FrameDecodeError::FrameTooLarge(rhs),
234+
) => lhs == rhs,
235+
(
236+
crate::yamux::FrameDecodeError::Header(lhs),
237+
crate::yamux::FrameDecodeError::Header(rhs),
238+
) => match (lhs, rhs) {
239+
(
240+
crate::yamux::HeaderDecodeError::Version(lhs),
241+
crate::yamux::HeaderDecodeError::Version(rhs),
242+
) => lhs == rhs,
243+
(
244+
crate::yamux::HeaderDecodeError::Type(lhs),
245+
crate::yamux::HeaderDecodeError::Type(rhs),
246+
) => lhs == rhs,
247+
_ => false,
248+
},
249+
_ => false,
250+
},
251+
(
252+
crate::yamux::ConnectionError::NoMoreStreamIds,
253+
crate::yamux::ConnectionError::NoMoreStreamIds,
254+
) => true,
255+
(
256+
crate::yamux::ConnectionError::Closed,
257+
crate::yamux::ConnectionError::Closed,
258+
) => true,
259+
(
260+
crate::yamux::ConnectionError::TooManyStreams,
261+
crate::yamux::ConnectionError::TooManyStreams,
262+
) => true,
263+
_ => false,
264+
}
265+
}
266+
267+
(Self::ReadFailure(lhs), Self::ReadFailure(rhs)) => lhs == rhs,
268+
(Self::WriteFailure(lhs), Self::WriteFailure(rhs)) => lhs == rhs,
269+
(Self::NegotiationError(lhs), Self::NegotiationError(rhs)) => lhs == rhs,
270+
_ => false,
271+
}
272+
}
273+
}
274+
205275
/// Error during the negotiation phase.
206276
#[derive(Debug, thiserror::Error)]
207277
pub enum NegotiationError {

src/yamux/chunks.rs

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)