Skip to content

Commit

Permalink
Fix type compatibility compilation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Dec 27, 2023
1 parent 777d7ef commit fb52645
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions ledger/narwhal/batch-certificate/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<N: Network> FromBytes for BatchCertificate<N> {
// Read the number of signatures.
let num_signatures = u32::read_le(&mut reader)?;
// Ensure the number of signatures is within bounds.
if num_signatures as usize > Self::MAX_SIGNATURES {
if num_signatures > Self::MAX_SIGNATURES as u32 {
return Err(error(format!(
"Number of signatures ({num_signatures}) exceeds the maximum ({})",
Self::MAX_SIGNATURES
Expand All @@ -56,7 +56,7 @@ impl<N: Network> FromBytes for BatchCertificate<N> {
// Read the number of signatures.
let num_signatures = u16::read_le(&mut reader)?;
// Ensure the number of signatures is within bounds.
if num_signatures as usize > Self::MAX_SIGNATURES {
if num_signatures > Self::MAX_SIGNATURES {
return Err(error(format!(
"Number of signatures ({num_signatures}) exceeds the maximum ({})",
Self::MAX_SIGNATURES
Expand Down
6 changes: 3 additions & 3 deletions ledger/narwhal/batch-certificate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum BatchCertificate<N: Network> {

impl<N: Network> BatchCertificate<N> {
/// The maximum number of signatures in a batch certificate.
pub const MAX_SIGNATURES: usize = BatchHeader::<N>::MAX_CERTIFICATES;
pub const MAX_SIGNATURES: u16 = BatchHeader::<N>::MAX_CERTIFICATES;
}

impl<N: Network> BatchCertificate<N> {
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<N: Network> BatchCertificate<N> {
N::hash_bhp1024(&preimage.to_bits_le())
}
// Ensure that the number of signatures is within bounds.
ensure!(signatures.len() <= Self::MAX_SIGNATURES, "Invalid number of signatures");
ensure!(signatures.len() <= Self::MAX_SIGNATURES as usize, "Invalid number of signatures");
// Compute the certificate ID.
if certificate_id != compute_certificate_id(batch_header.batch_id(), &signatures)? {
bail!("Invalid batch certificate ID")
Expand All @@ -103,7 +103,7 @@ impl<N: Network> BatchCertificate<N> {
/// Initializes a new batch certificate.
pub fn from(batch_header: BatchHeader<N>, signatures: IndexSet<Signature<N>>) -> Result<Self> {
// Ensure that the number of signatures is within bounds.
ensure!(signatures.len() <= Self::MAX_SIGNATURES, "Invalid number of signatures");
ensure!(signatures.len() <= Self::MAX_SIGNATURES as usize, "Invalid number of signatures");

// Verify the signatures are valid.
cfg_iter!(signatures).try_for_each(|signature| {
Expand Down
34 changes: 26 additions & 8 deletions ledger/narwhal/batch-header/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,22 @@ impl<N: Network> BatchHeader<N> {
}

// Ensure that the number of transmissions is within bounds.
ensure!(transmission_ids.len() <= Self::MAX_TRANSMISSIONS, "Invalid number of transmission ids");
ensure!(
transmission_ids.len() <= Self::MAX_TRANSMISSIONS_PER_BATCH,
"Invalid number of transmission ids ({})",
transmission_ids.len()
);
// Ensure that the number of previous certificate IDs is within bounds.
ensure!(previous_certificate_ids.len() <= Self::MAX_CERTIFICATES, "Invalid number of previous certificate IDs");
ensure!(
previous_certificate_ids.len() <= Self::MAX_CERTIFICATES as usize,
"Invalid number of previous certificate IDs ({})",
previous_certificate_ids.len()
);
// Ensure the number of last election certificate IDs is within bounds.
ensure!(
last_election_certificate_ids.len() <= Self::MAX_CERTIFICATES,
"Invalid number of last election certificate IDs"
last_election_certificate_ids.len() <= Self::MAX_CERTIFICATES as usize,
"Invalid number of last election certificate IDs ({})",
last_election_certificate_ids.len()
);

// Retrieve the address.
Expand Down Expand Up @@ -154,13 +163,22 @@ impl<N: Network> BatchHeader<N> {
}

// Ensure that the number of transmissions is within bounds.
ensure!(transmission_ids.len() <= Self::MAX_TRANSMISSIONS, "Invalid number of transmission ids");
ensure!(
transmission_ids.len() <= Self::MAX_TRANSMISSIONS_PER_BATCH,
"Invalid number of transmission ids ({})",
transmission_ids.len()
);
// Ensure that the number of previous certificate IDs is within bounds.
ensure!(previous_certificate_ids.len() <= Self::MAX_CERTIFICATES, "Invalid number of previous certificate IDs");
ensure!(
previous_certificate_ids.len() <= Self::MAX_CERTIFICATES as usize,
"Invalid number of previous certificate IDs ({})",
previous_certificate_ids.len()
);
// Ensure the number of last election certificate IDs is within bounds.
ensure!(
last_election_certificate_ids.len() <= Self::MAX_CERTIFICATES,
"Invalid number of last election certificate IDs"
last_election_certificate_ids.len() <= Self::MAX_CERTIFICATES as usize,
"Invalid number of last election certificate IDs ({})",
last_election_certificate_ids.len()
);

// Compute the batch ID.
Expand Down

0 comments on commit fb52645

Please sign in to comment.