Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create CompactSize64 for non-message-length fields #3008

Merged
merged 9 commits into from
Nov 4, 2021
2 changes: 1 addition & 1 deletion book/src/dev/rfcs/0010-v5-transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Orchard uses `Halo2Proof`s with corresponding signature type changes. Each Orcha
[other-transaction-v5-changes]: #other-transaction-v5-changes

V5 transactions split `Spend`s, `Output`s, and `AuthorizedAction`s into multiple arrays,
with a single `compactsize` count before the first array. We add new
with a single `CompactSize` count before the first array. We add new
`zcash_deserialize_external_count` and `zcash_serialize_external_count` utility functions,
which make it easier to serialize and deserialize these arrays correctly.

Expand Down
16 changes: 9 additions & 7 deletions zebra-chain/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! `ReadZcashExt`, extension traits for `io::Read` and `io::Write` with utility functions
//! for reading and writing data (e.g., the Bitcoin variable-integer format).

mod compact_size;
mod constraint;
mod date_time;
mod error;
Expand All @@ -14,13 +15,17 @@ mod write_zcash;
mod zcash_deserialize;
mod zcash_serialize;

pub(crate) mod serde_helpers;

pub mod sha256d;

pub(crate) mod serde_helpers;

#[cfg(any(test, feature = "proptest-impl"))]
pub mod arbitrary;

#[cfg(test)]
pub mod tests;

pub use compact_size::{CompactSize64, CompactSizeMessage};
pub use constraint::AtLeastOne;
pub use date_time::{DateTime32, Duration32};
pub use error::SerializationError;
Expand All @@ -31,9 +36,6 @@ pub use zcash_deserialize::{
ZcashDeserialize, ZcashDeserializeInto,
};
pub use zcash_serialize::{
zcash_serialize_bytes, zcash_serialize_bytes_external_count, zcash_serialize_external_count,
FakeWriter, ZcashSerialize, MAX_PROTOCOL_MESSAGE_LEN,
zcash_serialize_bytes, zcash_serialize_bytes_external_count, zcash_serialize_empty_list,
zcash_serialize_external_count, FakeWriter, ZcashSerialize, MAX_PROTOCOL_MESSAGE_LEN,
};

#[cfg(test)]
pub mod tests;
23 changes: 21 additions & 2 deletions zebra-chain/src/serialization/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! Arbitrary data generation for serialization proptests

use super::{read_zcash::canonical_socket_addr, DateTime32};
use std::{convert::TryInto, net::SocketAddr};

use chrono::{TimeZone, Utc, MAX_DATETIME, MIN_DATETIME};
use proptest::{arbitrary::any, prelude::*};
use std::net::SocketAddr;

use super::{
read_zcash::canonical_socket_addr, CompactSizeMessage, DateTime32, MAX_PROTOCOL_MESSAGE_LEN,
};

impl Arbitrary for DateTime32 {
type Parameters = ();
Expand Down Expand Up @@ -62,3 +66,18 @@ pub fn datetime_u32() -> impl Strategy<Value = chrono::DateTime<Utc>> {
pub fn canonical_socket_addr_strategy() -> impl Strategy<Value = SocketAddr> {
any::<SocketAddr>().prop_map(canonical_socket_addr)
}

impl Arbitrary for CompactSizeMessage {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(0..=MAX_PROTOCOL_MESSAGE_LEN)
.prop_map(|size| {
size.try_into()
.expect("MAX_PROTOCOL_MESSAGE_LEN fits in CompactSizeMessage")
})
.boxed()
}

type Strategy = BoxedStrategy<Self>;
}
Loading