-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(peer): Move
PeerInfo
to own file
- Loading branch information
1 parent
50752a1
commit 050071d
Showing
10 changed files
with
115 additions
and
106 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use std::net::SocketAddr; | ||
use std::time::SystemTime; | ||
|
||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
use super::InstanceId; | ||
use super::PeerStanding; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||
pub(crate) struct PeerConnectionInfo { | ||
port_for_incoming_connections: Option<u16>, | ||
connected_address: SocketAddr, | ||
inbound: bool, | ||
} | ||
|
||
impl PeerConnectionInfo { | ||
pub(crate) fn new( | ||
port_for_incoming_connections: Option<u16>, | ||
connected_address: SocketAddr, | ||
inbound: bool, | ||
) -> Self { | ||
Self { | ||
port_for_incoming_connections, | ||
connected_address, | ||
inbound, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||
pub struct PeerInfo { | ||
peer_connection_info: PeerConnectionInfo, | ||
instance_id: InstanceId, | ||
connection_established: SystemTime, | ||
pub(crate) standing: PeerStanding, | ||
version: String, | ||
is_archival_node: bool, | ||
} | ||
|
||
impl PeerInfo { | ||
pub(crate) fn new( | ||
peer_connection_info: PeerConnectionInfo, | ||
instance_id: InstanceId, | ||
connection_established: SystemTime, | ||
version: String, | ||
is_archival_node: bool, | ||
peer_tolerance: u16, | ||
) -> Self { | ||
assert!(peer_tolerance > 0, "Peer tolerance must be positive"); | ||
let standing = PeerStanding::new(peer_tolerance); | ||
Self { | ||
peer_connection_info, | ||
instance_id, | ||
connection_established, | ||
standing, | ||
version, | ||
is_archival_node, | ||
} | ||
} | ||
|
||
pub(crate) fn with_standing(mut self, standing: PeerStanding) -> Self { | ||
self.standing = standing; | ||
self | ||
} | ||
|
||
pub(crate) fn instance_id(&self) -> u128 { | ||
self.instance_id | ||
} | ||
|
||
pub fn standing(&self) -> PeerStanding { | ||
self.standing | ||
} | ||
|
||
pub fn connected_address(&self) -> SocketAddr { | ||
self.peer_connection_info.connected_address | ||
} | ||
|
||
pub fn connection_established(&self) -> SystemTime { | ||
self.connection_established | ||
} | ||
|
||
pub fn is_archival_node(&self) -> bool { | ||
self.is_archival_node | ||
} | ||
|
||
pub(crate) fn connection_is_inbound(&self) -> bool { | ||
self.peer_connection_info.inbound | ||
} | ||
|
||
/// Return the socket address that the peer is expected to listen on. Returns `None` if peer does not accept | ||
/// incoming connections. | ||
pub fn listen_address(&self) -> Option<SocketAddr> { | ||
self.peer_connection_info | ||
.port_for_incoming_connections | ||
.map(|port| SocketAddr::new(self.peer_connection_info.connected_address.ip(), port)) | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) fn set_connection_established(&mut self, new_timestamp: SystemTime) { | ||
self.connection_established = new_timestamp; | ||
} | ||
} |
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