From 79128d489ac7e435aff850d14d2f98132f0e4044 Mon Sep 17 00:00:00 2001 From: Jakub Zajkowski Date: Tue, 17 Dec 2024 11:59:33 +0100 Subject: [PATCH] fixing tests --- binary_port/src/error_code.rs | 4 --- binary_port/src/information_request.rs | 11 -------- binary_port/src/response_type.rs | 4 --- node/src/components/binary_port.rs | 23 +++------------ node/src/reactor/main_reactor/tests.rs | 39 ++++++++++++++++---------- 5 files changed, 28 insertions(+), 53 deletions(-) diff --git a/binary_port/src/error_code.rs b/binary_port/src/error_code.rs index 34efcdb31f..56cc370e37 100644 --- a/binary_port/src/error_code.rs +++ b/binary_port/src/error_code.rs @@ -307,9 +307,6 @@ pub enum ErrorCode { /// Malformed binary request #[error("malformed binary request")] MalformedBinaryRequest = 96, - /// Peer count unavailable - #[error("peer count unavailable")] - PeerCountUnavailable = 97, } impl TryFrom for ErrorCode { @@ -414,7 +411,6 @@ impl TryFrom for ErrorCode { 94 => Ok(ErrorCode::MalformedProtocolVersion), 95 => Ok(ErrorCode::MalformedBinaryRequestHeader), 96 => Ok(ErrorCode::MalformedBinaryRequest), - 97 => Ok(ErrorCode::PeerCountUnavailable), _ => Err(UnknownErrorCode), } } diff --git a/binary_port/src/information_request.rs b/binary_port/src/information_request.rs index 2b42f945e3..39b823bbc4 100644 --- a/binary_port/src/information_request.rs +++ b/binary_port/src/information_request.rs @@ -83,8 +83,6 @@ pub enum InformationRequest { /// Whether to return the bytecode with the entity. include_bytecode: bool, }, - /// Returns the count of connected peers. - PeerCount, } impl InformationRequest { @@ -117,7 +115,6 @@ impl InformationRequest { InformationRequest::ProtocolVersion => InformationRequestTag::ProtocolVersion, InformationRequest::Package { .. } => InformationRequestTag::Package, InformationRequest::Entity { .. } => InformationRequestTag::Entity, - InformationRequest::PeerCount => InformationRequestTag::PeerCount, } } @@ -135,7 +132,6 @@ impl InformationRequest { with_finalized_approvals: rng.gen(), }, InformationRequestTag::Peers => InformationRequest::Peers, - InformationRequestTag::PeerCount => InformationRequest::PeerCount, InformationRequestTag::Uptime => InformationRequest::Uptime, InformationRequestTag::LastProgress => InformationRequest::LastProgress, InformationRequestTag::ReactorState => InformationRequest::ReactorState, @@ -202,7 +198,6 @@ impl ToBytes for InformationRequest { with_finalized_approvals.write_bytes(writer) } InformationRequest::Peers - | InformationRequest::PeerCount | InformationRequest::Uptime | InformationRequest::LastProgress | InformationRequest::ReactorState @@ -258,7 +253,6 @@ impl ToBytes for InformationRequest { with_finalized_approvals, } => hash.serialized_length() + with_finalized_approvals.serialized_length(), InformationRequest::Peers - | InformationRequest::PeerCount | InformationRequest::Uptime | InformationRequest::LastProgress | InformationRequest::ReactorState @@ -323,7 +317,6 @@ impl TryFrom<(InformationRequestTag, &[u8])> for InformationRequest { ) } InformationRequestTag::Peers => (InformationRequest::Peers, key_bytes), - InformationRequestTag::PeerCount => (InformationRequest::PeerCount, key_bytes), InformationRequestTag::Uptime => (InformationRequest::Uptime, key_bytes), InformationRequestTag::LastProgress => (InformationRequest::LastProgress, key_bytes), InformationRequestTag::ReactorState => (InformationRequest::ReactorState, key_bytes), @@ -451,8 +444,6 @@ pub enum InformationRequestTag { Package = 18, /// Addressable entity request. Entity = 19, - /// Peer count. - PeerCount = 20, } impl InformationRequestTag { @@ -479,7 +470,6 @@ impl InformationRequestTag { 17 => InformationRequestTag::ProtocolVersion, 18 => InformationRequestTag::Package, 19 => InformationRequestTag::Entity, - 20 => InformationRequestTag::PeerCount, _ => unreachable!(), } } @@ -510,7 +500,6 @@ impl TryFrom for InformationRequestTag { 17 => Ok(InformationRequestTag::ProtocolVersion), 18 => Ok(InformationRequestTag::Package), 19 => Ok(InformationRequestTag::Entity), - 20 => Ok(InformationRequestTag::PeerCount), _ => Err(UnknownInformationRequestTag(value)), } } diff --git a/binary_port/src/response_type.rs b/binary_port/src/response_type.rs index dacc3ce2ce..e74bc9cf14 100644 --- a/binary_port/src/response_type.rs +++ b/binary_port/src/response_type.rs @@ -121,8 +121,6 @@ pub enum ResponseType { AddressableEntityInformation, /// Response to KeepAliveRequest query KeepAliveInformation, - /// Peer count. - PeerCount, } impl ResponseType { @@ -233,7 +231,6 @@ impl TryFrom for ResponseType { x if x == ResponseType::KeepAliveInformation as u8 => { Ok(ResponseType::KeepAliveInformation) } - x if x == ResponseType::PeerCount as u8 => Ok(ResponseType::PeerCount), _ => Err(()), } } @@ -299,7 +296,6 @@ impl fmt::Display for ResponseType { ResponseType::KeepAliveInformation => { write!(f, "KeepAliveInformation") } - ResponseType::PeerCount => write!(f, "PeerCount"), } } } diff --git a/node/src/components/binary_port.rs b/node/src/components/binary_port.rs index 760cf49b0a..1ab69c10f9 100644 --- a/node/src/components/binary_port.rs +++ b/node/src/components/binary_port.rs @@ -1097,24 +1097,10 @@ where protocol_version, ) } - InformationRequest::Peers => { - let map = effect_builder.network_peers().await; - if map.is_empty() { - BinaryResponse::new_empty(protocol_version) - } else { - let peers = Peers::from(map); - BinaryResponse::from_value(peers, protocol_version) - } - } - InformationRequest::PeerCount => { - let map = effect_builder.network_peers().await; - let count = map.len() as u32; - if let Ok(bytes) = u32::to_bytes(&count) { - BinaryResponse::from_raw_bytes(ResponseType::PeerCount, bytes, protocol_version) - } else { - BinaryResponse::new_error(ErrorCode::PeerCountUnavailable, protocol_version) - } - } + InformationRequest::Peers => BinaryResponse::from_value( + Peers::from(effect_builder.network_peers().await), + protocol_version, + ), InformationRequest::Uptime => { BinaryResponse::from_value(effect_builder.get_uptime().await, protocol_version) } @@ -1513,7 +1499,6 @@ where let (response, id) = handle_payload(effect_builder, payload, version, Arc::clone(&rate_limiter)).await; - framed .send(BinaryMessage::new( BinaryResponseAndRequest::new(response, payload, id).to_bytes()?, diff --git a/node/src/reactor/main_reactor/tests.rs b/node/src/reactor/main_reactor/tests.rs index c398eb7b2b..57174611f6 100644 --- a/node/src/reactor/main_reactor/tests.rs +++ b/node/src/reactor/main_reactor/tests.rs @@ -12,8 +12,8 @@ use std::{ }; use casper_binary_port::{ - BinaryMessage, BinaryMessageCodec, BinaryRequest, BinaryRequestHeader, InformationRequest, - Uptime, + BinaryMessage, BinaryMessageCodec, BinaryRequest, BinaryRequestHeader, + BinaryResponseAndRequest, InformationRequest, Uptime, }; use either::Either; use futures::{SinkExt, StreamExt}; @@ -48,9 +48,9 @@ use casper_types::{ AccountConfig, AccountsConfig, ActivationPoint, AddressableEntityHash, AvailableBlockRange, Block, BlockHash, BlockHeader, BlockV2, CLValue, Chainspec, ChainspecRawBytes, ConsensusProtocolName, Deploy, EraId, FeeHandling, Gas, HoldBalanceHandling, Key, Motes, - NextUpgrade, PricingHandling, PricingMode, ProtocolVersion, PublicKey, RefundHandling, Rewards, - SecretKey, StoredValue, SystemHashRegistry, TimeDiff, Timestamp, Transaction, TransactionHash, - TransactionV1Config, ValidatorConfig, U512, + NextUpgrade, Peers, PricingHandling, PricingMode, ProtocolVersion, PublicKey, RefundHandling, + Rewards, SecretKey, StoredValue, SystemHashRegistry, TimeDiff, Timestamp, Transaction, + TransactionHash, TransactionV1Config, ValidatorConfig, U512, }; use crate::{ @@ -1469,9 +1469,12 @@ async fn should_start_in_isolation() { .unwrap_or_else(|err| panic!("should complete uptime request without timeout: {}", err)) .unwrap_or_else(|| panic!("should have bytes")) .unwrap_or_else(|err| panic!("should have ok response: {}", err)); - let uptime: Uptime = FromBytes::from_bytes(response.payload()) - .expect("Uptime should be deserializable") - .0; + let (binary_response_and_request, _): (BinaryResponseAndRequest, _) = + FromBytes::from_bytes(response.payload()).expect("should deserialize response"); + let response = binary_response_and_request.response().payload(); + let (uptime, remainder): (Uptime, _) = + FromBytes::from_bytes(response).expect("Peers should be deserializable"); + assert!(remainder.is_empty()); assert!(uptime.into_inner() > 0); let (_net, _rng) = timeout(Duration::from_secs(20), finish_cranking) .await @@ -1490,9 +1493,9 @@ async fn should_be_peerless_in_isolation() { let (mut client, finish_cranking) = setup_network_and_get_binary_port_handle(initial_stakes, spec_override).await; - let peer_count_request_bytes = { + let peers_request_bytes = { let request = BinaryRequest::Get( - InformationRequest::PeerCount + InformationRequest::Peers .try_into() .expect("should convert"), ); @@ -1510,7 +1513,7 @@ async fn should_be_peerless_in_isolation() { .collect::>() }; client - .send(BinaryMessage::new(peer_count_request_bytes)) + .send(BinaryMessage::new(peers_request_bytes)) .await .expect("should send message"); let response = timeout(Duration::from_secs(20), client.next()) @@ -1518,11 +1521,17 @@ async fn should_be_peerless_in_isolation() { .unwrap_or_else(|err| panic!("should complete peers request without timeout: {}", err)) .unwrap_or_else(|| panic!("should have bytes")) .unwrap_or_else(|err| panic!("should have ok response: {}", err)); + let (binary_response_and_request, _): (BinaryResponseAndRequest, _) = + FromBytes::from_bytes(response.payload()).expect("should deserialize response"); + let response = binary_response_and_request.response().payload(); - let (peer_count, _) = - ::from_bytes(response.payload()).expect("Peers should be deserializable"); - - assert_eq!(peer_count, 0, "should not have peers in isolated mode"); + let (peers, remainder): (Peers, _) = + FromBytes::from_bytes(response).expect("Peers should be deserializable"); + assert!(remainder.is_empty()); + assert!( + peers.into_inner().is_empty(), + "should not have peers in isolated mode" + ); let (_net, _rng) = timeout(Duration::from_secs(20), finish_cranking) .await