Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Reuse the BlockLocators s11n impl #3177

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 7 additions & 38 deletions node/router/messages/src/ping.rs
Expand Up @@ -16,7 +16,6 @@ use super::*;

use snarkvm::prelude::{FromBytes, ToBytes};

use indexmap::IndexMap;
use std::borrow::Cow;

#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -40,18 +39,7 @@ impl<N: Network> ToBytes for Ping<N> {
self.node_type.write_le(&mut writer)?;
if let Some(locators) = &self.block_locators {
1u8.write_le(&mut writer)?;

(locators.recents.len().min(u32::MAX as usize) as u32).write_le(&mut writer)?;
for (height, hash) in locators.recents.iter() {
height.write_le(&mut writer)?;
hash.write_le(&mut writer)?;
}

(locators.checkpoints.len().min(u32::MAX as usize) as u32).write_le(&mut writer)?;
for (height, hash) in locators.checkpoints.iter() {
height.write_le(&mut writer)?;
hash.write_le(&mut writer)?;
}
locators.write_le(&mut writer)?;
} else {
0u8.write_le(&mut writer)?;
}
Expand All @@ -66,32 +54,13 @@ impl<N: Network> FromBytes for Ping<N> {
let node_type = NodeType::read_le(&mut reader)?;

let selector = u8::read_le(&mut reader)?;
let block_locators = match selector {
0 => None,
1 => Some(BlockLocators::read_le(&mut reader)?),
_ => return Err(error("Invalid block locators marker")),
};

if selector == 0 {
Ok(Self { version, node_type, block_locators: None })
} else if selector == 1 {
let mut recents = IndexMap::new();
let num_recents = u32::read_le(&mut reader)?;
for _ in 0..num_recents {
let height = u32::read_le(&mut reader)?;
let hash = N::BlockHash::read_le(&mut reader)?;
recents.insert(height, hash);
}

let mut checkpoints = IndexMap::new();
let num_checkpoints = u32::read_le(&mut reader)?;
for _ in 0..num_checkpoints {
let height = u32::read_le(&mut reader)?;
let hash = N::BlockHash::read_le(&mut reader)?;
checkpoints.insert(height, hash);
}

let block_locators = Some(BlockLocators { recents, checkpoints });

Ok(Self { version, node_type, block_locators })
} else {
Err(error("Invalid selector of optional block locators in ping message"))
}
Ok(Self { version, node_type, block_locators })
}
}

Expand Down