-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
network-libp2p: Migrate AutoNAT v1 protocol to v2
- Loading branch information
Showing
9 changed files
with
185 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
use libp2p::Multiaddr; | ||
|
||
#[derive(Copy, Clone, Debug, Default, PartialEq)] | ||
pub(crate) enum NatStatus { | ||
Public, | ||
Private, | ||
#[default] | ||
Unknown, | ||
} | ||
|
||
#[derive(Default)] | ||
pub(crate) struct NatState { | ||
confirmed_addresses: HashSet<Multiaddr>, | ||
address_status: HashMap<Multiaddr, NatStatus>, | ||
status: NatStatus, | ||
} | ||
|
||
impl NatState { | ||
/// Adds an address to track its NAT status | ||
pub fn add_address(&mut self, address: Multiaddr) { | ||
self.address_status.insert(address, NatStatus::Unknown); | ||
} | ||
|
||
/// Remove address and no longer track its NAT status | ||
pub fn remove_address(&mut self, address: &Multiaddr) { | ||
self.address_status.remove(address); | ||
self.confirmed_addresses.remove(address); | ||
self.update_state(); | ||
} | ||
|
||
/// Set NAT status of address | ||
pub fn set_address_nat(&mut self, address: Multiaddr, nat_status: NatStatus) { | ||
let address_status = self | ||
.address_status | ||
.entry(address.clone()) | ||
.or_insert(nat_status); | ||
|
||
if *address_status == NatStatus::Public { | ||
self.confirmed_addresses.insert(address); | ||
} else { | ||
self.confirmed_addresses.remove(&address); | ||
} | ||
self.update_state(); | ||
} | ||
|
||
/// Mark the address as confirmed thus publicly reachable | ||
pub fn add_confirmed_address(&mut self, address: Multiaddr) { | ||
let address_status = self.address_status.entry(address.clone()).or_default(); | ||
*address_status = NatStatus::Public; | ||
|
||
self.confirmed_addresses.insert(address); | ||
self.update_state(); | ||
} | ||
|
||
/// Remove address as it's no longer publicly reachable | ||
pub fn remove_confirmed_address(&mut self, address: &Multiaddr) { | ||
let address_status = self.address_status.entry(address.clone()).or_default(); | ||
*address_status = NatStatus::Private; | ||
|
||
self.confirmed_addresses.remove(address); | ||
self.update_state(); | ||
} | ||
|
||
/// Compute the overall NAT state | ||
fn update_state(&mut self) { | ||
let old_nat_status = self.status; | ||
|
||
if !self.confirmed_addresses.is_empty() { | ||
self.status = NatStatus::Public; | ||
} else if self | ||
.address_status | ||
.iter() | ||
.all(|(_, status)| *status == NatStatus::Private) | ||
{ | ||
self.status = NatStatus::Private; | ||
} else { | ||
self.status = NatStatus::Unknown; | ||
} | ||
|
||
if old_nat_status == self.status { | ||
return; | ||
} | ||
|
||
if self.status == NatStatus::Private { | ||
log::warn!("Couldn't detect a public reachable address. Validator network operations won't be possible"); | ||
log::warn!("You may need to find a relay to enable validator network operations"); | ||
} else if self.status == NatStatus::Public { | ||
log::info!( | ||
?old_nat_status, | ||
new_nat_status = ?self.status, | ||
"NAT status changed and detected public reachable address. Validator network operations will be possible" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters