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(kad): expose a kad query facility allowing dynamic num_results #5555

Merged

Conversation

maqi
Copy link
Contributor

@maqi maqi commented Aug 13, 2024

Description

This PR is to expose a kad query facility that allowing specify num_results dynamically.
It is related to the Sybil Defence issue,
that during the attempt of implementation on higher level code, it is find will be useful if libp2p-kad can expose such facility.

The PR try not to cause any interference to the existing work flow, only introduce an extra exposal.

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

@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from 7b9904a to 769f893 Compare August 13, 2024 10:38
Copy link
Contributor

@guillaumemichel guillaumemichel left a comment

Choose a reason for hiding this comment

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

Thanks @maqi for your contribution, this change is indeed needed! I left a few remarks.

protocols/kad/src/query.rs Outdated Show resolved Hide resolved
protocols/kad/src/query.rs Outdated Show resolved Hide resolved
protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
Copy link
Contributor

mergify bot commented Aug 14, 2024

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

@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from 769f893 to c0a8cc0 Compare August 14, 2024 15:15
@maqi
Copy link
Contributor Author

maqi commented Aug 14, 2024

@guillaumemichel PR updated with using your suggested approach of adding num_results into QueryInfo::GetClosestPeers, to avoid introduce extra functions.

also replied to your another comment regarding the with_config idea, which I think better not to be included in this PR, if really needed ?

btw. any idea why there is a failing CI https://github.com/libp2p/rust-libp2p/actions/runs/10390288352/job/28770402300?pr=5555 complaining failure enum_struct_variant_field_added: pub enum struct variant field added ?
and how to resolve it? thx

@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from c0a8cc0 to 4180ee8 Compare August 20, 2024 09:54
@maqi
Copy link
Contributor Author

maqi commented Aug 20, 2024

seems the failing CI requires a major version up on libp2p-kad, as a new variant added into the struct.

Though I think it is not necessary, I have to increase the libp2p-kad version to 0.47.0, to get an ALL green CI.

@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from 4180ee8 to 76a05d4 Compare August 20, 2024 12:39
@maqi
Copy link
Contributor Author

maqi commented Aug 20, 2024

Hi, @guillaumemichel

thx for the review.
PR updated according to your new comments.

@guillaumemichel
Copy link
Contributor

Thanks @maqi, it looks good. It would be great to add a test for the cases where num_result is higher and lower than K_VALUE.

@maqi
Copy link
Contributor Author

maqi commented Aug 21, 2024

Hi, @guillaumemichel

I have updated the PR to add a test covering the num_results range of K_VALUE / 2 .. K_VALUE * 2

However, it shows the returned result is capped after exceeds K_VALUE .
A quick read into the code shows it might needs a deeper refactor of the code base.

However, for us, a capped result is acceptable, hence I only put the test according to the current impl.
And put some comments to explain the situation.

I think to support results not capped , it will be better to have it as a separate PR ?

@guillaumemichel
Copy link
Contributor

@maqi thanks for adding the tests!

However, for us, a capped result is acceptable.

What is your use case? If you need to pass a num_results to receive less peers than K_VALUE you might as well just take a subset of the returned peers with the default get_closest_peers. The feature seems incomplete if it is not possible to increase the number of returned peers to match num_results and I am afraid it will be confusing for users.

@maqi
Copy link
Contributor Author

maqi commented Aug 22, 2024

Hi, @guillaumemichel

For our case, we need to set the replication_factor to a value lower than the K_VALUE, but still want the query of get_closest gives a definable num of results ranging from that replication_factor to K_VALUE.

Or, you mean the get_closest will gives K_VALUE results anyway (as long as there are enough nodes in the network) ?
but I see the code of set num_results to replication_factor ?

Copy link
Contributor

@guillaumemichel guillaumemichel left a comment

Choose a reason for hiding this comment

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

Alright, I see! A high replication_factor or num_results is never expected to return more than K_VALUE results anyway.

IMO it is fine, because it was never possible to have more than K_VALUE closest peers in response, this change simply allows users to define a number of expected closest peers between 1 and K_VALUE. Previously, the number of returned closest peers was set to replication_factor.

Removing the cap might be tricky, since it would involve forging a new key and additional requests. It can be left to a follow up PR 👍🏻

protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from d4b2a3e to b0538af Compare August 22, 2024 16:48
@maqi
Copy link
Contributor Author

maqi commented Aug 22, 2024

Hi, @guillaumemichel ,

thx for the summary comment.
PR updated according to your new comment and added some of your word as code comment

@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from b0538af to 7b5b952 Compare August 22, 2024 18:29
@guillaumemichel
Copy link
Contributor

Sorry for the back-and-forth, but it would be good to have different values of replication_factor in the test. K_VALUE (default) and a lower value (e.g K_VALUE / 2) should be enough.

@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch 2 times, most recently from 150c8e1 to 4acdf2c Compare August 23, 2024 10:23
@maqi
Copy link
Contributor Author

maqi commented Aug 23, 2024

Hi, @guillaumemichel ,

updated the test according to your suggestion.

@guillaumemichel guillaumemichel changed the title chore(kad): expose a kad query facility allowing dynamic num_results feat(kad): expose a kad query facility allowing dynamic num_results Aug 23, 2024
Copy link
Contributor

@guillaumemichel guillaumemichel 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 your contribution @maqi, it looks good to me!

@jxs could you also have a look before we merge?

Copy link
Contributor

mergify bot commented Aug 27, 2024

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

@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from 4acdf2c to 6fbda80 Compare August 27, 2024 11:57
@maqi
Copy link
Contributor Author

maqi commented Aug 27, 2024

Hi, @guillaumemichel ,

may I know the schedule of when the PR will get the second approval to be merged pls?
trying to resolve the conflicts contstantly is little bit annoying :)

Copy link
Member

@jxs jxs left a comment

Choose a reason for hiding this comment

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

Hi, and thanks for the interest! Left some comments

protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
protocols/kad/src/behaviour/test.rs Outdated Show resolved Hide resolved
protocols/kad/src/behaviour/test.rs Show resolved Hide resolved
protocols/kad/src/behaviour.rs Outdated Show resolved Hide resolved
@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from 6fbda80 to c540da9 Compare August 27, 2024 14:38
@maqi maqi force-pushed the expose_dynamic_num_results_get_closest_peers branch from c540da9 to 1bf218b Compare August 27, 2024 14:40
@maqi
Copy link
Contributor Author

maqi commented Aug 27, 2024

Hi, @jxs ,

Thx for the review.
PR updated according to your comments.

Replied to one comment, as I'm leading toward not to inline the code block.
Let me know if I have to, thx.

Copy link
Member

@jxs jxs left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@jxs jxs added the send-it label Aug 27, 2024
@mergify mergify bot merged commit 56b6c62 into libp2p:master Aug 27, 2024
72 checks passed
TimTinkers pushed a commit to unattended-backpack/rust-libp2p that referenced this pull request Sep 14, 2024
…ibp2p#5555)

## Description

This PR is to expose a kad query facility that allowing specify
num_results dynamically.
It is related to the [Sybil Defence
issue](libp2p#4769),
that during the attempt of implementation on higher level code, it is
find will be useful if libp2p-kad can expose such facility.

The PR try not to cause any interference to the existing work flow, only
introduce an `extra exposal`.

## Change checklist

<!-- Please add a Changelog entry in the appropriate crates and bump the
crate versions if needed. See
<https://github.com/libp2p/rust-libp2p/blob/master/docs/release.md#development-between-releases>-->

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

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants