Skip to content

Commit

Permalink
Merge pull request #2136 from AleoHQ/feat/cert-v2
Browse files Browse the repository at this point in the history
Updates batch certificate to V2
  • Loading branch information
howardwu authored Oct 31, 2023
2 parents 1583ecb + 2e245e3 commit 55fe709
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 151 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions ledger/narwhal/batch-certificate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ edition = "2021"
default = [ ]
serial = [ "console/serial" ]
wasm = [ "console/wasm" ]
test-helpers = [ "narwhal-batch-header/test-helpers", "time" ]
test-helpers = [ "narwhal-batch-header/test-helpers" ]

[dependencies.console]
package = "snarkvm-console"
Expand All @@ -52,10 +52,6 @@ features = [ "serde" ]
version = "1.0"
features = [ "preserve_order" ]

[dependencies.time]
version = "0.3"
optional = true

[dev-dependencies.bincode]
version = "1.3"

Expand Down
102 changes: 69 additions & 33 deletions ledger/narwhal/batch-certificate/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,84 @@ impl<N: Network> FromBytes for BatchCertificate<N> {
// Read the version.
let version = u8::read_le(&mut reader)?;
// Ensure the version is valid.
if version != 1 {
return Err(error("Invalid batch version"));
if version != 1 && version != 2 {
return Err(error("Invalid batch certificate version"));
}

// Read the certificate ID.
let certificate_id = Field::read_le(&mut reader)?;
// Read the batch header.
let batch_header = BatchHeader::read_le(&mut reader)?;
// Read the number of signatures.
let num_signatures = u32::read_le(&mut reader)?;
// Read the signatures.
let mut signatures = IndexMap::with_capacity(num_signatures as usize);
for _ in 0..num_signatures {
// Read the signature.
let signature = Signature::read_le(&mut reader)?;
// Read the timestamp.
let timestamp = i64::read_le(&mut reader)?;
// Insert the signature and timestamp.
signatures.insert(signature, timestamp);
if version == 1 {
// Read the certificate ID.
let certificate_id = Field::read_le(&mut reader)?;
// Read the batch header.
let batch_header = BatchHeader::read_le(&mut reader)?;
// Read the number of signatures.
let num_signatures = u32::read_le(&mut reader)?;
// Read the signatures.
let mut signatures = IndexMap::with_capacity(num_signatures as usize);
for _ in 0..num_signatures {
// Read the signature.
let signature = Signature::read_le(&mut reader)?;
// Read the timestamp.
let timestamp = i64::read_le(&mut reader)?;
// Insert the signature and timestamp.
signatures.insert(signature, timestamp);
}
// Return the batch certificate.
Self::from_v1_deprecated(certificate_id, batch_header, signatures).map_err(error)
} else if version == 2 {
// Read the batch header.
let batch_header = BatchHeader::read_le(&mut reader)?;
// Read the number of signatures.
let num_signatures = u16::read_le(&mut reader)?;
// Read the signatures.
let mut signatures = IndexSet::with_capacity(num_signatures as usize);
for _ in 0..num_signatures {
// Read the signature.
let signature = Signature::read_le(&mut reader)?;
// Insert the signature.
signatures.insert(signature);
}
// Return the batch certificate.
Self::from(batch_header, signatures).map_err(error)
} else {
unreachable!("Invalid batch certificate version")
}
// Return the batch certificate.
Self::from(certificate_id, batch_header, signatures).map_err(|e| error(e.to_string()))
}
}

impl<N: Network> ToBytes for BatchCertificate<N> {
/// Writes the batch certificate to the buffer.
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
// Write the version.
1u8.write_le(&mut writer)?;
// Write the certificate ID.
self.certificate_id.write_le(&mut writer)?;
// Write the batch header.
self.batch_header.write_le(&mut writer)?;
// Write the number of signatures.
u32::try_from(self.signatures.len()).map_err(|e| error(e.to_string()))?.write_le(&mut writer)?;
// Write the signatures.
for (signature, timestamp) in &self.signatures {
// Write the signature.
signature.write_le(&mut writer)?;
// Write the timestamp.
timestamp.write_le(&mut writer)?;
match self {
Self::V1 { certificate_id, batch_header, signatures } => {
// Write the version.
1u8.write_le(&mut writer)?;
// Write the certificate ID.
certificate_id.write_le(&mut writer)?;
// Write the batch header.
batch_header.write_le(&mut writer)?;
// Write the number of signatures.
u32::try_from(signatures.len()).map_err(error)?.write_le(&mut writer)?;
// Write the signatures.
for (signature, timestamp) in signatures.iter() {
// Write the signature.
signature.write_le(&mut writer)?;
// Write the timestamp.
timestamp.write_le(&mut writer)?;
}
}
Self::V2 { batch_header, signatures } => {
// Write the version.
2u8.write_le(&mut writer)?;
// Write the batch header.
batch_header.write_le(&mut writer)?;
// Write the number of signatures.
u16::try_from(signatures.len()).map_err(error)?.write_le(&mut writer)?;
// Write the signatures.
for signature in signatures.iter() {
// Write the signature.
signature.write_le(&mut writer)?;
}
}
}
Ok(())
}
Expand Down
Loading

0 comments on commit 55fe709

Please sign in to comment.