Skip to content

Commit e5b3392

Browse files
authored
fix: correct list of addresses for delegated routing (#9920)
1 parent 50daf64 commit e5b3392

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

cmd/ipfs/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
429429
cfg.Routing.Routers,
430430
cfg.Routing.Methods,
431431
cfg.Identity.PeerID,
432-
cfg.Addresses.Swarm,
432+
cfg.Addresses,
433433
cfg.Identity.PrivKey,
434434
)
435435
default:

core/core_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func GetNode(t *testing.T, reframeURLs ...string) *IpfsNode {
256256
cfg.Routing.Routers,
257257
cfg.Routing.Methods,
258258
cfg.Identity.PeerID,
259-
cfg.Addresses.Swarm,
259+
cfg.Addresses,
260260
cfg.Identity.PrivKey,
261261
),
262262
},

core/node/libp2p/routingopt.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func constructDefaultHTTPRouters(cfg *config.Config) ([]*routinghelpers.Parallel
4747
var routers []*routinghelpers.ParallelRouter
4848
// Append HTTP routers for additional speed
4949
for _, endpoint := range defaultHTTPRouters {
50-
httpRouter, err := irouting.ConstructHTTPRouter(endpoint, cfg.Identity.PeerID, cfg.Addresses.Swarm, cfg.Identity.PrivKey)
50+
httpRouter, err := irouting.ConstructHTTPRouter(endpoint, cfg.Identity.PeerID, httpAddrsFromConfig(cfg.Addresses), cfg.Identity.PrivKey)
5151
if err != nil {
5252
return nil, err
5353
}
@@ -123,7 +123,7 @@ func constructDHTRouting(mode dht.ModeOpt) RoutingOption {
123123
}
124124

125125
// ConstructDelegatedRouting is used when Routing.Type = "custom"
126-
func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, peerID string, addrs []string, privKey string) RoutingOption {
126+
func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, peerID string, addrs config.Addresses, privKey string) RoutingOption {
127127
return func(args RoutingOptionArgs) (routing.Routing, error) {
128128
return irouting.Parse(routers, methods,
129129
&irouting.ExtraDHTParams{
@@ -135,7 +135,7 @@ func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, p
135135
},
136136
&irouting.ExtraHTTPParams{
137137
PeerID: peerID,
138-
Addrs: addrs,
138+
Addrs: httpAddrsFromConfig(addrs),
139139
PrivKeyB64: privKey,
140140
})
141141
}
@@ -151,3 +151,31 @@ var (
151151
DHTServerOption = constructDHTRouting(dht.ModeServer)
152152
NilRouterOption = constructNilRouting
153153
)
154+
155+
// httpAddrsFromConfig creates a list of addresses from the provided configuration to be used by HTTP delegated routers.
156+
func httpAddrsFromConfig(cfgAddrs config.Addresses) []string {
157+
// Swarm addrs are announced by default
158+
addrs := cfgAddrs.Swarm
159+
// if Announce addrs are specified - override Swarm
160+
if len(cfgAddrs.Announce) > 0 {
161+
addrs = cfgAddrs.Announce
162+
} else if len(cfgAddrs.NoAnnounce) > 0 {
163+
// if Announce adds are not specified - filter Swarm addrs with NoAnnounce list
164+
maddrs := map[string]struct{}{}
165+
for _, addr := range addrs {
166+
maddrs[addr] = struct{}{}
167+
}
168+
for _, addr := range cfgAddrs.NoAnnounce {
169+
delete(maddrs, addr)
170+
}
171+
addrs = make([]string, 0, len(maddrs))
172+
for k := range maddrs {
173+
addrs = append(addrs, k)
174+
}
175+
}
176+
// append AppendAnnounce addrs to the result list
177+
if len(cfgAddrs.AppendAnnounce) > 0 {
178+
addrs = append(addrs, cfgAddrs.AppendAnnounce...)
179+
}
180+
return addrs
181+
}

core/node/libp2p/routingopt_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package libp2p
2+
3+
import (
4+
"testing"
5+
6+
config "github.com/ipfs/kubo/config"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestHttpAddrsFromConfig(t *testing.T) {
11+
require.Equal(t, []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
12+
httpAddrsFromConfig(config.Addresses{
13+
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
14+
}), "Swarm addrs should be taken by default")
15+
16+
require.Equal(t, []string{"/ip4/192.168.0.1/tcp/4001"},
17+
httpAddrsFromConfig(config.Addresses{
18+
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
19+
Announce: []string{"/ip4/192.168.0.1/tcp/4001"},
20+
}), "Announce addrs should override Swarm if specified")
21+
22+
require.Equal(t, []string{"/ip4/0.0.0.0/udp/4001/quic"},
23+
httpAddrsFromConfig(config.Addresses{
24+
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
25+
NoAnnounce: []string{"/ip4/0.0.0.0/tcp/4001"},
26+
}), "Swarm addrs should not contain NoAnnounce addrs")
27+
28+
require.Equal(t, []string{"/ip4/192.168.0.1/tcp/4001", "/ip4/192.168.0.2/tcp/4001"},
29+
httpAddrsFromConfig(config.Addresses{
30+
Swarm: []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"},
31+
Announce: []string{"/ip4/192.168.0.1/tcp/4001"},
32+
AppendAnnounce: []string{"/ip4/192.168.0.2/tcp/4001"},
33+
}), "AppendAnnounce addrs should be included if specified")
34+
}

0 commit comments

Comments
 (0)