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 22, 2023
1 parent 2b7c20f commit 5bdea7b
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 169 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()
}

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

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)
}
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
}
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
35 changes: 17 additions & 18 deletions docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ go 1.20
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/boxo v0.12.0
github.com/ipfs/boxo v0.12.1-0.20230822135301-303595bcdba7
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.29.2
github.com/multiformats/go-multiaddr v0.10.1
github.com/libp2p/go-libp2p v0.30.0
github.com/multiformats/go-multiaddr v0.11.0
)

require (
Expand Down Expand Up @@ -50,7 +50,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20230705174524-200ffdc848b8 // indirect
github.com/google/pprof v0.0.0-20230821062121-407c9e7a662f // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand All @@ -59,7 +59,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.2 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/huin/goupnp v1.2.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-bitfield v1.1.0 // indirect
Expand Down Expand Up @@ -114,7 +114,7 @@ require (
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
github.com/libp2p/go-reuseport v0.3.0 // indirect
github.com/libp2p/go-reuseport v0.4.0 // indirect
github.com/libp2p/go-yamux/v4 v4.0.1 // indirect
github.com/libp2p/zeroconf/v2 v2.2.0 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
Expand All @@ -136,7 +136,7 @@ require (
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
github.com/opencontainers/runtime-spec v1.0.2 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
Expand All @@ -146,11 +146,10 @@ require (
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.3 // indirect
github.com/quic-go/qtls-go1-20 v0.2.3 // indirect
github.com/quic-go/quic-go v0.36.4 // indirect
github.com/quic-go/qtls-go1-20 v0.3.2 // indirect
github.com/quic-go/quic-go v0.38.0 // indirect
github.com/quic-go/webtransport-go v0.5.3 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/samber/lo v1.36.0 // indirect
Expand Down Expand Up @@ -180,16 +179,16 @@ require (
go.uber.org/dig v1.17.0 // indirect
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
go.uber.org/zap v1.25.0 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.11.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gonum.org/v1/gonum v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
Expand Down
Loading

0 comments on commit 5bdea7b

Please sign in to comment.