feat(kad): allow to explicitly set Mode::{Client,Server}#4132
feat(kad): allow to explicitly set Mode::{Client,Server}#4132mergify[bot] merged 33 commits intolibp2p:masterfrom
Mode::{Client,Server}#4132Conversation
thomaseizinger
left a comment
There was a problem hiding this comment.
Thanks for opening this PR @dariusc93 !
I realised that the issue with Option<Mode> is that we can't tell whether we should reconfigure the mode automatically. Thus my suggestion would be:
- Delete
ExplicitMode - Introduce a new boolean field:
auto_mode, defaulting totrue - Make the signature of
set_explicit_modeOption<None> - If an explicit mode is set, set
auto_modeto false
Also, to be technically correct in regards to the poll contract, I think we will need a Waker that is set in poll that we can call once we change any state around the mode, be it the auto_mode boolean or the mode field itself. Otherwise we rely on another event in the network to call poll on Behaviour which might not happen immediately.
Let me know what you think!
That is just for logging though right? We don't need to pass an address to the
The suggested waker should address this. |
Mode::{Client,Server}
Looks good to me. Originally I had a boolean field to determine if it should be done automatically or not but I think I also had mode be |
thomaseizinger
left a comment
There was a problem hiding this comment.
Great!
Just missing some tests now :)
…into kad-explicit-mode
…into kad-explicit-mode
mxinden
left a comment
There was a problem hiding this comment.
Thank you for tackling this @dariusc93.
protocols/kad/src/behaviour.rs
Outdated
| id | ||
| } | ||
|
|
||
| pub fn set_explicit_mode(&mut self, mode: Option<Mode>) { |
There was a problem hiding this comment.
| pub fn set_explicit_mode(&mut self, mode: Option<Mode>) { | |
| /// Either set Kademlia [`Mode`] explicitly via `Some(_)` or enable automatic configuration of the Kademlia [`Mode`] based on the external addresses available via `None`. | |
| pub fn set_mode(&mut self, mode: Option<Mode>) { |
- Nit pick, passing
Noneenables auto-mode, thus the function name (xxx_explicit_xxx) is not accurate. Would you agree? - Adding some documentation. Feel free to rephrase. Just a suggestion.
There was a problem hiding this comment.
Nit pick, passing None enables auto-mode, thus the function name (xxx_explicit_xxx) is not accurate. Would you agree?
I disagree. If you call set_explicit_mode with None, you are not setting an explicit mode. Seems quite logical to me?
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Max Inden <mail@max-inden.de>
…into kad-explicit-mode
thomaseizinger
left a comment
There was a problem hiding this comment.
Thanks! Some final input but looks good already :)
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
thomaseizinger
left a comment
There was a problem hiding this comment.
Thanks! Some final nits.
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
…into kad-explicit-mode
thomaseizinger
left a comment
There was a problem hiding this comment.
Thank you for your patience! This turned out quite clean :)
mxinden
left a comment
There was a problem hiding this comment.
👍 thank you for the follow-ups!
Description
The current implementation in Kademlia relies on an external address to determine if it should be in client or server mode. However there are instances where a node, which may know an external address, wishes to remain in client mode and switch to server mode at a later point.
This PR introduces
Kademlia::set_mode, which accepts anOption<Mode>that would allow one to setMode::Clientfor client mode,Mode::Serverfor server mode, orNoneto determine if we should operate as a client or server based on our external addresses.Resolves #4074.
Notes & open questions
This is just my thoughts on how it should be based on what was discussed in #4074. IntroducingExplicitModewould remove the possibility of complexity if we were to haveMode::Autodue to it being passed into the handler, but open to suggestions on changing this to something different instead.From what it looks like, settingExplicitMode::Server(which internally would set it toMode::Server) still require an external address. Do we wish to keep this behaviour so the node is reachable or should we rely on another form of address (eg address(es) being listened on)?WhenExplicitMode::Autois set, do we want to automatically determine the mode at that point base on the external address and notify the handler(s) or should we leaveKademlia::modeas is untilNetworkBehaviour::on_swarm_eventis executed to determine that for us as long as the mode is set to auto?Change checklist