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: swarm: refactor address resolution #2990

Merged
merged 7 commits into from
Oct 16, 2024
Merged

Conversation

MarcoPolo
Copy link
Collaborator

@MarcoPolo MarcoPolo commented Oct 1, 2024

Fixes an issue around p2p-circuit multiaddrs and DNS resolution.

@MarcoPolo MarcoPolo marked this pull request as draft October 1, 2024 21:38
@MarcoPolo MarcoPolo force-pushed the marco/filter-dial-addrs branch 3 times, most recently from 16c1688 to 99d2253 Compare October 1, 2024 23:25
@MarcoPolo MarcoPolo changed the title fix: Skip addresses with many dns components fix: Better limits around address resolution Oct 1, 2024
@MarcoPolo MarcoPolo marked this pull request as ready for review October 1, 2024 23:26
p2p/net/swarm/swarm_dial.go Outdated Show resolved Hide resolved
p2p/net/swarm/swarm_dial.go Outdated Show resolved Hide resolved
p2p/net/swarm/swarm_dial.go Outdated Show resolved Hide resolved
p2p/net/swarm/swarm_dial.go Outdated Show resolved Hide resolved
p2p/net/swarm/swarm_dial.go Outdated Show resolved Hide resolved
p2p/net/swarm/swarm_dial.go Outdated Show resolved Hide resolved
@MarcoPolo
Copy link
Collaborator Author

I'm going to refactor this function and clean up things around here. As a side effect, I think we won't need #2989 anymore.

@MarcoPolo MarcoPolo marked this pull request as draft October 3, 2024 03:41
@MarcoPolo MarcoPolo marked this pull request as ready for review October 7, 2024 21:33
@MarcoPolo MarcoPolo requested a review from sukunrt October 7, 2024 21:33
@MarcoPolo
Copy link
Collaborator Author

@sukunrt this is ready now. Hopefully it's a lot clearer

@MarcoPolo MarcoPolo changed the title fix: Better limits around address resolution fix: Refactor swarm address resolution Oct 7, 2024
@MarcoPolo MarcoPolo changed the title fix: Refactor swarm address resolution fix: swarm: refactor address resolution Oct 7, 2024
@MarcoPolo MarcoPolo mentioned this pull request Oct 9, 2024
30 tasks
if recursionLimit <= 0 {
return []ma.Multiaddr{maddr}, nil
}
var resolved, toResolve []ma.Multiaddr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create these just before using them a couple of lines below and avoid allocations by providing capacity = len(addrs)

p2p/net/swarm/swarm.go Outdated Show resolved Hide resolved
options.go Outdated
Comment on lines 496 to 497
// MultiaddrResolver sets the libp2p dns resolver
func MultiaddrResolver(rslv *madns.Resolver) Option {
func MultiaddrResolver(rslv swarm.MultiaddrDNSResolver) Option {
Copy link
Member

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.

options.go Outdated
Comment on lines 496 to 497
// MultiaddrResolver sets the libp2p dns resolver
func MultiaddrResolver(rslv *madns.Resolver) Option {
func MultiaddrResolver(rslv swarm.MultiaddrDNSResolver) Option {
Copy link
Member

Choose a reason for hiding this comment

The 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 SwarmOpts.

Otherwise I'd prefer the name be changed to MultiaddrDNSResolver or just DNSResolver

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Comment on lines 904 to 907
for _, addr := range toResolve {
resolvedAddrs, err := r.ResolveDNSAddr(ctx, expectedPeerID, addr, recursionLimit-1, outputLimit-len(resolved))
if err != nil {
log.Warnf("failed to resolve dnsaddr %v %s: ", addr, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a potential exponential case here?

Consider a dnsaddr that resolves to 10 dnsaddrs. Each of these 10 dnsaddrs resolve to 10 dnsaddrs and so on.

outputLimit-len(resolved) will be 10 because all of these are dnsaddrs and len(resolved) will be 0. So we call ResolveDNSAddrs on these 10 addrs and then call it again with outputLimit 10 for each of these and so on.

p2p/net/swarm/swarm.go Show resolved Hide resolved
p2p/net/swarm/swarm.go Show resolved Hide resolved
* Rebase on top of resolveAddrs refactor

* Add comments

* Sanitize address inputs when returning a reservation message (#3006)
@MarcoPolo MarcoPolo merged commit e8b6685 into master Oct 16, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants