Arbitrary trait for eth2/types (#1040)

* Add the arbitrary type to eth2/types and their deps

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Wrap arbitrary in a feature flag

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Fix feature for types

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Fix comment

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Patch versioning

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Allow expanded crate reference for arbitrary 0.4.3

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Add arbitrary to remaining types

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* use cmp::min

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Derive Arbitrary trait for ValidatorStatus, TotalBalances and InclusionInfo

* Add CI check for state processing arbitrary faetures

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Fix indentation

Signed-off-by: Kirk Baird <baird.k@outlook.com>

Co-authored-by: Mehdi Zerouali <mehdi@sigmaprime.io>
This commit is contained in:
Kirk Baird
2020-05-06 09:12:28 +10:00
committed by GitHub
parent fcccf63d29
commit 611a0c7d19
70 changed files with 436 additions and 292 deletions

View File

@@ -88,3 +88,12 @@ impl<'de> Deserialize<'de> for AggregatePublicKey {
Ok(agg_sig)
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for AggregatePublicKey {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BLS_PUBLIC_KEY_BYTE_SIZE];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

View File

@@ -179,6 +179,15 @@ impl<'de> Deserialize<'de> for AggregateSignature {
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for AggregateSignature {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BLS_AGG_SIG_BYTE_SIZE];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}
#[cfg(test)]
mod tests {
use super::super::{Keypair, Signature};

View File

@@ -119,3 +119,12 @@ impl<'de> Deserialize<'de> for FakeAggregatePublicKey {
Ok(pubkey)
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for FakeAggregatePublicKey {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BLS_PUBLIC_KEY_BYTE_SIZE];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

View File

@@ -132,6 +132,15 @@ impl<'de> Deserialize<'de> for FakeAggregateSignature {
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for FakeAggregateSignature {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BLS_AGG_SIG_BYTE_SIZE];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}
#[cfg(test)]
mod tests {
use super::super::{Keypair, Signature};

View File

@@ -157,6 +157,15 @@ impl Hash for FakePublicKey {
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for FakePublicKey {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BLS_PUBLIC_KEY_BYTE_SIZE];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -114,6 +114,15 @@ impl<'de> Deserialize<'de> for FakeSignature {
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for FakeSignature {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BLS_SIG_BYTE_SIZE];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}
#[cfg(test)]
mod tests {
use super::super::Keypair;

View File

@@ -16,6 +16,9 @@ pub use crate::signature_bytes::SignatureBytes;
pub use milagro_bls::{compress_g2, hash_to_curve_g2};
pub use signature_set::{verify_signature_sets, SignatureSet};
#[cfg(feature = "arbitrary")]
pub use arbitrary;
#[cfg(feature = "fake_crypto")]
mod fake_aggregate_public_key;
#[cfg(feature = "fake_crypto")]

View File

@@ -252,5 +252,14 @@ macro_rules! bytes_struct {
Ok(signature)
}
}
#[cfg(feature = "arbitrary")]
impl $crate::arbitrary::Arbitrary for $name {
fn arbitrary(u: &mut $crate::arbitrary::Unstructured<'_>) -> $crate::arbitrary::Result<Self> {
let mut bytes = [0u8; $byte_size];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| $crate::arbitrary::Error::IncorrectFormat)
}
}
};
}

View File

@@ -145,6 +145,15 @@ impl Hash for PublicKey {
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for PublicKey {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BLS_PUBLIC_KEY_BYTE_SIZE];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -111,6 +111,15 @@ impl<'de> Deserialize<'de> for Signature {
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for Signature {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BLS_SIG_BYTE_SIZE];
u.fill_buffer(&mut bytes)?;
Self::from_bytes(&bytes).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}
#[cfg(test)]
mod tests {
use super::super::Keypair;