snarkvm_console_program/data/register/
mod.rsmod bytes;
mod parse;
mod serialize;
use crate::Access;
use snarkvm_console_network::prelude::*;
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Register<N: Network> {
Locator(u64),
Access(u64, Vec<Access<N>>),
}
impl<N: Network> Register<N> {
#[inline]
pub const fn locator(&self) -> u64 {
match self {
Self::Locator(locator) => *locator,
Self::Access(locator, _) => *locator,
}
}
}
impl<N: Network> Ord for Register<N> {
fn cmp(&self, other: &Self) -> Ordering {
self.locator().cmp(&other.locator())
}
}
impl<N: Network> PartialOrd for Register<N> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Identifier, U32};
use snarkvm_console_network::MainnetV0;
type CurrentNetwork = MainnetV0;
#[test]
fn test_register_partial_ord() -> Result<()> {
assert_eq!(
Some(Ordering::Equal),
Register::<CurrentNetwork>::Locator(0).partial_cmp(&Register::<CurrentNetwork>::Locator(0))
);
assert_eq!(
Some(Ordering::Less),
Register::<CurrentNetwork>::Locator(0).partial_cmp(&Register::<CurrentNetwork>::Locator(1))
);
assert_eq!(
Some(Ordering::Greater),
Register::<CurrentNetwork>::Locator(1).partial_cmp(&Register::<CurrentNetwork>::Locator(0))
);
assert_eq!(
Some(Ordering::Equal),
Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)]).partial_cmp(
&Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)])
)
);
assert_eq!(
Some(Ordering::Less),
Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)]).partial_cmp(
&Register::<CurrentNetwork>::Access(1, vec![Access::Member(Identifier::from_str("owner")?)])
)
);
assert_eq!(
Some(Ordering::Greater),
Register::<CurrentNetwork>::Access(1, vec![Access::Member(Identifier::from_str("owner")?)]).partial_cmp(
&Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)])
)
);
assert_eq!(
Some(Ordering::Equal),
Register::<CurrentNetwork>::Access(0, vec![Access::Index(U32::new(0))]).partial_cmp(&Register::<
CurrentNetwork,
>::Access(
0,
vec![Access::Index(U32::new(0))]
))
);
assert_eq!(
Some(Ordering::Less),
Register::<CurrentNetwork>::Access(0, vec![Access::Index(U32::new(0))]).partial_cmp(&Register::<
CurrentNetwork,
>::Access(
1,
vec![Access::Index(U32::new(0))]
))
);
assert_eq!(
Some(Ordering::Greater),
Register::<CurrentNetwork>::Access(1, vec![Access::Index(U32::new(0))]).partial_cmp(&Register::<
CurrentNetwork,
>::Access(
0,
vec![Access::Index(U32::new(0))]
))
);
assert_eq!(
Some(Ordering::Equal),
Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
])
.partial_cmp(&Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
]))
);
assert_eq!(
Some(Ordering::Less),
Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
])
.partial_cmp(&Register::<CurrentNetwork>::Access(1, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
]))
);
assert_eq!(
Some(Ordering::Greater),
Register::<CurrentNetwork>::Access(1, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
])
.partial_cmp(&Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
]))
);
Ok(())
}
#[test]
fn test_register_eq() -> Result<()> {
assert_eq!(Register::<CurrentNetwork>::Locator(0), Register::<CurrentNetwork>::Locator(0));
assert_ne!(Register::<CurrentNetwork>::Locator(0), Register::<CurrentNetwork>::Locator(1));
assert_ne!(Register::<CurrentNetwork>::Locator(0), Register::<CurrentNetwork>::Locator(2));
assert_ne!(Register::<CurrentNetwork>::Locator(0), Register::<CurrentNetwork>::Locator(3));
assert_ne!(Register::<CurrentNetwork>::Locator(0), Register::<CurrentNetwork>::Locator(4));
assert_eq!(
Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)]),
Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)]),
Register::<CurrentNetwork>::Access(1, vec![Access::Member(Identifier::from_str("owner")?)])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)]),
Register::<CurrentNetwork>::Access(2, vec![Access::Member(Identifier::from_str("owner")?)])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)]),
Register::<CurrentNetwork>::Access(3, vec![Access::Member(Identifier::from_str("owner")?)])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![Access::Member(Identifier::from_str("owner")?)]),
Register::<CurrentNetwork>::Access(4, vec![Access::Member(Identifier::from_str("owner")?)])
);
assert_eq!(
Register::<CurrentNetwork>::Access(0, vec![Access::Index(U32::new(0))]),
Register::<CurrentNetwork>::Access(0, vec![Access::Index(U32::new(0))])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![Access::Index(U32::new(0))]),
Register::<CurrentNetwork>::Access(1, vec![Access::Index(U32::new(0))])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![Access::Index(U32::new(0))]),
Register::<CurrentNetwork>::Access(2, vec![Access::Index(U32::new(0))])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![Access::Index(U32::new(0))]),
Register::<CurrentNetwork>::Access(3, vec![Access::Index(U32::new(0))])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![Access::Index(U32::new(0))]),
Register::<CurrentNetwork>::Access(4, vec![Access::Index(U32::new(0))])
);
assert_eq!(
Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
]),
Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
]),
Register::<CurrentNetwork>::Access(1, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
]),
Register::<CurrentNetwork>::Access(2, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
]),
Register::<CurrentNetwork>::Access(3, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
])
);
assert_ne!(
Register::<CurrentNetwork>::Access(0, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
]),
Register::<CurrentNetwork>::Access(4, vec![
Access::Member(Identifier::from_str("owner")?),
Access::Index(U32::new(0))
])
);
Ok(())
}
}