Skip to content

Commit

Permalink
yamux: Switch to upstream implementation while keeping the controller…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>
  • Loading branch information
lexnv authored Jan 29, 2025
1 parent b7511c8 commit 42dbb9c
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 3,061 deletions.
31 changes: 28 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ x25519-dalek = "2.0.0"
x509-parser = "0.16.0"
yasna = "0.5.0"
zeroize = "1.8.1"
nohash-hasher = "0.2.0"
static_assertions = "1.1.0"
yamux = "0.13.4"

# Exposed dependencies. Breaking changes to these are breaking changes to us.
[dependencies.rustls]
Expand Down
72 changes: 71 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub enum ParseError {
InvalidData,
}

#[derive(Debug, thiserror::Error, PartialEq)]
#[derive(Debug, thiserror::Error)]
pub enum SubstreamError {
#[error("Connection closed")]
ConnectionClosed,
Expand All @@ -202,6 +202,76 @@ pub enum SubstreamError {
NegotiationError(#[from] NegotiationError),
}

// Libp2p yamux does not implement PartialEq for ConnectionError.
impl PartialEq for SubstreamError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::ConnectionClosed, Self::ConnectionClosed) => true,
(Self::ChannelClogged, Self::ChannelClogged) => true,
(Self::PeerDoesNotExist(lhs), Self::PeerDoesNotExist(rhs)) => lhs == rhs,
(Self::IoError(lhs), Self::IoError(rhs)) => lhs == rhs,
(Self::YamuxError(lhs, lhs_1), Self::YamuxError(rhs, rhs_1)) => {
if lhs_1 != rhs_1 {
return false;
}

match (lhs, rhs) {
(
crate::yamux::ConnectionError::Io(lhs),
crate::yamux::ConnectionError::Io(rhs),
) => lhs.kind() == rhs.kind(),
(
crate::yamux::ConnectionError::Decode(lhs),
crate::yamux::ConnectionError::Decode(rhs),
) => match (lhs, rhs) {
(
crate::yamux::FrameDecodeError::Io(lhs),
crate::yamux::FrameDecodeError::Io(rhs),
) => lhs.kind() == rhs.kind(),
(
crate::yamux::FrameDecodeError::FrameTooLarge(lhs),
crate::yamux::FrameDecodeError::FrameTooLarge(rhs),
) => lhs == rhs,
(
crate::yamux::FrameDecodeError::Header(lhs),
crate::yamux::FrameDecodeError::Header(rhs),
) => match (lhs, rhs) {
(
crate::yamux::HeaderDecodeError::Version(lhs),
crate::yamux::HeaderDecodeError::Version(rhs),
) => lhs == rhs,
(
crate::yamux::HeaderDecodeError::Type(lhs),
crate::yamux::HeaderDecodeError::Type(rhs),
) => lhs == rhs,
_ => false,
},
_ => false,
},
(
crate::yamux::ConnectionError::NoMoreStreamIds,
crate::yamux::ConnectionError::NoMoreStreamIds,
) => true,
(
crate::yamux::ConnectionError::Closed,
crate::yamux::ConnectionError::Closed,
) => true,
(
crate::yamux::ConnectionError::TooManyStreams,
crate::yamux::ConnectionError::TooManyStreams,
) => true,
_ => false,
}
}

(Self::ReadFailure(lhs), Self::ReadFailure(rhs)) => lhs == rhs,
(Self::WriteFailure(lhs), Self::WriteFailure(rhs)) => lhs == rhs,
(Self::NegotiationError(lhs), Self::NegotiationError(rhs)) => lhs == rhs,
_ => false,
}
}
}

/// Error during the negotiation phase.
#[derive(Debug, thiserror::Error)]
pub enum NegotiationError {
Expand Down
111 changes: 0 additions & 111 deletions src/yamux/chunks.rs

This file was deleted.

Loading

0 comments on commit 42dbb9c

Please sign in to comment.