From 103a210e277788e8d40f0077d0db30cdae03ac35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Swen=20Sch=C3=A4ferjohann?= <42959314+SwenSchaeferjohann@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:28:50 +0100 Subject: [PATCH] chore: reapply multiple `tokenPoolPda` accounts (#1407) (#1433) * 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 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 <58553958+ananas-block@users.noreply.github.com> Co-authored-by: Swenschaeferjohann --- .../src/instructions/create_token_pool.rs | 36 +++++++++---------- programs/compressed-token/src/lib.rs | 4 +-- programs/compressed-token/src/process_mint.rs | 6 ++-- .../compressed-token/src/spl_compression.rs | 17 ++++----- .../compressed-token-test/tests/test.rs | 9 +++-- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/programs/compressed-token/src/instructions/create_token_pool.rs b/programs/compressed-token/src/instructions/create_token_pool.rs index caa671ac1..6d7f5ab2b 100644 --- a/programs/compressed-token/src/instructions/create_token_pool.rs +++ b/programs/compressed-token/src/instructions/create_token_pool.rs @@ -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. @@ -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(()) @@ -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()); } } diff --git a/programs/compressed-token/src/lib.rs b/programs/compressed-token/src/lib.rs index df73cd46f..99718dcb9 100644 --- a/programs/compressed-token/src/lib.rs +++ b/programs/compressed-token/src/lib.rs @@ -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::*; @@ -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)], diff --git a/programs/compressed-token/src/process_mint.rs b/programs/compressed-token/src/process_mint.rs index faf6fbf4a..b5d5f5020 100644 --- a/programs/compressed-token/src/process_mint.rs +++ b/programs/compressed-token/src/process_mint.rs @@ -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. @@ -274,7 +274,7 @@ pub fn serialize_mint_to_cpi_instruction_data( #[inline(never)] pub fn mint_spl_to_pool_pda(ctx: &Context, 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 @@ -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>, diff --git a/programs/compressed-token/src/spl_compression.rs b/programs/compressed-token/src/spl_compression.rs index 572f6fc98..8101a28d4 100644 --- a/programs/compressed-token/src/spl_compression.rs +++ b/programs/compressed-token/src/spl_compression.rs @@ -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, }; @@ -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] @@ -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( @@ -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 diff --git a/test-programs/compressed-token-test/tests/test.rs b/test-programs/compressed-token-test/tests/test.rs index a4c41a75f..debb8ba0b 100644 --- a/test-programs/compressed-token-test/tests/test.rs +++ b/test-programs/compressed-token-test/tests/test.rs @@ -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, }; @@ -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); } }