Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct list of addresses for delegated routing #9920

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
cfg.Routing.Routers,
cfg.Routing.Methods,
cfg.Identity.PeerID,
cfg.Addresses.Swarm,
cfg.Addresses,
cfg.Identity.PrivKey,
)
default:
Expand Down
2 changes: 1 addition & 1 deletion core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func GetNode(t *testing.T, reframeURLs ...string) *IpfsNode {
cfg.Routing.Routers,
cfg.Routing.Methods,
cfg.Identity.PeerID,
cfg.Addresses.Swarm,
cfg.Addresses,
cfg.Identity.PrivKey,
),
},
Expand Down
34 changes: 31 additions & 3 deletions core/node/libp2p/routingopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func constructDefaultHTTPRouters(cfg *config.Config) ([]*routinghelpers.Parallel
var routers []*routinghelpers.ParallelRouter
// Append HTTP routers for additional speed
for _, endpoint := range defaultHTTPRouters {
httpRouter, err := irouting.ConstructHTTPRouter(endpoint, cfg.Identity.PeerID, cfg.Addresses.Swarm, cfg.Identity.PrivKey)
httpRouter, err := irouting.ConstructHTTPRouter(endpoint, cfg.Identity.PeerID, httpAddrsFromConfig(cfg.Addresses), cfg.Identity.PrivKey)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func constructDHTRouting(mode dht.ModeOpt) RoutingOption {
}

// ConstructDelegatedRouting is used when Routing.Type = "custom"
func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, peerID string, addrs []string, privKey string) RoutingOption {
func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, peerID string, addrs config.Addresses, privKey string) RoutingOption {
return func(args RoutingOptionArgs) (routing.Routing, error) {
return irouting.Parse(routers, methods,
&irouting.ExtraDHTParams{
Expand All @@ -135,7 +135,7 @@ func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, p
},
&irouting.ExtraHTTPParams{
PeerID: peerID,
Addrs: addrs,
Addrs: httpAddrsFromConfig(addrs),
PrivKeyB64: privKey,
})
}
Expand All @@ -151,3 +151,31 @@ var (
DHTServerOption = constructDHTRouting(dht.ModeServer)
NilRouterOption = constructNilRouting
)

// httpAddrsFromConfig creates a list of addresses from the provided configuration to be used by HTTP delegated routers.
func httpAddrsFromConfig(cfgAddrs config.Addresses) []string {
// Swarm addrs are announced by default
addrs := cfgAddrs.Swarm
// if Announce addrs are specified - override Swarm
if len(cfgAddrs.Announce) > 0 {
addrs = cfgAddrs.Announce
} else if len(cfgAddrs.NoAnnounce) > 0 {
// if Announce adds are not specified - filter Swarm addrs with NoAnnounce list
maddrs := map[string]struct{}{}
for _, addr := range addrs {
maddrs[addr] = struct{}{}
}
for _, addr := range cfgAddrs.NoAnnounce {
delete(maddrs, addr)
}
addrs = make([]string, 0, len(maddrs))
for k := range maddrs {
addrs = append(addrs, k)
}
}
// append AppendAnnounce addrs to the result list
if len(cfgAddrs.AppendAnnounce) > 0 {
addrs = append(addrs, cfgAddrs.AppendAnnounce...)
}
return addrs
}
34 changes: 34 additions & 0 deletions core/node/libp2p/routingopt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package libp2p

import (
"testing"

config "github.com/ipfs/kubo/config"
"github.com/stretchr/testify/require"
)

func TestHttpAddrsFromConfig(t *testing.T) {
require.Equal(t, []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
httpAddrsFromConfig(config.Addresses{
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
}), "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"},
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"},
httpAddrsFromConfig(config.Addresses{
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
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"},
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")
}