Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Zajkowski committed Dec 17, 2024
1 parent e0d1bdb commit 79128d4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 53 deletions.
4 changes: 0 additions & 4 deletions binary_port/src/error_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16> for ErrorCode {
Expand Down Expand Up @@ -414,7 +411,6 @@ impl TryFrom<u16> for ErrorCode {
94 => Ok(ErrorCode::MalformedProtocolVersion),
95 => Ok(ErrorCode::MalformedBinaryRequestHeader),
96 => Ok(ErrorCode::MalformedBinaryRequest),
97 => Ok(ErrorCode::PeerCountUnavailable),
_ => Err(UnknownErrorCode),
}
}
Expand Down
11 changes: 0 additions & 11 deletions binary_port/src/information_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -117,7 +115,6 @@ impl InformationRequest {
InformationRequest::ProtocolVersion => InformationRequestTag::ProtocolVersion,
InformationRequest::Package { .. } => InformationRequestTag::Package,
InformationRequest::Entity { .. } => InformationRequestTag::Entity,
InformationRequest::PeerCount => InformationRequestTag::PeerCount,
}
}

Expand All @@ -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,
Expand Down Expand Up @@ -202,7 +198,6 @@ impl ToBytes for InformationRequest {
with_finalized_approvals.write_bytes(writer)
}
InformationRequest::Peers
| InformationRequest::PeerCount
| InformationRequest::Uptime
| InformationRequest::LastProgress
| InformationRequest::ReactorState
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -451,8 +444,6 @@ pub enum InformationRequestTag {
Package = 18,
/// Addressable entity request.
Entity = 19,
/// Peer count.
PeerCount = 20,
}

impl InformationRequestTag {
Expand All @@ -479,7 +470,6 @@ impl InformationRequestTag {
17 => InformationRequestTag::ProtocolVersion,
18 => InformationRequestTag::Package,
19 => InformationRequestTag::Entity,
20 => InformationRequestTag::PeerCount,
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -510,7 +500,6 @@ impl TryFrom<u16> for InformationRequestTag {
17 => Ok(InformationRequestTag::ProtocolVersion),
18 => Ok(InformationRequestTag::Package),
19 => Ok(InformationRequestTag::Entity),
20 => Ok(InformationRequestTag::PeerCount),
_ => Err(UnknownInformationRequestTag(value)),
}
}
Expand Down
4 changes: 0 additions & 4 deletions binary_port/src/response_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ pub enum ResponseType {
AddressableEntityInformation,
/// Response to KeepAliveRequest query
KeepAliveInformation,
/// Peer count.
PeerCount,
}

impl ResponseType {
Expand Down Expand Up @@ -233,7 +231,6 @@ impl TryFrom<u8> for ResponseType {
x if x == ResponseType::KeepAliveInformation as u8 => {
Ok(ResponseType::KeepAliveInformation)
}
x if x == ResponseType::PeerCount as u8 => Ok(ResponseType::PeerCount),
_ => Err(()),
}
}
Expand Down Expand Up @@ -299,7 +296,6 @@ impl fmt::Display for ResponseType {
ResponseType::KeepAliveInformation => {
write!(f, "KeepAliveInformation")
}
ResponseType::PeerCount => write!(f, "PeerCount"),
}
}
}
Expand Down
23 changes: 4 additions & 19 deletions node/src/components/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()?,
Expand Down
39 changes: 24 additions & 15 deletions node/src/reactor/main_reactor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::{
Expand Down Expand Up @@ -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
Expand All @@ -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"),
);
Expand All @@ -1510,19 +1513,25 @@ async fn should_be_peerless_in_isolation() {
.collect::<Vec<_>>()
};
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())
.await
.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, _) =
<u32>::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
Expand Down

0 comments on commit 79128d4

Please sign in to comment.