Skip to content

Commit

Permalink
Merge pull request #8750 from ipfs/fix-muxer-prios
Browse files Browse the repository at this point in the history
fix prioritization of stream muxers
  • Loading branch information
Stebalien authored Feb 28, 2022
2 parents 3ea5631 + 3674b66 commit da08ace
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
4 changes: 2 additions & 2 deletions core/node/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type priorityOption struct {

func prioritizeOptions(opts []priorityOption) libp2p.Option {
type popt struct {
priority int64
priority int64 // lower priority values mean higher priority
opt libp2p.Option
}
enabledOptions := make([]popt, 0, len(opts))
Expand All @@ -71,7 +71,7 @@ func prioritizeOptions(opts []priorityOption) libp2p.Option {
}
}
sort.Slice(enabledOptions, func(i, j int) bool {
return enabledOptions[i].priority > enabledOptions[j].priority
return enabledOptions[i].priority < enabledOptions[j].priority
})
p2pOpts := make([]libp2p.Option, len(enabledOptions))
for i, opt := range enabledOptions {
Expand Down
58 changes: 58 additions & 0 deletions core/node/libp2p/libp2p_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package libp2p

import (
"fmt"
"strconv"
"testing"

"github.com/libp2p/go-libp2p"
ma "github.com/multiformats/go-multiaddr"

"github.com/stretchr/testify/require"
)

func TestPrioritize(t *testing.T) {
// The option is encoded into the port number of a TCP multiaddr.
// By extracting the port numbers obtained from the applied option, we can make sure that
// prioritization sorted the options correctly.
newOption := func(num int) libp2p.Option {
return func(cfg *libp2p.Config) error {
cfg.ListenAddrs = append(cfg.ListenAddrs, ma.StringCast(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", num)))
return nil
}
}

extractNums := func(cfg *libp2p.Config) []int {
addrs := cfg.ListenAddrs
nums := make([]int, 0, len(addrs))
for _, addr := range addrs {
_, comp := ma.SplitLast(addr)
num, err := strconv.Atoi(comp.Value())
require.NoError(t, err)
nums = append(nums, num)
}
return nums
}

t.Run("using default priorities", func(t *testing.T) {
opts := []priorityOption{
{defaultPriority: 200, opt: newOption(200)},
{defaultPriority: 1, opt: newOption(1)},
{defaultPriority: 300, opt: newOption(300)},
}
var cfg libp2p.Config
require.NoError(t, prioritizeOptions(opts)(&cfg))
require.Equal(t, extractNums(&cfg), []int{1, 200, 300})
})

t.Run("using custom priorities", func(t *testing.T) {
opts := []priorityOption{
{defaultPriority: 200, priority: 1, opt: newOption(1)},
{defaultPriority: 1, priority: 300, opt: newOption(300)},
{defaultPriority: 300, priority: 20, opt: newOption(20)},
}
var cfg libp2p.Config
require.NoError(t, prioritizeOptions(opts)(&cfg))
require.Equal(t, extractNums(&cfg), []int{1, 20, 300})
})
}
4 changes: 2 additions & 2 deletions core/node/libp2p/sec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func Security(enabled bool, tptConfig config.Transports) interface{} {
return func() (opts Libp2pOpts) {
opts.Opts = append(opts.Opts, prioritizeOptions([]priorityOption{{
priority: tptConfig.Security.TLS,
defaultPriority: 100,
defaultPriority: 200,
opt: libp2p.Security(tls.ID, tls.New),
}, {
priority: tptConfig.Security.Noise,
defaultPriority: 300,
defaultPriority: 100,
opt: libp2p.Security(noise.ID, noise.New),
}}))
return opts
Expand Down

0 comments on commit da08ace

Please sign in to comment.