diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index ae3bdae8..d8715dc9 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -24,7 +24,7 @@ fn byte_length(bound: usize) -> usize { type BitlistInner = BitVec; /// A homogenous collection of a variable number of boolean values. -#[derive(PartialEq, Eq, Clone)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)] pub struct Bitlist(BitlistInner); impl fmt::Debug for Bitlist { diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index c474323d..e4bcd3d3 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -33,7 +33,7 @@ type BitvectorInner = BitVec; // bitvec::array::BitArray where T: BitRegister, [T; {N / 8}]: BitViewSized // // Refer: -#[derive(PartialEq, Eq, Clone)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)] pub struct Bitvector(BitvectorInner); impl fmt::Debug for Bitvector { diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index abab9c56..40430dd6 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -13,7 +13,7 @@ use crate::{ }; /// A homogenous collection of a variable number of values. -#[derive(Clone)] +#[derive(PartialOrd, Ord, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))] pub struct List { data: Vec, @@ -334,7 +334,7 @@ impl<'de, T: Serializable + serde::Deserialize<'de>, const N: usize> serde::Dese #[cfg(test)] mod tests { use super::*; - use crate::{serialize, U256}; + use crate::{lib::cmp::Ordering, serialize, U256}; const COUNT: usize = 32; @@ -495,4 +495,25 @@ mod tests { let path = &[0.into()]; crate::proofs::tests::compute_and_verify_proof_for_path(&data, path) } + + #[test] + fn test_ord() { + type L = List; + let data = vec![1u8, 22]; + let input = L::try_from(data).unwrap(); + + let mut other = L::default(); + assert_eq!(input.cmp(&other), Ordering::Greater); + + other.push(0); + assert_eq!(input.cmp(&other), Ordering::Greater); + other.push(0); + assert_eq!(input.cmp(&other), Ordering::Greater); + other.push(0); + assert_eq!(input.cmp(&other), Ordering::Greater); + other.push(0); + assert_eq!(input.cmp(&other), Ordering::Greater); + other[0] = 244; + assert_eq!(input.cmp(&other), Ordering::Less); + } } diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 6b631884..c73956ac 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -15,7 +15,7 @@ use crate::{ /// A homogenous collection of a fixed number of values. /// /// NOTE: a `Vector` of length `0` is illegal. -#[derive(Clone)] +#[derive(PartialOrd, Ord, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))] pub struct Vector { data: Vec, @@ -321,7 +321,7 @@ impl<'de, T: Serializable + serde::Deserialize<'de>, const N: usize> serde::Dese #[cfg(test)] mod tests { use super::*; - use crate::{list::List, serialize, U256}; + use crate::{lib::cmp::Ordering, list::List, serialize, U256}; const COUNT: usize = 32; @@ -701,4 +701,46 @@ mod tests { } } } + + #[test] + fn test_ord() { + type V = Vector; + + let data = V::try_from(vec![ + U256::from(23), + U256::from(34), + U256::from(45), + U256::from(56), + U256::from(67), + U256::from(78), + U256::from(11), + ]) + .unwrap(); + + let other = V::try_from(vec![ + U256::from(23), + U256::from(34), + U256::from(45), + U256::from(56), + U256::from(67), + U256::from(78), + U256::from(11), + ]) + .unwrap(); + + assert_eq!(data.cmp(&other), Ordering::Equal); + + let another = V::try_from(vec![ + U256::from(23), + U256::from(34), + U256::from(45), + U256::from(56), + U256::from(67), + U256::from(78), + U256::from(12), + ]) + .unwrap(); + + assert_eq!(data.cmp(&another), Ordering::Less); + } }