use alloc::vec::Vec;
use crate::{
auction::EraId,
bytesrepr::{self, FromBytes, ToBytes},
CLType, CLTyped, PublicKey, URef, U512,
};
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct UnbondingPurse {
bonding_purse: URef,
unbonding_purse: URef,
validator_public_key: PublicKey,
unbonder_public_key: PublicKey,
era_of_creation: EraId,
amount: U512,
}
impl UnbondingPurse {
pub const fn new(
bonding_purse: URef,
unbonding_purse: URef,
validator_public_key: PublicKey,
unbonder_public_key: PublicKey,
era_of_creation: EraId,
amount: U512,
) -> Self {
Self {
bonding_purse,
unbonding_purse,
validator_public_key,
unbonder_public_key,
era_of_creation,
amount,
}
}
pub fn is_validator(&self) -> bool {
self.validator_public_key == self.unbonder_public_key
}
pub fn bonding_purse(&self) -> &URef {
&self.bonding_purse
}
pub fn unbonding_purse(&self) -> &URef {
&self.unbonding_purse
}
pub fn validator_public_key(&self) -> &PublicKey {
&self.validator_public_key
}
pub fn unbonder_public_key(&self) -> &PublicKey {
&self.unbonder_public_key
}
pub fn era_of_creation(&self) -> EraId {
self.era_of_creation
}
pub fn amount(&self) -> &U512 {
&self.amount
}
}
impl ToBytes for UnbondingPurse {
fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
let mut result = bytesrepr::allocate_buffer(self)?;
result.extend(&self.bonding_purse.to_bytes()?);
result.extend(&self.unbonding_purse.to_bytes()?);
result.extend(&self.validator_public_key.to_bytes()?);
result.extend(&self.unbonder_public_key.to_bytes()?);
result.extend(&self.era_of_creation.to_bytes()?);
result.extend(&self.amount.to_bytes()?);
Ok(result)
}
fn serialized_length(&self) -> usize {
self.bonding_purse.serialized_length()
+ self.unbonding_purse.serialized_length()
+ self.validator_public_key.serialized_length()
+ self.unbonder_public_key.serialized_length()
+ self.era_of_creation.serialized_length()
+ self.amount.serialized_length()
}
}
impl FromBytes for UnbondingPurse {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (bonding_purse, bytes) = FromBytes::from_bytes(bytes)?;
let (unbonding_purse, bytes) = FromBytes::from_bytes(bytes)?;
let (validator_public_key, bytes) = FromBytes::from_bytes(bytes)?;
let (unbonder_public_key, bytes) = FromBytes::from_bytes(bytes)?;
let (era_of_creation, bytes) = FromBytes::from_bytes(bytes)?;
let (amount, bytes) = FromBytes::from_bytes(bytes)?;
Ok((
UnbondingPurse {
bonding_purse,
unbonding_purse,
validator_public_key,
unbonder_public_key,
era_of_creation,
amount,
},
bytes,
))
}
}
impl CLTyped for UnbondingPurse {
fn cl_type() -> CLType {
CLType::Any
}
}
#[cfg(test)]
mod tests {
use once_cell::sync::Lazy;
use crate::{
auction::{EraId, UnbondingPurse},
bytesrepr, AccessRights, PublicKey, SecretKey, URef, U512,
};
const BONDING_PURSE: URef = URef::new([41; 32], AccessRights::READ_ADD_WRITE);
const UNBONDING_PURSE: URef = URef::new([42; 32], AccessRights::READ_ADD_WRITE);
const ERA_OF_WITHDRAWAL: EraId = EraId::max_value();
static VALIDATOR_PUBLIC_KEY: Lazy<PublicKey> =
Lazy::new(|| SecretKey::ed25519([42; SecretKey::ED25519_LENGTH]).into());
static UNBONDER_PUBLIC_KEY: Lazy<PublicKey> =
Lazy::new(|| SecretKey::ed25519([43; SecretKey::ED25519_LENGTH]).into());
static AMOUNT: Lazy<U512> = Lazy::new(|| U512::max_value() - 1);
#[test]
fn serialization_roundtrip() {
let unbonding_purse = UnbondingPurse {
bonding_purse: BONDING_PURSE,
unbonding_purse: UNBONDING_PURSE,
validator_public_key: *VALIDATOR_PUBLIC_KEY,
unbonder_public_key: *UNBONDER_PUBLIC_KEY,
era_of_creation: ERA_OF_WITHDRAWAL,
amount: *AMOUNT,
};
bytesrepr::test_serialization_roundtrip(&unbonding_purse);
}
#[test]
fn should_be_validator_condition() {
let validator_unbonding_purse = UnbondingPurse::new(
BONDING_PURSE,
UNBONDING_PURSE,
*VALIDATOR_PUBLIC_KEY,
*VALIDATOR_PUBLIC_KEY,
ERA_OF_WITHDRAWAL,
*AMOUNT,
);
assert!(validator_unbonding_purse.is_validator());
}
#[test]
fn should_be_delegator_condition() {
let delegator_unbonding_purse = UnbondingPurse::new(
BONDING_PURSE,
UNBONDING_PURSE,
*VALIDATOR_PUBLIC_KEY,
*UNBONDER_PUBLIC_KEY,
ERA_OF_WITHDRAWAL,
*AMOUNT,
);
assert!(!delegator_unbonding_purse.is_validator());
}
}