From 837a1a0cd7c64a3a1162a3177dee6f222c2490b1 Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 25 Oct 2023 14:22:07 +0200 Subject: [PATCH] feedback: rework rent check and test tx dedupe --- token/client/src/token.rs | 24 +++++++++++-------- token/program-2022-test/tests/program_test.rs | 2 +- .../tests/token_group_initialize.rs | 4 ++-- .../src/extension/token_group/processor.rs | 23 ++++-------------- 4 files changed, 21 insertions(+), 32 deletions(-) diff --git a/token/client/src/token.rs b/token/client/src/token.rs index fd5350703eb..104601d702d 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -41,8 +41,8 @@ use { ConfidentialTransferFeeConfig, }, cpi_guard, default_account_state, group_pointer, interest_bearing_mint, memo_transfer, - metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, ExtensionType, - StateWithExtensions, StateWithExtensionsOwned, + metadata_pointer, transfer_fee, transfer_hook, BaseStateWithExtensions, Extension, + ExtensionType, StateWithExtensionsOwned, }, instruction, offchain, proof::ProofLocation, @@ -3878,14 +3878,18 @@ where ) -> TokenResult { let account = self.get_account(self.pubkey).await?; let account_lamports = account.lamports; - let mint_state = StateWithExtensions::::unpack(&account.data)?; - let new_account_len = mint_state.try_get_new_account_len::(size_of::())?; - let new_rent_exempt_minimum = self - .client - .get_minimum_balance_for_rent_exemption(new_account_len) - .await - .map_err(TokenError::Client)?; - Ok(new_rent_exempt_minimum.saturating_sub(account_lamports)) + let mint_state = self.unpack_mint_info(account)?; + if mint_state.get_extension::().is_ok() { + Ok(0) + } else { + let new_account_len = mint_state.try_get_new_account_len::(size_of::())?; + let new_rent_exempt_minimum = self + .client + .get_minimum_balance_for_rent_exemption(new_account_len) + .await + .map_err(TokenError::Client)?; + Ok(new_rent_exempt_minimum.saturating_sub(account_lamports)) + } } /// Initialize token-group on a mint diff --git a/token/program-2022-test/tests/program_test.rs b/token/program-2022-test/tests/program_test.rs index 7c301979fba..1400285a0c0 100644 --- a/token/program-2022-test/tests/program_test.rs +++ b/token/program-2022-test/tests/program_test.rs @@ -146,7 +146,7 @@ impl TestContext { let token_unchecked = Token::new_native(Arc::clone(&client), &id(), Arc::new(payer)); self.token_context = Some(TokenContext { decimals: native_mint::DECIMALS, - mint_authority: Keypair::new(), /*bogus*/ + mint_authority: Keypair::new(), /* bogus */ token, token_unchecked, alice: Keypair::new(), diff --git a/token/program-2022-test/tests/token_group_initialize.rs b/token/program-2022-test/tests/token_group_initialize.rs index 54ecf3c5dab..e9d1296c31f 100644 --- a/token/program-2022-test/tests/token_group_initialize.rs +++ b/token/program-2022-test/tests/token_group_initialize.rs @@ -131,7 +131,7 @@ async fn success_initialize() { &payer_pubkey, &token_context.mint_authority.pubkey(), &update_authority, - max_size, + 12, // Change so we get a different transaction &[&token_context.mint_authority], ) .await @@ -140,7 +140,7 @@ async fn success_initialize() { error, TokenClientError::Client(Box::new(TransportError::TransactionError( TransactionError::InstructionError( - 1, + 0, // No additional rent InstructionError::Custom(TokenError::ExtensionAlreadyInitialized as u32) ) ))) diff --git a/token/program-2022/src/extension/token_group/processor.rs b/token/program-2022/src/extension/token_group/processor.rs index cd00b5a01a9..9966f02ff5e 100644 --- a/token/program-2022/src/extension/token_group/processor.rs +++ b/token/program-2022/src/extension/token_group/processor.rs @@ -5,8 +5,8 @@ use { check_program_account, error::TokenError, extension::{ - group_pointer::GroupPointer, BaseStateWithExtensions, ExtensionType, - StateWithExtensions, StateWithExtensionsMut, + alloc_and_serialize, group_pointer::GroupPointer, BaseStateWithExtensions, + StateWithExtensions, }, state::Mint, }, @@ -25,16 +25,6 @@ use { }, }; -fn realloc_mint(mint_info: &AccountInfo, extension: ExtensionType) -> Result<(), ProgramError> { - let extension_len = extension.try_get_tlv_len()?; - let new_account_len = mint_info - .data_len() - .checked_add(extension_len) - .ok_or::(TokenError::Overflow.into())?; - mint_info.realloc(new_account_len, false)?; - Ok(()) -} - /// Processes a [InitializeGroup](enum.TokenGroupInstruction.html) instruction. pub fn process_initialize_group( _program_id: &Pubkey, @@ -78,15 +68,10 @@ pub fn process_initialize_group( } } - // Reallocate the mint for the new extension - realloc_mint(mint_info, ExtensionType::TokenGroup)?; - // Allocate a TLV entry for the space and write it in // Assumes that there's enough SOL for the new rent-exemption - let mut mint_data = mint_info.try_borrow_mut_data()?; - let mut mint = StateWithExtensionsMut::::unpack(&mut mint_data)?; - let group = mint.init_extension::(false)?; - *group = TokenGroup::new(mint_info.key, data.update_authority, data.max_size.into()); + let group = TokenGroup::new(mint_info.key, data.update_authority, data.max_size.into()); + alloc_and_serialize::(group_info, &group, false)?; Ok(()) }