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

Commit 6f887ba

Browse files
committed
backport #434 to v2
1 parent 45cebaf commit 6f887ba

File tree

1 file changed

+219
-6
lines changed

1 file changed

+219
-6
lines changed

token/program/src/processor.rs

Lines changed: 219 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ impl Processor {
257257
let delegate_info = next_account_info(account_info_iter)?;
258258
let owner_info = next_account_info(account_info_iter)?;
259259

260-
let mut source_data = source_account_info.data.borrow_mut();
261-
Account::unpack_mut(&mut source_data, &mut |source_account: &mut Account| {
260+
let mut source_account = Account::unpack(&source_account_info.data.borrow())?;
261+
262262
if source_account.is_frozen() {
263263
return Err(TokenError::AccountFrozen.into());
264264
}
@@ -284,17 +284,18 @@ impl Processor {
284284
source_account.delegate = COption::Some(*delegate_info.key);
285285
source_account.delegated_amount = amount;
286286

287+
Account::pack(source_account, &mut source_account_info.data.borrow_mut())?;
288+
287289
Ok(())
288-
})
289290
}
290291

291292
/// Processes an [Revoke](enum.TokenInstruction.html) instruction.
292293
pub fn process_revoke(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
293294
let account_info_iter = &mut accounts.iter();
294295
let source_account_info = next_account_info(account_info_iter)?;
295296

296-
let mut source_data = source_account_info.data.borrow_mut();
297-
Account::unpack_mut(&mut source_data, &mut |source_account: &mut Account| {
297+
let mut source_account = Account::unpack(&source_account_info.data.borrow())?;
298+
298299
let owner_info = next_account_info(account_info_iter)?;
299300

300301
if source_account.is_frozen() {
@@ -311,8 +312,9 @@ impl Processor {
311312
source_account.delegate = COption::None;
312313
source_account.delegated_amount = 0;
313314

315+
Account::pack(source_account, &mut source_account_info.data.borrow_mut())?;
316+
314317
Ok(())
315-
})
316318
}
317319

318320
/// Processes a [SetAuthority](enum.TokenInstruction.html) instruction.
@@ -1976,6 +1978,217 @@ mod tests {
19761978
.unwrap();
19771979
}
19781980

1981+
#[test]
1982+
fn test_approve_dups() {
1983+
let program_id = pubkey_rand();
1984+
let account1_key = pubkey_rand();
1985+
let mut account1_account = SolanaAccount::new(
1986+
account_minimum_balance(),
1987+
Account::get_packed_len(),
1988+
&program_id,
1989+
);
1990+
let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
1991+
let account2_key = pubkey_rand();
1992+
let mut account2_account = SolanaAccount::new(
1993+
account_minimum_balance(),
1994+
Account::get_packed_len(),
1995+
&program_id,
1996+
);
1997+
let account2_info: AccountInfo = (&account2_key, false, &mut account2_account).into();
1998+
let account3_key = pubkey_rand();
1999+
let mut account3_account = SolanaAccount::new(
2000+
account_minimum_balance(),
2001+
Account::get_packed_len(),
2002+
&program_id,
2003+
);
2004+
let account3_info: AccountInfo = (&account3_key, true, &mut account3_account).into();
2005+
let multisig_key = pubkey_rand();
2006+
let mut multisig_account = SolanaAccount::new(
2007+
multisig_minimum_balance(),
2008+
Multisig::get_packed_len(),
2009+
&program_id,
2010+
);
2011+
let multisig_info: AccountInfo = (&multisig_key, true, &mut multisig_account).into();
2012+
let owner_key = pubkey_rand();
2013+
let mut owner_account = SolanaAccount::default();
2014+
let owner_info: AccountInfo = (&owner_key, true, &mut owner_account).into();
2015+
let mint_key = pubkey_rand();
2016+
let mut mint_account =
2017+
SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
2018+
let mint_info: AccountInfo = (&mint_key, false, &mut mint_account).into();
2019+
let rent_key = rent::id();
2020+
let mut rent_sysvar = rent_sysvar();
2021+
let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
2022+
2023+
// create mint
2024+
do_process_instruction_dups(
2025+
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
2026+
vec![mint_info.clone(), rent_info.clone()],
2027+
)
2028+
.unwrap();
2029+
2030+
// create account
2031+
do_process_instruction_dups(
2032+
initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
2033+
vec![
2034+
account1_info.clone(),
2035+
mint_info.clone(),
2036+
account1_info.clone(),
2037+
rent_info.clone(),
2038+
],
2039+
)
2040+
.unwrap();
2041+
2042+
// create another account
2043+
do_process_instruction_dups(
2044+
initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
2045+
vec![
2046+
account2_info.clone(),
2047+
mint_info.clone(),
2048+
owner_info.clone(),
2049+
rent_info.clone(),
2050+
],
2051+
)
2052+
.unwrap();
2053+
2054+
// mint to account
2055+
do_process_instruction_dups(
2056+
mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
2057+
vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
2058+
)
2059+
.unwrap();
2060+
2061+
// source-owner approve
2062+
do_process_instruction_dups(
2063+
approve(
2064+
&program_id,
2065+
&account1_key,
2066+
&account2_key,
2067+
&account1_key,
2068+
&[],
2069+
500,
2070+
)
2071+
.unwrap(),
2072+
vec![
2073+
account1_info.clone(),
2074+
account2_info.clone(),
2075+
account1_info.clone(),
2076+
],
2077+
)
2078+
.unwrap();
2079+
2080+
// source-owner approve2
2081+
do_process_instruction_dups(
2082+
approve2(
2083+
&program_id,
2084+
&account1_key,
2085+
&mint_key,
2086+
&account2_key,
2087+
&account1_key,
2088+
&[],
2089+
500,
2090+
2,
2091+
)
2092+
.unwrap(),
2093+
vec![
2094+
account1_info.clone(),
2095+
mint_info.clone(),
2096+
account2_info.clone(),
2097+
account1_info.clone(),
2098+
],
2099+
)
2100+
.unwrap();
2101+
2102+
// source-owner revoke
2103+
do_process_instruction_dups(
2104+
revoke(&program_id, &account1_key, &account1_key, &[]).unwrap(),
2105+
vec![account1_info.clone(), account1_info.clone()],
2106+
)
2107+
.unwrap();
2108+
2109+
// test source-multisig signer
2110+
do_process_instruction_dups(
2111+
initialize_multisig(&program_id, &multisig_key, &[&account3_key], 1).unwrap(),
2112+
vec![
2113+
multisig_info.clone(),
2114+
rent_info.clone(),
2115+
account3_info.clone(),
2116+
],
2117+
)
2118+
.unwrap();
2119+
2120+
do_process_instruction_dups(
2121+
initialize_account(&program_id, &account3_key, &mint_key, &multisig_key).unwrap(),
2122+
vec![
2123+
account3_info.clone(),
2124+
mint_info.clone(),
2125+
multisig_info.clone(),
2126+
rent_info.clone(),
2127+
],
2128+
)
2129+
.unwrap();
2130+
2131+
do_process_instruction_dups(
2132+
mint_to(&program_id, &mint_key, &account3_key, &owner_key, &[], 1000).unwrap(),
2133+
vec![mint_info.clone(), account3_info.clone(), owner_info.clone()],
2134+
)
2135+
.unwrap();
2136+
2137+
// source-multisig-signer approve
2138+
do_process_instruction_dups(
2139+
approve(
2140+
&program_id,
2141+
&account3_key,
2142+
&account2_key,
2143+
&multisig_key,
2144+
&[&account3_key],
2145+
500,
2146+
)
2147+
.unwrap(),
2148+
vec![
2149+
account3_info.clone(),
2150+
account2_info.clone(),
2151+
multisig_info.clone(),
2152+
account3_info.clone(),
2153+
],
2154+
)
2155+
.unwrap();
2156+
2157+
// source-multisig-signer approve2
2158+
do_process_instruction_dups(
2159+
approve2(
2160+
&program_id,
2161+
&account3_key,
2162+
&mint_key,
2163+
&account2_key,
2164+
&multisig_key,
2165+
&[&account3_key],
2166+
500,
2167+
2,
2168+
)
2169+
.unwrap(),
2170+
vec![
2171+
account3_info.clone(),
2172+
mint_info.clone(),
2173+
account2_info.clone(),
2174+
multisig_info.clone(),
2175+
account3_info.clone(),
2176+
],
2177+
)
2178+
.unwrap();
2179+
2180+
// source-owner multisig-signer
2181+
do_process_instruction_dups(
2182+
revoke(&program_id, &account3_key, &multisig_key, &[&account3_key]).unwrap(),
2183+
vec![
2184+
account3_info.clone(),
2185+
multisig_info.clone(),
2186+
account3_info.clone(),
2187+
],
2188+
)
2189+
.unwrap();
2190+
}
2191+
19792192
#[test]
19802193
fn test_approve() {
19812194
let program_id = pubkey_rand();

0 commit comments

Comments
 (0)