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

Commit 308e027

Browse files
committed
backport #437 to v2
1 parent 36ec1b2 commit 308e027

File tree

1 file changed

+221
-6
lines changed

1 file changed

+221
-6
lines changed

token/program/src/processor.rs

Lines changed: 221 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,9 @@ impl Processor {
480480
let mint_info = next_account_info(account_info_iter)?;
481481
let authority_info = next_account_info(account_info_iter)?;
482482

483-
let mut mint_data = mint_info.data.borrow_mut();
484-
let mut source_data = source_account_info.data.borrow_mut();
485-
Mint::unpack_mut(&mut mint_data, &mut |mint: &mut Mint| {
486-
Account::unpack_mut(&mut source_data, &mut |source_account: &mut Account| {
483+
let mut source_account = Account::unpack(&source_account_info.data.borrow())?;
484+
let mut mint = Mint::unpack(&mint_info.data.borrow())?;
485+
487486
if source_account.is_native() {
488487
return Err(TokenError::NativeNotSupported.into());
489488
}
@@ -540,9 +539,10 @@ impl Processor {
540539
.checked_sub(amount)
541540
.ok_or(TokenError::Overflow)?;
542541

542+
Account::pack(source_account, &mut source_account_info.data.borrow_mut())?;
543+
Mint::pack(mint, &mut mint_info.data.borrow_mut())?;
544+
543545
Ok(())
544-
})
545-
})
546546
}
547547

548548
/// Processes a [CloseAccount](enum.TokenInstruction.html) instruction.
@@ -2741,6 +2741,221 @@ mod tests {
27412741
);
27422742
}
27432743

2744+
#[test]
2745+
fn test_burn_dups() {
2746+
let program_id = pubkey_rand();
2747+
let account1_key = pubkey_rand();
2748+
let mut account1_account = SolanaAccount::new(
2749+
account_minimum_balance(),
2750+
Account::get_packed_len(),
2751+
&program_id,
2752+
);
2753+
let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
2754+
let owner_key = pubkey_rand();
2755+
let mut owner_account = SolanaAccount::default();
2756+
let owner_info: AccountInfo = (&owner_key, true, &mut owner_account).into();
2757+
let mint_key = pubkey_rand();
2758+
let mut mint_account =
2759+
SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
2760+
let mint_info: AccountInfo = (&mint_key, true, &mut mint_account).into();
2761+
let rent_key = rent::id();
2762+
let mut rent_sysvar = rent_sysvar();
2763+
let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
2764+
2765+
// create mint
2766+
do_process_instruction_dups(
2767+
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
2768+
vec![mint_info.clone(), rent_info.clone()],
2769+
)
2770+
.unwrap();
2771+
2772+
// create account
2773+
do_process_instruction_dups(
2774+
initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
2775+
vec![
2776+
account1_info.clone(),
2777+
mint_info.clone(),
2778+
account1_info.clone(),
2779+
rent_info.clone(),
2780+
],
2781+
)
2782+
.unwrap();
2783+
2784+
// mint to account
2785+
do_process_instruction_dups(
2786+
mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
2787+
vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
2788+
)
2789+
.unwrap();
2790+
2791+
// source-owner burn
2792+
do_process_instruction_dups(
2793+
burn(
2794+
&program_id,
2795+
&mint_key,
2796+
&account1_key,
2797+
&account1_key,
2798+
&[],
2799+
500,
2800+
)
2801+
.unwrap(),
2802+
vec![
2803+
account1_info.clone(),
2804+
mint_info.clone(),
2805+
account1_info.clone(),
2806+
],
2807+
)
2808+
.unwrap();
2809+
2810+
// source-owner burn2
2811+
do_process_instruction_dups(
2812+
burn2(
2813+
&program_id,
2814+
&account1_key,
2815+
&mint_key,
2816+
&account1_key,
2817+
&[],
2818+
500,
2819+
2,
2820+
)
2821+
.unwrap(),
2822+
vec![
2823+
account1_info.clone(),
2824+
mint_info.clone(),
2825+
account1_info.clone(),
2826+
],
2827+
)
2828+
.unwrap();
2829+
2830+
// mint-owner burn
2831+
do_process_instruction_dups(
2832+
mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
2833+
vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
2834+
)
2835+
.unwrap();
2836+
Account::unpack_unchecked_mut(
2837+
&mut account1_info.data.borrow_mut(),
2838+
&mut |account: &mut Account| {
2839+
account.owner = mint_key;
2840+
Ok(())
2841+
},
2842+
)
2843+
.unwrap();
2844+
do_process_instruction_dups(
2845+
burn(&program_id, &account1_key, &mint_key, &mint_key, &[], 500).unwrap(),
2846+
vec![account1_info.clone(), mint_info.clone(), mint_info.clone()],
2847+
)
2848+
.unwrap();
2849+
2850+
// mint-owner burn2
2851+
do_process_instruction_dups(
2852+
burn2(
2853+
&program_id,
2854+
&account1_key,
2855+
&mint_key,
2856+
&mint_key,
2857+
&[],
2858+
500,
2859+
2,
2860+
)
2861+
.unwrap(),
2862+
vec![account1_info.clone(), mint_info.clone(), mint_info.clone()],
2863+
)
2864+
.unwrap();
2865+
2866+
// source-delegate burn
2867+
do_process_instruction_dups(
2868+
mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
2869+
vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
2870+
)
2871+
.unwrap();
2872+
Account::unpack_unchecked_mut(
2873+
&mut account1_info.data.borrow_mut(),
2874+
&mut |account: &mut Account| {
2875+
account.delegated_amount = 1000;
2876+
account.delegate = COption::Some(account1_key);
2877+
account.owner = owner_key;
2878+
Ok(())
2879+
},
2880+
)
2881+
.unwrap();
2882+
do_process_instruction_dups(
2883+
burn(
2884+
&program_id,
2885+
&account1_key,
2886+
&mint_key,
2887+
&account1_key,
2888+
&[],
2889+
500,
2890+
)
2891+
.unwrap(),
2892+
vec![
2893+
account1_info.clone(),
2894+
mint_info.clone(),
2895+
account1_info.clone(),
2896+
],
2897+
)
2898+
.unwrap();
2899+
2900+
// source-delegate burn2
2901+
do_process_instruction_dups(
2902+
burn2(
2903+
&program_id,
2904+
&account1_key,
2905+
&mint_key,
2906+
&account1_key,
2907+
&[],
2908+
500,
2909+
2,
2910+
)
2911+
.unwrap(),
2912+
vec![
2913+
account1_info.clone(),
2914+
mint_info.clone(),
2915+
account1_info.clone(),
2916+
],
2917+
)
2918+
.unwrap();
2919+
2920+
// mint-delegate burn
2921+
do_process_instruction_dups(
2922+
mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
2923+
vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
2924+
)
2925+
.unwrap();
2926+
Account::unpack_unchecked_mut(
2927+
&mut account1_info.data.borrow_mut(),
2928+
&mut |account: &mut Account| {
2929+
account.delegated_amount = 1000;
2930+
account.delegate = COption::Some(mint_key);
2931+
account.owner = owner_key;
2932+
Ok(())
2933+
},
2934+
)
2935+
.unwrap();
2936+
do_process_instruction_dups(
2937+
burn(&program_id, &account1_key, &mint_key, &mint_key, &[], 500).unwrap(),
2938+
vec![account1_info.clone(), mint_info.clone(), mint_info.clone()],
2939+
)
2940+
.unwrap();
2941+
2942+
// mint-delegate burn2
2943+
do_process_instruction_dups(
2944+
burn2(
2945+
&program_id,
2946+
&account1_key,
2947+
&mint_key,
2948+
&mint_key,
2949+
&[],
2950+
500,
2951+
2,
2952+
)
2953+
.unwrap(),
2954+
vec![account1_info.clone(), mint_info.clone(), mint_info.clone()],
2955+
)
2956+
.unwrap();
2957+
}
2958+
27442959
#[test]
27452960
fn test_burn() {
27462961
let program_id = pubkey_rand();

0 commit comments

Comments
 (0)