Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4599: Expose latest switch block information in the binary port r=jacek-casper a=jacek-casper

Added two things:
- new field, `latest_switch_block_hash` in the status
- new request type, `InformationRequest::LatestSwitchBlockHeader`, so that we can look up the latest switch block in the sidecar

Ticket: https://app.zenhub.com/workspaces/core-protocol-60953fafb1945f0011a3592d/issues/gh/casper-network/casper-node/4504

Co-authored-by: Jacek Malec <[email protected]>
  • Loading branch information
casperlabs-bors-ng[bot] and jacek-casper authored Mar 15, 2024
2 parents b44fb1d + 12dbfbe commit bd731db
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
10 changes: 10 additions & 0 deletions node/src/components/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@ where
(*effect_builder.get_chainspec_raw_bytes().await).clone(),
protocol_version,
),
InformationRequest::LatestSwitchBlockHeader => BinaryResponse::from_option(
effect_builder
.get_latest_switch_block_header_from_storage()
.await,
protocol_version,
),
InformationRequest::NodeStatus => {
let (
node_uptime,
Expand All @@ -737,6 +743,7 @@ where
last_progress,
available_block_range,
block_sync,
latest_switch_block_header,
) = join!(
effect_builder.get_uptime(),
effect_builder.get_network_name(),
Expand All @@ -748,6 +755,7 @@ where
effect_builder.get_last_progress(),
effect_builder.get_available_block_range_from_storage(),
effect_builder.get_block_synchronizer_status(),
effect_builder.get_latest_switch_block_header_from_storage(),
);
let starting_state_root_hash = effect_builder
.get_block_header_at_height_from_storage(available_block_range.low(), true)
Expand Down Expand Up @@ -784,6 +792,8 @@ where
last_progress: last_progress.into(),
available_block_range,
block_sync,
latest_switch_block_hash: latest_switch_block_header
.map(|header| header.block_hash()),
};
BinaryResponse::from_value(status, protocol_version)
}
Expand Down
5 changes: 5 additions & 0 deletions node/src/components/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,11 @@ impl Storage {
.read_block_header_by_height(block_height, only_from_available_block_range)?;
responder.respond(maybe_header).ignore()
}
StorageRequest::GetLatestSwitchBlockHeader { responder } => {
let txn = self.block_store.checkout_ro()?;
let maybe_header = txn.read(LatestSwitchBlock)?;
responder.respond(maybe_header).ignore()
}
StorageRequest::GetSwitchBlockHeaderByEra { era_id, responder } => {
let txn = self.block_store.checkout_ro()?;
let maybe_header = txn.read(era_id)?;
Expand Down
11 changes: 11 additions & 0 deletions node/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,17 @@ impl<REv> EffectBuilder<REv> {
.await
}

pub(crate) async fn get_latest_switch_block_header_from_storage(self) -> Option<BlockHeader>
where
REv: From<StorageRequest>,
{
self.make_request(
|responder| StorageRequest::GetLatestSwitchBlockHeader { responder },
QueueKind::FromStorage,
)
.await
}

pub(crate) async fn get_switch_block_header_by_era_id_from_storage(
self,
era_id: EraId,
Expand Down
6 changes: 6 additions & 0 deletions node/src/effect/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ pub(crate) enum StorageRequest {
/// local storage.
responder: Responder<Option<BlockHeader>>,
},
GetLatestSwitchBlockHeader {
responder: Responder<Option<BlockHeader>>,
},
GetSwitchBlockHeaderByEra {
/// Era ID for which to get the block header.
era_id: EraId,
Expand Down Expand Up @@ -532,6 +535,9 @@ impl Display for StorageRequest {
StorageRequest::GetBlockHeaderByHeight { block_height, .. } => {
write!(formatter, "get header for height {}", block_height)
}
StorageRequest::GetLatestSwitchBlockHeader { .. } => {
write!(formatter, "get latest switch block header")
}
StorageRequest::GetSwitchBlockHeaderByEra { era_id, .. } => {
write!(formatter, "get header for era {}", era_id)
}
Expand Down
17 changes: 17 additions & 0 deletions node/src/reactor/main_reactor/tests/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ async fn binary_port_component() {
next_upgrade(),
consensus_status(),
chainspec_raw_bytes(network_chainspec_raw_bytes),
latest_switch_block_header(),
node_status(),
get_block_header(highest_block.clone_header()),
get_block_transfers(highest_block.clone_header()),
Expand Down Expand Up @@ -609,6 +610,21 @@ fn chainspec_raw_bytes(network_chainspec_raw_bytes: ChainspecRawBytes) -> TestCa
}
}

fn latest_switch_block_header() -> TestCase {
TestCase {
name: "latest_switch_block_header",
request: BinaryRequest::Get(GetRequest::Information {
info_type_tag: InformationRequestTag::LatestSwitchBlockHeader.into(),
key: vec![],
}),
asserter: Box::new(move |response| {
assert_response::<BlockHeader, _>(response, Some(PayloadType::BlockHeader), |header| {
header.is_switch_block()
})
}),
}
}

fn node_status() -> TestCase {
TestCase {
name: "node_status",
Expand All @@ -628,6 +644,7 @@ fn node_status() -> TestCase {
&& node_status.block_sync.historical().is_none()
&& node_status.block_sync.forward().is_none()
&& matches!(node_status.reactor_state.into_inner().as_str(), "Validate")
&& node_status.latest_switch_block_hash.is_some()
},
)
}),
Expand Down
23 changes: 20 additions & 3 deletions types/src/binary_port/information_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub enum InformationRequest {
ChainspecRawBytes,
/// Returns the status information of the node.
NodeStatus,
/// Returns the latest switch block header.
LatestSwitchBlockHeader,
}

impl InformationRequest {
Expand All @@ -76,6 +78,9 @@ impl InformationRequest {
InformationRequest::ConsensusStatus => InformationRequestTag::ConsensusStatus,
InformationRequest::ChainspecRawBytes => InformationRequestTag::ChainspecRawBytes,
InformationRequest::NodeStatus => InformationRequestTag::NodeStatus,
InformationRequest::LatestSwitchBlockHeader => {
InformationRequestTag::LatestSwitchBlockHeader
}
}
}

Expand Down Expand Up @@ -116,6 +121,9 @@ impl InformationRequest {
InformationRequestTag::ConsensusStatus => InformationRequest::ConsensusStatus,
InformationRequestTag::ChainspecRawBytes => InformationRequest::ChainspecRawBytes,
InformationRequestTag::NodeStatus => InformationRequest::NodeStatus,
InformationRequestTag::LatestSwitchBlockHeader => {
InformationRequest::LatestSwitchBlockHeader
}
}
}
}
Expand Down Expand Up @@ -153,7 +161,8 @@ impl ToBytes for InformationRequest {
| InformationRequest::NextUpgrade
| InformationRequest::ConsensusStatus
| InformationRequest::ChainspecRawBytes
| InformationRequest::NodeStatus => Ok(()),
| InformationRequest::NodeStatus
| InformationRequest::LatestSwitchBlockHeader => Ok(()),
}
}

Expand All @@ -180,7 +189,8 @@ impl ToBytes for InformationRequest {
| InformationRequest::NextUpgrade
| InformationRequest::ConsensusStatus
| InformationRequest::ChainspecRawBytes
| InformationRequest::NodeStatus => 0,
| InformationRequest::NodeStatus
| InformationRequest::LatestSwitchBlockHeader => 0,
}
}
}
Expand Down Expand Up @@ -231,6 +241,9 @@ impl TryFrom<(InformationRequestTag, &[u8])> for InformationRequest {
(InformationRequest::ChainspecRawBytes, key_bytes)
}
InformationRequestTag::NodeStatus => (InformationRequest::NodeStatus, key_bytes),
InformationRequestTag::LatestSwitchBlockHeader => {
(InformationRequest::LatestSwitchBlockHeader, key_bytes)
}
};
if !remainder.is_empty() {
return Err(bytesrepr::Error::LeftOverBytes);
Expand Down Expand Up @@ -284,12 +297,14 @@ pub enum InformationRequestTag {
ChainspecRawBytes = 13,
/// Node status request.
NodeStatus = 14,
/// Latest switch block header request.
LatestSwitchBlockHeader = 15,
}

impl InformationRequestTag {
#[cfg(test)]
pub(crate) fn random(rng: &mut TestRng) -> Self {
match rng.gen_range(0..15) {
match rng.gen_range(0..16) {
0 => InformationRequestTag::BlockHeader,
1 => InformationRequestTag::SignedBlock,
2 => InformationRequestTag::Transaction,
Expand All @@ -305,6 +320,7 @@ impl InformationRequestTag {
12 => InformationRequestTag::ConsensusStatus,
13 => InformationRequestTag::ChainspecRawBytes,
14 => InformationRequestTag::NodeStatus,
15 => InformationRequestTag::LatestSwitchBlockHeader,
_ => unreachable!(),
}
}
Expand All @@ -330,6 +346,7 @@ impl TryFrom<u16> for InformationRequestTag {
12 => Ok(InformationRequestTag::ConsensusStatus),
13 => Ok(InformationRequestTag::ChainspecRawBytes),
14 => Ok(InformationRequestTag::NodeStatus),
15 => Ok(InformationRequestTag::LatestSwitchBlockHeader),
_ => Err(UnknownInformationRequestTag(value)),
}
}
Expand Down
14 changes: 11 additions & 3 deletions types/src/binary_port/node_status.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
bytesrepr::{self, FromBytes, ToBytes},
AvailableBlockRange, BlockSynchronizerStatus, Digest, NextUpgrade, Peers, PublicKey, TimeDiff,
Timestamp,
AvailableBlockRange, BlockHash, BlockSynchronizerStatus, Digest, NextUpgrade, Peers, PublicKey,
TimeDiff, Timestamp,
};
use alloc::{string::String, vec::Vec};

Expand Down Expand Up @@ -42,6 +42,8 @@ pub struct NodeStatus {
pub available_block_range: AvailableBlockRange,
/// The status of the block synchronizer builders.
pub block_sync: BlockSynchronizerStatus,
/// The hash of the latest switch block.
pub latest_switch_block_hash: Option<BlockHash>,
}

impl NodeStatus {
Expand All @@ -63,6 +65,7 @@ impl NodeStatus {
last_progress: Timestamp::random(rng),
available_block_range: AvailableBlockRange::random(rng),
block_sync: BlockSynchronizerStatus::random(rng),
latest_switch_block_hash: rng.gen::<bool>().then_some(BlockHash::random(rng)),
}
}
}
Expand All @@ -82,6 +85,7 @@ impl FromBytes for NodeStatus {
let (last_progress, remainder) = Timestamp::from_bytes(remainder)?;
let (available_block_range, remainder) = AvailableBlockRange::from_bytes(remainder)?;
let (block_sync, remainder) = BlockSynchronizerStatus::from_bytes(remainder)?;
let (latest_switch_block_hash, remainder) = Option::<BlockHash>::from_bytes(remainder)?;
Ok((
NodeStatus {
peers,
Expand All @@ -97,6 +101,7 @@ impl FromBytes for NodeStatus {
last_progress,
available_block_range,
block_sync,
latest_switch_block_hash,
},
remainder,
))
Expand Down Expand Up @@ -125,6 +130,7 @@ impl ToBytes for NodeStatus {
last_progress,
available_block_range,
block_sync,
latest_switch_block_hash,
} = self;
peers.write_bytes(writer)?;
build_version.write_bytes(writer)?;
Expand All @@ -138,7 +144,8 @@ impl ToBytes for NodeStatus {
reactor_state.write_bytes(writer)?;
last_progress.write_bytes(writer)?;
available_block_range.write_bytes(writer)?;
block_sync.write_bytes(writer)
block_sync.write_bytes(writer)?;
latest_switch_block_hash.write_bytes(writer)
}

fn serialized_length(&self) -> usize {
Expand All @@ -155,6 +162,7 @@ impl ToBytes for NodeStatus {
+ self.last_progress.serialized_length()
+ self.available_block_range.serialized_length()
+ self.block_sync.serialized_length()
+ self.latest_switch_block_hash.serialized_length()
}
}

Expand Down

0 comments on commit bd731db

Please sign in to comment.