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

Commit b31b5d0

Browse files
committed
feedback: rework rent check and test tx dedupe
1 parent 1a502bc commit b31b5d0

File tree

4 files changed

+21
-32
lines changed

4 files changed

+21
-32
lines changed

token/client/src/token.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use {
4141
ConfidentialTransferFeeConfig,
4242
},
4343
cpi_guard, default_account_state, group_pointer, interest_bearing_mint, memo_transfer,
44-
metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, ExtensionType,
45-
StateWithExtensions, StateWithExtensionsOwned,
44+
metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, Extension,
45+
ExtensionType, StateWithExtensionsOwned,
4646
},
4747
instruction, offchain,
4848
proof::ProofLocation,
@@ -3878,14 +3878,18 @@ where
38783878
) -> TokenResult<u64> {
38793879
let account = self.get_account(self.pubkey).await?;
38803880
let account_lamports = account.lamports;
3881-
let mint_state = StateWithExtensions::<Mint>::unpack(&account.data)?;
3882-
let new_account_len = mint_state.try_get_new_account_len::<V>(size_of::<V>())?;
3883-
let new_rent_exempt_minimum = self
3884-
.client
3885-
.get_minimum_balance_for_rent_exemption(new_account_len)
3886-
.await
3887-
.map_err(TokenError::Client)?;
3888-
Ok(new_rent_exempt_minimum.saturating_sub(account_lamports))
3881+
let mint_state = self.unpack_mint_info(account)?;
3882+
if mint_state.get_extension::<V>().is_ok() {
3883+
Ok(0)
3884+
} else {
3885+
let new_account_len = mint_state.try_get_new_account_len::<V>(size_of::<V>())?;
3886+
let new_rent_exempt_minimum = self
3887+
.client
3888+
.get_minimum_balance_for_rent_exemption(new_account_len)
3889+
.await
3890+
.map_err(TokenError::Client)?;
3891+
Ok(new_rent_exempt_minimum.saturating_sub(account_lamports))
3892+
}
38893893
}
38903894

38913895
/// Initialize token-group on a mint

token/program-2022-test/tests/program_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl TestContext {
146146
let token_unchecked = Token::new_native(Arc::clone(&client), &id(), Arc::new(payer));
147147
self.token_context = Some(TokenContext {
148148
decimals: native_mint::DECIMALS,
149-
mint_authority: Keypair::new(), /*bogus*/
149+
mint_authority: Keypair::new(), /* bogus */
150150
token,
151151
token_unchecked,
152152
alice: Keypair::new(),

token/program-2022-test/tests/token_group_initialize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ async fn success_initialize() {
131131
&payer_pubkey,
132132
&token_context.mint_authority.pubkey(),
133133
&update_authority,
134-
max_size,
134+
12, // Change so we get a different transaction
135135
&[&token_context.mint_authority],
136136
)
137137
.await
@@ -140,7 +140,7 @@ async fn success_initialize() {
140140
error,
141141
TokenClientError::Client(Box::new(TransportError::TransactionError(
142142
TransactionError::InstructionError(
143-
1,
143+
0, // No additional rent
144144
InstructionError::Custom(TokenError::ExtensionAlreadyInitialized as u32)
145145
)
146146
)))

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

+4-19
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use {
55
check_program_account,
66
error::TokenError,
77
extension::{
8-
group_pointer::GroupPointer, BaseStateWithExtensions, ExtensionType,
9-
StateWithExtensions, StateWithExtensionsMut,
8+
alloc_and_serialize, group_pointer::GroupPointer, BaseStateWithExtensions,
9+
StateWithExtensions,
1010
},
1111
state::Mint,
1212
},
@@ -25,16 +25,6 @@ use {
2525
},
2626
};
2727

28-
fn realloc_mint(mint_info: &AccountInfo, extension: ExtensionType) -> Result<(), ProgramError> {
29-
let extension_len = extension.try_get_tlv_len()?;
30-
let new_account_len = mint_info
31-
.data_len()
32-
.checked_add(extension_len)
33-
.ok_or::<ProgramError>(TokenError::Overflow.into())?;
34-
mint_info.realloc(new_account_len, false)?;
35-
Ok(())
36-
}
37-
3828
/// Processes a [InitializeGroup](enum.TokenGroupInstruction.html) instruction.
3929
pub fn process_initialize_group(
4030
_program_id: &Pubkey,
@@ -78,15 +68,10 @@ pub fn process_initialize_group(
7868
}
7969
}
8070

81-
// Reallocate the mint for the new extension
82-
realloc_mint(mint_info, ExtensionType::TokenGroup)?;
83-
8471
// Allocate a TLV entry for the space and write it in
8572
// Assumes that there's enough SOL for the new rent-exemption
86-
let mut mint_data = mint_info.try_borrow_mut_data()?;
87-
let mut mint = StateWithExtensionsMut::<Mint>::unpack(&mut mint_data)?;
88-
let group = mint.init_extension::<TokenGroup>(false)?;
89-
*group = TokenGroup::new(mint_info.key, data.update_authority, data.max_size.into());
73+
let group = TokenGroup::new(mint_info.key, data.update_authority, data.max_size.into());
74+
alloc_and_serialize::<Mint, TokenGroup>(group_info, &group, false)?;
9075

9176
Ok(())
9277
}

0 commit comments

Comments
 (0)