Skip to content

Commit

Permalink
token-group: Increase max size type to u64 (#7130)
Browse files Browse the repository at this point in the history
* Change max size to u64 in interface

* Update interface example and token client

* Update token CLI

* Update additional token2022 tests

* Update collection example

* Update JS client to bigint

* Update e2e tests

* Update additional e2e tests
  • Loading branch information
jacksondoherty authored Aug 20, 2024
1 parent 42d465a commit dc89cbe
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 82 deletions.
2 changes: 1 addition & 1 deletion token-collection/program/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub async fn setup_group(
mint: &Keypair,
mint_authority: &Keypair,
update_authority: Option<Pubkey>,
max_size: u32,
max_size: u64,
rent_lamports: u64,
space: usize,
) {
Expand Down
4 changes: 2 additions & 2 deletions token-collection/program/tests/token_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ async fn test_token_collection() {
.data;
let state = TlvStateBorrowed::unpack(&buffer).unwrap();
let collection = state.get_first_value::<TokenGroup>().unwrap();
assert_eq!(u32::from(collection.size), 3);
assert_eq!(u64::from(collection.size), 3);

// The "Snakes" collection should have 2 members
let buffer = context
Expand All @@ -337,7 +337,7 @@ async fn test_token_collection() {
.data;
let state = TlvStateBorrowed::unpack(&buffer).unwrap();
let collection = state.get_first_value::<TokenGroup>().unwrap();
assert_eq!(u32::from(collection.size), 2);
assert_eq!(u64::from(collection.size), 2);

// The "Python" should be a member of 2 collections
let buffer = context
Expand Down
2 changes: 1 addition & 1 deletion token-group/example/tests/initialize_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,5 @@ async fn test_initialize_group_member() {
let fetched_meta = TlvStateBorrowed::unpack(&member_account.data).unwrap();
let fetched_group_member_state = fetched_meta.get_first_value::<TokenGroupMember>().unwrap();
assert_eq!(fetched_group_member_state.group, group.pubkey());
assert_eq!(u32::from(fetched_group_member_state.member_number), 1);
assert_eq!(u64::from(fetched_group_member_state.member_number), 1);
}
8 changes: 4 additions & 4 deletions token-group/interface/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ For a group:

```rust
type OptionalNonZeroPubkey = Pubkey; // if all zeroes, interpreted as `None`
type PodU32 = [u8; 4];
type PodU64 = [u8; 8];
type Pubkey = [u8; 32];

/// Type discriminant: [214, 15, 63, 132, 49, 119, 209, 40]
Expand All @@ -107,9 +107,9 @@ pub struct TokenGroup {
/// belongs to a particular mint
pub mint: Pubkey,
/// The current number of group members
pub size: PodU32,
pub size: PodU64,
/// The maximum number of group members
pub max_size: PodU32,
pub max_size: PodU64,
}
```

Expand All @@ -125,7 +125,7 @@ pub struct TokenGroupMember {
/// The pubkey of the `TokenGroup`
pub group: Pubkey,
/// The member number
pub member_number: PodU32,
pub member_number: PodU64,
}
```

Expand Down
10 changes: 5 additions & 5 deletions token-group/interface/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
spl_pod::{
bytemuck::{pod_bytes_of, pod_from_bytes},
optional_keys::OptionalNonZeroPubkey,
primitives::PodU32,
primitives::PodU64,
},
};

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

/// Instruction data for updating the max size of a `Group`
Expand All @@ -32,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: PodU32,
pub max_size: PodU64,
}

/// Instruction data for updating the authority of a `Group`
Expand Down Expand Up @@ -155,7 +155,7 @@ pub fn initialize_group(
mint: &Pubkey,
mint_authority: &Pubkey,
update_authority: Option<Pubkey>,
max_size: u32,
max_size: u64,
) -> Instruction {
let update_authority = OptionalNonZeroPubkey::try_from(update_authority)
.expect("Failed to deserialize `Option<Pubkey>`");
Expand All @@ -180,7 +180,7 @@ pub fn update_group_max_size(
program_id: &Pubkey,
group: &Pubkey,
update_authority: &Pubkey,
max_size: u32,
max_size: u64,
) -> Instruction {
let data = TokenGroupInstruction::UpdateGroupMaxSize(UpdateGroupMaxSize {
max_size: max_size.into(),
Expand Down
30 changes: 15 additions & 15 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, primitives::PodU32},
spl_pod::{error::PodSliceError, optional_keys::OptionalNonZeroPubkey, primitives::PodU64},
};

/// Data struct for a `TokenGroup`
Expand All @@ -19,39 +19,39 @@ pub struct TokenGroup {
/// belongs to a particular mint
pub mint: Pubkey,
/// The current number of group members
pub size: PodU32,
pub size: PodU64,
/// The maximum number of group members
pub max_size: PodU32,
pub max_size: PodU64,
}

impl TokenGroup {
/// Creates a new `TokenGroup` state
pub fn new(mint: &Pubkey, update_authority: OptionalNonZeroPubkey, max_size: u32) -> Self {
pub fn new(mint: &Pubkey, update_authority: OptionalNonZeroPubkey, max_size: u64) -> Self {
Self {
mint: *mint,
update_authority,
size: PodU32::default(), // [0, 0, 0, 0]
size: PodU64::default(), // [0, 0, 0, 0, 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> {
pub fn update_max_size(&mut self, new_max_size: u64) -> Result<(), ProgramError> {
// The new max size cannot be less than the current size
if new_max_size < u32::from(self.size) {
if new_max_size < u64::from(self.size) {
return Err(TokenGroupError::SizeExceedsNewMaxSize.into());
}
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> {
pub fn increment_size(&mut self) -> Result<u64, ProgramError> {
// The new size cannot be greater than the max size
let new_size = u32::from(self.size)
let new_size = u64::from(self.size)
.checked_add(1)
.ok_or::<ProgramError>(PodSliceError::CalculationFailure.into())?;
if new_size > u32::from(self.max_size) {
if new_size > u64::from(self.max_size) {
return Err(TokenGroupError::SizeExceedsMaxSize.into());
}
self.size = new_size.into();
Expand All @@ -70,11 +70,11 @@ pub struct TokenGroupMember {
/// The pubkey of the `TokenGroup`
pub group: Pubkey,
/// The member number
pub member_number: PodU32,
pub member_number: PodU64,
}
impl TokenGroupMember {
/// Creates a new `TokenGroupMember` state
pub fn new(mint: &Pubkey, group: &Pubkey, member_number: u32) -> Self {
pub fn new(mint: &Pubkey, group: &Pubkey, member_number: u64) -> Self {
Self {
mint: *mint,
group: *group,
Expand Down Expand Up @@ -156,7 +156,7 @@ mod tests {

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

// Change the current size to 30
group.size = 30.into();
Expand All @@ -170,7 +170,7 @@ mod tests {

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

#[test]
Expand All @@ -183,7 +183,7 @@ mod tests {
};

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

// Try to increase the current size to 2, which is greater than the max size
assert_eq!(
Expand Down
10 changes: 5 additions & 5 deletions token-group/js/src/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getBytesEncoder,
getStructEncoder,
getTupleEncoder,
getU32Encoder,
getU64Encoder,
transformEncoder,
} from '@solana/codecs';
import { splDiscriminate } from '@solana/spl-type-length-value';
Expand All @@ -28,7 +28,7 @@ export interface InitializeGroupInstruction {
mint: PublicKey;
mintAuthority: PublicKey;
updateAuthority: PublicKey | null;
maxSize: number;
maxSize: bigint;
}

export function createInitializeGroupInstruction(args: InitializeGroupInstruction): TransactionInstruction {
Expand All @@ -46,7 +46,7 @@ export function createInitializeGroupInstruction(args: InitializeGroupInstructio
splDiscriminate('spl_token_group_interface:initialize_token_group'),
getStructEncoder([
['updateAuthority', getPublicKeyEncoder()],
['maxSize', getU32Encoder()],
['maxSize', getU64Encoder()],
]),
).encode({ updateAuthority: updateAuthority ?? SystemProgram.programId, maxSize }),
),
Expand All @@ -57,7 +57,7 @@ export interface UpdateGroupMaxSize {
programId: PublicKey;
group: PublicKey;
updateAuthority: PublicKey;
maxSize: number;
maxSize: bigint;
}

export function createUpdateGroupMaxSizeInstruction(args: UpdateGroupMaxSize): TransactionInstruction {
Expand All @@ -71,7 +71,7 @@ export function createUpdateGroupMaxSizeInstruction(args: UpdateGroupMaxSize): T
data: Buffer.from(
getInstructionEncoder(
splDiscriminate('spl_token_group_interface:update_group_max_size'),
getStructEncoder([['maxSize', getU32Encoder()]]),
getStructEncoder([['maxSize', getU64Encoder()]]),
).encode({ maxSize }),
),
});
Expand Down
10 changes: 5 additions & 5 deletions token-group/js/src/state/tokenGroup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PublicKey } from '@solana/web3.js';
import type { ReadonlyUint8Array } from '@solana/codecs';
import { fixCodecSize, getBytesCodec, getStructCodec, getU32Codec } from '@solana/codecs';
import { fixCodecSize, getBytesCodec, getStructCodec, getU64Codec } from '@solana/codecs';

const tokenGroupCodec = getStructCodec([
['updateAuthority', fixCodecSize(getBytesCodec(), 32)],
['mint', fixCodecSize(getBytesCodec(), 32)],
['size', getU32Codec()],
['maxSize', getU32Codec()],
['size', getU64Codec()],
['maxSize', getU64Codec()],
]);

export const TOKEN_GROUP_SIZE = tokenGroupCodec.fixedSize;
Expand All @@ -17,9 +17,9 @@ export interface TokenGroup {
/** The associated mint, used to counter spoofing to be sure that group belongs to a particular mint */
mint: PublicKey;
/** The current number of group members */
size: number;
size: bigint;
/** The maximum number of group members */
maxSize: number;
maxSize: bigint;
}

// Checks if all elements in the array are 0
Expand Down
6 changes: 3 additions & 3 deletions token-group/js/src/state/tokenGroupMember.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PublicKey } from '@solana/web3.js';
import type { ReadonlyUint8Array } from '@solana/codecs';
import { fixCodecSize, getBytesCodec, getStructCodec, getU32Codec } from '@solana/codecs';
import { fixCodecSize, getBytesCodec, getStructCodec, getU64Codec } from '@solana/codecs';

const tokenGroupMemberCodec = getStructCodec([
['mint', fixCodecSize(getBytesCodec(), 32)],
['group', fixCodecSize(getBytesCodec(), 32)],
['memberNumber', getU32Codec()],
['memberNumber', getU64Codec()],
]);

export const TOKEN_GROUP_MEMBER_SIZE = tokenGroupMemberCodec.fixedSize;
Expand All @@ -16,7 +16,7 @@ export interface TokenGroupMember {
/** The pubkey of the `TokenGroup` */
group: PublicKey;
/** The member number */
memberNumber: number;
memberNumber: bigint;
}

// Pack TokenGroupMember into byte slab
Expand Down
8 changes: 4 additions & 4 deletions token-group/js/test/instruction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import type { Decoder } from '@solana/codecs';
import { fixDecoderSize, getBytesDecoder, getStructDecoder, getU32Decoder } from '@solana/codecs';
import { fixDecoderSize, getBytesDecoder, getStructDecoder, getU64Decoder } from '@solana/codecs';
import { splDiscriminate } from '@solana/spl-type-length-value';
import { PublicKey, type TransactionInstruction } from '@solana/web3.js';

Expand Down Expand Up @@ -28,7 +28,7 @@ describe('Token Group Instructions', () => {
const updateAuthority = new PublicKey('44444444444444444444444444444444444444444444');
const mint = new PublicKey('55555555555555555555555555555555555555555555');
const mintAuthority = new PublicKey('66666666666666666666666666666666666666666666');
const maxSize = 100;
const maxSize = BigInt(100);

it('Can create InitializeGroup Instruction', () => {
checkPackUnpack(
Expand All @@ -43,7 +43,7 @@ describe('Token Group Instructions', () => {
splDiscriminate('spl_token_group_interface:initialize_token_group'),
getStructDecoder([
['updateAuthority', fixDecoderSize(getBytesDecoder(), 32)],
['maxSize', getU32Decoder()],
['maxSize', getU64Decoder()],
]),
{ updateAuthority: Uint8Array.from(updateAuthority.toBuffer()), maxSize },
);
Expand All @@ -58,7 +58,7 @@ describe('Token Group Instructions', () => {
maxSize,
}),
splDiscriminate('spl_token_group_interface:update_group_max_size'),
getStructDecoder([['maxSize', getU32Decoder()]]),
getStructDecoder([['maxSize', getU64Decoder()]]),
{ maxSize },
);
});
Expand Down
10 changes: 5 additions & 5 deletions token-group/js/test/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ describe('Token Group State', () => {
checkPackUnpack({
mint: new PublicKey('44444444444444444444444444444444444444444444'),
updateAuthority: new PublicKey('55555555555555555555555555555555555555555555'),
size: 10,
maxSize: 20,
size: BigInt(10),
maxSize: BigInt(20),
});
});

it('Can pack and unpack TokenGroup without updateAuthoritygroup', () => {
checkPackUnpack({
mint: new PublicKey('44444444444444444444444444444444444444444444'),
size: 10,
maxSize: 20,
size: BigInt(10),
maxSize: BigInt(20),
});
});
});
Expand All @@ -40,7 +40,7 @@ describe('Token Group State', () => {
checkPackUnpack({
mint: new PublicKey('55555555555555555555555555555555555555555555'),
group: new PublicKey('66666666666666666666666666666666666666666666'),
memberNumber: 8,
memberNumber: BigInt(8),
});
});
});
Expand Down
Loading

0 comments on commit dc89cbe

Please sign in to comment.