From fb526454e9f15ffd84939c5c8287904bec4092e0 Mon Sep 17 00:00:00 2001 From: Howard Wu <9260812+howardwu@users.noreply.github.com> Date: Tue, 26 Dec 2023 16:48:26 -0800 Subject: [PATCH] Fix type compatibility compilation issues --- ledger/narwhal/batch-certificate/src/bytes.rs | 4 +-- ledger/narwhal/batch-certificate/src/lib.rs | 6 ++-- ledger/narwhal/batch-header/src/lib.rs | 34 ++++++++++++++----- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/ledger/narwhal/batch-certificate/src/bytes.rs b/ledger/narwhal/batch-certificate/src/bytes.rs index 446a0d4193..baa793c1ff 100644 --- a/ledger/narwhal/batch-certificate/src/bytes.rs +++ b/ledger/narwhal/batch-certificate/src/bytes.rs @@ -32,7 +32,7 @@ impl FromBytes for BatchCertificate { // 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 @@ -56,7 +56,7 @@ impl FromBytes for BatchCertificate { // 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 diff --git a/ledger/narwhal/batch-certificate/src/lib.rs b/ledger/narwhal/batch-certificate/src/lib.rs index 3e9fede619..c053433e5c 100644 --- a/ledger/narwhal/batch-certificate/src/lib.rs +++ b/ledger/narwhal/batch-certificate/src/lib.rs @@ -54,7 +54,7 @@ pub enum BatchCertificate { impl BatchCertificate { /// The maximum number of signatures in a batch certificate. - pub const MAX_SIGNATURES: usize = BatchHeader::::MAX_CERTIFICATES; + pub const MAX_SIGNATURES: u16 = BatchHeader::::MAX_CERTIFICATES; } impl BatchCertificate { @@ -84,7 +84,7 @@ impl BatchCertificate { 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") @@ -103,7 +103,7 @@ impl BatchCertificate { /// Initializes a new batch certificate. pub fn from(batch_header: BatchHeader, signatures: IndexSet>) -> Result { // 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| { diff --git a/ledger/narwhal/batch-header/src/lib.rs b/ledger/narwhal/batch-header/src/lib.rs index 8bfe2d8673..237406a4ec 100644 --- a/ledger/narwhal/batch-header/src/lib.rs +++ b/ledger/narwhal/batch-header/src/lib.rs @@ -94,13 +94,22 @@ impl BatchHeader { } // 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. @@ -154,13 +163,22 @@ impl BatchHeader { } // 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.