Skip to content

Allow using go-ibp2p with fewer mandatory dependencies #3495

Description

@brandond

At the moment, simply importing go-libp2p (or a project that uses it, such as spegel) pulls in a VERY large set of indirect dependencies. These dependencies are all used even if only a single transport is enabled with libp2p.ChainOptions(libp2p.NoTransports, libp2p.Transport(tcp.NewTCPTransport))

For example, even if only tcp is used, dependencies for all available transports are pulled in due to all transports being referenced in DefaultTransports variable:

go-libp2p/defaults.go

Lines 45 to 51 in 2f15862

var DefaultTransports = ChainOptions(
Transport(tcp.NewTCPTransport),
Transport(quic.NewTransport),
Transport(ws.New),
Transport(webtransport.New),
Transport(libp2pwebrtc.New),
)

Even if DefaultPrivateTransports and DefaultTransports are modified to avoid importing unwanted transports, it is impossible to avoid pulling in github.com/quic-go/quic-go as the quicreuse transport is baked deeply into the base and config packages:

  • func QUICReuse(constructor any, opts ...quicreuse.Option) Option {
  • go-libp2p/libp2p_test.go

    Lines 200 to 211 in e5b7bf0

    func TestTransportConstructorQUIC(t *testing.T) {
    h, err := New(
    Transport(quic.NewTransport),
    DisableRelay(),
    )
    require.NoError(t, err)
    defer h.Close()
    require.NoError(t, h.Network().Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/quic-v1")))
    err = h.Network().Listen(ma.StringCast("/ip4/127.0.0.1/tcp/0"))
    require.Error(t, err)
    require.Contains(t, err.Error(), swarm.ErrNoTransport.Error())
    }
  • go-libp2p/config/config.go

    Lines 391 to 422 in e5b7bf0

    fx.Provide(func(key quic.StatelessResetKey, tokenGenerator quic.TokenGeneratorKey, rcmgr network.ResourceManager, lifecycle fx.Lifecycle) (*quicreuse.ConnManager, error) {
    opts := []quicreuse.Option{
    quicreuse.ConnContext(func(ctx context.Context, clientInfo *quic.ClientInfo) (context.Context, error) {
    // even if creating the quic maddr fails, let the rcmgr decide what to do with the connection
    addr, err := quicreuse.ToQuicMultiaddr(clientInfo.RemoteAddr, quic.Version1)
    if err != nil {
    addr = nil
    }
    scope, err := rcmgr.OpenConnection(network.DirInbound, false, addr)
    if err != nil {
    return ctx, err
    }
    ctx = network.WithConnManagementScope(ctx, scope)
    context.AfterFunc(ctx, func() {
    scope.Done()
    })
    return ctx, nil
    }),
    quicreuse.VerifySourceAddress(func(addr net.Addr) bool {
    return rcmgr.VerifySourceAddress(addr)
    }),
    }
    if !cfg.DisableMetrics {
    opts = append(opts, quicreuse.EnableMetrics(cfg.PrometheusRegisterer))
    }
    cm, err := quicreuse.NewConnManager(key, tokenGenerator, opts...)
    if err != nil {
    return nil, err
    }
    lifecycle.Append(fx.StopHook(cm.Close))
    return cm, nil
    }),

Eliminating unused transports (other than quic) saves ~8MB off the size of a simple binary that uses go-libp2p. Eliminating quic when not used would further reduce the size by ~1.6MB.

Ideally the transport abstractions would not leak into the Options and Configs (and tests for same), and the base package would not pull in all transports by default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions