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

refactor(kad): make Kademlia::add_address return Result #4280

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

arsenron
Copy link
Contributor

@arsenron arsenron commented Aug 1, 2023

Description

This PR makes a clear separation between successful and unsuccessful result of adding a peer to the DHT. It seems to me that it improves ergonomics of the public API.

Notes & open questions

If you agree on this PR, I will add some tests and update examples.

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

@thomaseizinger thomaseizinger changed the title refactor(kad): Make Kademlia::add_address return Result refactor(kad): make Kademlia::add_address return Result Aug 1, 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.

Thanks! This looks good to me :)

It needs a changelog entry and a version bump. Given that this is a breaking change, we'll hold off on merging this for a bit to allow us to batch breaking changes together. Hence, I'll convert this to a draft so that it doesn't get merged accidentially!

@thomaseizinger thomaseizinger added this to the v0.53.0 milestone Aug 1, 2023
@thomaseizinger thomaseizinger marked this pull request as draft August 1, 2023 12:56
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.

I am in favor of the change. Thanks.

@thomaseizinger
Copy link
Contributor

@arsenron Can you fix the CI errors please? We are thinking of cutting a new (breaking) release and thus would like all the queued PRs to be ready for merge :)

@mergify
Copy link
Contributor

mergify bot commented Sep 21, 2023

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

@arsenron
Copy link
Contributor Author

@arsenron Can you fix the CI errors please? We are thinking of cutting a new (breaking) release and thus would like all the queued PRs to be ready for merge :)

Sure! I will do it today.

@arsenron
Copy link
Contributor Author

@thomaseizinger Done. But just cargo-deny fails, but it is unrelevant to the updates made by this PR, so I skipped it.

@thomaseizinger
Copy link
Contributor

@thomaseizinger Done. But just cargo-deny fails, but it is unrelevant to the updates made by this PR, so I skipped it.

Great! Yeah good call on that one, we already have a PR queued to fix that :)

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.

I noticed an overlap with an upcoming feature that I think we have to discuss before moving forward here. Sorry for the late notice!

Comment on lines +527 to +531
pub fn add_address(
&mut self,
peer: &PeerId,
address: Multiaddr,
) -> Result<RoutingUpdateOk, RoutingUpdateError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry that this is only coming up now but I just realized that we have a feature coming up that conflicts with this idea. It is here: #4371

This function will likely be deprecated in favor of a general add_address_of_peer function on Swarm. This means, we won't be able to return a specific error there!

In fact, we won't be able to return anything at all.

Thus, I think we should actually change this PR into preparing this API for the above change. Here is what I'd suggest but I am interested to hear your and @mxinden's opinion:

  1. Mark RoutingUpdate as deprecated but retain the current functionality.
  2. Always emit KademliaEvent::RoutingUpdated, even for the immediate RoutingUpdate::Success case
  3. Add a debug_assert to the SelfEntry case. If we attempt to add ourselves to the routing table, this is a programmer bug and should not happen. We can ignore that in production (but should probably emit a fat warn!), hence the debug_assert.
  4. We already log a warning for the bucket-full case so we can just leave it at that.

This makes this a backwards-compatible change and we can land it right away.

Copy link
Member

Choose a reason for hiding this comment

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

I missed this as well. I am sorry @arsenron.

Suggestion overall sounds good to me.

Add a debug_assert to the SelfEntry case. If we attempt to add ourselves to the routing table, this is a programmer bug and should not happen. We can ignore that in production (but should probably emit a fat warn!), hence the debug_assert.

Panicing on invalid user input seems rather drastic. Wouldn't a warn! suffice in all cases? Also, not as part of this pull request, we should handle the SelfEntry on the libp2p-swarm level. We should at least log when a NetworkBehaviour or user (via Swarm::add_address_of_peer) wants to report a an external address of a remote peer where that peer is actually the local peer.

Copy link
Contributor

Choose a reason for hiding this comment

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

I missed this as well. I am sorry @arsenron.

Suggestion overall sounds good to me.

Add a debug_assert to the SelfEntry case. If we attempt to add ourselves to the routing table, this is a programmer bug and should not happen. We can ignore that in production (but should probably emit a fat warn!), hence the debug_assert.

Panicing on invalid user input seems rather drastic. Wouldn't a warn! suffice in all cases?

The warn will be there in all cases but pancking in debug builds is fine, no? You didn't validate your inputs correctly in that case.

Also, not as part of this pull request, we should handle the SelfEntry on the libp2p-swarm level. We should at least log when a NetworkBehaviour or user (via Swarm::add_address_of_peer) wants to report a an external address of a remote peer where that peer is actually the local peer.

True, as part of #4371, we should probably not forward any of these events! cc @StemCll

Copy link
Contributor

Choose a reason for hiding this comment

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

@arsenron I fixed the merge conflicts for you. Are you still interested in working on this given the above insight?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@thomaseizinger sorry, little busy now, but I will check it a bit later

@mergify
Copy link
Contributor

mergify bot commented Sep 22, 2023

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

@libp2p libp2p deleted a comment from mxinden Sep 22, 2023
@mergify
Copy link
Contributor

mergify bot commented Oct 20, 2023

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

@mergify
Copy link
Contributor

mergify bot commented Oct 20, 2023

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

@thomaseizinger
Copy link
Contributor

Removing this from the milestone because the proposed way forward is backwards-compatible: https://github.com/libp2p/rust-libp2p/pull/4280/files#r1333693065.

@thomaseizinger thomaseizinger removed this from the v0.53.0 milestone Oct 24, 2023
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.

4 participants