Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions ledger/narwhal/batch-certificate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,7 @@ impl<N: Network> BatchCertificate<N> {
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()? as usize, "Invalid number of signatures");

// Ensure that the signature is from a unique signer and not from the author.
let signature_authors = signatures.iter().map(|signature| signature.to_address()).collect::<HashSet<_>>();
ensure!(
!signature_authors.contains(&batch_header.author()),
"The author's signature was included in the signers"
);
ensure!(signature_authors.len() == signatures.len(), "A duplicate author was found in the set of signatures");

Self::check_signature_basic(&batch_header, &signatures)?;
// Verify the signatures are valid.
cfg_iter!(signatures).try_for_each(|signature| {
if !signature.verify(&signature.to_address(), &[batch_header.batch_id()]) {
Expand All @@ -82,6 +72,20 @@ impl<N: Network> BatchCertificate<N> {
// Return the batch certificate.
Ok(Self { batch_header, signatures })
}

pub fn check_signature_basic(batch_header: &BatchHeader<N>, signatures: &IndexSet<Signature<N>>) -> Result<()> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you document this function? It should be clear what it does compared to the checks in BatchCertificate::from.

Maybe you could document BatchCertificate::from a little better as well.

// Ensure that the number of signatures is within bounds.
ensure!(signatures.len() <= Self::max_signatures()? as usize, "Invalid number of signatures");

// Ensure that the signature is from a unique signer and not from the author.
let signature_authors = signatures.iter().map(|signature| signature.to_address()).collect::<HashSet<_>>();
ensure!(
!signature_authors.contains(&batch_header.author()),
"The author's signature was included in the signers"
);
ensure!(signature_authors.len() == signatures.len(), "A duplicate author was found in the set of signatures");
Ok(())
}
}

impl<N: Network> PartialEq for BatchCertificate<N> {
Expand Down