Skip to content

Commit

Permalink
change muxer flag to string to support multiple muxers
Browse files Browse the repository at this point in the history
  • Loading branch information
GheisMohammadi committed May 31, 2024
1 parent 7233e1f commit 9ba1d05
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func main() {
maxConnPerIP := flag.Int("max_conn_per_ip", 10, "max connections number for same ip")
forceReachabilityPublic := flag.Bool("force_public", false, "forcing the local node to believe it is reachable externally")
noTransportSecurity := flag.Bool("no_transport_security", true, "disable TLS encrypted transport")
muxerType := flag.Int("muxer", 0, "protocol muxer to mux per-protocol streams (0:Mplex, 1:Yamux, default:0)")
muxer := flag.String("muxer", "mplex, yamux", "protocol muxer to mux per-protocol streams (mplex, yamux)")
userAgent := flag.String("user_agent", defUserAgent, "explicitly set the user-agent, so we can differentiate from other Go libp2p users")
noRelay := flag.Bool("no_relay", true, "no relay services, direct connections between peers only")

Expand Down Expand Up @@ -152,7 +152,7 @@ func main() {
NAT: true,
UserAgent: *userAgent,
DialTimeout: time.Minute,
MuxerType: p2p.MuxerType(*muxerType),
Muxer: *muxer,
NoRelay: *noRelay,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/harmony/config_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ func init() {
if confTree.Get("P2P.DialTimeout") == nil {
confTree.Set("P2P.DialTimeout", defaultConfig.P2P.DialTimeout)
}
if confTree.Get("P2P.MuxerType") == nil {
confTree.Set("P2P.MuxerType", defaultConfig.P2P.MuxerType)
if confTree.Get("P2P.Muxer") == nil {
confTree.Set("P2P.Muxer", defaultConfig.P2P.Muxer)
}
if confTree.Get("P2P.NoRelay") == nil {
confTree.Set("P2P.NoRelay", defaultConfig.P2P.NoRelay)
Expand Down
2 changes: 1 addition & 1 deletion cmd/harmony/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
NAT: nodeconfig.DefaultNAT,
UserAgent: nodeconfig.DefaultUserAgent,
DialTimeout: nodeconfig.DefaultDialTimeout,
MuxerType: nodeconfig.DefaultMuxerType,
Muxer: nodeconfig.DefaultMuxer,
NoRelay: nodeconfig.DefaultNoRelay,
},
HTTP: harmonyconfig.HttpConfig{
Expand Down
10 changes: 5 additions & 5 deletions cmd/harmony/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (
noTransportSecurityFlag,
natFlag,
userAgentFlag,
muxerTypeFlag,
muxerFlag,
noRelayFlag,
}

Expand Down Expand Up @@ -657,10 +657,10 @@ var (
Usage: "explicitly set the user-agent, so we can differentiate from other Go libp2p users",
DefValue: defaultConfig.P2P.UserAgent,
}
muxerTypeFlag = cli.IntFlag{
muxerFlag = cli.StringFlag{
Name: "p2p.muxer",
Usage: "protocol muxer to mux per-protocol streams (0:Mplex, 1:Yamux, default:0)",
DefValue: defaultConfig.P2P.MuxerType,
DefValue: defaultConfig.P2P.Muxer,
}
noRelayFlag = cli.BoolFlag{
Name: "p2p.no-relay",
Expand Down Expand Up @@ -731,8 +731,8 @@ func applyP2PFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
config.P2P.UserAgent = cli.GetStringFlagValue(cmd, userAgentFlag)
}

if cli.IsFlagChanged(cmd, muxerTypeFlag) {
config.P2P.MuxerType = cli.GetIntFlagValue(cmd, muxerTypeFlag)
if cli.IsFlagChanged(cmd, muxerFlag) {
config.P2P.Muxer = cli.GetStringFlagValue(cmd, muxerFlag)
}

if cli.IsFlagChanged(cmd, noRelayFlag) {
Expand Down
30 changes: 15 additions & 15 deletions cmd/harmony/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestHarmonyFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
HTTP: harmonyconfig.HttpConfig{
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -440,7 +440,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -461,7 +461,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -482,7 +482,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -503,7 +503,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -524,7 +524,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -545,7 +545,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -566,7 +566,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -587,7 +587,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -608,7 +608,7 @@ func TestP2PFlags(t *testing.T) {
NAT: false,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -629,12 +629,12 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: "test1",
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: defaultConfig.P2P.NoRelay,
},
},
{
args: []string{"--p2p.muxer", "1"},
args: []string{"--p2p.muxer", "mplex"},
expConfig: harmonyconfig.P2pConfig{
Port: nodeconfig.DefaultP2PPort,
IP: nodeconfig.DefaultPublicListenIP,
Expand All @@ -650,7 +650,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: 1,
Muxer: "mplex",
NoRelay: defaultConfig.P2P.NoRelay,
},
},
Expand All @@ -671,7 +671,7 @@ func TestP2PFlags(t *testing.T) {
NAT: defaultConfig.P2P.NAT,
UserAgent: defaultConfig.P2P.UserAgent,
DialTimeout: defaultConfig.P2P.DialTimeout,
MuxerType: defaultConfig.P2P.MuxerType,
Muxer: defaultConfig.P2P.Muxer,
NoRelay: false,
},
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func createGlobalConfig(hc harmonyconfig.HarmonyConfig) (*nodeconfig.ConfigType,
NAT: hc.P2P.NAT,
UserAgent: hc.P2P.UserAgent,
DialTimeout: hc.P2P.DialTimeout,
MuxerType: p2p.MuxerType(hc.P2P.MuxerType),
Muxer: hc.P2P.Muxer,
NoRelay: hc.P2P.NoRelay,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/configs/harmony/harmony.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ type P2pConfig struct {
UserAgent string
// p2p dial timeout
DialTimeout time.Duration
// P2P multiplexer type (default:Mplex, 0:Mplex, 1:Yamux)
MuxerType int
// P2P multiplexer type, should be comma separated (mplex, Yamux)
Muxer string
// No relay services, direct connections between peers only
NoRelay bool
}
Expand Down
4 changes: 2 additions & 2 deletions internal/configs/node/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ const (
DefaultUserAgent = ""
// DefaultDialTimeout dial timeout
DefaultDialTimeout = time.Minute
// DefaultMuxerType P2P multiplexer type (default:Mplex, 0:Mplex, 1:Yamux)
DefaultMuxerType = 0 // 0:Mplex 1:Yamux
// DefaultMuxerType P2P multiplexer type
DefaultMuxer = "mplex, yamux"
// DefaultNoRelay disables p2p host relay
DefaultNoRelay = true
)
Expand Down
39 changes: 18 additions & 21 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ const (
MaxMessageSize = 1 << 21
)

// Host Multiplexer's Type
type MuxerType int

const (
Mplex MuxerType = iota
Yamux
)

// HostConfig is the config structure to create a new host
type HostConfig struct {
Self *Peer
Expand All @@ -117,7 +109,7 @@ type HostConfig struct {
NAT bool
UserAgent string
DialTimeout time.Duration
MuxerType MuxerType
Muxer string
NoRelay bool
}

Expand Down Expand Up @@ -184,7 +176,6 @@ func NewHost(cfg HostConfig) (Host, error) {
}

// prepare host options
var idht *dht.IpfsDHT
p2pHostConfig := []libp2p.Option{
libp2p.Identity(key),
// Explicitly set the user-agent, so we can differentiate from other Go libp2p users.
Expand Down Expand Up @@ -225,17 +216,23 @@ func NewHost(cfg HostConfig) (Host, error) {
p2pHostConfig = append(p2pHostConfig, NoiseC(), TlsC())
}

// Set Muxer (default: Mplex)
switch cfg.MuxerType {
case Yamux:
p2pHostConfig = append(p2pHostConfig, YamuxC())
case Mplex:
p2pHostConfig = append(p2pHostConfig, MplexC())
default:
utils.Logger().Error().
Interface("muxer", cfg.MuxerType).
Msg("Muxer type is invalid, it is set on default value (mplex)")
p2pHostConfig = append(p2pHostConfig, MplexC())
// Set Muxer
if cfg.Muxer != "" {
for _, v := range strings.Split(cfg.Muxer, ",") {
v = strings.ToLower(strings.TrimSpace(v))
switch v {
case "yamux":
p2pHostConfig = append(p2pHostConfig, YamuxC())
case "mplex":
p2pHostConfig = append(p2pHostConfig, MplexC())
default:
cancel()
utils.Logger().Error().
Str("muxer", v).
Msg("Muxer type is invalid")
return nil, fmt.Errorf("could not recognize mux %s", v)
}
}
}

if cfg.ForceReachabilityPublic {
Expand Down

0 comments on commit 9ba1d05

Please sign in to comment.