-
Notifications
You must be signed in to change notification settings - Fork 0
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: Add re-registration on expiry for p2p node #685
base: develop
Are you sure you want to change the base?
Changes from all commits
b764064
b95b682
7e5a451
112e999
d8be7ee
c40a8b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ use libp2p::{ | |
}; | ||
|
||
use super::P2PError; | ||
use crate::config::DEFAULT_REGISTRATION_TTL; | ||
|
||
#[derive(NetworkBehaviour)] | ||
pub struct BootstrapBehaviour { | ||
|
@@ -39,7 +40,7 @@ impl BootstrapConfig { | |
.with_behaviour(|key| BootstrapBehaviour { | ||
// Rendezvous server behaviour for serving new peers to connecting nodes. | ||
rendezvous: rendezvous::server::Behaviour::new( | ||
rendezvous::server::Config::default(), | ||
rendezvous::server::Config::default().with_max_ttl(DEFAULT_REGISTRATION_TTL), // Max TTL of 24 hours | ||
), | ||
// The identify behaviour is used to share the external address and the public key with connecting clients. | ||
identify: identify::Behaviour::new(identify::Config::new( | ||
|
@@ -65,11 +66,8 @@ pub(crate) async fn bootstrap( | |
swarm.listen_on(addr)?; | ||
while let Some(event) = swarm.next().await { | ||
match event { | ||
SwarmEvent::ConnectionEstablished { peer_id, .. } => { | ||
tracing::info!("Connected to {}", peer_id); | ||
} | ||
SwarmEvent::ConnectionClosed { peer_id, .. } => { | ||
tracing::info!("Disconnected from {}", peer_id); | ||
SwarmEvent::NewListenAddr { address, .. } => { | ||
tracing::info!("Listening on {}", address); | ||
} | ||
SwarmEvent::Behaviour(BootstrapBehaviourEvent::Rendezvous( | ||
rendezvous::server::Event::PeerRegistered { peer, registration }, | ||
|
@@ -95,7 +93,16 @@ pub(crate) async fn bootstrap( | |
); | ||
} | ||
} | ||
_other => {} | ||
SwarmEvent::Behaviour(BootstrapBehaviourEvent::Rendezvous( | ||
rendezvous::server::Event::RegistrationExpired(registration), | ||
)) => { | ||
tracing::info!( | ||
"Registration for peer {} expired in namespace {}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do we do when the registration expires? What does it mean for the node to be registered? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the registration expires the registration node re-registers automatically. |
||
registration.record.peer_id(), | ||
registration.namespace | ||
); | ||
} | ||
other => tracing::debug!("Encountered event: {other:?}"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this catch the previous |
||
} | ||
} | ||
Ok(()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ pub(crate) use register::RegisterConfig; | |
const P2P_NAMESPACE: &str = "polka-storage"; | ||
|
||
#[derive(Default, Debug, Clone, Copy, ValueEnum, Deserialize)] | ||
#[serde(rename_all = "lowercase")] | ||
pub enum NodeType { | ||
#[default] | ||
Bootstrap, | ||
|
@@ -67,6 +68,9 @@ pub(crate) struct P2PState { | |
/// PeerID of the bootstrap node used by the registration node. | ||
/// Optional because it is not used by the bootstrap node. | ||
pub(crate) rendezvous_point: Option<PeerId>, | ||
|
||
/// TTL of the p2p registration in seconds | ||
pub(crate) registration_ttl: u64, | ||
} | ||
|
||
/// Deserializes a ED25519 private key into a Keypair. | ||
|
@@ -124,12 +128,11 @@ pub async fn run_bootstrap_node( | |
}, | ||
_ = token.cancelled() => { | ||
tracing::info!("P2P node has been stopped by the cancellation token..."); | ||
tracker.close(); | ||
tracker.wait().await; | ||
}, | ||
} | ||
|
||
tracker.close(); | ||
tracker.wait().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -141,14 +144,17 @@ pub async fn run_register_node( | |
) -> Result<(), P2PError> { | ||
tracing::info!("Starting P2P register node"); | ||
let tracker = TaskTracker::new(); | ||
aidan46 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let (swarm, rendezvous_point_address, rendezvous_point) = config.create_swarm()?; | ||
let rendezvous_point = config.rendezvous_point; | ||
let rendezvous_point_address = config.rendezvous_point_address.clone(); | ||
let registration_ttl = config.registration_ttl; | ||
let mut swarm = config.create_swarm()?; | ||
|
||
tokio::select! { | ||
res = register( | ||
swarm, | ||
&mut swarm, | ||
rendezvous_point, | ||
rendezvous_point_address, | ||
None, | ||
registration_ttl, | ||
Namespace::from_static(P2P_NAMESPACE), | ||
) => { | ||
if let Err(e) = res { | ||
|
@@ -158,11 +164,10 @@ pub async fn run_register_node( | |
}, | ||
_ = token.cancelled() => { | ||
tracing::info!("P2P node has been stopped by the cancellation token..."); | ||
tracker.close(); | ||
tracker.wait().await; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for using TaskTracker here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, I don't even see what task this is tracking. I think this might be a leftover from an experiment? |
||
}, | ||
} | ||
|
||
tracker.close(); | ||
tracker.wait().await; | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better if we use Duration here. That way the reader can immediately see how the duration is parsed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the tick it makes sense but we re-use this value for the register call in the swarm which expects a
Option<u64>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok as is, but for reference, you can always just
.as_secs