Skip to content

Commit

Permalink
Expose protocol version in NodeStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-casper committed Jul 3, 2024
1 parent 1043e79 commit 3fe5b20
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
16 changes: 12 additions & 4 deletions binary_port/src/node_status.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use casper_types::{
bytesrepr::{self, FromBytes, ToBytes},
AvailableBlockRange, BlockHash, BlockSynchronizerStatus, Digest, NextUpgrade, Peers, PublicKey,
TimeDiff, Timestamp,
AvailableBlockRange, BlockHash, BlockSynchronizerStatus, Digest, NextUpgrade, Peers,
ProtocolVersion, PublicKey, TimeDiff, Timestamp,
};

#[cfg(test)]
Expand All @@ -15,6 +15,8 @@ use crate::{minimal_block_info::MinimalBlockInfo, type_wrappers::ReactorStateNam
/// Status information about the node.
#[derive(Debug, PartialEq, Serialize)]
pub struct NodeStatus {
/// The current protocol version.
pub protocol_version: ProtocolVersion,
/// The node ID and network address of each connected peer.
pub peers: Peers,
/// The compiled node version.
Expand Down Expand Up @@ -49,6 +51,7 @@ impl NodeStatus {
#[cfg(test)]
pub(crate) fn random(rng: &mut TestRng) -> Self {
Self {
protocol_version: ProtocolVersion::from_parts(rng.gen(), rng.gen(), rng.gen()),
peers: Peers::random(rng),
build_version: rng.random_string(5..10),
chainspec_name: rng.random_string(5..10),
Expand All @@ -71,7 +74,8 @@ impl NodeStatus {

impl FromBytes for NodeStatus {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (peers, remainder) = FromBytes::from_bytes(bytes)?;
let (protocol_version, remainder) = ProtocolVersion::from_bytes(bytes)?;
let (peers, remainder) = Peers::from_bytes(remainder)?;
let (build_version, remainder) = String::from_bytes(remainder)?;
let (chainspec_name, remainder) = String::from_bytes(remainder)?;
let (starting_state_root_hash, remainder) = Digest::from_bytes(remainder)?;
Expand All @@ -87,6 +91,7 @@ impl FromBytes for NodeStatus {
let (latest_switch_block_hash, remainder) = Option::<BlockHash>::from_bytes(remainder)?;
Ok((
NodeStatus {
protocol_version,
peers,
build_version,
chainspec_name,
Expand Down Expand Up @@ -116,6 +121,7 @@ impl ToBytes for NodeStatus {

fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
let NodeStatus {
protocol_version,
peers,
build_version,
chainspec_name,
Expand All @@ -131,6 +137,7 @@ impl ToBytes for NodeStatus {
block_sync,
latest_switch_block_hash,
} = self;
protocol_version.write_bytes(writer)?;
peers.write_bytes(writer)?;
build_version.write_bytes(writer)?;
chainspec_name.write_bytes(writer)?;
Expand All @@ -148,7 +155,8 @@ impl ToBytes for NodeStatus {
}

fn serialized_length(&self) -> usize {
self.peers.serialized_length()
self.protocol_version.serialized_length()
+ self.peers.serialized_length()
+ self.build_version.serialized_length()
+ self.chainspec_name.serialized_length()
+ self.starting_state_root_hash.serialized_length()
Expand Down
1 change: 1 addition & 0 deletions node/src/components/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ where
};

let status = NodeStatus {
protocol_version,
peers: Peers::from(peers),
build_version: crate::VERSION_STRING.clone(),
chainspec_name: network_name.into(),
Expand Down
11 changes: 8 additions & 3 deletions node/src/reactor/main_reactor/tests/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const MESSAGE_SIZE: u32 = 1024 * 1024 * 10;

struct TestData {
rng: TestRng,
protocol_version: ProtocolVersion,
chainspec_raw_bytes: ChainspecRawBytes,
highest_block: Block,
secret_signing_key: Arc<SecretKey>,
Expand Down Expand Up @@ -149,6 +150,7 @@ async fn setup() -> (
.bind_address()
.expect("should be bound");

let protocol_version = first_node.main_reactor().chainspec.protocol_version();
// We let the entire network run in the background, until our request completes.
let finish_cranking = fixture.run_until_stopped(rng.create_child());

Expand All @@ -164,6 +166,7 @@ async fn setup() -> (
finish_cranking,
TestData {
rng,
protocol_version,
chainspec_raw_bytes,
highest_block,
secret_signing_key,
Expand Down Expand Up @@ -298,6 +301,7 @@ async fn binary_port_component_handles_all_requests() {
finish_cranking,
TestData {
mut rng,
protocol_version,
chainspec_raw_bytes: network_chainspec_raw_bytes,
highest_block,
secret_signing_key,
Expand Down Expand Up @@ -325,7 +329,7 @@ async fn binary_port_component_handles_all_requests() {
consensus_status(),
chainspec_raw_bytes(network_chainspec_raw_bytes),
latest_switch_block_header(),
node_status(),
node_status(protocol_version),
get_block_header(highest_block.clone_header()),
get_block_transfers(highest_block.clone_header()),
get_era_summary(state_root_hash),
Expand Down Expand Up @@ -656,7 +660,7 @@ fn latest_switch_block_header() -> TestCase {
}
}

fn node_status() -> TestCase {
fn node_status(expected_version: ProtocolVersion) -> TestCase {
TestCase {
name: "node_status",
request: BinaryRequest::Get(GetRequest::Information {
Expand All @@ -668,7 +672,8 @@ fn node_status() -> TestCase {
response,
Some(PayloadType::NodeStatus),
|node_status| {
!node_status.peers.into_inner().is_empty()
node_status.protocol_version == expected_version
&& !node_status.peers.into_inner().is_empty()
&& node_status.chainspec_name == "casper-example"
&& node_status.last_added_block_info.is_some()
&& node_status.our_public_signing_key.is_some()
Expand Down

0 comments on commit 3fe5b20

Please sign in to comment.