-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: swarm: refactor address resolution #2990
Changes from 3 commits
4094280
efd1e0e
88d958b
343ac75
fc2d270
972275f
53ffa3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ import ( | |
"github.com/prometheus/client_golang/prometheus" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
madns "github.com/multiformats/go-multiaddr-dns" | ||
"go.uber.org/fx" | ||
) | ||
|
||
|
@@ -495,7 +494,7 @@ func UserAgent(userAgent string) Option { | |
} | ||
|
||
// MultiaddrResolver sets the libp2p dns resolver | ||
func MultiaddrResolver(rslv *madns.Resolver) Option { | ||
func MultiaddrResolver(rslv swarm.MultiaddrDNSResolver) Option { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do want to export the new interface, I prefer just exporting the Swarm Option directly. Swarm options can be used directly with the top level libp2p option Otherwise I'd prefer the name be changed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now only the swarm uses this. No strong opinion on whether it should be a swarm opt or libp2p opt. It feels general enough to be a libp2p opt. |
||
return func(cfg *Config) error { | ||
cfg.MultiaddrResolver = rslv | ||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package swarm | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/multiformats/go-multiaddr" | ||
madns "github.com/multiformats/go-multiaddr-dns" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSwarmResolver(t *testing.T) { | ||
mockResolver := madns.MockResolver{IP: make(map[string][]net.IPAddr)} | ||
ipaddr, err := net.ResolveIPAddr("ip4", "127.0.0.1") | ||
require.NoError(t, err) | ||
mockResolver.IP["example.com"] = []net.IPAddr{*ipaddr} | ||
mockResolver.TXT = map[string][]string{ | ||
"_dnsaddr.example.com": {"dnsaddr=/ip4/127.0.0.1"}, | ||
} | ||
madnsResolver, err := madns.NewResolver(madns.WithDomainResolver("example.com", &mockResolver)) | ||
require.NoError(t, err) | ||
swarmResolver := ResolverFromMaDNS{madnsResolver} | ||
|
||
ctx := context.Background() | ||
res, err := swarmResolver.ResolveDNSComponent(ctx, multiaddr.StringCast("/dns/example.com"), 10) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(res)) | ||
require.Equal(t, "/ip4/127.0.0.1", res[0].String()) | ||
|
||
res, err = swarmResolver.ResolveDNSAddr(ctx, "", multiaddr.StringCast("/dnsaddr/example.com"), 1, 10) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(res)) | ||
require.Equal(t, "/ip4/127.0.0.1", res[0].String()) | ||
|
||
t.Run("Test Limits", func(t *testing.T) { | ||
var ipaddrs []net.IPAddr | ||
var manyDNSAddrs []string | ||
for i := 0; i < 255; i++ { | ||
ip := "1.2.3." + strconv.Itoa(i) | ||
ipaddrs = append(ipaddrs, net.IPAddr{IP: net.ParseIP(ip)}) | ||
manyDNSAddrs = append(manyDNSAddrs, "dnsaddr=/ip4/"+ip) | ||
} | ||
|
||
mockResolver.IP = map[string][]net.IPAddr{ | ||
"example.com": ipaddrs, | ||
} | ||
mockResolver.TXT = map[string][]string{ | ||
"_dnsaddr.example.com": manyDNSAddrs, | ||
} | ||
|
||
res, err := swarmResolver.ResolveDNSComponent(ctx, multiaddr.StringCast("/dns/example.com"), 10) | ||
require.NoError(t, err) | ||
require.Equal(t, 10, len(res)) | ||
for i := 0; i < 10; i++ { | ||
require.Equal(t, "/ip4/1.2.3."+strconv.Itoa(i), res[i].String()) | ||
} | ||
|
||
res, err = swarmResolver.ResolveDNSAddr(ctx, "", multiaddr.StringCast("/dnsaddr/example.com"), 1, 10) | ||
require.NoError(t, err) | ||
require.Equal(t, 10, len(res)) | ||
for i := 0; i < 10; i++ { | ||
require.Equal(t, "/ip4/1.2.3."+strconv.Itoa(i), res[i].String()) | ||
} | ||
}) | ||
|
||
t.Run("Test Recursive Limits", func(t *testing.T) { | ||
recursiveDNSAddr := make(map[string][]string) | ||
for i := 0; i < 255; i++ { | ||
recursiveDNSAddr["_dnsaddr."+strconv.Itoa(i)+".example.com"] = []string{"dnsaddr=/dnsaddr/" + strconv.Itoa(i+1) + ".example.com"} | ||
} | ||
recursiveDNSAddr["_dnsaddr.255.example.com"] = []string{"dnsaddr=/ip4/127.0.0.1"} | ||
mockResolver.TXT = recursiveDNSAddr | ||
|
||
res, err = swarmResolver.ResolveDNSAddr(ctx, "", multiaddr.StringCast("/dnsaddr/0.example.com"), 256, 10) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(res)) | ||
require.Equal(t, "/ip4/127.0.0.1", res[0].String()) | ||
|
||
res, err = swarmResolver.ResolveDNSAddr(ctx, "", multiaddr.StringCast("/dnsaddr/0.example.com"), 255, 10) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(res)) | ||
require.Equal(t, "/dnsaddr/255.example.com", res[0].String()) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this? We can wrap the provided DNS to this swarm interface when constructing the swarm.