Skip to content

Commit

Permalink
feedback: rework rent check and test tx dedupe
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Oct 26, 2023
1 parent 1a502bc commit b31b5d0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
24 changes: 14 additions & 10 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -3878,14 +3878,18 @@ where
) -> TokenResult<u64> {
let account = self.get_account(self.pubkey).await?;
let account_lamports = account.lamports;
let mint_state = StateWithExtensions::<Mint>::unpack(&account.data)?;
let new_account_len = mint_state.try_get_new_account_len::<V>(size_of::<V>())?;
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::<V>().is_ok() {
Ok(0)
} else {
let new_account_len = mint_state.try_get_new_account_len::<V>(size_of::<V>())?;
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
Expand Down
2 changes: 1 addition & 1 deletion token/program-2022-test/tests/program_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions token/program-2022-test/tests/token_group_initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
)))
Expand Down
23 changes: 4 additions & 19 deletions token/program-2022/src/extension/token_group/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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::<ProgramError>(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,
Expand Down Expand Up @@ -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::<Mint>::unpack(&mut mint_data)?;
let group = mint.init_extension::<TokenGroup>(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::<Mint, TokenGroup>(group_info, &group, false)?;

Ok(())
}
Expand Down

0 comments on commit b31b5d0

Please sign in to comment.