Skip to content

Commit dd1a038

Browse files
Aursenswaroop-osec
andauthored
lang: Remove program account info from CPI context (#2762)
* lang: Remove program account info from CPI context * Fix tests and update the bench * Fix token extensions * Fix bench test * Update benchmark * Update benchmark * Fix missing tests * chore(bench): Update --------- Co-authored-by: Aursen <aursen@users.noreply.github.com> Co-authored-by: swaroop-osec <swaroop@osec.io>
1 parent 5da4d6a commit dd1a038

File tree

24 files changed

+198
-200
lines changed

24 files changed

+198
-200
lines changed

bench/BINARY_SIZE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Solana version: 2.3.0
4242

4343
Solana version: 2.1.0
4444

45-
| Program | Binary Size | - |
46-
| ------- | ----------- | --- |
47-
| bench | 1,041,928 | - |
45+
| Program | Binary Size | - |
46+
| ------- | ----------- | ---------------------- |
47+
| bench | 945,568 | 🟢 **-96,360 (9.25%)** |
4848

4949
### Notable changes
5050

bench/COMPUTE_UNITS.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -218,53 +218,53 @@ Solana version: 2.1.0
218218
| accountInfo2 | 895 | - |
219219
| accountInfo4 | 1,553 | - |
220220
| accountInfo8 | 2,923 | - |
221-
| accountEmptyInit1 | 4,829 | 🟢 **-254 (5.00%)** |
221+
| accountEmptyInit1 | 4,784 | 🟢 **-299 (5.88%)** |
222222
| accountEmpty1 | 645 | - |
223-
| accountEmptyInit2 | 8,793 | 🟢 **-508 (5.46%)** |
223+
| accountEmptyInit2 | 8,669 | 🟢 **-632 (6.79%)** |
224224
| accountEmpty2 | 1,007 | - |
225-
| accountEmptyInit4 | 16,748 | 🟢 **-1,016 (5.72%)** |
225+
| accountEmptyInit4 | 16,500 | 🟢 **-1,264 (7.12%)** |
226226
| accountEmpty4 | 1,724 | - |
227-
| accountEmptyInit8 | 32,691 | 🟢 **-2,032 (5.85%)** |
227+
| accountEmptyInit8 | 32,331 | 🟢 **-2,392 (6.89%)** |
228228
| accountEmpty8 | 3,163 | - |
229-
| accountSizedInit1 | 4,938 | 🟢 **-254 (4.89%)** |
229+
| accountSizedInit1 | 4,893 | 🟢 **-299 (5.76%)** |
230230
| accountSized1 | 693 | - |
231-
| accountSizedInit2 | 8,981 | 🟢 **-508 (5.35%)** |
231+
| accountSizedInit2 | 8,857 | 🟢 **-632 (6.66%)** |
232232
| accountSized2 | 1,075 | - |
233-
| accountSizedInit4 | 17,154 | 🟢 **-1,016 (5.59%)** |
233+
| accountSizedInit4 | 16,906 | 🟢 **-1,264 (6.96%)** |
234234
| accountSized4 | 1,848 | - |
235-
| accountSizedInit8 | 33,401 | 🟢 **-2,032 (5.73%)** |
235+
| accountSizedInit8 | 33,041 | 🟢 **-2,392 (6.75%)** |
236236
| accountSized8 | 3,387 | - |
237-
| accountUnsizedInit1 | 5,051 | 🟢 **-254 (4.79%)** |
237+
| accountUnsizedInit1 | 5,006 | 🟢 **-299 (5.64%)** |
238238
| accountUnsized1 | 746 | - |
239-
| accountUnsizedInit2 | 9,251 | 🟢 **-508 (5.21%)** |
239+
| accountUnsizedInit2 | 9,127 | 🟢 **-632 (6.48%)** |
240240
| accountUnsized2 | 1,163 | - |
241-
| accountUnsizedInit4 | 17,587 | 🟢 **-1,016 (5.46%)** |
241+
| accountUnsizedInit4 | 17,407 | 🟢 **-1,196 (6.43%)** |
242242
| accountUnsized4 | 2,002 | - |
243-
| accountUnsizedInit8 | 33,961 | 🟢 **-2,032 (5.65%)** |
243+
| accountUnsizedInit8 | 33,465 | 🟢 **-2,528 (7.02%)** |
244244
| accountUnsized8 | 3,673 | - |
245-
| boxedAccountEmptyInit1 | 4,921 | 🟢 **-254 (4.91%)** |
245+
| boxedAccountEmptyInit1 | 4,876 | 🟢 **-299 (5.78%)** |
246246
| boxedAccountEmpty1 | 734 | - |
247-
| boxedAccountEmptyInit2 | 8,906 | 🟢 **-508 (5.40%)** |
247+
| boxedAccountEmptyInit2 | 8,783 | 🟢 **-631 (6.70%)** |
248248
| boxedAccountEmpty2 | 1,116 | - |
249-
| boxedAccountEmptyInit4 | 16,902 | 🟢 **-1,016 (5.67%)** |
249+
| boxedAccountEmptyInit4 | 16,655 | 🟢 **-1,263 (7.05%)** |
250250
| boxedAccountEmpty4 | 1,872 | - |
251-
| boxedAccountEmptyInit8 | 32,921 | 🟢 **-2,032 (5.81%)** |
251+
| boxedAccountEmptyInit8 | 32,561 | 🟢 **-2,392 (6.84%)** |
252252
| boxedAccountEmpty8 | 3,401 | - |
253-
| boxedAccountSizedInit1 | 5,017 | 🟢 **-254 (4.82%)** |
253+
| boxedAccountSizedInit1 | 4,972 | 🟢 **-299 (5.67%)** |
254254
| boxedAccountSized1 | 783 | - |
255-
| boxedAccountSizedInit2 | 9,075 | 🟢 **-508 (5.30%)** |
255+
| boxedAccountSizedInit2 | 8,952 | 🟢 **-631 (6.58%)** |
256256
| boxedAccountSized2 | 1,190 | - |
257-
| boxedAccountSizedInit4 | 17,214 | 🟢 **-1,016 (5.57%)** |
257+
| boxedAccountSizedInit4 | 16,967 | 🟢 **-1,263 (6.93%)** |
258258
| boxedAccountSized4 | 1,996 | - |
259-
| boxedAccountSizedInit8 | 33,521 | 🟢 **-2,032 (5.72%)** |
259+
| boxedAccountSizedInit8 | 33,161 | 🟢 **-2,392 (6.73%)** |
260260
| boxedAccountSized8 | 3,628 | - |
261-
| boxedAccountUnsizedInit1 | 5,117 | 🟢 **-254 (4.73%)** |
261+
| boxedAccountUnsizedInit1 | 5,072 | 🟢 **-299 (5.57%)** |
262262
| boxedAccountUnsized1 | 836 | - |
263-
| boxedAccountUnsizedInit2 | 9,251 | 🟢 **-508 (5.21%)** |
263+
| boxedAccountUnsizedInit2 | 9,128 | 🟢 **-631 (6.47%)** |
264264
| boxedAccountUnsized2 | 1,270 | - |
265-
| boxedAccountUnsizedInit4 | 17,542 | 🟢 **-1,016 (5.47%)** |
265+
| boxedAccountUnsizedInit4 | 17,295 | 🟢 **-1,263 (6.81%)** |
266266
| boxedAccountUnsized4 | 2,132 | - |
267-
| boxedAccountUnsizedInit8 | 34,153 | 🟢 **-2,032 (5.62%)** |
267+
| boxedAccountUnsizedInit8 | 33,793 | 🟢 **-2,392 (6.61%)** |
268268
| boxedAccountUnsized8 | 3,881 | - |
269269
| boxedInterfaceAccountMint1 | 1,351 | - |
270270
| boxedInterfaceAccountMint2 | 2,123 | - |

docs/content/docs/features/declare-program.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ pub mod example_cpi {
244244
pub fn initialize_cpi(ctx: Context<InitializeCpi>) -> Result<()> {
245245
// Create CPI context for initialize
246246
let cpi_ctx = CpiContext::new(
247-
ctx.accounts.example_program.to_account_info(),
247+
ctx.accounts.example_program.key(),
248248
Initialize {
249249
payer: ctx.accounts.payer.to_account_info(),
250250
counter: ctx.accounts.counter.to_account_info(),
@@ -260,7 +260,7 @@ pub mod example_cpi {
260260
pub fn increment_cpi(ctx: Context<IncrementCpi>) -> Result<()> {
261261
// Create CPI context for increment
262262
let cpi_ctx = CpiContext::new(
263-
ctx.accounts.example_program.to_account_info(),
263+
ctx.accounts.example_program.key(),
264264
Increment {
265265
counter: ctx.accounts.counter.to_account_info(),
266266
},
@@ -404,7 +404,7 @@ Use the CPI module to invoke the program's instructions:
404404
pub fn initialize_cpi(ctx: Context<InitializeCpi>) -> Result<()> {
405405
// Create CPI context for initialize
406406
let cpi_ctx = CpiContext::new(
407-
ctx.accounts.example_program.to_account_info(),
407+
ctx.accounts.example_program.key(),
408408
Initialize {
409409
payer: ctx.accounts.payer.to_account_info(),
410410
counter: ctx.accounts.counter.to_account_info(),
@@ -423,7 +423,7 @@ pub fn initialize_cpi(ctx: Context<InitializeCpi>) -> Result<()> {
423423
pub fn increment_cpi(ctx: Context<IncrementCpi>) -> Result<()> {
424424
// Create CPI context for increment
425425
let cpi_ctx = CpiContext::new(
426-
ctx.accounts.example_program.to_account_info(),
426+
ctx.accounts.example_program.key(),
427427
Increment {
428428
counter: ctx.accounts.counter.to_account_info(),
429429
},

docs/content/docs/tokens/basics/mint-tokens.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ pub mod token_example {
7171
to: ctx.accounts.token_account.to_account_info(),
7272
authority: ctx.accounts.signer.to_account_info(),
7373
};
74-
let cpi_program = ctx.accounts.token_program.to_account_info();
75-
let cpi_context = CpiContext::new(cpi_program, cpi_accounts);
74+
let cpi_program_id = ctx.accounts.token_program.key();
75+
let cpi_context = CpiContext::new(cpi_program_id, cpi_accounts);
7676
token_interface::mint_to(cpi_context, amount)?;
7777
Ok(())
7878
}
@@ -124,10 +124,10 @@ pub fn mint_tokens(ctx: Context<MintTokens>, amount: u64) -> Result<()> {
124124
};
125125

126126
// The program being invoked in the CPI
127-
let cpi_program = ctx.accounts.token_program.to_account_info();
127+
let cpi_program_id = ctx.accounts.token_program.key();
128128

129129
// Combine the accounts and program into a "CpiContext"
130-
let cpi_context = CpiContext::new(cpi_program, cpi_accounts);
130+
let cpi_context = CpiContext::new(cpi_program_id, cpi_accounts);
131131

132132
// Make CPI to mint_to instruction on the token program
133133
token_interface::mint_to(cpi_context, amount)?;
@@ -172,8 +172,8 @@ pub mod token_example {
172172
to: ctx.accounts.token_account.to_account_info(),
173173
authority: ctx.accounts.mint.to_account_info(),
174174
};
175-
let cpi_program = ctx.accounts.token_program.to_account_info();
176-
let cpi_context = CpiContext::new(cpi_program, cpi_accounts).with_signer(signer_seeds);
175+
let cpi_program_id = ctx.accounts.token_program.key();
176+
let cpi_context = CpiContext::new(cpi_program_id, cpi_accounts).with_signer(signer_seeds);
177177
// [!code highlight]
178178
token_interface::mint_to(cpi_context, amount)?;
179179
Ok(())

examples/tutorial/basic-3/programs/puppet-master/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ declare_id!("HmbTLCmaGvZhKnn1Zfa1JVnp7vkMV4DYVxPLWBVoN65L");
1010
mod puppet_master {
1111
use super::*;
1212
pub fn pull_strings(ctx: Context<PullStrings>, data: u64) -> anchor_lang::Result<()> {
13-
let cpi_program = ctx.accounts.puppet_program.to_account_info();
13+
let cpi_program_id = ctx.accounts.puppet_program.key();
1414
let cpi_accounts = SetData {
1515
puppet: ctx.accounts.puppet.to_account_info(),
1616
};
17-
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
17+
let cpi_ctx = CpiContext::new(cpi_program_id, cpi_accounts);
1818
puppet::cpi::set_data(cpi_ctx, data)
1919
}
2020
}

lang/attribute/program/src/declare_program/mods/cpi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn gen_cpi_instructions(idl: &Idl) -> proc_macro2::TokenStream {
7777

7878
let accounts = ctx.to_account_metas(None);
7979
anchor_lang::solana_program::instruction::Instruction {
80-
program_id: ctx.program.key(),
80+
program_id: ctx.program_id.key(),
8181
accounts,
8282
data,
8383
}

lang/src/context.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ where
127127
/// pub mod caller {
128128
/// use super::*;
129129
/// pub fn do_cpi(ctx: Context<DoCpi>, data: u64) -> Result<()> {
130-
/// let callee_id = ctx.accounts.callee.to_account_info();
130+
/// let callee_id = ctx.accounts.callee.key();
131131
/// let callee_accounts = callee::cpi::accounts::SetData {
132132
/// data_acc: ctx.accounts.data_acc.to_account_info(),
133133
/// authority: ctx.accounts.callee_authority.to_account_info(),
@@ -174,32 +174,33 @@ where
174174
{
175175
pub accounts: T,
176176
pub remaining_accounts: Vec<AccountInfo<'info>>,
177-
pub program: AccountInfo<'info>,
177+
pub program_id: Pubkey,
178178
pub signer_seeds: &'a [&'b [&'c [u8]]],
179179
}
180180

181181
impl<'a, 'b, 'c, 'info, T> CpiContext<'a, 'b, 'c, 'info, T>
182182
where
183183
T: ToAccountMetas + ToAccountInfos<'info>,
184184
{
185-
pub fn new(program: AccountInfo<'info>, accounts: T) -> Self {
185+
#[must_use]
186+
pub fn new(program_id: Pubkey, accounts: T) -> Self {
186187
Self {
187188
accounts,
188-
program,
189+
program_id,
189190
remaining_accounts: Vec::new(),
190191
signer_seeds: &[],
191192
}
192193
}
193194

194195
#[must_use]
195196
pub fn new_with_signer(
196-
program: AccountInfo<'info>,
197+
program_id: Pubkey,
197198
accounts: T,
198199
signer_seeds: &'a [&'b [&'c [u8]]],
199200
) -> Self {
200201
Self {
201202
accounts,
202-
program,
203+
program_id,
203204
signer_seeds,
204205
remaining_accounts: Vec::new(),
205206
}
@@ -224,7 +225,6 @@ impl<'info, T: ToAccountInfos<'info> + ToAccountMetas> ToAccountInfos<'info>
224225
fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
225226
let mut infos = self.accounts.to_account_infos();
226227
infos.extend_from_slice(&self.remaining_accounts);
227-
infos.push(self.program.clone());
228228
infos
229229
}
230230
}

0 commit comments

Comments
 (0)