Skip to content

Commit

Permalink
chore: update boxo and internalize mplex
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Aug 23, 2023
1 parent 2b7c20f commit 94fd3ac
Show file tree
Hide file tree
Showing 21 changed files with 368 additions and 183 deletions.
4 changes: 2 additions & 2 deletions config/bootstrap_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var DefaultBootstrapAddresses = []string{
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
"/ip4/104.131.131.82/udp/4001/quic-v1/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
}

// ErrInvalidPeerAddr signals an address is not a valid peer address.
Expand Down
2 changes: 0 additions & 2 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ func addressesConfig() Addresses {
Swarm: []string{
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport",
},
Expand Down
4 changes: 2 additions & 2 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestInitialization(t *testing.T) {
{
Identity: id,
Addresses: config.Addresses{
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic-v1"},
API: []string{"/ip4/127.0.0.1/tcp/8000"},
},
},

{
Identity: id,
Addresses: config.Addresses{
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic-v1"},
API: []string{"/ip4/127.0.0.1/tcp/8000"},
},
},
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestPeersTotal(t *testing.T) {
t.Fatalf("expected at most 2 peers transport (tcp and upd/quic), got %d, transport map %v",
len(peersTransport), peersTransport)
}
totalPeers := peersTransport["/ip4/tcp"] + peersTransport["/ip4/udp/quic"]
totalPeers := peersTransport["/ip4/tcp"] + peersTransport["/ip4/udp/quic-v1"]
if totalPeers != 3 {
t.Fatalf("expected 3 peers in either tcp or upd/quic transport, got %f", totalPeers)
}
Expand Down
2 changes: 1 addition & 1 deletion core/node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
}

c.Bootstrap = cfg.DefaultBootstrapAddresses
c.Addresses.Swarm = []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"}
c.Addresses.Swarm = []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic-v1"}
c.Identity.PeerID = pid.Pretty()
c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb)

Expand Down
49 changes: 49 additions & 0 deletions core/node/libp2p/internal/mplex/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Code copied from https://github.com/libp2p/go-libp2p/blob/9bd85029550a084fca63ec6ff9184122cdf06591/p2p/muxer/mplex/conn.go
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()

Check warning on line 26 in core/node/libp2p/internal/mplex/conn.go

View check run for this annotation

Codecov / codecov/patch

core/node/libp2p/internal/mplex/conn.go#L25-L26

Added lines #L25 - L26 were not covered by tests
}

// 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
}

Check warning on line 34 in core/node/libp2p/internal/mplex/conn.go

View check run for this annotation

Codecov / codecov/patch

core/node/libp2p/internal/mplex/conn.go#L33-L34

Added lines #L33 - L34 were not covered by tests
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)
}
65 changes: 65 additions & 0 deletions core/node/libp2p/internal/mplex/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Code copied from https://github.com/libp2p/go-libp2p/blob/9bd85029550a084fca63ec6ff9184122cdf06591/p2p/muxer/mplex/stream.go
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()

Check warning on line 44 in core/node/libp2p/internal/mplex/stream.go

View check run for this annotation

Codecov / codecov/patch

core/node/libp2p/internal/mplex/stream.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}

func (s *stream) Reset() error {
return s.mplex().Reset()
}

func (s *stream) SetDeadline(t time.Time) error {
return s.mplex().SetDeadline(t)

Check warning on line 52 in core/node/libp2p/internal/mplex/stream.go

View check run for this annotation

Codecov / codecov/patch

core/node/libp2p/internal/mplex/stream.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

func (s *stream) SetReadDeadline(t time.Time) error {
return s.mplex().SetReadDeadline(t)

Check warning on line 56 in core/node/libp2p/internal/mplex/stream.go

View check run for this annotation

Codecov / codecov/patch

core/node/libp2p/internal/mplex/stream.go#L55-L56

Added lines #L55 - L56 were not covered by tests
}

func (s *stream) SetWriteDeadline(t time.Time) error {
return s.mplex().SetWriteDeadline(t)

Check warning on line 60 in core/node/libp2p/internal/mplex/stream.go

View check run for this annotation

Codecov / codecov/patch

core/node/libp2p/internal/mplex/stream.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}

func (s *stream) mplex() *mp.Stream {
return (*mp.Stream)(s)
}
29 changes: 29 additions & 0 deletions core/node/libp2p/internal/mplex/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Code copied from https://github.com/libp2p/go-libp2p/blob/9bd85029550a084fca63ec6ff9184122cdf06591/p2p/muxer/mplex/transport.go
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
}

Check warning on line 27 in core/node/libp2p/internal/mplex/transport.go

View check run for this annotation

Codecov / codecov/patch

core/node/libp2p/internal/mplex/transport.go#L26-L27

Added lines #L26 - L27 were not covered by tests
return NewMuxedConn(m), nil
}
53 changes: 53 additions & 0 deletions core/node/libp2p/internal/mplex/transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Code copied from https://github.com/libp2p/go-libp2p/blob/9bd85029550a084fca63ec6ff9184122cdf06591/p2p/muxer/mplex/transport_test.go
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,
})
}
12 changes: 6 additions & 6 deletions core/node/libp2p/routingopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ import (
)

func TestHttpAddrsFromConfig(t *testing.T) {
require.Equal(t, []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
require.Equal(t, []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic-v1"},
httpAddrsFromConfig(config.Addresses{
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic-v1"},
}), "Swarm addrs should be taken by default")

require.Equal(t, []string{"/ip4/192.168.0.1/tcp/4001"},
httpAddrsFromConfig(config.Addresses{
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic-v1"},
Announce: []string{"/ip4/192.168.0.1/tcp/4001"},
}), "Announce addrs should override Swarm if specified")

require.Equal(t, []string{"/ip4/0.0.0.0/udp/4001/quic"},
require.Equal(t, []string{"/ip4/0.0.0.0/udp/4001/quic-v1"},
httpAddrsFromConfig(config.Addresses{
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic-v1"},
NoAnnounce: []string{"/ip4/0.0.0.0/tcp/4001"},
}), "Swarm addrs should not contain NoAnnounce addrs")

require.Equal(t, []string{"/ip4/192.168.0.1/tcp/4001", "/ip4/192.168.0.2/tcp/4001"},
httpAddrsFromConfig(config.Addresses{
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic-v1"},
Announce: []string{"/ip4/192.168.0.1/tcp/4001"},
AppendAnnounce: []string{"/ip4/192.168.0.2/tcp/4001"},
}), "AppendAnnounce addrs should be included if specified")
Expand Down
2 changes: 1 addition & 1 deletion core/node/libp2p/smux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/ipfs/kubo/config"

"github.com/ipfs/kubo/core/node/libp2p/internal/mplex"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
)

Expand Down
13 changes: 5 additions & 8 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,16 @@ Supported Transports:

* tcp/ip{4,6} - `/ipN/.../tcp/...`
* websocket - `/ipN/.../tcp/.../ws`
* quic (Draft-29) - `/ipN/.../udp/.../quic` - can share the same two tuple with `/quic-v1` and `/quic-v1/webtransport`
* quicv1 (RFC9000) - `/ipN/.../udp/.../quic-v1` - can share the same two tuple with `/quic` and `/quic-v1/webtransport`
* webtransport `/ipN/.../udp/.../quic-v1/webtransport` - can share the same two tuple with `/quic` and `/quic-v1`
* quicv1 (RFC9000) - `/ipN/.../udp/.../quic-v1` - can share the same two tuple with `/quic-v1/webtransport`
* webtransport `/ipN/.../udp/.../quic-v1/webtransport` - can share the same two tuple with `/quic-v1`

Default:
```json
[
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
]
Expand Down Expand Up @@ -1315,7 +1312,7 @@ The set of peers with which to peer.
},
{
"ID": "QmPeerID2",
"Addrs": ["/ip4/18.1.1.2/tcp/4001", "/ip4/18.1.1.2/udp/4001/quic"]
"Addrs": ["/ip4/18.1.1.2/tcp/4001", "/ip4/18.1.1.2/udp/4001/quic-v1"]
}
]
}
Expand Down Expand Up @@ -1999,8 +1996,8 @@ Default: Enabled
Type: `flag`

Listen Addresses:
* /ip4/0.0.0.0/udp/4001/quic (default)
* /ip6/::/udp/4001/quic (default)
* /ip4/0.0.0.0/udp/4001/quic-v1 (default)
* /ip6/::/udp/4001/quic-v1 (default)

#### `Swarm.Transports.Network.Relay`

Expand Down
Loading

0 comments on commit 94fd3ac

Please sign in to comment.