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

feat(swarm): allow behaviours to share addresses of peers #4371

Merged
merged 28 commits into from
Jan 24, 2024

Conversation

StemCll
Copy link
Contributor

@StemCll StemCll commented Aug 21, 2023

Description

Resolves: #4302.

Notes & open questions

Still WIP

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

@StemCll StemCll changed the title swarm: Add NewExternalAddrOfPeer feat(swarm): Add NewExternalAddrOfPeer Aug 21, 2023
@StemCll StemCll force-pushed the feat/swarm/report_remote_address branch from e435d95 to 711b03b Compare August 21, 2023 21:45
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

This is going in the right direction. Thank you for the work!

protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
@thomaseizinger thomaseizinger changed the title feat(swarm): Add NewExternalAddrOfPeer feat(swarm): allow behaviours to share addresses of peers Sep 12, 2023
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Great progress! I've left a few comments :)

What I'd also like to see in this PR is:

  • mDNS emitting ToSwarm::NewExternalAddrOfPeer
  • identify emitting ToSwarm::NewExternalAddrOfPeer for the self-advertised listen addresses of a peer
  • the rendezvous client emitting ToSwarm::NewExternalAddrOfPeer whenever it receives a peer record

Something that we should discuss is whether we want to add a public function to Swarm that allows users to trigger this event without having to call particular behaviour. For example, at the moment, libp2p-request-response offers a function add_address to populate an internal map of peer addresses. I'd like that function to go away because I think it is none of libp2p-request-responses business to do address management.

If we were to add a function to Swarm that triggers this event, we could deprecate the function in libp2p-request-response and instead add an internal cache that stores peer addresses. To avoid duplicated code across multiple behaviours, we could add a PeerAddresses to libp2p-swarm similar to how we have ExternalAddresses.

I don't think all of this has to be done in one PR but what we do should be consistent. For example, I think it would be to merge this whilst also deprecating libp2p-request-response's add_address function to point to the new Swarm function instead. We can always introduce the PeerAddresses struct at a later point because it is a backwards-compatible change.

protocols/autonat/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/identify/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/request-response/src/lib.rs Outdated Show resolved Hide resolved
swarm-derive/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/kad/src/behaviour/test.rs Outdated Show resolved Hide resolved
@thomaseizinger
Copy link
Contributor

For example, at the moment, libp2p-request-response offers a function add_address to populate an internal map of peer addresses. I'd like that function to go away because I think it is none of libp2p-request-responses business to do address management.

The same actually applies to Kademlia::add_address. This function could be made crate-private in favour of the new function on Swarm.

@StemCll StemCll force-pushed the feat/swarm/report_remote_address branch from 18ba25d to 0b292c0 Compare September 17, 2023 08:01
@thomaseizinger
Copy link
Contributor

Something that we should discuss is whether we want to add a public function to Swarm that allows users to trigger this event without having to call particular behaviour. For example, at the moment, libp2p-request-response offers a function add_address to populate an internal map of peer addresses. I'd like that function to go away because I think it is none of libp2p-request-responses business to do address management.

If we were to add a function to Swarm that triggers this event, we could deprecate the function in libp2p-request-response and instead add an internal cache that stores peer addresses. To avoid duplicated code across multiple behaviours, we could add a PeerAddresses to libp2p-swarm similar to how we have ExternalAddresses.

I don't think all of this has to be done in one PR but what we do should be consistent. For example, I think it would be to merge this whilst also deprecating libp2p-request-response's add_address function to point to the new Swarm function instead. We can always introduce the PeerAddresses struct at a later point because it is a backwards-compatible change.

@mxinden Let me know what you think of this.

swarm/src/behaviour.rs Outdated Show resolved Hide resolved
swarm-derive/CHANGELOG.md Outdated Show resolved Hide resolved
swarm-derive/CHANGELOG.md Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
@mxinden
Copy link
Member

mxinden commented Sep 17, 2023

Something that we should discuss is whether we want to add a public function to Swarm that allows users to trigger this event without having to call particular behaviour. For example, at the moment, libp2p-request-response offers a function add_address to populate an internal map of peer addresses. I'd like that function to go away because I think it is none of libp2p-request-responses business to do address management.
If we were to add a function to Swarm that triggers this event, we could deprecate the function in libp2p-request-response and instead add an internal cache that stores peer addresses. To avoid duplicated code across multiple behaviours, we could add a PeerAddresses to libp2p-swarm similar to how we have ExternalAddresses.
I don't think all of this has to be done in one PR but what we do should be consistent. For example, I think it would be to merge this whilst also deprecating libp2p-request-response's add_address function to point to the new Swarm function instead. We can always introduce the PeerAddresses struct at a later point because it is a backwards-compatible change.

@mxinden Let me know what you think of this.

I am in favor of the additional method on Swarm. I am as well fine for that to happen in a follow-up pull request.

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

As always, appreciate the thorough work @StemCll!

swarm/src/behaviour.rs Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
@thomaseizinger
Copy link
Contributor

thomaseizinger commented Sep 17, 2023

Something that we should discuss is whether we want to add a public function to Swarm that allows users to trigger this event without having to call particular behaviour. For example, at the moment, libp2p-request-response offers a function add_address to populate an internal map of peer addresses. I'd like that function to go away because I think it is none of libp2p-request-responses business to do address management.
If we were to add a function to Swarm that triggers this event, we could deprecate the function in libp2p-request-response and instead add an internal cache that stores peer addresses. To avoid duplicated code across multiple behaviours, we could add a PeerAddresses to libp2p-swarm similar to how we have ExternalAddresses.
I don't think all of this has to be done in one PR but what we do should be consistent. For example, I think it would be to merge this whilst also deprecating libp2p-request-response's add_address function to point to the new Swarm function instead. We can always introduce the PeerAddresses struct at a later point because it is a backwards-compatible change.

@mxinden Let me know what you think of this.

I am in favor of the additional method on Swarm. I am as well fine for that to happen in a follow-up pull request.

I am wondering what a good scope is for this PR. It is not very big at this stage. Given that it is a breaking change, perhaps we should at least use it "end-to-end":

  1. Have at least 1 behaviour emit this event (perhaps identify?)
  2. Have at least 1 behaviour consume it, most likely kademlia
  3. Add the function to Swarm
  4. Deprecate at least 1 of the existing add_address functions and use the new Swarm method everywhere instead. This will have to be the same behaviour as in (2).

If that works, we can write follow-up issues to "transfer" the design in this PR to other behaviours.

Thoughts @StemCll @mxinden ?

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the continued work!

I made some more comments :)
Plus, I'd still like to see the Swarm::add_address_of_peer function and deprecate Kademlia::add_address with a note pointing to the other function. In the future, we will then make that private.

protocols/identify/src/behaviour.rs Outdated Show resolved Hide resolved
swarm-derive/CHANGELOG.md Outdated Show resolved Hide resolved
swarm/CHANGELOG.md Outdated Show resolved Hide resolved
protocols/identify/Cargo.toml Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
swarm/src/behaviour.rs Outdated Show resolved Hide resolved
@thomaseizinger
Copy link
Contributor

We will also need to update the docs here: https://github.com/libp2p/rust-libp2p/blob/master/protocols/kad/src/lib.rs#L21-L34

@thomaseizinger thomaseizinger added this to the v0.53.0 milestone Sep 28, 2023
@mergify
Copy link
Contributor

mergify bot commented Sep 28, 2023

This pull request has merge conflicts. Could you please resolve them @StemCll? 🙏

protocols/identify/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/identify/src/behaviour.rs Outdated Show resolved Hide resolved
@thomaseizinger
Copy link
Contributor

I am removing this from the next milestone because we can hopefully ship it as a non-breaking change once #4581 lands.

@thomaseizinger thomaseizinger removed this from the v0.53.0 milestone Oct 24, 2023
@dhuseby dhuseby mentioned this pull request Nov 2, 2023
@StemCll StemCll force-pushed the feat/swarm/report_remote_address branch 3 times, most recently from 3cdcfac to 1704ee9 Compare November 28, 2023 18:21
@StemCll
Copy link
Contributor Author

StemCll commented Nov 29, 2023

hi @thomaseizinger @mxinden 👋
Had to rework the PR because of the multiple conflicts with the newest master

@StemCll StemCll requested a review from thomaseizinger January 1, 2024 22:22
@thomaseizinger
Copy link
Contributor

I have limited availability the next weeks, will re-review mid-January :)

Copy link
Contributor

mergify bot commented Jan 5, 2024

This pull request has merge conflicts. Could you please resolve them @StemCll? 🙏

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thanks! Sorry for the delay on this.

Let me know if you are able to work in a few more comments, otherwise I'll finalize this at some point in the next two weeks.

protocols/identify/src/behaviour.rs Show resolved Hide resolved
protocols/request-response/src/lib.rs Show resolved Hide resolved
swarm/src/behaviour/peer_addresses.rs Outdated Show resolved Hide resolved
swarm/src/behaviour/peer_addresses.rs Outdated Show resolved Hide resolved
@StemCll
Copy link
Contributor Author

StemCll commented Jan 16, 2024

Thanks! Sorry for the delay on this.

Let me know if you are able to work in a few more comments, otherwise I'll finalize this at some point in the next two weeks.

Hi @thomaseizinger. No worries at all and thank you for getting back to this 😄. I will add these changes in a day or two.

thomaseizinger
thomaseizinger previously approved these changes Jan 20, 2024
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Awesome! Thank you so much for this contribution and working in all my feedback :)

I've made a note for myself to add another test, will merge that in the upcoming days!

protocols/identify/src/behaviour.rs Outdated Show resolved Hide resolved
This allows us to remove the `put` function and we can now safely
add all addresses individually to the cache. With the addition of
the `ToSwarm::NewExternalAddrOfPeer` event, the safeguard of
overwriting all addresses in the peer cache was no longer useful
because any other behaviour would have to apply this as well.

As such, we needed to build this safe-guard directly _into_ `PeerAddresses`.

This also makes the added test pass. Previously, the events would
never get emitted because after calling `.put`, the addresses were
already in the cache and the `add`-function always returned `false`.
@thomaseizinger
Copy link
Contributor

The test didn't initially pass so I had to make some more changes to the implementation.

@mergify mergify bot dismissed thomaseizinger’s stale review January 21, 2024 06:16

Approvals have been dismissed because the PR was updated after the send-it label was applied.

@mergify mergify bot merged commit c48a16a into libp2p:master Jan 24, 2024
72 of 73 checks passed
umgefahren pushed a commit to umgefahren/rust-libp2p that referenced this pull request Mar 8, 2024
guillaumemichel pushed a commit that referenced this pull request Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal-change Pull requests that make internal changes to crates and thus don't need to include a changelog entry. send-it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(swarm): allow NetworkBehaviour to report remote address
3 participants