Skip to content

Commit

Permalink
Reserve output space for addresses left to resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Oct 10, 2024
1 parent 343ac75 commit fc2d270
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
34 changes: 34 additions & 0 deletions p2p/net/swarm/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,38 @@ func TestSwarmResolver(t *testing.T) {
require.Equal(t, 1, len(res))
require.Equal(t, "/dnsaddr/255.example.com", res[0].String())
})

t.Run("Test Resolve at output limit", func(t *testing.T) {
recursiveDNSAddr := make(map[string][]string)
recursiveDNSAddr["_dnsaddr.example.com"] = []string{
"dnsaddr=/dnsaddr/0.example.com",
"dnsaddr=/dnsaddr/1.example.com",
"dnsaddr=/dnsaddr/2.example.com",
"dnsaddr=/dnsaddr/3.example.com",
"dnsaddr=/dnsaddr/4.example.com",
"dnsaddr=/dnsaddr/5.example.com",
"dnsaddr=/dnsaddr/6.example.com",
"dnsaddr=/dnsaddr/7.example.com",
"dnsaddr=/dnsaddr/8.example.com",
"dnsaddr=/dnsaddr/9.example.com",
}
recursiveDNSAddr["_dnsaddr.0.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.1.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.2.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.3.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.4.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.5.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.6.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.7.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.8.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
recursiveDNSAddr["_dnsaddr.9.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"}
mockResolver.TXT = recursiveDNSAddr

res, err = swarmResolver.ResolveDNSAddr(ctx, "", multiaddr.StringCast("/dnsaddr/example.com"), 256, 10)
require.NoError(t, err)
require.Equal(t, 10, len(res))
for _, r := range res {
require.Equal(t, "/ip4/127.0.0.1", r.String())
}
})
}
12 changes: 10 additions & 2 deletions p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,16 @@ func (r ResolverFromMaDNS) ResolveDNSAddr(ctx context.Context, expectedPeerID pe
}
}

for _, addr := range toResolve {
resolvedAddrs, err := r.ResolveDNSAddr(ctx, expectedPeerID, addr, recursionLimit-1, outputLimit-len(resolved))
for i, addr := range toResolve {
// Set the nextOutputLimit to:
// outputLimit
// - len(resolved) // What we already have resolved
// - (len(toResolve) - i) // How many addresses we have left to resolve
// + 1 // The current address we are resolving
// This assumes that each DNSADDR address will resolve to at least one multiaddr.
// This assumption lets us bound the space we reserve for resolving.
nextOutputLimit := outputLimit - len(resolved) - (len(toResolve) - i) + 1
resolvedAddrs, err := r.ResolveDNSAddr(ctx, expectedPeerID, addr, recursionLimit-1, nextOutputLimit)
if err != nil {
log.Warnf("failed to resolve dnsaddr %v %s: ", addr, err)
// Dropping this address
Expand Down

0 comments on commit fc2d270

Please sign in to comment.