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

Commit 2b1b9de

Browse files
committed
backport #436 to v2
1 parent 308e027 commit 2b1b9de

File tree

1 file changed

+103
-6
lines changed

1 file changed

+103
-6
lines changed

token/program/src/processor.rs

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,7 @@ impl Processor {
421421
let dest_account_info = next_account_info(account_info_iter)?;
422422
let owner_info = next_account_info(account_info_iter)?;
423423

424-
let mut dest_account_data = dest_account_info.data.borrow_mut();
425-
Account::unpack_mut(&mut dest_account_data, &mut |dest_account: &mut Account| {
424+
let mut dest_account = Account::unpack(&dest_account_info.data.borrow())?;
426425
if dest_account.is_frozen() {
427426
return Err(TokenError::AccountFrozen.into());
428427
}
@@ -434,8 +433,7 @@ impl Processor {
434433
return Err(TokenError::MintMismatch.into());
435434
}
436435

437-
let mut mint_data = mint_info.data.borrow_mut();
438-
Mint::unpack_mut(&mut mint_data, &mut |mint: &mut Mint| {
436+
let mut mint = Mint::unpack(&mint_info.data.borrow())?;
439437
if let Some(expected_decimals) = expected_decimals {
440438
if expected_decimals != mint.decimals {
441439
return Err(TokenError::MintDecimalsMismatch.into());
@@ -462,9 +460,10 @@ impl Processor {
462460
.checked_add(amount)
463461
.ok_or(TokenError::Overflow)?;
464462

463+
Account::pack(dest_account, &mut dest_account_info.data.borrow_mut())?;
464+
Mint::pack(mint, &mut mint_info.data.borrow_mut())?;
465+
465466
Ok(())
466-
})
467-
})
468467
}
469468

470469
/// Processes a [Burn](enum.TokenInstruction.html) instruction.
@@ -2524,6 +2523,104 @@ mod tests {
25242523
);
25252524
}
25262525

2526+
#[test]
2527+
fn test_mint_to_dups() {
2528+
let program_id = pubkey_rand();
2529+
let account1_key = pubkey_rand();
2530+
let mut account1_account = SolanaAccount::new(
2531+
account_minimum_balance(),
2532+
Account::get_packed_len(),
2533+
&program_id,
2534+
);
2535+
let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
2536+
let owner_key = pubkey_rand();
2537+
let mut owner_account = SolanaAccount::default();
2538+
let owner_info: AccountInfo = (&owner_key, true, &mut owner_account).into();
2539+
let mint_key = pubkey_rand();
2540+
let mut mint_account =
2541+
SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
2542+
let mint_info: AccountInfo = (&mint_key, true, &mut mint_account).into();
2543+
let rent_key = rent::id();
2544+
let mut rent_sysvar = rent_sysvar();
2545+
let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
2546+
2547+
// create mint
2548+
do_process_instruction_dups(
2549+
initialize_mint(&program_id, &mint_key, &mint_key, None, 2).unwrap(),
2550+
vec![mint_info.clone(), rent_info.clone()],
2551+
)
2552+
.unwrap();
2553+
2554+
// create account
2555+
do_process_instruction_dups(
2556+
initialize_account(&program_id, &account1_key, &mint_key, &owner_key).unwrap(),
2557+
vec![
2558+
account1_info.clone(),
2559+
mint_info.clone(),
2560+
owner_info.clone(),
2561+
rent_info.clone(),
2562+
],
2563+
)
2564+
.unwrap();
2565+
2566+
// mint_to when mint_authority is self
2567+
do_process_instruction_dups(
2568+
mint_to(&program_id, &mint_key, &account1_key, &mint_key, &[], 42).unwrap(),
2569+
vec![mint_info.clone(), account1_info.clone(), mint_info.clone()],
2570+
)
2571+
.unwrap();
2572+
2573+
// mint_to2 when mint_authority is self
2574+
do_process_instruction_dups(
2575+
mint_to2(&program_id, &mint_key, &account1_key, &mint_key, &[], 42, 2).unwrap(),
2576+
vec![mint_info.clone(), account1_info.clone(), mint_info.clone()],
2577+
)
2578+
.unwrap();
2579+
2580+
// mint_to when mint_authority is account owner
2581+
Mint::unpack_unchecked_mut(&mut mint_info.data.borrow_mut(), &mut |mint: &mut Mint| {
2582+
mint.mint_authority = COption::Some(account1_key);
2583+
Ok(())
2584+
})
2585+
.unwrap();
2586+
do_process_instruction_dups(
2587+
mint_to(
2588+
&program_id,
2589+
&mint_key,
2590+
&account1_key,
2591+
&account1_key,
2592+
&[],
2593+
42,
2594+
)
2595+
.unwrap(),
2596+
vec![
2597+
mint_info.clone(),
2598+
account1_info.clone(),
2599+
account1_info.clone(),
2600+
],
2601+
)
2602+
.unwrap();
2603+
2604+
// mint_to2 when mint_authority is account owner
2605+
do_process_instruction_dups(
2606+
mint_to(
2607+
&program_id,
2608+
&mint_key,
2609+
&account1_key,
2610+
&account1_key,
2611+
&[],
2612+
42,
2613+
)
2614+
.unwrap(),
2615+
vec![
2616+
mint_info.clone(),
2617+
account1_info.clone(),
2618+
account1_info.clone(),
2619+
],
2620+
)
2621+
.unwrap();
2622+
}
2623+
25272624
#[test]
25282625
fn test_mint_to() {
25292626
let program_id = pubkey_rand();

0 commit comments

Comments
 (0)