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

Add zcash_serialized_size() to ZcashSerialize trait #2824

Merged
merged 9 commits into from
Oct 6, 2021
10 changes: 9 additions & 1 deletion zebra-chain/src/serialization/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use proptest::prelude::*;

use std::io::Cursor;

use crate::serialization::{ReadZcashExt, WriteZcashExt};
use crate::serialization::{ReadZcashExt, WriteZcashExt, ZcashSerialize};

proptest! {
#[test]
Expand Down Expand Up @@ -35,4 +35,12 @@ proptest! {
prop_assert_eq!(bytes, expect_bytes);
}
}

#[test]
fn transaction_serialized_size(transaction in any::<crate::transaction::UnminedTx>()) {
zebra_test::init();

// just make sure all transactions are bigger than zero
assert!(transaction.transaction.zcash_serialized_size().unwrap() > 0);
}
}
6 changes: 6 additions & 0 deletions zebra-chain/src/serialization/zcash_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ pub trait ZcashSerialize: Sized {
self.zcash_serialize(&mut data)?;
Ok(data)
}

/// Get the size of `self` by using the `zcash_serialize_to_vec()` helper
/// to get a vector of bytes, then just apply `len()` on it.
fn zcash_serialized_size(&self) -> Result<usize, io::Error> {
Ok(self.zcash_serialize_to_vec()?.len())
}
}

/// Serialize a `Vec` as a compactsize number of items, then the items. This is
Expand Down