Skip to content

Commit

Permalink
Record the latest compatible version for the request response protoco…
Browse files Browse the repository at this point in the history
…l in the PeerManager table
  • Loading branch information
acerone85 committed Oct 15, 2024
1 parent ab26a0b commit c9ba074
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/services/p2p/src/p2p_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
Punisher,
},
peer_report::PeerReportEvent,
request_response as fuel_request_response,
request_response::messages::{
RequestError,
RequestMessage,
Expand Down Expand Up @@ -717,6 +718,8 @@ impl FuelP2PService {
identify::Event::Received { peer_id, info } => {
self.update_metrics(increment_unique_peers);

let request_response_protocol_version =
fuel_request_response::ProtocolVersion::latest_compatible_version_for_peer(&info);
let mut addresses = info.listen_addrs;
let agent_version = info.agent_version;
if addresses.len() > MAX_IDENTIFY_ADDRESSES {
Expand All @@ -733,6 +736,7 @@ impl FuelP2PService {
&peer_id,
addresses.clone(),
agent_version,
request_response_protocol_version,
);

self.swarm
Expand Down
25 changes: 25 additions & 0 deletions crates/services/p2p/src/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ use tracing::{
use crate::{
gossipsub_config::GRAYLIST_THRESHOLD,
peer_manager::heartbeat_data::HeartbeatData,
request_response::{
self,
ProtocolVersion,
},
};

pub mod heartbeat_data;
Expand All @@ -45,6 +49,9 @@ pub struct PeerInfo {
pub client_version: Option<String>,
pub heartbeat_data: HeartbeatData,
pub score: AppScore,
/// The latest protocol version that the peer supports and that is
/// compatible with the current version of the node
pub request_response_protocol_version: Option<request_response::ProtocolVersion>,
}

impl PeerInfo {
Expand All @@ -54,6 +61,7 @@ impl PeerInfo {
client_version: None,
heartbeat_data: HeartbeatData::new(heartbeat_avg_window),
score: DEFAULT_APP_SCORE,
request_response_protocol_version: None,
}
}
}
Expand Down Expand Up @@ -135,10 +143,14 @@ impl PeerManager {
peer_id: &PeerId,
addresses: Vec<Multiaddr>,
agent_version: String,
protocol_version: Option<ProtocolVersion>,
) {
let peers = self.get_assigned_peer_table_mut(peer_id);
insert_client_version(peers, peer_id, agent_version);
insert_peer_addresses(peers, peer_id, addresses);
protocol_version.into_iter().for_each(|protocol| {
update_request_response_protocol_version(peers, peer_id, protocol)
});
}

pub fn batch_update_score_with_decay(&mut self) {
Expand Down Expand Up @@ -356,6 +368,19 @@ fn insert_client_version(
}
}

// Updates the latest request response protocol version that the peer supports
fn update_request_response_protocol_version(
peers: &mut HashMap<PeerId, PeerInfo>,
peer_id: &PeerId,
protocol: ProtocolVersion,
) {
if let Some(peer) = peers.get_mut(peer_id) {
peer.request_response_protocol_version = Some(protocol);
} else {
log_missing_peer(peer_id);
}
}

fn log_missing_peer(peer_id: &PeerId) {
debug!(target: "fuel-p2p", "Peer with PeerId: {:?} is not among the connected peers", peer_id)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/services/p2p/src/request_response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod messages;

pub mod protocols;

pub use protocols::ProtocolVersion;
6 changes: 3 additions & 3 deletions crates/services/p2p/src/request_response/protocols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ProtocolVersion {
}

pub fn latest_compatible_version_for_peer(
info: identify::Info,
info: &identify::Info,
) -> Option<ProtocolVersion> {
info.protocols
.iter()
Expand Down Expand Up @@ -93,7 +93,7 @@ mod tests {
let peer_info =
peer_info(&[MessageExchangePostcardProtocol.as_ref(), HEARTBEAT_PROTOCOL]);
let latest_compatible_version_for_peer =
ProtocolVersion::latest_compatible_version_for_peer(peer_info).unwrap();
ProtocolVersion::latest_compatible_version_for_peer(&peer_info).unwrap();
assert_eq!(
latest_compatible_version_for_peer,
crate::request_response::protocols::ProtocolVersion::V1(
Expand All @@ -106,7 +106,7 @@ mod tests {
fn test_latest_protocol_version_undefined() {
let peer_info = peer_info(&[HEARTBEAT_PROTOCOL, "/some/other/protocol/1.0.0"]);
let latest_compatible_version_for_peer =
ProtocolVersion::latest_compatible_version_for_peer(peer_info);
ProtocolVersion::latest_compatible_version_for_peer(&peer_info);
assert!(latest_compatible_version_for_peer.is_none(),);
}
}
2 changes: 2 additions & 0 deletions crates/services/p2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,7 @@ pub mod tests {
client_version: None,
heartbeat_data,
score: 100.0,
request_response_protocol_version: None,
};
let peer_info = vec![(peer_id, peer_info)];
let p2p_service = FakeP2PService {
Expand Down Expand Up @@ -1624,6 +1625,7 @@ pub mod tests {
client_version: None,
heartbeat_data,
score: 100.0,
request_response_protocol_version: None,
};
let peer_info = vec![(peer_id, peer_info)];
let p2p_service = FakeP2PService {
Expand Down

0 comments on commit c9ba074

Please sign in to comment.