Skip to content

Commit

Permalink
swap u32 for PodU32
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Oct 17, 2023
1 parent b8cd911 commit 03c53db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
19 changes: 12 additions & 7 deletions token-group/interface/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
spl_pod::{
bytemuck::{pod_bytes_of, pod_from_bytes},
optional_keys::OptionalNonZeroPubkey,
primitives::PodU32,
},
};

Expand All @@ -22,7 +23,7 @@ pub struct InitializeGroup {
/// Update authority for the group
pub update_authority: OptionalNonZeroPubkey,
/// The maximum number of group members
pub max_size: u32,
pub max_size: PodU32,
}

/// Instruction data for updating the max size of a `Group`
Expand All @@ -31,7 +32,7 @@ pub struct InitializeGroup {
#[discriminator_hash_input("spl_token_group_interface:update_group_max_size")]
pub struct UpdateGroupMaxSize {
/// New max size for the group
pub max_size: u32,
pub max_size: PodU32,
}

/// Instruction data for updating the authority of a `Group`
Expand Down Expand Up @@ -159,7 +160,7 @@ pub fn initialize_group(
.expect("Failed to deserialize `Option<Pubkey>`");
let data = TokenGroupInterfaceInstruction::InitializeGroup(InitializeGroup {
update_authority,
max_size,
max_size: max_size.into(),
})
.pack();
let mut accounts = vec![
Expand All @@ -183,8 +184,10 @@ pub fn update_group_max_size(
update_authority: &Pubkey,
max_size: u32,
) -> Instruction {
let data =
TokenGroupInterfaceInstruction::UpdateGroupMaxSize(UpdateGroupMaxSize { max_size }).pack();
let data = TokenGroupInterfaceInstruction::UpdateGroupMaxSize(UpdateGroupMaxSize {
max_size: max_size.into(),
})
.pack();
Instruction {
program_id: *program_id,
accounts: vec![
Expand Down Expand Up @@ -269,7 +272,7 @@ mod test {
fn initialize_group_pack() {
let data = InitializeGroup {
update_authority: OptionalNonZeroPubkey::default(),
max_size: 100,
max_size: 100.into(),
};
let instruction = TokenGroupInterfaceInstruction::InitializeGroup(data);
let preimage = hash::hashv(&[format!("{NAMESPACE}:initialize_group").as_bytes()]);
Expand All @@ -279,7 +282,9 @@ mod test {

#[test]
fn update_group_max_size_pack() {
let data = UpdateGroupMaxSize { max_size: 200 };
let data = UpdateGroupMaxSize {
max_size: 200.into(),
};
let instruction = TokenGroupInterfaceInstruction::UpdateGroupMaxSize(data);
let preimage = hash::hashv(&[format!("{NAMESPACE}:update_group_max_size").as_bytes()]);
let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
Expand Down
43 changes: 21 additions & 22 deletions token-group/interface/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
bytemuck::{Pod, Zeroable},
solana_program::{program_error::ProgramError, pubkey::Pubkey},
spl_discriminator::SplDiscriminate,
spl_pod::{error::PodSliceError, optional_keys::OptionalNonZeroPubkey},
spl_pod::{error::PodSliceError, optional_keys::OptionalNonZeroPubkey, primitives::PodU32},
};

/// Data struct for a `Group`
Expand All @@ -16,43 +16,42 @@ pub struct Group {
/// The authority that can sign to update the group
pub update_authority: OptionalNonZeroPubkey,
/// The current number of group members
pub size: u32,
pub size: PodU32,
/// The maximum number of group members
pub max_size: u32,
pub max_size: PodU32,
}

impl Group {
/// Creates a new `Group` state
pub fn new(update_authority: OptionalNonZeroPubkey, max_size: u32) -> Self {
Self {
update_authority,
size: 0,
max_size,
size: PodU32::default(), // [0, 0, 0, 0]
max_size: max_size.into(),
}
}

/// Updates the max size for a group
pub fn update_max_size(&mut self, new_max_size: u32) -> Result<(), ProgramError> {
// The new max size cannot be less than the current size
if new_max_size < self.size {
if new_max_size < u32::from(self.size) {
return Err(TokenGroupError::SizeExceedsNewMaxSize.into());
}
self.max_size = new_max_size;
self.max_size = new_max_size.into();
Ok(())
}

/// Increment the size for a group, returning the new size
pub fn increment_size(&mut self) -> Result<u32, ProgramError> {
// The new size cannot be greater than the max size
let new_size = self
.size
let new_size = u32::from(self.size)
.checked_add(1)
.ok_or::<ProgramError>(PodSliceError::CalculationFailure.into())?;
if new_size > self.max_size {
if new_size > u32::from(self.max_size) {
return Err(TokenGroupError::SizeExceedsMaxSize.into());
}
self.size = new_size;
Ok(self.size)
self.size = new_size.into();
Ok(new_size)
}
}

Expand Down Expand Up @@ -105,8 +104,8 @@ mod tests {
// Make sure we can pack more than one instance of each type
let group = Group {
update_authority: OptionalNonZeroPubkey::try_from(Some(Pubkey::new_unique())).unwrap(),
size: 10,
max_size: 20,
size: 10.into(),
max_size: 20.into(),
};

let member = Member {
Expand Down Expand Up @@ -137,16 +136,16 @@ mod tests {
let max_size = 10;
let mut group = Group {
update_authority: OptionalNonZeroPubkey::try_from(Some(Pubkey::new_unique())).unwrap(),
size: 0,
max_size,
size: 0.into(),
max_size: max_size.into(),
};

let new_max_size = 30;
group.update_max_size(new_max_size).unwrap();
assert_eq!(group.max_size, new_max_size);
assert_eq!(u32::from(group.max_size), new_max_size);

// Change the current size to 30
group.size = 30;
group.size = 30.into();

// Try to set the max size to 20, which is less than the current size
let new_max_size = 20;
Expand All @@ -157,19 +156,19 @@ mod tests {

let new_max_size = 30;
group.update_max_size(new_max_size).unwrap();
assert_eq!(group.max_size, new_max_size);
assert_eq!(u32::from(group.max_size), new_max_size);
}

#[test]
fn increment_current_size() {
let mut group = Group {
update_authority: OptionalNonZeroPubkey::try_from(Some(Pubkey::new_unique())).unwrap(),
size: 0,
max_size: 1,
size: 0.into(),
max_size: 1.into(),
};

group.increment_size().unwrap();
assert_eq!(group.size, 1);
assert_eq!(u32::from(group.size), 1);

// Try to increase the current size to 2, which is greater than the max size
assert_eq!(
Expand Down

0 comments on commit 03c53db

Please sign in to comment.