Skip to content

Commit

Permalink
chore: reapply multiple tokenPoolPda accounts (#1407) (#1433)
Browse files Browse the repository at this point in the history
* feat: multiple `tokenPoolPda` accounts (#1407)

* feat: multiple token pool accounts

* fix: lint

* chore: unify execute token program instruction with multiple token pool accounts

---------

Co-authored-by: ananas-block <[email protected]>

chore: fix additonal typo (#1426)

fix: invoke_token_program_with_multiple_token_pool_accounts (#1427)

chore: unify is_valid_token_pool_pda in compress spl tokens & mint to, fix comments in invoke_token_program_with_multiple_token_pool_accounts (#1429)

* rename is_valid_token_pool_pda, check_spl_token_pool_derviation, _with_bump

---------

Co-authored-by: ananas-block <[email protected]>
Co-authored-by: Swenschaeferjohann <[email protected]>
  • Loading branch information
3 people authored Dec 26, 2024
1 parent f648104 commit 103a210
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
36 changes: 18 additions & 18 deletions programs/compressed-token/src/instructions/create_token_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use spl_token_2022::{

use crate::{
constants::{NUM_MAX_POOL_ACCOUNTS, POOL_SEED},
spl_compression::check_spl_token_pool_derivation,
spl_compression::is_valid_token_pool_pda,
};

/// Creates an SPL or token-2022 token pool account, which is owned by the token authority PDA.
Expand Down Expand Up @@ -107,11 +107,11 @@ pub struct AddTokenPoolInstruction<'info> {
/// Checks if the token pool PDA is valid.
/// Iterates over all possible bump seeds to check if the token pool PDA is valid.
#[inline(always)]
pub fn is_valid_token_pool_pda(token_pool_pda: &Pubkey, mint: &Pubkey) -> Result<()> {
pub fn check_spl_token_pool_derivation(token_pool_pda: &Pubkey, mint: &Pubkey) -> Result<()> {
let mint_bytes = mint.to_bytes();
let is_valid_token_pool_pda = (0..NUM_MAX_POOL_ACCOUNTS)
.any(|i| check_spl_token_pool_derivation(mint_bytes.as_slice(), token_pool_pda, &[i]));
if !is_valid_token_pool_pda {
let is_valid = (0..NUM_MAX_POOL_ACCOUNTS)
.any(|i| is_valid_token_pool_pda(mint_bytes.as_slice(), token_pool_pda, &[i]));
if !is_valid {
err!(crate::ErrorCode::InvalidTokenPoolPda)
} else {
Ok(())
Expand All @@ -123,32 +123,32 @@ mod test {
use super::*;

/// Test:
/// 1. Functional: test_is_valid_token_pool_pda_valid
/// 2. Failing: test_is_valid_token_pool_pda_invalid_derivation
/// 3. Failing: test_is_valid_token_pool_pda_bump_seed_equal_to_num_max_accounts
/// 4. Failing: test_is_valid_token_pool_pda_bump_seed_larger_than_num_max_accounts
/// 1. Functional: test_check_spl_token_pool_derivation
/// 2. Failing: test_check_spl_token_pool_derivation_invalid_derivation
/// 3. Failing: test_check_spl_token_pool_derivation_bump_seed_equal_to_num_max_accounts
/// 4. Failing: test_check_spl_token_pool_derivation_bump_seed_larger_than_num_max_accounts
#[test]
fn test_is_valid_token_pool_pda() {
// 1. Functional: test_is_valid_token_pool_pda_valid
fn test_check_spl_token_pool_derivation() {
// 1. Functional: test_check_spl_token_pool_derivation_valid
let mint = Pubkey::new_unique();
for i in 0..NUM_MAX_POOL_ACCOUNTS {
let valid_pda = get_token_pool_pda_with_bump(&mint, i);
assert!(is_valid_token_pool_pda(&valid_pda, &mint).is_ok());
assert!(check_spl_token_pool_derivation(&valid_pda, &mint).is_ok());
}

// 2. Failing: test_is_valid_token_pool_pda_invalid_derivation
// 2. Failing: test_check_spl_token_pool_derivation_invalid_derivation
let mint = Pubkey::new_unique();
let invalid_pda = Pubkey::new_unique();
assert!(is_valid_token_pool_pda(&invalid_pda, &mint).is_err());
assert!(check_spl_token_pool_derivation(&invalid_pda, &mint).is_err());

// 3. Failing: test_is_valid_token_pool_pda_bump_seed_equal_to_num_max_accounts
// 3. Failing: test_check_spl_token_pool_derivation_bump_seed_equal_to_num_max_accounts
let mint = Pubkey::new_unique();
let invalid_pda = get_token_pool_pda_with_bump(&mint, NUM_MAX_POOL_ACCOUNTS);
assert!(is_valid_token_pool_pda(&invalid_pda, &mint).is_err());
assert!(check_spl_token_pool_derivation(&invalid_pda, &mint).is_err());

// 4. Failing: test_is_valid_token_pool_pda_bump_seed_larger_than_num_max_accounts
// 4. Failing: test_check_spl_token_pool_derivation_bump_seed_larger_than_num_max_accounts
let mint = Pubkey::new_unique();
let invalid_pda = get_token_pool_pda_with_bump(&mint, NUM_MAX_POOL_ACCOUNTS + 1);
assert!(is_valid_token_pool_pda(&invalid_pda, &mint).is_err());
assert!(check_spl_token_pool_derivation(&invalid_pda, &mint).is_err());
}
}
4 changes: 2 additions & 2 deletions programs/compressed-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ solana_security_txt::security_txt! {
pub mod light_compressed_token {

use constants::{NOT_FROZEN, NUM_MAX_POOL_ACCOUNTS};
use spl_compression::spl_token_pool_derivation;
use spl_compression::check_spl_token_pool_derivation_with_bump;

use super::*;

Expand All @@ -59,7 +59,7 @@ pub mod light_compressed_token {
return err!(ErrorCode::InvalidTokenPoolBump);
}
// Check that token pool account with previous bump already exists.
spl_token_pool_derivation(
check_spl_token_pool_derivation_with_bump(
&ctx.accounts.mint.key().to_bytes(),
&ctx.accounts.existing_token_pool_pda.key(),
&[token_pool_bump.saturating_sub(1)],
Expand Down
6 changes: 3 additions & 3 deletions programs/compressed-token/src/process_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
light_utils::hash_to_bn254_field_size_be,
};

use crate::{is_valid_token_pool_pda, program::LightCompressedToken};
use crate::{check_spl_token_pool_derivation, program::LightCompressedToken};

/// Mints tokens from an spl token mint to a list of compressed accounts and
/// stores minted tokens in spl token pool account.
Expand Down Expand Up @@ -274,7 +274,7 @@ pub fn serialize_mint_to_cpi_instruction_data(

#[inline(never)]
pub fn mint_spl_to_pool_pda(ctx: &Context<MintToInstruction>, amounts: &[u64]) -> Result<()> {
is_valid_token_pool_pda(&ctx.accounts.token_pool_pda.key(), &ctx.accounts.mint.key())?;
check_spl_token_pool_derivation(&ctx.accounts.token_pool_pda.key(), &ctx.accounts.mint.key())?;
let mut mint_amount: u64 = 0;
for amount in amounts.iter() {
mint_amount = mint_amount
Expand Down Expand Up @@ -325,7 +325,7 @@ pub struct MintToInstruction<'info> {
@ crate::ErrorCode::InvalidAuthorityMint
)]
pub mint: InterfaceAccount<'info, Mint>,
/// CHECK: with is_valid_token_pool_pda().
/// CHECK: with check_spl_token_pool_derivation().
#[account(mut)]
pub token_pool_pda: InterfaceAccount<'info, TokenAccount>,
pub token_program: Interface<'info, TokenInterface>,
Expand Down
17 changes: 6 additions & 11 deletions programs/compressed-token/src/spl_compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use anchor_lang::{prelude::*, solana_program::account_info::AccountInfo};
use anchor_spl::{token::TokenAccount, token_interface};

use crate::{
check_spl_token_pool_derivation,
constants::{NUM_MAX_POOL_ACCOUNTS, POOL_SEED},
is_valid_token_pool_pda,
process_transfer::get_cpi_signer_seeds,
CompressedTokenInstructionDataTransfer, ErrorCode, TransferInstruction,
};
Expand All @@ -20,23 +20,19 @@ pub fn process_compression_or_decompression<'info>(
}
}

pub fn spl_token_pool_derivation(
pub fn check_spl_token_pool_derivation_with_bump(
mint_bytes: &[u8],
token_pool_pubkey: &Pubkey,
bump: &[u8],
) -> Result<()> {
if check_spl_token_pool_derivation(mint_bytes, token_pool_pubkey, bump) {
if is_valid_token_pool_pda(mint_bytes, token_pool_pubkey, bump) {
Ok(())
} else {
err!(ErrorCode::InvalidTokenPoolPda)
}
}

pub fn check_spl_token_pool_derivation(
mint_bytes: &[u8],
token_pool_pubkey: &Pubkey,
bump: &[u8],
) -> bool {
pub fn is_valid_token_pool_pda(mint_bytes: &[u8], token_pool_pubkey: &Pubkey, bump: &[u8]) -> bool {
let seeds = [POOL_SEED, mint_bytes, bump];
let seeds = if bump[0] == 0 {
&seeds[..2]
Expand Down Expand Up @@ -125,8 +121,7 @@ pub fn invoke_token_program_with_multiple_token_pool_accounts<'info, const IS_BU
}
// 5. Check if the token pool account is derived from the mint for any bump.
for (index, i) in token_pool_bumps.iter().enumerate() {
if check_spl_token_pool_derivation(mint_bytes.as_slice(), &token_pool_pda.key(), &[*i])
{
if is_valid_token_pool_pda(mint_bytes.as_slice(), &token_pool_pda.key(), &[*i]) {
// 7. Burn or transfer the amount from the token pool account.
if IS_BURN {
crate::burn::spl_burn_cpi(
Expand Down Expand Up @@ -189,7 +184,7 @@ pub fn compress_spl_tokens<'info>(
None => return err!(ErrorCode::DeCompressAmountUndefinedForCompress),
};

is_valid_token_pool_pda(&recipient_token_pool.key(), &inputs.mint)?;
check_spl_token_pool_derivation(&recipient_token_pool.key(), &inputs.mint)?;
spl_token_transfer(
ctx.accounts
.compress_or_decompress_token_account
Expand Down
9 changes: 7 additions & 2 deletions test-programs/compressed-token-test/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use light_compressed_token::{
process_transfer::{
get_cpi_authority_pda, transfer_sdk::create_transfer_instruction, TokenTransferOutputData,
},
spl_compression::spl_token_pool_derivation,
spl_compression::check_spl_token_pool_derivation_with_bump,
token_data::{AccountState, TokenData},
ErrorCode,
};
Expand Down Expand Up @@ -321,7 +321,12 @@ async fn test_failing_create_token_pool() {

let token_pool_pubkey = get_token_pool_pda(&mint.pubkey());
let token_pool_account = rpc.get_account(token_pool_pubkey).await.unwrap().unwrap();
spl_token_pool_derivation(&mint.pubkey().to_bytes(), &token_pool_pubkey, &[0]).unwrap();
check_spl_token_pool_derivation_with_bump(
&mint.pubkey().to_bytes(),
&token_pool_pubkey,
&[0],
)
.unwrap();
assert_eq!(token_pool_account.data.len(), TokenAccount::LEN);
}
}
Expand Down

0 comments on commit 103a210

Please sign in to comment.