Repository: libp2p/go-libp2p
Pull request #2498 (2023-08-17 09:09:42)

muxer: remove support for mplex
For https://github.com/libp2p/specs/issues/553.

Kubo recently removed mplex support: https://github.com/ipfs/kubo/issues/9958.


Patch:
diff --git a/go.mod b/go.mod
index 2ee0eb88..5c00422b 100644
--- a/go.mod
+++ b/go.mod
@@ -25,7 +25,6 @@ require (
 	github.com/libp2p/go-flow-metrics v0.1.0
 	github.com/libp2p/go-libp2p-asn-util v0.3.0
 	github.com/libp2p/go-libp2p-testing v0.12.0
-	github.com/libp2p/go-mplex v0.7.0
 	github.com/libp2p/go-msgio v0.3.0
 	github.com/libp2p/go-nat v0.2.0
 	github.com/libp2p/go-netroute v0.2.1
diff --git a/go.sum b/go.sum
index b8f0b3fe..38297475 100644
--- a/go.sum
+++ b/go.sum
@@ -299,8 +299,6 @@ github.com/libp2p/go-libp2p-asn-util v0.3.0 h1:gMDcMyYiZKkocGXDQ5nsUQyquC9+H+iLE
 github.com/libp2p/go-libp2p-asn-util v0.3.0/go.mod h1:B1mcOrKUE35Xq/ASTmQ4tN3LNzVVaMNmq2NACuqyB9w=
 github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA=
 github.com/libp2p/go-libp2p-testing v0.12.0/go.mod h1:KcGDRXyN7sQCllucn1cOOS+Dmm7ujhfEyXQL5lvkcPg=
-github.com/libp2p/go-mplex v0.7.0 h1:BDhFZdlk5tbr0oyFq/xv/NPGfjbnrsDam1EvutpBDbY=
-github.com/libp2p/go-mplex v0.7.0/go.mod h1:rW8ThnRcYWft/Jb2jeORBmPd6xuG3dGxWN/W168L9EU=
 github.com/libp2p/go-msgio v0.3.0 h1:mf3Z8B1xcFN314sWX+2vOTShIE0Mmn2TXn3YCUQGNj0=
 github.com/libp2p/go-msgio v0.3.0/go.mod h1:nyRM819GmVaF9LX3l03RMh10QdOroF++NBbxAb0mmDM=
 github.com/libp2p/go-nat v0.2.0 h1:Tyz+bUFAYqGyJ/ppPPymMGbIgNRH+WqC5QrT5fKrrGk=
diff --git a/p2p/muxer/mplex/conn.go b/p2p/muxer/mplex/conn.go
deleted file mode 100644
index ed4e847e..00000000
--- a/p2p/muxer/mplex/conn.go
+++ /dev/null
@@ -1,48 +0,0 @@
-package mplex
-
-import (
-	"context"
-
-	"github.com/libp2p/go-libp2p/core/network"
-
-	mp "github.com/libp2p/go-mplex"
-)
-
-type conn mp.Multiplex
-
-var _ network.MuxedConn = &conn{}
-
-// NewMuxedConn constructs a new Conn from a *mp.Multiplex.
-func NewMuxedConn(m *mp.Multiplex) network.MuxedConn {
-	return (*conn)(m)
-}
-
-func (c *conn) Close() error {
-	return c.mplex().Close()
-}
-
-func (c *conn) IsClosed() bool {
-	return c.mplex().IsClosed()
-}
-
-// OpenStream creates a new stream.
-func (c *conn) OpenStream(ctx context.Context) (network.MuxedStream, error) {
-	s, err := c.mplex().NewStream(ctx)
-	if err != nil {
-		return nil, err
-	}
-	return (*stream)(s), nil
-}
-
-// AcceptStream accepts a stream opened by the other side.
-func (c *conn) AcceptStream() (network.MuxedStream, error) {
-	s, err := c.mplex().Accept()
-	if err != nil {
-		return nil, err
-	}
-	return (*stream)(s), nil
-}
-
-func (c *conn) mplex() *mp.Multiplex {
-	return (*mp.Multiplex)(c)
-}
diff --git a/p2p/muxer/mplex/stream.go b/p2p/muxer/mplex/stream.go
deleted file mode 100644
index 53e9e6da..00000000
--- a/p2p/muxer/mplex/stream.go
+++ /dev/null
@@ -1,64 +0,0 @@
-package mplex
-
-import (
-	"time"
-
-	"github.com/libp2p/go-libp2p/core/network"
-
-	mp "github.com/libp2p/go-mplex"
-)
-
-// stream implements network.MuxedStream over mplex.Stream.
-type stream mp.Stream
-
-var _ network.MuxedStream = &stream{}
-
-func (s *stream) Read(b []byte) (n int, err error) {
-	n, err = s.mplex().Read(b)
-	if err == mp.ErrStreamReset {
-		err = network.ErrReset
-	}
-
-	return n, err
-}
-
-func (s *stream) Write(b []byte) (n int, err error) {
-	n, err = s.mplex().Write(b)
-	if err == mp.ErrStreamReset {
-		err = network.ErrReset
-	}
-
-	return n, err
-}
-
-func (s *stream) Close() error {
-	return s.mplex().Close()
-}
-
-func (s *stream) CloseWrite() error {
-	return s.mplex().CloseWrite()
-}
-
-func (s *stream) CloseRead() error {
-	return s.mplex().CloseRead()
-}
-
-func (s *stream) Reset() error {
-	return s.mplex().Reset()
-}
-
-func (s *stream) SetDeadline(t time.Time) error {
-	return s.mplex().SetDeadline(t)
-}
-
-func (s *stream) SetReadDeadline(t time.Time) error {
-	return s.mplex().SetReadDeadline(t)
-}
-
-func (s *stream) SetWriteDeadline(t time.Time) error {
-	return s.mplex().SetWriteDeadline(t)
-}
-
-func (s *stream) mplex() *mp.Stream {
-	return (*mp.Stream)(s)
-}
diff --git a/p2p/muxer/mplex/transport.go b/p2p/muxer/mplex/transport.go
deleted file mode 100644
index 10d54d15..00000000
--- a/p2p/muxer/mplex/transport.go
+++ /dev/null
@@ -1,28 +0,0 @@
-package mplex
-
-import (
-	"net"
-
-	"github.com/libp2p/go-libp2p/core/network"
-
-	mp "github.com/libp2p/go-mplex"
-)
-
-// DefaultTransport has default settings for Transport
-var DefaultTransport = &Transport{}
-
-const ID = "/mplex/6.7.0"
-
-var _ network.Multiplexer = &Transport{}
-
-// Transport implements mux.Multiplexer that constructs
-// mplex-backed muxed connections.
-type Transport struct{}
-
-func (t *Transport) NewConn(nc net.Conn, isServer bool, scope network.PeerScope) (network.MuxedConn, error) {
-	m, err := mp.NewMultiplex(nc, isServer, scope)
-	if err != nil {
-		return nil, err
-	}
-	return NewMuxedConn(m), nil
-}


Test patch:
diff --git a/p2p/muxer/mplex/transport_test.go b/p2p/muxer/mplex/transport_test.go
deleted file mode 100644
index 3a7d00fa..00000000
--- a/p2p/muxer/mplex/transport_test.go
+++ /dev/null
@@ -1,52 +0,0 @@
-package mplex
-
-import (
-	"errors"
-	"net"
-	"testing"
-
-	"github.com/libp2p/go-libp2p/core/network"
-	test "github.com/libp2p/go-libp2p/p2p/muxer/testsuite"
-)
-
-func TestDefaultTransport(t *testing.T) {
-	test.SubtestAll(t, DefaultTransport)
-}
-
-type memoryScope struct {
-	network.PeerScope
-	limit    int
-	reserved int
-}
-
-func (m *memoryScope) ReserveMemory(size int, prio uint8) error {
-	if m.reserved+size > m.limit {
-		return errors.New("too much")
-	}
-	m.reserved += size
-	return nil
-}
-
-func (m *memoryScope) ReleaseMemory(size int) {
-	m.reserved -= size
-	if m.reserved < 0 {
-		panic("too much memory released")
-	}
-}
-
-type memoryLimitedTransport struct {
-	Transport
-}
-
-func (t *memoryLimitedTransport) NewConn(nc net.Conn, isServer bool, scope network.PeerScope) (network.MuxedConn, error) {
-	return t.Transport.NewConn(nc, isServer, &memoryScope{
-		limit:     3 * 1 << 20,
-		PeerScope: scope,
-	})
-}
-
-func TestDefaultTransportWithMemoryLimit(t *testing.T) {
-	test.SubtestAll(t, &memoryLimitedTransport{
-		Transport: *DefaultTransport,
-	})
-}
diff --git a/p2p/net/swarm/swarm_metrics_test.go b/p2p/net/swarm/swarm_metrics_test.go
index 261942f3..da9f6063 100644
--- a/p2p/net/swarm/swarm_metrics_test.go
+++ b/p2p/net/swarm/swarm_metrics_test.go
@@ -53,7 +53,7 @@ func TestMetricsNoAllocNoCover(t *testing.T) {
 		{StreamMultiplexer: "yamux", Security: "tls", Transport: "tcp", UsedEarlyMuxerNegotiation: true},
 		{StreamMultiplexer: "yamux", Security: "noise", Transport: "tcp", UsedEarlyMuxerNegotiation: false},
 		{StreamMultiplexer: "", Security: "", Transport: "quic"},
-		{StreamMultiplexer: "mplex", Security: "noise", Transport: "tcp"},
+		{StreamMultiplexer: "another-yamux", Security: "noise", Transport: "tcp"},
 	}
 
 	directions := []network.Direction{network.DirInbound, network.DirOutbound}
diff --git a/p2p/test/negotiation/muxer_test.go b/p2p/test/negotiation/muxer_test.go
index 9301e66c..52c66223 100644
--- a/p2p/test/negotiation/muxer_test.go
+++ b/p2p/test/negotiation/muxer_test.go
@@ -11,7 +11,6 @@ import (
 	"github.com/libp2p/go-libp2p/core/peer"
 	"github.com/libp2p/go-libp2p/core/protocol"
 	"github.com/libp2p/go-libp2p/core/sec/insecure"
-	"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
 	"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
 	"github.com/libp2p/go-libp2p/p2p/security/noise"
 	tls "github.com/libp2p/go-libp2p/p2p/security/tls"
@@ -21,8 +20,8 @@ import (
 )
 
 var (
-	yamuxOpt = libp2p.Muxer("/yamux", yamux.DefaultTransport)
-	mplexOpt = libp2p.Muxer("/mplex", mplex.DefaultTransport)
+	yamuxOpt        = libp2p.Muxer("/yamux", yamux.DefaultTransport)
+	anotherYamuxOpt = libp2p.Muxer("/another-yamux", yamux.DefaultTransport)
 )
 
 type testcase struct {
@@ -43,32 +42,32 @@ func TestMuxerNegotiation(t *testing.T) {
 	testcases := []testcase{
 		{
 			Name:             "server and client have the same preference",
-			ServerPreference: []libp2p.Option{yamuxOpt, mplexOpt},
-			ClientPreference: []libp2p.Option{yamuxOpt, mplexOpt},
+			ServerPreference: []libp2p.Option{yamuxOpt, anotherYamuxOpt},
+			ClientPreference: []libp2p.Option{yamuxOpt, anotherYamuxOpt},
 			Expected:         "/yamux",
 		},
 		{
 			Name:             "client only supports one muxer",
-			ServerPreference: []libp2p.Option{yamuxOpt, mplexOpt},
+			ServerPreference: []libp2p.Option{yamuxOpt, anotherYamuxOpt},
 			ClientPreference: []libp2p.Option{yamuxOpt},
 			Expected:         "/yamux",
 		},
 		{
 			Name:             "server only supports one muxer",
 			ServerPreference: []libp2p.Option{yamuxOpt},
-			ClientPreference: []libp2p.Option{mplexOpt, yamuxOpt},
+			ClientPreference: []libp2p.Option{anotherYamuxOpt, yamuxOpt},
 			Expected:         "/yamux",
 		},
 		{
 			Name:             "client preference preferred",
-			ServerPreference: []libp2p.Option{yamuxOpt, mplexOpt},
-			ClientPreference: []libp2p.Option{mplexOpt, yamuxOpt},
-			Expected:         "/mplex",
+			ServerPreference: []libp2p.Option{yamuxOpt, anotherYamuxOpt},
+			ClientPreference: []libp2p.Option{anotherYamuxOpt, yamuxOpt},
+			Expected:         "/another-yamux",
 		},
 		{
 			Name:             "no preference overlap",
 			ServerPreference: []libp2p.Option{yamuxOpt},
-			ClientPreference: []libp2p.Option{mplexOpt},
+			ClientPreference: []libp2p.Option{anotherYamuxOpt},
 			Error:            "failed to negotiate stream multiplexer: protocols not supported",
 		},
 	}
diff --git a/test-plans/cmd/ping/main.go b/test-plans/cmd/ping/main.go
index 3e77618b..df90a4bf 100644
--- a/test-plans/cmd/ping/main.go
+++ b/test-plans/cmd/ping/main.go
@@ -18,10 +18,9 @@ import (
 	"github.com/go-redis/redis/v8"
 	"github.com/libp2p/go-libp2p"
 	"github.com/libp2p/go-libp2p/core/peer"
-	"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
 	"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
 	"github.com/libp2p/go-libp2p/p2p/protocol/ping"
-	noise "github.com/libp2p/go-libp2p/p2p/security/noise"
+	"github.com/libp2p/go-libp2p/p2p/security/noise"
 	libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
 	libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
 	"github.com/libp2p/go-libp2p/p2p/transport/tcp"
@@ -139,8 +138,6 @@ func main() {
 		switch muxer {
 		case "yamux":
 			options = append(options, libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport))
-		case "mplex":
-			options = append(options, libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport))
 		default:
 			log.Fatalf("Unsupported muxer: %s", muxer)
 		}
diff --git a/test-plans/go.mod b/test-plans/go.mod
index c9f5c339..a6907d44 100644
--- a/test-plans/go.mod
+++ b/test-plans/go.mod
@@ -41,7 +41,6 @@ require (
 	github.com/libp2p/go-cidranger v1.1.0 // indirect
 	github.com/libp2p/go-flow-metrics v0.1.0 // indirect
 	github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect
-	github.com/libp2p/go-mplex v0.7.0 // indirect
 	github.com/libp2p/go-msgio v0.3.0 // indirect
 	github.com/libp2p/go-nat v0.2.0 // indirect
 	github.com/libp2p/go-netroute v0.2.1 // indirect
diff --git a/test-plans/go.sum b/test-plans/go.sum
index 7ffc7e63..0e90c78e 100644
--- a/test-plans/go.sum
+++ b/test-plans/go.sum
@@ -142,8 +142,6 @@ github.com/libp2p/go-flow-metrics v0.1.0/go.mod h1:4Xi8MX8wj5aWNDAZttg6UPmc0ZrnF
 github.com/libp2p/go-libp2p-asn-util v0.3.0 h1:gMDcMyYiZKkocGXDQ5nsUQyquC9+H+iLEQHwOCZ7s8s=
 github.com/libp2p/go-libp2p-asn-util v0.3.0/go.mod h1:B1mcOrKUE35Xq/ASTmQ4tN3LNzVVaMNmq2NACuqyB9w=
 github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA=
-github.com/libp2p/go-mplex v0.7.0 h1:BDhFZdlk5tbr0oyFq/xv/NPGfjbnrsDam1EvutpBDbY=
-github.com/libp2p/go-mplex v0.7.0/go.mod h1:rW8ThnRcYWft/Jb2jeORBmPd6xuG3dGxWN/W168L9EU=
 github.com/libp2p/go-msgio v0.3.0 h1:mf3Z8B1xcFN314sWX+2vOTShIE0Mmn2TXn3YCUQGNj0=
 github.com/libp2p/go-msgio v0.3.0/go.mod h1:nyRM819GmVaF9LX3l03RMh10QdOroF++NBbxAb0mmDM=
 github.com/libp2p/go-nat v0.2.0 h1:Tyz+bUFAYqGyJ/ppPPymMGbIgNRH+WqC5QrT5fKrrGk=
diff --git a/test-plans/ping-version.json b/test-plans/ping-version.json
index 9734db26..311cb58e 100644
--- a/test-plans/ping-version.json
+++ b/test-plans/ping-version.json
@@ -13,7 +13,6 @@
         "noise"
     ],
     "muxers": [
-        "mplex",
         "yamux"
     ]
 }