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

lang: error if bump target is given on init #1380

Merged
merged 7 commits into from
Feb 7, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ incremented for features.
* lang: Put `init_if_needed` behind a feature flag to decrease wrong usage ([#1258](https://github.com/project-serum/anchor/pull/1258)).
* lang: rename `loader_account` module to `account_loader` module ([#1279](https://github.com/project-serum/anchor/pull/1279))
* lang: The `Accounts` trait's `try_accounts` method now has an additional `bumps: &mut BTreeMap<String, u8>` argument, which accumulates bump seeds ([#1367](https://github.com/project-serum/anchor/pull/1367)).
* lang: Providing `bump = <target>` targets with `init` will now error. On `init` only, it is required to use `bump` without a target and access the seed inside function handlers via `ctx.bumps.get("<pda-account-name")`. For subsequent seeds constraints (without init), it is recommended to store the bump on your account and use it as a `bump = <target>` target to minimize compute units used ([#1380](https://github.com/project-serum/anchor/pull/1380)).
* ts: `Coder` is now an interface and the existing class has been renamed to `BorshCoder`. This change allows the generation of Anchor clients for non anchor programs ([#1259](https://github.com/project-serum/anchor/pull/1259/files)).
* cli: [[test.clone]] key in Anchor.toml is renamed to [[test.validator.clone]] ([#1366](https://github.com/project-serum/anchor/pull/1366)).

Expand Down
10 changes: 10 additions & 0 deletions lang/syn/src/parser/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
self.signer
.replace(Context::new(i.span(), ConstraintSigner { error: None }));
}

// Assert a bump target is not given on init.
if let Some(b) = &self.bump {
if b.bump.is_some() {
return Err(ParseError::new(
b.span(),
"bump targets should not be provided with init. Please use bump without a target."
));
}
}
}

// Zero.
Expand Down
16 changes: 8 additions & 8 deletions tests/cfo/programs/cfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,14 @@ pub struct CreateOfficer<'info> {
#[account(
init,
seeds = [dex_program.key.as_ref()],
bump = bumps.bump,
bump,
payer = authority,
)]
officer: Box<Account<'info, Officer>>,
#[account(
init,
seeds = [b"token", officer.key().as_ref(), srm_mint.key().as_ref()],
bump = bumps.srm,
bump,
payer = authority,
token::mint = srm_mint,
token::authority = officer,
Expand All @@ -338,7 +338,7 @@ pub struct CreateOfficer<'info> {
#[account(
init,
seeds = [b"token", officer.key().as_ref(), usdc_mint.key().as_ref()],
bump = bumps.usdc,
bump,
payer = authority,
token::mint = usdc_mint,
token::authority = officer,
Expand All @@ -347,7 +347,7 @@ pub struct CreateOfficer<'info> {
#[account(
init,
seeds = [b"stake", officer.key().as_ref()],
bump = bumps.stake,
bump,
payer = authority,
token::mint = srm_mint,
token::authority = officer,
Expand All @@ -356,7 +356,7 @@ pub struct CreateOfficer<'info> {
#[account(
init,
seeds = [b"treasury", officer.key().as_ref()],
bump = bumps.treasury,
bump,
payer = authority,
token::mint = srm_mint,
token::authority = officer,
Expand Down Expand Up @@ -390,7 +390,7 @@ pub struct AuthorizeMarket<'info> {
init,
payer = payer,
seeds = [b"market-auth", officer.key().as_ref(), market.key.as_ref()],
bump = bump,
bump,
)]
market_auth: Account<'info, MarketAuth>,
payer: Signer<'info>,
Expand All @@ -416,7 +416,7 @@ pub struct CreateOfficerToken<'info> {
#[account(
init,
seeds = [b"token", officer.key().as_ref(), mint.key().as_ref()],
bump = bump,
bump,
token::mint = mint,
token::authority = officer,
payer = payer,
Expand All @@ -437,7 +437,7 @@ pub struct CreateOfficerOpenOrders<'info> {
#[account(
init,
seeds = [b"open-orders", officer.key().as_ref(), market.key.as_ref()],
bump = bump,
bump,
space = 12 + size_of::<OpenOrders>(),
payer = payer,
owner = dex::ID,
Expand Down
10 changes: 5 additions & 5 deletions tests/chat/programs/chat/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//! A simple chat program using a ring buffer to store messages.

use anchor_lang::prelude::*;
use anchor_lang::accounts::loader::Loader;
use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod chat {
use super::*;

pub fn create_user(ctx: Context<CreateUser>, name: String, bump: u8) -> Result<()> {
pub fn create_user(ctx: Context<CreateUser>, name: String) -> Result<()> {
ctx.accounts.user.name = name;
ctx.accounts.user.authority = *ctx.accounts.authority.key;
ctx.accounts.user.bump = bump;
ctx.accounts.user.bump = *ctx.bumps.get("user").unwrap();
Ok(())
}
pub fn create_chat_room(ctx: Context<CreateChatRoom>, name: String) -> Result<()> {
Expand All @@ -39,12 +39,12 @@ pub mod chat {
}

#[derive(Accounts)]
#[instruction(name: String, bump: u8)]
#[instruction(name: String)]
pub struct CreateUser<'info> {
#[account(
init,
seeds = [authority.key().as_ref()],
bump = bump,
bump,
payer = authority,
space = 320,
)]
Expand Down
2 changes: 1 addition & 1 deletion tests/chat/tests/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("chat", () => {
[authority.toBuffer()],
program.programId
);
await program.rpc.createUser("My User", bump, {
await program.rpc.createUser("My User", {
accounts: {
user,
authority,
Expand Down
17 changes: 11 additions & 6 deletions tests/ido-pool/programs/ido-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod ido_pool {
pub fn initialize_pool(
ctx: Context<InitializePool>,
ido_name: String,
bumps: PoolBumps,
_bumps: PoolBumps, // No longer used.
num_ido_tokens: u64,
ido_times: IdoTimes,
) -> ProgramResult {
Expand All @@ -32,7 +32,12 @@ pub mod ido_pool {
name_data[..name_bytes.len()].copy_from_slice(name_bytes);

ido_account.ido_name = name_data;
ido_account.bumps = bumps;
ido_account.bumps = PoolBumps {
ido_account: *ctx.bumps.get("ido_account").unwrap(),
redeemable_mint: *ctx.bumps.get("redeemable_mint").unwrap(),
pool_watermelon: *ctx.bumps.get("pool_watermelon").unwrap(),
pool_usdc: *ctx.bumps.get("pool_usdc").unwrap(),
};
ido_account.ido_authority = ctx.accounts.ido_authority.key();

ido_account.usdc_mint = ctx.accounts.usdc_mint.key();
Expand Down Expand Up @@ -289,7 +294,7 @@ pub struct InitializePool<'info> {
// IDO Accounts
#[account(init,
seeds = [ido_name.as_bytes()],
bump = bumps.ido_account,
bump,
payer = ido_authority)]
pub ido_account: Box<Account<'info, IdoAccount>>,
// TODO Confirm USDC mint address on mainnet or leave open as an option for other stables
Expand All @@ -299,7 +304,7 @@ pub struct InitializePool<'info> {
mint::decimals = DECIMALS,
mint::authority = ido_account,
seeds = [ido_name.as_bytes(), b"redeemable_mint".as_ref()],
bump = bumps.redeemable_mint,
bump,
payer = ido_authority)]
pub redeemable_mint: Box<Account<'info, Mint>>,
#[account(constraint = watermelon_mint.key() == ido_authority_watermelon.mint)]
Expand All @@ -308,14 +313,14 @@ pub struct InitializePool<'info> {
token::mint = watermelon_mint,
token::authority = ido_account,
seeds = [ido_name.as_bytes(), b"pool_watermelon"],
bump = bumps.pool_watermelon,
bump,
payer = ido_authority)]
pub pool_watermelon: Box<Account<'info, TokenAccount>>,
#[account(init,
token::mint = usdc_mint,
token::authority = ido_account,
seeds = [ido_name.as_bytes(), b"pool_usdc"],
bump = bumps.pool_usdc,
bump,
payer = ido_authority)]
pub pool_usdc: Box<Account<'info, TokenAccount>>,
// Programs and Sysvars
Expand Down
18 changes: 8 additions & 10 deletions tests/misc/programs/misc/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use crate::account::*;
use anchor_lang::prelude::*;
use anchor_lang::accounts::loader::Loader;
use anchor_lang::accounts::cpi_state::CpiState;
use anchor_lang::accounts::loader::Loader;
use anchor_lang::prelude::*;
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{Mint, Token, TokenAccount};
use misc2::misc2::MyState as Misc2State;
use std::mem::size_of;

#[derive(Accounts)]
#[instruction(token_bump: u8, mint_bump: u8)]
pub struct TestTokenSeedsInit<'info> {
#[account(
init,
seeds = [b"my-mint-seed".as_ref()],
bump = mint_bump,
bump,
payer = authority,
mint::decimals = 6,
mint::authority = authority,
Expand All @@ -22,7 +21,7 @@ pub struct TestTokenSeedsInit<'info> {
#[account(
init,
seeds = [b"my-token-seed".as_ref(),],
bump = token_bump,
bump,
payer = authority,
token::mint = mint,
token::authority = authority,
Expand Down Expand Up @@ -79,7 +78,7 @@ pub struct TestPdaInit<'info> {
#[account(
init,
seeds = [b"my-seed", domain.as_bytes(), foo.key.as_ref(), &seed],
bump = bump,
bump,
payer = my_payer,
)]
pub my_pda: Account<'info, DataU16>,
Expand All @@ -89,12 +88,11 @@ pub struct TestPdaInit<'info> {
}

#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct TestPdaInitZeroCopy<'info> {
#[account(
init,
seeds = [b"my-seed".as_ref()],
bump = bump,
bump,
payer = my_payer,
)]
pub my_pda: Loader<'info, DataZeroCopy>,
Expand Down Expand Up @@ -367,7 +365,7 @@ pub struct InitDecreaseLamports<'info> {
pub data: AccountInfo<'info>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
Expand All @@ -376,7 +374,7 @@ pub struct InitIfNeededChecksRentExemption<'info> {
pub data: AccountInfo<'info>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
Expand Down
20 changes: 9 additions & 11 deletions tests/misc/programs/misc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ pub mod misc {
Ok(())
}

pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>, bump: u8) -> ProgramResult {
pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>) -> ProgramResult {
let mut acc = ctx.accounts.my_pda.load_init()?;
acc.data = 9;
acc.bump = bump;
acc.bump = *ctx.bumps.get("my_pda").unwrap();
Ok(())
}

Expand All @@ -134,11 +134,7 @@ pub mod misc {
Ok(())
}

pub fn test_token_seeds_init(
_ctx: Context<TestTokenSeedsInit>,
_token_bump: u8,
_mint_bump: u8,
) -> ProgramResult {
pub fn test_token_seeds_init(_ctx: Context<TestTokenSeedsInit>) -> ProgramResult {
Ok(())
}

Expand Down Expand Up @@ -264,15 +260,17 @@ pub mod misc {
**ctx.accounts.user.try_borrow_mut_lamports()? += 1;
Ok(())
}

pub fn init_if_needed_checks_rent_exemption(_ctx: Context<InitIfNeededChecksRentExemption>) -> ProgramResult {

pub fn init_if_needed_checks_rent_exemption(
_ctx: Context<InitIfNeededChecksRentExemption>,
) -> ProgramResult {
Ok(())
}

pub fn test_program_id_constraint(
_ctx: Context<TestProgramIdConstraint>,
_bump: u8,
_second_bump: u8
_second_bump: u8,
) -> ProgramResult {
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions tests/misc/tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe("misc", () => {
[Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
program.programId
);
await program.rpc.testPdaInitZeroCopy(nonce, {
await program.rpc.testPdaInitZeroCopy({
accounts: {
myPda,
myPayer: program.provider.wallet.publicKey,
Expand Down Expand Up @@ -347,7 +347,7 @@ describe("misc", () => {
[Buffer.from(anchor.utils.bytes.utf8.encode("my-token-seed"))],
program.programId
);
await program.rpc.testTokenSeedsInit(token_bump, mint_bump, {
await program.rpc.testTokenSeedsInit({
accounts: {
myPda,
mint,
Expand Down
2 changes: 1 addition & 1 deletion ts/src/utils/features.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _AVAILABLE_FEATURES = new Set(["anchor-deprecated-state"]);
const _AVAILABLE_FEATURES = new Set(["anchor-deprecated-state", "debug-logs"]);

const _FEATURES = new Map();

Expand Down