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: Remove program account info from CPI context #2762

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions bench/BINARY_SIZE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ The programs and their tests are located in [/tests/bench](https://github.com/co

Solana version: 1.17.0

| Program | Binary Size | - |
| ------- | ----------- | ------------------- |
| bench | 743,208 | 🔴 **+152 (0.02%)** |
| Program | Binary Size | - |
| ------- | ----------- | ---------------------- |
| bench | 712,624 | 🟢 **-30,432 (4.10%)** |

### Notable changes

Expand Down
178 changes: 89 additions & 89 deletions bench/COMPUTE_UNITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,95 +16,95 @@ The programs and their tests are located in [/tests/bench](https://github.com/co

Solana version: 1.17.0

| Instruction | Compute Units | - |
| --------------------------- | ------------- | --- |
| accountInfo1 | 695 | - |
| accountInfo2 | 1,035 | - |
| accountInfo4 | 1,730 | - |
| accountInfo8 | 3,342 | - |
| accountEmptyInit1 | 5,552 | - |
| accountEmpty1 | 819 | - |
| accountEmptyInit2 | 10,421 | - |
| accountEmpty2 | 1,275 | - |
| accountEmptyInit4 | 19,803 | - |
| accountEmpty4 | 2,177 | - |
| accountEmptyInit8 | 38,609 | - |
| accountEmpty8 | 3,990 | - |
| accountSizedInit1 | 5,647 | - |
| accountSized1 | 843 | - |
| accountSizedInit2 | 10,607 | - |
| accountSized2 | 1,317 | - |
| accountSizedInit4 | 20,225 | - |
| accountSized4 | 2,274 | - |
| accountSizedInit8 | 39,376 | - |
| accountSized8 | 4,185 | - |
| accountUnsizedInit1 | 5,740 | - |
| accountUnsized1 | 870 | - |
| accountUnsizedInit2 | 10,856 | - |
| accountUnsized2 | 1,379 | - |
| accountUnsizedInit4 | 20,652 | - |
| accountUnsized4 | 2,411 | - |
| accountUnsizedInit8 | 39,969 | - |
| accountUnsized8 | 4,478 | - |
| boxedAccountEmptyInit1 | 5,605 | - |
| boxedAccountEmpty1 | 856 | - |
| boxedAccountEmptyInit2 | 10,522 | - |
| boxedAccountEmpty2 | 1,347 | - |
| boxedAccountEmptyInit4 | 20,002 | - |
| boxedAccountEmpty4 | 2,324 | - |
| boxedAccountEmptyInit8 | 39,002 | - |
| boxedAccountEmpty8 | 4,311 | - |
| boxedAccountSizedInit1 | 5,686 | - |
| boxedAccountSized1 | 878 | - |
| boxedAccountSizedInit2 | 10,690 | - |
| boxedAccountSized2 | 1,394 | - |
| boxedAccountSizedInit4 | 20,338 | - |
| boxedAccountSized4 | 2,413 | - |
| boxedAccountSizedInit8 | 39,670 | - |
| boxedAccountSized8 | 4,493 | - |
| boxedAccountUnsizedInit1 | 5,774 | - |
| boxedAccountUnsized1 | 908 | - |
| boxedAccountUnsizedInit2 | 10,866 | - |
| boxedAccountUnsized2 | 1,457 | - |
| boxedAccountUnsizedInit4 | 20,688 | - |
| boxedAccountUnsized4 | 2,546 | - |
| boxedAccountUnsizedInit8 | 40,375 | - |
| boxedAccountUnsized8 | 4,759 | - |
| boxedInterfaceAccountMint1 | 2,196 | - |
| boxedInterfaceAccountMint2 | 3,847 | - |
| boxedInterfaceAccountMint4 | 7,132 | - |
| boxedInterfaceAccountMint8 | 13,743 | - |
| boxedInterfaceAccountToken1 | 2,126 | - |
| boxedInterfaceAccountToken2 | 3,706 | - |
| boxedInterfaceAccountToken4 | 6,853 | - |
| boxedInterfaceAccountToken8 | 13,184 | - |
| interfaceAccountMint1 | 2,285 | - |
| interfaceAccountMint2 | 4,178 | - |
| interfaceAccountMint4 | 7,964 | - |
| interfaceAccountMint8 | 15,538 | - |
| interfaceAccountToken1 | 2,212 | - |
| interfaceAccountToken2 | 4,030 | - |
| interfaceAccountToken4 | 7,663 | - |
| interface1 | 741 | - |
| interface2 | 934 | - |
| interface4 | 1,315 | - |
| interface8 | 2,086 | - |
| program1 | 741 | - |
| program2 | 934 | - |
| program4 | 1,317 | - |
| program8 | 2,086 | - |
| signer1 | 675 | - |
| signer2 | 987 | - |
| signer4 | 1,606 | - |
| signer8 | 2,846 | - |
| systemAccount1 | 729 | - |
| systemAccount2 | 1,093 | - |
| systemAccount4 | 1,817 | - |
| systemAccount8 | 3,271 | - |
| uncheckedAccount1 | 657 | - |
| uncheckedAccount2 | 949 | - |
| uncheckedAccount4 | 1,526 | - |
| uncheckedAccount8 | 2,688 | - |
| Instruction | Compute Units | - |
| --------------------------- | ------------- | ------------------- |
| accountInfo1 | 695 | - |
| accountInfo2 | 1,035 | - |
| accountInfo4 | 1,730 | - |
| accountInfo8 | 3,342 | - |
| accountEmptyInit1 | 5,497 | 🟢 **-55 (0.99%)** |
| accountEmpty1 | 819 | - |
| accountEmptyInit2 | 10,183 | 🟢 **-238 (2.28%)** |
| accountEmpty2 | 1,275 | - |
| accountEmptyInit4 | 19,327 | 🟢 **-476 (2.40%)** |
| accountEmpty4 | 2,177 | - |
| accountEmptyInit8 | 37,655 | 🟢 **-954 (2.47%)** |
| accountEmpty8 | 3,990 | - |
| accountSizedInit1 | 5,647 | - |
| accountSized1 | 868 | 🔴 **+25 (2.97%)** |
| accountSizedInit2 | 10,419 | 🟢 **-188 (1.77%)** |
| accountSized2 | 1,367 | 🔴 **+50 (3.80%)** |
| accountSizedInit4 | 19,849 | 🟢 **-376 (1.86%)** |
| accountSized4 | 2,374 | 🔴 **+100 (4.40%)** |
| accountSizedInit8 | 38,622 | 🟢 **-754 (1.91%)** |
| accountSized8 | 4,385 | 🔴 **+200 (4.78%)** |
| accountUnsizedInit1 | 5,682 | 🟢 **-58 (1.01%)** |
| accountUnsized1 | 870 | - |
| accountUnsizedInit2 | 10,618 | 🟢 **-238 (2.19%)** |
| accountUnsized2 | 1,379 | - |
| accountUnsizedInit4 | 20,180 | 🟢 **-472 (2.29%)** |
| accountUnsized4 | 2,411 | - |
| accountUnsizedInit8 | 39,013 | 🟢 **-956 (2.39%)** |
| accountUnsized8 | 4,478 | - |
| boxedAccountEmptyInit1 | 5,547 | 🟢 **-58 (1.03%)** |
| boxedAccountEmpty1 | 856 | - |
| boxedAccountEmptyInit2 | 10,284 | 🟢 **-238 (2.26%)** |
| boxedAccountEmpty2 | 1,347 | - |
| boxedAccountEmptyInit4 | 19,526 | 🟢 **-476 (2.38%)** |
| boxedAccountEmpty4 | 2,324 | - |
| boxedAccountEmptyInit8 | 38,048 | 🟢 **-954 (2.45%)** |
| boxedAccountEmpty8 | 4,311 | - |
| boxedAccountSizedInit1 | 5,686 | - |
| boxedAccountSized1 | 903 | 🔴 **+25 (2.85%)** |
| boxedAccountSizedInit2 | 10,502 | 🟢 **-188 (1.76%)** |
| boxedAccountSized2 | 1,444 | 🔴 **+50 (3.59%)** |
| boxedAccountSizedInit4 | 19,960 | 🟢 **-378 (1.86%)** |
| boxedAccountSized4 | 2,513 | 🔴 **+100 (4.14%)** |
| boxedAccountSizedInit8 | 38,916 | 🟢 **-754 (1.90%)** |
| boxedAccountSized8 | 4,693 | 🔴 **+200 (4.45%)** |
| boxedAccountUnsizedInit1 | 5,774 | - |
| boxedAccountUnsized1 | 908 | - |
| boxedAccountUnsizedInit2 | 10,628 | 🟢 **-238 (2.19%)** |
| boxedAccountUnsized2 | 1,457 | - |
| boxedAccountUnsizedInit4 | 20,212 | 🟢 **-476 (2.30%)** |
| boxedAccountUnsized4 | 2,546 | - |
| boxedAccountUnsizedInit8 | 39,421 | 🟢 **-954 (2.36%)** |
| boxedAccountUnsized8 | 4,759 | - |
| boxedInterfaceAccountMint1 | 2,196 | - |
| boxedInterfaceAccountMint2 | 3,847 | - |
| boxedInterfaceAccountMint4 | 7,132 | - |
| boxedInterfaceAccountMint8 | 13,743 | - |
| boxedInterfaceAccountToken1 | 2,126 | - |
| boxedInterfaceAccountToken2 | 3,706 | - |
| boxedInterfaceAccountToken4 | 6,853 | - |
| boxedInterfaceAccountToken8 | 13,184 | - |
| interfaceAccountMint1 | 2,285 | - |
| interfaceAccountMint2 | 4,178 | - |
| interfaceAccountMint4 | 7,964 | - |
| interfaceAccountMint8 | 15,538 | - |
| interfaceAccountToken1 | 2,212 | - |
| interfaceAccountToken2 | 4,030 | - |
| interfaceAccountToken4 | 7,663 | - |
| interface1 | 741 | - |
| interface2 | 934 | - |
| interface4 | 1,315 | - |
| interface8 | 2,086 | - |
| program1 | 741 | - |
| program2 | 934 | - |
| program4 | 1,317 | - |
| program8 | 2,086 | - |
| signer1 | 675 | - |
| signer2 | 987 | - |
| signer4 | 1,606 | - |
| signer8 | 2,846 | - |
| systemAccount1 | 729 | - |
| systemAccount2 | 1,093 | - |
| systemAccount4 | 1,817 | - |
| systemAccount8 | 3,271 | - |
| uncheckedAccount1 | 657 | - |
| uncheckedAccount2 | 949 | - |
| uncheckedAccount4 | 1,526 | - |
| uncheckedAccount8 | 2,688 | - |

### Notable changes

Expand Down
3 changes: 1 addition & 2 deletions examples/tutorial/basic-3/programs/puppet-master/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ declare_id!("HmbTLCmaGvZhKnn1Zfa1JVnp7vkMV4DYVxPLWBVoN65L");
mod puppet_master {
use super::*;
pub fn pull_strings(ctx: Context<PullStrings>, data: u64) -> anchor_lang::Result<()> {
let cpi_program = ctx.accounts.puppet_program.to_account_info();
let cpi_accounts = SetData {
puppet: ctx.accounts.puppet.to_account_info(),
};
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
let cpi_ctx = CpiContext::new(cpi_accounts);
puppet::cpi::set_data(cpi_ctx, data)
}
}
Expand Down
40 changes: 33 additions & 7 deletions lang/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,37 +174,64 @@ where
{
pub accounts: T,
pub remaining_accounts: Vec<AccountInfo<'info>>,
pub program: AccountInfo<'info>,
pub program_id: Option<Pubkey>,
pub signer_seeds: &'a [&'b [&'c [u8]]],
}

impl<'a, 'b, 'c, 'info, T> CpiContext<'a, 'b, 'c, 'info, T>
where
T: ToAccountMetas + ToAccountInfos<'info>,
{
pub fn new(program: AccountInfo<'info>, accounts: T) -> Self {
#[must_use]
pub fn new(accounts: T) -> Self {
Self {
accounts,
program,
program_id: None,
remaining_accounts: Vec::new(),
signer_seeds: &[],
}
}

#[must_use]
pub fn new_with_signer(
program: AccountInfo<'info>,
pub fn new_with_id(accounts: T, program_id: Pubkey) -> Self {
Self {
accounts,
program_id: Some(program_id),
remaining_accounts: Vec::new(),
signer_seeds: &[],
}
}

#[must_use]
pub fn new_with_signer(accounts: T, signer_seeds: &'a [&'b [&'c [u8]]]) -> Self {
Self {
accounts,
program_id: None,
signer_seeds,
remaining_accounts: Vec::new(),
}
}

#[must_use]
pub fn new_with_id_and_signer(
accounts: T,
program_id: Pubkey,
signer_seeds: &'a [&'b [&'c [u8]]],
) -> Self {
Self {
accounts,
program,
program_id: Some(program_id),
signer_seeds,
remaining_accounts: Vec::new(),
}
}

#[must_use]
pub fn with_program_id(mut self, program_id: Pubkey) -> Self {
self.program_id = Some(program_id);
self
}

#[must_use]
pub fn with_signer(mut self, signer_seeds: &'a [&'b [&'c [u8]]]) -> Self {
self.signer_seeds = signer_seeds;
Expand All @@ -224,7 +251,6 @@ impl<'info, T: ToAccountInfos<'info> + ToAccountMetas> ToAccountInfos<'info>
fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
let mut infos = self.accounts.to_account_infos();
infos.extend_from_slice(&self.remaining_accounts);
infos.push(self.program.clone());
infos
}
}
Expand Down
20 changes: 9 additions & 11 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ fn generate_constraint_realloc(
if __new_rent_minimum > __field_info.lamports() {
anchor_lang::system_program::transfer(
anchor_lang::context::CpiContext::new(
system_program.to_account_info(),
anchor_lang::system_program::Transfer {
from: #payer.to_account_info(),
to: __field_info.clone(),
Expand Down Expand Up @@ -558,13 +557,13 @@ fn generate_constraint_init_group(
#create_account

// Initialize the token account.
let cpi_program = #token_program.to_account_info();
let cpi_program_id = #token_program.key();
let accounts = ::anchor_spl::token_interface::InitializeAccount3 {
account: #field.to_account_info(),
mint: #mint.to_account_info(),
authority: #owner.to_account_info(),
};
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
let cpi_ctx = anchor_lang::context::CpiContext::new_with_id(accounts, cpi_program_id);
::anchor_spl::token_interface::initialize_account3(cpi_ctx)?;
}

Expand Down Expand Up @@ -625,7 +624,6 @@ fn generate_constraint_init_group(
if !#if_needed || owner_program == &anchor_lang::solana_program::system_program::ID {
#payer_optional_check

let cpi_program = associated_token_program.to_account_info();
let cpi_accounts = ::anchor_spl::associated_token::Create {
payer: #payer.to_account_info(),
associated_token: #field.to_account_info(),
Expand All @@ -634,7 +632,7 @@ fn generate_constraint_init_group(
system_program: system_program.to_account_info(),
token_program: #token_program.to_account_info(),
};
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, cpi_accounts);
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_accounts);
::anchor_spl::associated_token::create(cpi_ctx)?;
}
let pa: #ty_decl = #from_account_info_unchecked;
Expand Down Expand Up @@ -717,11 +715,11 @@ fn generate_constraint_init_group(
#create_account

// Initialize the mint account.
let cpi_program = #token_program.to_account_info();
let cpi_program_id = #token_program.key();
let accounts = ::anchor_spl::token_interface::InitializeMint2 {
mint: #field.to_account_info(),
};
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
let cpi_ctx = anchor_lang::context::CpiContext::new_with_id(accounts, cpi_program_id);
::anchor_spl::token_interface::initialize_mint2(cpi_ctx, #decimals, &#owner.key(), #freeze_authority)?;
}
let pa: #ty_decl = #from_account_info_unchecked;
Expand Down Expand Up @@ -1166,7 +1164,7 @@ fn generate_create_account(
from: #payer.to_account_info(),
to: #field.to_account_info()
};
let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts);
let cpi_context = anchor_lang::context::CpiContext::new(cpi_accounts);
anchor_lang::system_program::create_account(cpi_context.with_signer(&[#seeds_with_nonce]), lamports, space as u64, #owner)?;
} else {
require_keys_neq!(#payer.key(), #field.key(), anchor_lang::error::ErrorCode::TryingToInitPayerAsProgramAccount);
Expand All @@ -1180,20 +1178,20 @@ fn generate_create_account(
from: #payer.to_account_info(),
to: #field.to_account_info(),
};
let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts);
let cpi_context = anchor_lang::context::CpiContext::new(cpi_accounts);
anchor_lang::system_program::transfer(cpi_context, required_lamports)?;
}
// Allocate space.
let cpi_accounts = anchor_lang::system_program::Allocate {
account_to_allocate: #field.to_account_info()
};
let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts);
let cpi_context = anchor_lang::context::CpiContext::new(cpi_accounts);
anchor_lang::system_program::allocate(cpi_context.with_signer(&[#seeds_with_nonce]), #space as u64)?;
// Assign to the spl token program.
let cpi_accounts = anchor_lang::system_program::Assign {
account_to_assign: #field.to_account_info()
};
let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts);
let cpi_context = anchor_lang::context::CpiContext::new(cpi_accounts);
anchor_lang::system_program::assign(cpi_context.with_signer(&[#seeds_with_nonce]), #owner)?;
}
}
Expand Down
Loading
Loading