Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for mutability of init payer #1271

Merged
merged 5 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ incremented for features.

## [Unreleased]

### Breaking

* lang: Enforce that the payer for an init-ed account be marked `mut` ([#1271](https://github.com/project-serum/anchor/pull/1271)).

## [0.21.0] - 2022-02-07

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 62 additions & 32 deletions lang/syn/src/parser/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,55 +39,85 @@ pub fn parse(strct: &syn::ItemStruct) -> ParseResult<AccountsStruct> {

fn constraints_cross_checks(fields: &[AccountField]) -> ParseResult<()> {
// INIT
let init_field = fields.iter().find(|f| {
if let AccountField::Field(field) = f {
field.constraints.init.is_some()
} else {
false
}
});
if let Some(init_field) = init_field {
let init_fields: Vec<&Field> = fields
.iter()
.filter_map(|f| match f {
AccountField::Field(field) if field.constraints.init.is_some() => Some(field),
_ => None,
})
.collect();

if !init_fields.is_empty() {
// init needs system program.
if fields.iter().all(|f| f.ident() != "system_program") {
return Err(ParseError::new(
init_field.ident().span(),
init_fields[0].ident.span(),
"the init constraint requires \
the system_program field to exist in the account \
validation struct. Use the program type to add \
the system_program field to your validation struct.",
));
}
if let AccountField::Field(field) = init_field {
let kind = &field.constraints.init.as_ref().unwrap().kind;
// init token/a_token/mint needs token program.
match kind {
InitKind::Program { .. } => (),
InitKind::Token { .. }
| InitKind::AssociatedToken { .. }
| InitKind::Mint { .. } => {
if fields.iter().all(|f| f.ident() != "token_program") {
return Err(ParseError::new(
init_field.ident().span(),
"the init constraint requires \

let kind = &init_fields[0].constraints.init.as_ref().unwrap().kind;
// init token/a_token/mint needs token program.
match kind {
InitKind::Program { .. } => (),
InitKind::Token { .. } | InitKind::AssociatedToken { .. } | InitKind::Mint { .. } => {
if fields.iter().all(|f| f.ident() != "token_program") {
return Err(ParseError::new(
init_fields[0].ident.span(),
"the init constraint requires \
the token_program field to exist in the account \
validation struct. Use the program type to add \
the token_program field to your validation struct.",
));
}
));
}
}
// a_token needs associated token program.
if let InitKind::AssociatedToken { .. } = kind {
if fields
.iter()
.all(|f| f.ident() != "associated_token_program")
{
return Err(ParseError::new(
init_field.ident().span(),
"the init constraint requires \
}
// a_token needs associated token program.
if let InitKind::AssociatedToken { .. } = kind {
if fields
.iter()
.all(|f| f.ident() != "associated_token_program")
{
return Err(ParseError::new(
init_fields[0].ident.span(),
"the init constraint requires \
the associated_token_program field to exist in the account \
validation struct. Use the program type to add \
the associated_token_program field to your validation struct.",
));
}
}

for field in init_fields {
// Get payer for init-ed account
let associated_payer_name = match field.constraints.init.clone().unwrap().payer.unwrap()
{
// composite payer, check not supported
Expr::Field(_) => continue,
field_name => field_name.to_token_stream().to_string(),
};
losman0s marked this conversation as resolved.
Show resolved Hide resolved

// Check payer is mutable
let associated_payer_field = fields.iter().find_map(|f| match f {
AccountField::Field(field) if *f.ident() == associated_payer_name => Some(field),
_ => None,
});
match associated_payer_field {
Some(associated_payer_field) => {
if !associated_payer_field.constraints.is_mutable() {
return Err(ParseError::new(
field.ident.span(),
"the payer specified for an init constraint must be mutable.",
));
}
}
_ => {
return Err(ParseError::new(
field.ident.span(),
"the payer specified does not exist.",
));
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/auction-house
6 changes: 2 additions & 4 deletions tests/cashiers-check/programs/cashiers-check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ pub struct CashCheck<'info> {
check_signer: AccountInfo<'info>,
#[account(mut, has_one = owner)]
to: Account<'info, TokenAccount>,
#[account(signer)]
owner: AccountInfo<'info>,
owner: Signer<'info>,
token_program: AccountInfo<'info>,
}

Expand All @@ -148,8 +147,7 @@ pub struct CancelCheck<'info> {
check_signer: AccountInfo<'info>,
#[account(mut, has_one = owner)]
from: Account<'info, TokenAccount>,
#[account(signer)]
owner: AccountInfo<'info>,
owner: Signer<'info>,
token_program: AccountInfo<'info>,
}

Expand Down
2 changes: 2 additions & 0 deletions tests/cfo/programs/cfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ pub struct CreateOfficer<'info> {
token::authority = officer,
)]
treasury: Box<Account<'info, TokenAccount>>,
#[account(mut)]
authority: Signer<'info>,
#[cfg_attr(
not(feature = "test"),
Expand Down Expand Up @@ -393,6 +394,7 @@ pub struct AuthorizeMarket<'info> {
bump,
)]
market_auth: Account<'info, MarketAuth>,
#[account(mut)]
payer: Signer<'info>,
// Not read or written to so not validated.
market: UncheckedAccount<'info>,
Expand Down
7 changes: 3 additions & 4 deletions tests/chat/programs/chat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub struct CreateUser<'info> {
space = 320,
)]
user: Account<'info, User>,
#[account(signer)]
authority: AccountInfo<'info>,
#[account(mut)]
authority: Signer<'info>,
system_program: AccountInfo<'info>,
}

Expand All @@ -68,8 +68,7 @@ pub struct SendMessage<'info> {
has_one = authority,
)]
user: Account<'info, User>,
#[account(signer)]
authority: AccountInfo<'info>,
authority: Signer<'info>,
#[account(mut)]
chat_room: Loader<'info, ChatRoom>,
}
Expand Down
3 changes: 1 addition & 2 deletions tests/errors/programs/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ pub struct HasOneError<'info> {

#[derive(Accounts)]
pub struct SignerError<'info> {
#[account(signer)]
my_account: AccountInfo<'info>,
my_account: Signer<'info>,
}

#[account]
Expand Down
2 changes: 1 addition & 1 deletion tests/errors/tests/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe("errors", () => {
assert.ok(false);
} catch (err) {
const errMsg =
"Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x7d2";
"Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0xbc2";
assert.equal(err.toString(), errMsg);
}
});
Expand Down
6 changes: 3 additions & 3 deletions tests/escrow/programs/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ pub mod escrow {
#[derive(Accounts)]
#[instruction(initializer_amount: u64)]
pub struct InitializeEscrow<'info> {
#[account(signer)]
pub initializer: AccountInfo<'info>,
#[account(mut)]
pub initializer: Signer<'info>,
#[account(
mut,
constraint = initializer_deposit_token_account.amount >= initializer_amount
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'info> From<&mut InitializeEscrow<'info>>
.initializer_deposit_token_account
.to_account_info()
.clone(),
current_authority: accounts.initializer.clone(),
current_authority: accounts.initializer.to_account_info().clone(),
};
let cpi_program = accounts.token_program.to_account_info();
CpiContext::new(cpi_program, cpi_accounts)
Expand Down
6 changes: 2 additions & 4 deletions tests/lockup/programs/lockup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ pub struct Withdraw<'info> {
// Vesting.
#[account(mut, has_one = beneficiary, has_one = vault)]
vesting: Account<'info, Vesting>,
#[account(signer)]
beneficiary: AccountInfo<'info>,
beneficiary: Signer<'info>,
#[account(mut)]
vault: Account<'info, TokenAccount>,
#[account(
Expand Down Expand Up @@ -281,8 +280,7 @@ pub struct WhitelistDeposit<'info> {
#[derive(Accounts)]
pub struct WhitelistTransfer<'info> {
lockup: ProgramState<'info, Lockup>,
#[account(signer)]
beneficiary: AccountInfo<'info>,
beneficiary: Signer<'info>,
whitelisted_program: AccountInfo<'info>,

// Whitelist interface.
Expand Down
Loading