Skip to content

Commit f0a5fb3

Browse files
refactor(p2p): use map for enabled transports instead of separate flags
1 parent e3e6938 commit f0a5fb3

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

pkg/p2p/libp2p/export_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package libp2p
77
import (
88
"context"
99

10+
"github.com/ethersphere/bee/v2/pkg/bzz"
1011
handshake "github.com/ethersphere/bee/v2/pkg/p2p/libp2p/internal/handshake"
1112
libp2pm "github.com/libp2p/go-libp2p"
1213
"github.com/libp2p/go-libp2p/core/host"
@@ -63,7 +64,9 @@ func (s *Service) FilterSupportedAddresses(addrs []ma.Multiaddr) []ma.Multiaddr
6364
}
6465

6566
func (s *Service) SetTransportFlags(hasTCP, hasWS, hasWSS bool) {
66-
s.hasTCPTransport = hasTCP
67-
s.hasWSTransport = hasWS
68-
s.hasWSSTransport = hasWSS
67+
s.enabledTransports = map[bzz.TransportType]bool{
68+
bzz.TransportTCP: hasTCP,
69+
bzz.TransportWS: hasWS,
70+
bzz.TransportWSS: hasWSS,
71+
}
6972
}

pkg/p2p/libp2p/libp2p.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ type Service struct {
124124
enableWS bool
125125
autoTLSCertManager autoTLSCertManager
126126
zapLogger *zap.Logger
127-
hasTCPTransport bool
128-
hasWSTransport bool
129-
hasWSSTransport bool
127+
enabledTransports map[bzz.TransportType]bool
130128
}
131129

132130
type lightnodes interface {
@@ -549,9 +547,11 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
549547
enableWS: o.EnableWS,
550548
autoTLSCertManager: certManager,
551549
zapLogger: zapLogger,
552-
hasTCPTransport: true, // TCP transport is always included
553-
hasWSTransport: o.EnableWS,
554-
hasWSSTransport: o.EnableWSS,
550+
enabledTransports: map[bzz.TransportType]bool{
551+
bzz.TransportTCP: true, // TCP transport is always included
552+
bzz.TransportWS: o.EnableWS,
553+
bzz.TransportWSS: o.EnableWSS,
554+
},
555555
}
556556

557557
peerRegistry.setDisconnecter(s)
@@ -806,16 +806,7 @@ func (s *Service) handleIncoming(stream network.Stream) {
806806

807807
// isTransportSupported checks if the given transport type is supported by this service.
808808
func (s *Service) isTransportSupported(t bzz.TransportType) bool {
809-
switch t {
810-
case bzz.TransportTCP:
811-
return s.hasTCPTransport
812-
case bzz.TransportWS:
813-
return s.hasWSTransport
814-
case bzz.TransportWSS:
815-
return s.hasWSSTransport
816-
default:
817-
return false
818-
}
809+
return s.enabledTransports[t]
819810
}
820811

821812
// filterSupportedAddresses filters multiaddresses to only include those

0 commit comments

Comments
 (0)