Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit db408b1

Browse files
authored
token-2022: Update alloc_and_serialize to use Pod types (#6343)
1 parent 970f1d5 commit db408b1

File tree

3 files changed

+71
-37
lines changed

3 files changed

+71
-37
lines changed

token/program-2022/src/extension/mod.rs

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,15 +1398,15 @@ impl Extension for AccountPaddingTest {
13981398
/// NOTE: Since this function deals with fixed-size extensions, it does not
13991399
/// handle _decreasing_ the size of an account's data buffer, like the function
14001400
/// `alloc_and_serialize_variable_len_extension` does.
1401-
pub fn alloc_and_serialize<S: BaseState + Pack, V: Default + Extension + Pod>(
1401+
pub(crate) fn alloc_and_serialize<S: BaseState + Pod, V: Default + Extension + Pod>(
14021402
account_info: &AccountInfo,
14031403
new_extension: &V,
14041404
overwrite: bool,
14051405
) -> Result<(), ProgramError> {
14061406
let previous_account_len = account_info.try_data_len()?;
14071407
let new_account_len = {
14081408
let data = account_info.try_borrow_data()?;
1409-
let state = StateWithExtensions::<S>::unpack(&data)?;
1409+
let state = PodStateWithExtensions::<S>::unpack(&data)?;
14101410
state.try_get_new_account_len::<V>()?
14111411
};
14121412

@@ -1418,7 +1418,7 @@ pub fn alloc_and_serialize<S: BaseState + Pack, V: Default + Extension + Pod>(
14181418
if previous_account_len <= BASE_ACCOUNT_LENGTH {
14191419
set_account_type::<S>(*buffer)?;
14201420
}
1421-
let mut state = StateWithExtensionsMut::<S>::unpack(&mut buffer)?;
1421+
let mut state = PodStateWithExtensionsMut::<S>::unpack(&mut buffer)?;
14221422

14231423
// Write the extension
14241424
let extension = state.init_extension::<V>(overwrite)?;
@@ -1435,8 +1435,8 @@ pub fn alloc_and_serialize<S: BaseState + Pack, V: Default + Extension + Pod>(
14351435
///
14361436
/// NOTE: Unlike the `reallocate` instruction, this function will reduce the
14371437
/// size of an account if it has too many bytes allocated for the given value.
1438-
pub fn alloc_and_serialize_variable_len_extension<
1439-
S: BaseState + Pack,
1438+
pub(crate) fn alloc_and_serialize_variable_len_extension<
1439+
S: BaseState + Pod,
14401440
V: Extension + VariableLenPack,
14411441
>(
14421442
account_info: &AccountInfo,
@@ -1446,7 +1446,7 @@ pub fn alloc_and_serialize_variable_len_extension<
14461446
let previous_account_len = account_info.try_data_len()?;
14471447
let (new_account_len, extension_already_exists) = {
14481448
let data = account_info.try_borrow_data()?;
1449-
let state = StateWithExtensions::<S>::unpack(&data)?;
1449+
let state = PodStateWithExtensions::<S>::unpack(&data)?;
14501450
let new_account_len =
14511451
state.try_get_new_account_len_for_variable_len_extension(new_extension)?;
14521452
let extension_already_exists = state.get_extension_bytes::<V>().is_ok();
@@ -1463,20 +1463,20 @@ pub fn alloc_and_serialize_variable_len_extension<
14631463
account_info.realloc(new_account_len, false)?;
14641464
let mut buffer = account_info.try_borrow_mut_data()?;
14651465
if extension_already_exists {
1466-
let mut state = StateWithExtensionsMut::<S>::unpack(&mut buffer)?;
1466+
let mut state = PodStateWithExtensionsMut::<S>::unpack(&mut buffer)?;
14671467
state.realloc_variable_len_extension(new_extension)?;
14681468
} else {
14691469
if previous_account_len <= BASE_ACCOUNT_LENGTH {
14701470
set_account_type::<S>(*buffer)?;
14711471
}
14721472
// now alloc in the TLV buffer and write the data
1473-
let mut state = StateWithExtensionsMut::<S>::unpack(&mut buffer)?;
1473+
let mut state = PodStateWithExtensionsMut::<S>::unpack(&mut buffer)?;
14741474
state.init_variable_len_extension(new_extension, false)?;
14751475
}
14761476
} else {
14771477
// do it backwards otherwise, write the state, realloc TLV, then the account
14781478
let mut buffer = account_info.try_borrow_mut_data()?;
1479-
let mut state = StateWithExtensionsMut::<S>::unpack(&mut buffer)?;
1479+
let mut state = PodStateWithExtensionsMut::<S>::unpack(&mut buffer)?;
14801480
if extension_already_exists {
14811481
state.realloc_variable_len_extension(new_extension)?;
14821482
} else {
@@ -2657,7 +2657,7 @@ mod test {
26572657
let key = Pubkey::new_unique();
26582658
let account_info = (&key, &mut data).into_account_info();
26592659

2660-
alloc_and_serialize::<Mint, _>(&account_info, &fixed_len, false).unwrap();
2660+
alloc_and_serialize::<PodMint, _>(&account_info, &fixed_len, false).unwrap();
26612661
let new_account_len = BASE_ACCOUNT_AND_TYPE_LENGTH + add_type_and_length_to_len(value_len);
26622662
assert_eq!(data.len(), new_account_len);
26632663
let state = StateWithExtensions::<Mint>::unpack(data.data()).unwrap();
@@ -2668,12 +2668,12 @@ mod test {
26682668

26692669
// alloc again succeeds with "overwrite"
26702670
let account_info = (&key, &mut data).into_account_info();
2671-
alloc_and_serialize::<Mint, _>(&account_info, &fixed_len, true).unwrap();
2671+
alloc_and_serialize::<PodMint, _>(&account_info, &fixed_len, true).unwrap();
26722672

26732673
// alloc again fails without "overwrite"
26742674
let account_info = (&key, &mut data).into_account_info();
26752675
assert_eq!(
2676-
alloc_and_serialize::<Mint, _>(&account_info, &fixed_len, false).unwrap_err(),
2676+
alloc_and_serialize::<PodMint, _>(&account_info, &fixed_len, false).unwrap_err(),
26772677
TokenError::ExtensionAlreadyInitialized.into()
26782678
);
26792679
}
@@ -2692,8 +2692,12 @@ mod test {
26922692
let key = Pubkey::new_unique();
26932693
let account_info = (&key, &mut data).into_account_info();
26942694

2695-
alloc_and_serialize_variable_len_extension::<Mint, _>(&account_info, &variable_len, false)
2696-
.unwrap();
2695+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
2696+
&account_info,
2697+
&variable_len,
2698+
false,
2699+
)
2700+
.unwrap();
26972701
let new_account_len = BASE_ACCOUNT_AND_TYPE_LENGTH + add_type_and_length_to_len(value_len);
26982702
assert_eq!(data.len(), new_account_len);
26992703
let state = StateWithExtensions::<Mint>::unpack(data.data()).unwrap();
@@ -2706,13 +2710,17 @@ mod test {
27062710

27072711
// alloc again succeeds with "overwrite"
27082712
let account_info = (&key, &mut data).into_account_info();
2709-
alloc_and_serialize_variable_len_extension::<Mint, _>(&account_info, &variable_len, true)
2710-
.unwrap();
2713+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
2714+
&account_info,
2715+
&variable_len,
2716+
true,
2717+
)
2718+
.unwrap();
27112719

27122720
// alloc again fails without "overwrite"
27132721
let account_info = (&key, &mut data).into_account_info();
27142722
assert_eq!(
2715-
alloc_and_serialize_variable_len_extension::<Mint, _>(
2723+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
27162724
&account_info,
27172725
&variable_len,
27182726
false,
@@ -2748,7 +2756,7 @@ mod test {
27482756
let key = Pubkey::new_unique();
27492757
let account_info = (&key, &mut data).into_account_info();
27502758

2751-
alloc_and_serialize::<Mint, _>(&account_info, &fixed_len, false).unwrap();
2759+
alloc_and_serialize::<PodMint, _>(&account_info, &fixed_len, false).unwrap();
27522760
let new_account_len = BASE_ACCOUNT_AND_TYPE_LENGTH
27532761
+ add_type_and_length_to_len(value_len)
27542762
+ add_type_and_length_to_len(size_of::<GroupPointer>());
@@ -2764,12 +2772,12 @@ mod test {
27642772

27652773
// alloc again succeeds with "overwrite"
27662774
let account_info = (&key, &mut data).into_account_info();
2767-
alloc_and_serialize::<Mint, _>(&account_info, &fixed_len, true).unwrap();
2775+
alloc_and_serialize::<PodMint, _>(&account_info, &fixed_len, true).unwrap();
27682776

27692777
// alloc again fails without "overwrite"
27702778
let account_info = (&key, &mut data).into_account_info();
27712779
assert_eq!(
2772-
alloc_and_serialize::<Mint, _>(&account_info, &fixed_len, false).unwrap_err(),
2780+
alloc_and_serialize::<PodMint, _>(&account_info, &fixed_len, false).unwrap_err(),
27732781
TokenError::ExtensionAlreadyInitialized.into()
27742782
);
27752783
}
@@ -2798,8 +2806,12 @@ mod test {
27982806
let key = Pubkey::new_unique();
27992807
let account_info = (&key, &mut data).into_account_info();
28002808

2801-
alloc_and_serialize_variable_len_extension::<Mint, _>(&account_info, &variable_len, false)
2802-
.unwrap();
2809+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
2810+
&account_info,
2811+
&variable_len,
2812+
false,
2813+
)
2814+
.unwrap();
28032815
let new_account_len = BASE_ACCOUNT_AND_TYPE_LENGTH
28042816
+ add_type_and_length_to_len(value_len)
28052817
+ add_type_and_length_to_len(size_of::<MetadataPointer>());
@@ -2817,13 +2829,17 @@ mod test {
28172829

28182830
// alloc again succeeds with "overwrite"
28192831
let account_info = (&key, &mut data).into_account_info();
2820-
alloc_and_serialize_variable_len_extension::<Mint, _>(&account_info, &variable_len, true)
2821-
.unwrap();
2832+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
2833+
&account_info,
2834+
&variable_len,
2835+
true,
2836+
)
2837+
.unwrap();
28222838

28232839
// alloc again fails without "overwrite"
28242840
let account_info = (&key, &mut data).into_account_info();
28252841
assert_eq!(
2826-
alloc_and_serialize_variable_len_extension::<Mint, _>(
2842+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
28272843
&account_info,
28282844
&variable_len,
28292845
false,
@@ -2864,8 +2880,12 @@ mod test {
28642880
let key = Pubkey::new_unique();
28652881
let account_info = (&key, &mut data).into_account_info();
28662882
let variable_len = VariableLenMintTest { data: vec![1, 2] };
2867-
alloc_and_serialize_variable_len_extension::<Mint, _>(&account_info, &variable_len, true)
2868-
.unwrap();
2883+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
2884+
&account_info,
2885+
&variable_len,
2886+
true,
2887+
)
2888+
.unwrap();
28692889

28702890
let state = StateWithExtensions::<Mint>::unpack(data.data()).unwrap();
28712891
let extension = state.get_extension::<MetadataPointer>().unwrap();
@@ -2882,8 +2902,12 @@ mod test {
28822902
let variable_len = VariableLenMintTest {
28832903
data: vec![1, 2, 3, 4, 5, 6, 7],
28842904
};
2885-
alloc_and_serialize_variable_len_extension::<Mint, _>(&account_info, &variable_len, true)
2886-
.unwrap();
2905+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
2906+
&account_info,
2907+
&variable_len,
2908+
true,
2909+
)
2910+
.unwrap();
28872911

28882912
let state = StateWithExtensions::<Mint>::unpack(data.data()).unwrap();
28892913
let extension = state.get_extension::<MetadataPointer>().unwrap();
@@ -2900,8 +2924,12 @@ mod test {
29002924
let variable_len = VariableLenMintTest {
29012925
data: vec![7, 6, 5, 4, 3, 2, 1],
29022926
};
2903-
alloc_and_serialize_variable_len_extension::<Mint, _>(&account_info, &variable_len, true)
2904-
.unwrap();
2927+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
2928+
&account_info,
2929+
&variable_len,
2930+
true,
2931+
)
2932+
.unwrap();
29052933

29062934
let state = StateWithExtensions::<Mint>::unpack(data.data()).unwrap();
29072935
let extension = state.get_extension::<MetadataPointer>().unwrap();

token/program-2022/src/extension/token_group/processor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use {
99
group_pointer::GroupPointer, BaseStateWithExtensions, BaseStateWithExtensionsMut,
1010
StateWithExtensions, StateWithExtensionsMut,
1111
},
12+
pod::PodMint,
1213
state::Mint,
1314
},
1415
solana_program::{
@@ -90,7 +91,7 @@ pub fn process_initialize_group(
9091
// Allocate a TLV entry for the space and write it in
9192
// Assumes that there's enough SOL for the new rent-exemption
9293
let group = TokenGroup::new(mint_info.key, data.update_authority, data.max_size.into());
93-
alloc_and_serialize::<Mint, TokenGroup>(group_info, &group, false)?;
94+
alloc_and_serialize::<PodMint, TokenGroup>(group_info, &group, false)?;
9495

9596
Ok(())
9697
}
@@ -201,7 +202,7 @@ pub fn process_initialize_member(_program_id: &Pubkey, accounts: &[AccountInfo])
201202

202203
// Allocate a TLV entry for the space and write it in
203204
let member = TokenGroupMember::new(member_mint_info.key, group_info.key, member_number);
204-
alloc_and_serialize::<Mint, TokenGroupMember>(member_info, &member, false)?;
205+
alloc_and_serialize::<PodMint, TokenGroupMember>(member_info, &member, false)?;
205206

206207
Ok(())
207208
}

token/program-2022/src/extension/token_metadata/processor.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use {
88
alloc_and_serialize_variable_len_extension, metadata_pointer::MetadataPointer,
99
BaseStateWithExtensions, StateWithExtensions,
1010
},
11+
pod::PodMint,
1112
state::Mint,
1213
},
1314
solana_program::{
@@ -98,7 +99,11 @@ pub fn process_initialize(
9899

99100
// allocate a TLV entry for the space and write it in, assumes that there's
100101
// enough SOL for the new rent-exemption
101-
alloc_and_serialize_variable_len_extension::<Mint, _>(metadata_info, &token_metadata, false)?;
102+
alloc_and_serialize_variable_len_extension::<PodMint, _>(
103+
metadata_info,
104+
&token_metadata,
105+
false,
106+
)?;
102107

103108
Ok(())
104109
}
@@ -127,7 +132,7 @@ pub fn process_update_field(
127132
token_metadata.update(data.field, data.value);
128133

129134
// Update / realloc the account
130-
alloc_and_serialize_variable_len_extension::<Mint, _>(metadata_info, &token_metadata, true)?;
135+
alloc_and_serialize_variable_len_extension::<PodMint, _>(metadata_info, &token_metadata, true)?;
131136

132137
Ok(())
133138
}
@@ -154,7 +159,7 @@ pub fn process_remove_key(
154159
if !token_metadata.remove_key(&data.key) && !data.idempotent {
155160
return Err(TokenMetadataError::KeyNotFound.into());
156161
}
157-
alloc_and_serialize_variable_len_extension::<Mint, _>(metadata_info, &token_metadata, true)?;
162+
alloc_and_serialize_variable_len_extension::<PodMint, _>(metadata_info, &token_metadata, true)?;
158163
Ok(())
159164
}
160165

@@ -180,7 +185,7 @@ pub fn process_update_authority(
180185
check_update_authority(update_authority_info, &token_metadata.update_authority)?;
181186
token_metadata.update_authority = data.new_authority;
182187
// Update the account, no realloc needed!
183-
alloc_and_serialize_variable_len_extension::<Mint, _>(metadata_info, &token_metadata, true)?;
188+
alloc_and_serialize_variable_len_extension::<PodMint, _>(metadata_info, &token_metadata, true)?;
184189

185190
Ok(())
186191
}

0 commit comments

Comments
 (0)