Skip to content

Commit

Permalink
Merge pull request AleoNet#2606 from ljedrz/narwhal_event_ToBytes
Browse files Browse the repository at this point in the history
[Narwhal] use ToBytes and FromBytes in Events
  • Loading branch information
howardwu authored Aug 17, 2023
2 parents 548191d + eea1586 commit d074167
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 214 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.

3 changes: 0 additions & 3 deletions node/narwhal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ version = "1.0"
[dependencies.async-trait]
version = "0.1"

[dependencies.bincode]
version = "1.0"

[dependencies.bytes]
version = "1"

Expand Down
31 changes: 14 additions & 17 deletions node/narwhal/src/event/batch_certified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,29 @@ impl<N: Network> EventTrait for BatchCertified<N> {
fn name(&self) -> &'static str {
"BatchCertified"
}
}

/// Serializes the event into the buffer.
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
self.certificate.serialize_blocking_into(writer)
impl<N: Network> ToBytes for BatchCertified<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.certificate.write_le(&mut writer)?;
Ok(())
}
}

/// Deserializes the given buffer into an event.
#[inline]
fn deserialize(bytes: BytesMut) -> Result<Self> {
let reader = bytes.reader();

let certificate = Data::Buffer(reader.into_inner().freeze());
impl<N: Network> FromBytes for BatchCertified<N> {
fn read_le<R: Read>(reader: R) -> IoResult<Self> {
let certificate = Data::read_le(reader)?;

Ok(Self { certificate })
}
}

#[cfg(test)]
pub mod prop_tests {
use crate::{
event::{certificate_response::prop_tests::any_batch_certificate, EventTrait},
BatchCertified,
};
use bytes::{BufMut, BytesMut};
use crate::{event::certificate_response::prop_tests::any_batch_certificate, BatchCertified};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{BoxedStrategy, Strategy};
use snarkvm::console::prelude::{FromBytes, ToBytes};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -76,9 +73,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_batch_certified())] original: BatchCertified<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
BatchCertified::serialize(&original, &mut buf).unwrap();
BatchCertified::write_le(&original, &mut buf).unwrap();

let deserialized: BatchCertified<CurrentNetwork> = BatchCertified::deserialize(buf.get_ref().clone()).unwrap();
let deserialized: BatchCertified<CurrentNetwork> = BatchCertified::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(
original.certificate.deserialize_blocking().unwrap(),
deserialized.certificate.deserialize_blocking().unwrap()
Expand Down
37 changes: 18 additions & 19 deletions node/narwhal/src/event/batch_propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,35 @@ impl<N: Network> EventTrait for BatchPropose<N> {
fn name(&self) -> &'static str {
"BatchPropose"
}
}

/// Serializes the event into the buffer.
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
self.round.write_le(&mut *writer)?;
self.batch_header.serialize_blocking_into(writer)
impl<N: Network> ToBytes for BatchPropose<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.round.write_le(&mut writer)?;
self.batch_header.write_le(&mut writer)?;
Ok(())
}
}

/// Deserializes the given buffer into an event.
#[inline]
fn deserialize(bytes: BytesMut) -> Result<Self> {
let mut reader = bytes.reader();

impl<N: Network> FromBytes for BatchPropose<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let round = u64::read_le(&mut reader)?;
let batch_header = Data::Buffer(reader.into_inner().freeze());
let batch_header = Data::read_le(&mut reader)?;

Ok(Self { round, batch_header })
}
}

#[cfg(test)]
pub mod prop_tests {
use crate::{
event::{certificate_response::prop_tests::any_batch_header, EventTrait},
BatchPropose,
};
use bytes::{BufMut, BytesMut};
use crate::{event::certificate_response::prop_tests::any_batch_header, BatchPropose};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use snarkos_node_narwhal_committee::prop_tests::CommitteeContext;
use snarkvm::prelude::narwhal::Data;
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
prelude::narwhal::Data,
};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -84,9 +83,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_batch_propose())] original: BatchPropose<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
BatchPropose::serialize(&original, &mut buf).unwrap();
BatchPropose::write_le(&original, &mut buf).unwrap();

let deserialized: BatchPropose<CurrentNetwork> = BatchPropose::deserialize(buf.get_ref().clone()).unwrap();
let deserialized: BatchPropose<CurrentNetwork> = BatchPropose::read_le(buf.into_inner().reader()).unwrap();
// because of the Data enum, we cannot compare the structs directly even though it derives PartialEq
assert_eq!(original.round, deserialized.round);
assert_eq!(
Expand Down
42 changes: 19 additions & 23 deletions node/narwhal/src/event/batch_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,37 @@ impl<N: Network> EventTrait for BatchSignature<N> {
fn name(&self) -> &'static str {
"BatchSignature"
}
}

/// Serializes the event into the buffer.
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
self.batch_id.write_le(&mut *writer)?;
self.signature.write_le(&mut *writer)?;
self.timestamp.write_le(&mut *writer)?;
impl<N: Network> ToBytes for BatchSignature<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.batch_id.write_le(&mut writer)?;
self.signature.write_le(&mut writer)?;
self.timestamp.write_le(&mut writer)?;
Ok(())
}
}

/// Deserializes the given buffer into an event.
#[inline]
fn deserialize(bytes: BytesMut) -> Result<Self> {
let mut reader = bytes.reader();
Ok(Self {
batch_id: Field::read_le(&mut reader)?,
signature: Signature::read_le(&mut reader)?,
timestamp: i64::read_le(&mut reader)?,
})
impl<N: Network> FromBytes for BatchSignature<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let batch_id = Field::read_le(&mut reader)?;
let signature = Signature::read_le(&mut reader)?;
let timestamp = i64::read_le(&mut reader)?;

Ok(Self { batch_id, signature, timestamp })
}
}

#[cfg(test)]
pub mod prop_tests {
use crate::{
event::{
certificate_request::prop_tests::any_field,
challenge_response::prop_tests::any_signature,
EventTrait,
},
event::{certificate_request::prop_tests::any_field, challenge_response::prop_tests::any_signature},
helpers::now,
BatchSignature,
};
use bytes::{BufMut, BytesMut};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{BoxedStrategy, Just, Strategy};
use snarkvm::console::prelude::{FromBytes, ToBytes};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -84,9 +80,9 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_batch_signature())] original: BatchSignature<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
BatchSignature::serialize(&original, &mut buf).unwrap();
BatchSignature::write_le(&original, &mut buf).unwrap();

let deserialized: BatchSignature<CurrentNetwork> = BatchSignature::deserialize(buf.get_ref().clone()).unwrap();
let deserialized: BatchSignature<CurrentNetwork> = BatchSignature::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
29 changes: 15 additions & 14 deletions node/narwhal/src/event/certificate_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,17 @@ impl<N: Network> EventTrait for CertificateRequest<N> {
fn name(&self) -> &'static str {
"CertificateRequest"
}
}

/// Serializes the event into the buffer.
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
self.certificate_id.write_le(writer)?;
impl<N: Network> ToBytes for CertificateRequest<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.certificate_id.write_le(&mut writer)?;
Ok(())
}
}

/// Deserializes the given buffer into an event.
#[inline]
fn deserialize(bytes: BytesMut) -> Result<Self> {
let mut reader = bytes.reader();

impl<N: Network> FromBytes for CertificateRequest<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let certificate_id = Field::read_le(&mut reader)?;

Ok(Self { certificate_id })
Expand All @@ -60,10 +58,13 @@ impl<N: Network> EventTrait for CertificateRequest<N> {

#[cfg(test)]
pub mod prop_tests {
use crate::{event::EventTrait, helpers::storage::prop_tests::CryptoTestRng, CertificateRequest};
use bytes::{BufMut, BytesMut};
use crate::{helpers::storage::prop_tests::CryptoTestRng, CertificateRequest};
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use snarkvm::prelude::{Field, Uniform};
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
prelude::{Field, Uniform},
};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -79,10 +80,10 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_certificate_request())] original: CertificateRequest<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
CertificateRequest::serialize(&original, &mut buf).unwrap();
CertificateRequest::write_le(&original, &mut buf).unwrap();

let deserialized: CertificateRequest<CurrentNetwork> =
CertificateRequest::deserialize(buf.get_ref().clone()).unwrap();
CertificateRequest::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
29 changes: 15 additions & 14 deletions node/narwhal/src/event/certificate_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,17 @@ impl<N: Network> EventTrait for CertificateResponse<N> {
fn name(&self) -> &'static str {
"CertificateResponse"
}
}

/// Serializes the event into the buffer.
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
self.certificate.write_le(writer)?;
impl<N: Network> ToBytes for CertificateResponse<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.certificate.write_le(&mut writer)?;
Ok(())
}
}

/// Deserializes the given buffer into an event.
#[inline]
fn deserialize(bytes: BytesMut) -> Result<Self> {
let mut reader = bytes.reader();

impl<N: Network> FromBytes for CertificateResponse<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let certificate = BatchCertificate::read_le(&mut reader)?;

Ok(Self { certificate })
Expand All @@ -61,21 +59,24 @@ impl<N: Network> EventTrait for CertificateResponse<N> {
#[cfg(test)]
pub mod prop_tests {
use crate::{
event::{transmission_response::prop_tests::any_transmission, EventTrait},
event::transmission_response::prop_tests::any_transmission,
helpers::{
now,
storage::prop_tests::{sign_batch_header, CryptoTestRng},
},
CertificateResponse,
};
use bytes::{BufMut, BytesMut};
use bytes::{Buf, BufMut, BytesMut};
use proptest::{
collection::vec,
prelude::{any, BoxedStrategy, Just, Strategy},
sample::Selector,
};
use snarkos_node_narwhal_committee::prop_tests::{CommitteeContext, ValidatorSet};
use snarkvm::ledger::narwhal::{BatchCertificate, BatchHeader};
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
ledger::narwhal::{BatchCertificate, BatchHeader},
};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand Down Expand Up @@ -111,10 +112,10 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_certificate_response())] original: CertificateResponse<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
CertificateResponse::serialize(&original, &mut buf).unwrap();
CertificateResponse::write_le(&original, &mut buf).unwrap();

let deserialized: CertificateResponse<CurrentNetwork> =
CertificateResponse::deserialize(buf.get_ref().clone()).unwrap();
CertificateResponse::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
33 changes: 21 additions & 12 deletions node/narwhal/src/event/challenge_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,36 @@ impl<N: Network> EventTrait for ChallengeRequest<N> {
fn name(&self) -> &'static str {
"ChallengeRequest"
}
}

/// Serializes the event into the buffer.
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
Ok(bincode::serialize_into(writer, &(self.version, self.listener_port, self.address, self.nonce))?)
impl<N: Network> ToBytes for ChallengeRequest<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.version.write_le(&mut writer)?;
self.listener_port.write_le(&mut writer)?;
self.address.write_le(&mut writer)?;
self.nonce.write_le(&mut writer)?;
Ok(())
}
}

impl<N: Network> FromBytes for ChallengeRequest<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let version = u32::read_le(&mut reader)?;
let listener_port = u16::read_le(&mut reader)?;
let address = Address::<N>::read_le(&mut reader)?;
let nonce = u64::read_le(&mut reader)?;

/// Deserializes the given buffer into an event.
#[inline]
fn deserialize(bytes: BytesMut) -> Result<Self> {
let (version, listener_port, address, nonce) = bincode::deserialize_from(&mut bytes.reader())?;
Ok(Self { version, listener_port, address, nonce })
}
}

#[cfg(test)]
pub mod prop_tests {
use crate::{event::EventTrait, ChallengeRequest};
use bytes::{BufMut, BytesMut};
use crate::ChallengeRequest;
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use snarkos_node_narwhal_committee::prop_tests::any_valid_account;
use snarkvm::console::prelude::{FromBytes, ToBytes};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::Testnet3;
Expand All @@ -74,10 +83,10 @@ pub mod prop_tests {
#[proptest]
fn serialize_deserialize(#[strategy(any_challenge_request())] original: ChallengeRequest<CurrentNetwork>) {
let mut buf = BytesMut::default().writer();
ChallengeRequest::serialize(&original, &mut buf).unwrap();
ChallengeRequest::write_le(&original, &mut buf).unwrap();

let deserialized: ChallengeRequest<CurrentNetwork> =
ChallengeRequest::deserialize(buf.get_ref().clone()).unwrap();
ChallengeRequest::read_le(buf.into_inner().reader()).unwrap();
assert_eq!(original, deserialized);
}
}
Loading

0 comments on commit d074167

Please sign in to comment.