Skip to content

Commit eba2f75

Browse files
user.emailuser.email
authored andcommitted
fix(lang): sync CpiContext lifetimes, InterfaceAccount::reload, and lockfile
Made-with: Cursor
1 parent dd5f7af commit eba2f75

File tree

5 files changed

+22
-60
lines changed

5 files changed

+22
-60
lines changed

Cargo.lock

Lines changed: 5 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lang/src/accounts/interface_account.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Account container that checks ownership on deserialization.
22
33
use crate::accounts::account::Account;
4-
use crate::error::{Error, ErrorCode};
4+
use crate::error::ErrorCode;
55
use crate::pinocchio_runtime::account_info::AccountInfo;
66
use crate::pinocchio_runtime::instruction::AccountMeta;
77
use crate::pinocchio_runtime::pubkey::Pubkey;
@@ -176,39 +176,13 @@ impl<T: AccountSerialize + AccountDeserialize + Clone + fmt::Debug> fmt::Debug
176176

177177
impl<T: AccountSerialize + AccountDeserialize + Clone> InterfaceAccount<T> {
178178
fn new(info: AccountInfo, account: T) -> Self {
179-
let owner = unsafe { *info.owner() };
179+
let owner = *info.owner();
180180
Self {
181181
account: Account::new(info, account),
182182
owner,
183183
}
184184
}
185185

186-
/// Reloads the account from storage. This is useful, for example, when
187-
/// observing side effects after CPI.
188-
///
189-
/// No Anchor discriminator is checked during reload. Instead, this method enforces
190-
/// owner stability by verifying that `info.owner == self.owner` (i.e., the pubkey
191-
/// validated at construction) to avoid TOCTOU issues, and then re-deserializes `T`
192-
/// from the updated bytes.
193-
///
194-
/// If you need discriminator validation on reload, use `Account<T>` with an Anchor
195-
/// #[account] type.
196-
pub fn reload(&mut self) -> Result<()> {
197-
let info = self.account.to_account_info();
198-
199-
// Enforce owner stability: must match the one validated at construction.
200-
if info.owned_by(&self.owner) {
201-
return Err(Error::from(ErrorCode::AccountOwnedByWrongProgram)
202-
.with_pubkeys((unsafe { *info.owner() }, self.owner)));
203-
}
204-
205-
// Re-deserialize fresh data into the inner account.
206-
let mut data: &[u8] = &info.try_borrow()?;
207-
let new_val = T::try_deserialize_unchecked(&mut data)?;
208-
self.account.set_inner(new_val);
209-
Ok(())
210-
}
211-
212186
pub fn into_inner(self) -> T {
213187
self.account.into_inner()
214188
}
@@ -259,7 +233,7 @@ impl<T: AccountSerialize + AccountDeserialize + CheckOwner + Clone> InterfaceAcc
259233
if info.owned_by(&system_program::ID) && info.lamports() == 0 {
260234
return Err(ErrorCode::AccountNotInitialized.into());
261235
}
262-
T::check_owner(&unsafe { *info.owner() })?;
236+
T::check_owner(info.owner())?;
263237
let mut data: &[u8] = &info.try_borrow()?;
264238
Ok(Self::new(info, T::try_deserialize_unchecked(&mut data)?))
265239
}

lang/src/system_program.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct AdvanceNonceAccount {
2020
pub authorized: AccountInfo,
2121
pub recent_blockhashes: AccountInfo,
2222
}
23-
pub fn advance_nonce_account(ctx: CpiContext<'_, '_, 'static, AdvanceNonceAccount>) -> Result<()> {
23+
pub fn advance_nonce_account(ctx: CpiContext<'_, '_, AdvanceNonceAccount>) -> Result<()> {
2424
let instruction = system_instruction::AdvanceNonceAccount {
2525
account: &ctx.accounts.nonce,
2626
recent_blockhashes_sysvar: &ctx.accounts.recent_blockhashes,
@@ -37,7 +37,7 @@ pub struct Allocate {
3737
pub account_to_allocate: AccountInfo,
3838
}
3939

40-
pub fn allocate(ctx: CpiContext<'_, '_, 'static, Allocate>, space: u64) -> Result<()> {
40+
pub fn allocate(ctx: CpiContext<'_, '_, Allocate>, space: u64) -> Result<()> {
4141
let instruction = system_instruction::Allocate {
4242
account: &ctx.accounts.account_to_allocate,
4343
space,
@@ -54,7 +54,7 @@ pub struct AllocateWithSeed {
5454
}
5555

5656
pub fn allocate_with_seed(
57-
ctx: CpiContext<'_, '_, 'static, AllocateWithSeed>,
57+
ctx: CpiContext<'_, '_, AllocateWithSeed>,
5858
seed: &str,
5959
space: u64,
6060
owner: &Pubkey,
@@ -76,7 +76,7 @@ pub struct Assign {
7676
pub account_to_assign: AccountInfo,
7777
}
7878

79-
pub fn assign(ctx: CpiContext<'_, '_, 'static, Assign>, owner: &Pubkey) -> Result<()> {
79+
pub fn assign(ctx: CpiContext<'_, '_, Assign>, owner: &Pubkey) -> Result<()> {
8080
// Build instruction accounts
8181
let instruction = system_instruction::Assign {
8282
account: &ctx.accounts.account_to_assign,
@@ -95,7 +95,7 @@ pub struct AssignWithSeed {
9595
}
9696

9797
pub fn assign_with_seed(
98-
ctx: CpiContext<'_, '_, 'static, AssignWithSeed>,
98+
ctx: CpiContext<'_, '_, AssignWithSeed>,
9999
seed: &str,
100100
owner: &Pubkey,
101101
) -> Result<()> {
@@ -117,7 +117,7 @@ pub struct AuthorizeNonceAccount {
117117
}
118118

119119
pub fn authorize_nonce_account(
120-
ctx: CpiContext<'_, '_, 'static, AuthorizeNonceAccount>,
120+
ctx: CpiContext<'_, '_, AuthorizeNonceAccount>,
121121
new_authority: &Pubkey,
122122
) -> Result<()> {
123123
let instruction = system_instruction::AuthorizeNonceAccount {
@@ -138,7 +138,7 @@ pub struct CreateAccount {
138138
}
139139

140140
pub fn create_account(
141-
ctx: CpiContext<'_, '_, 'static, CreateAccount>,
141+
ctx: CpiContext<'_, '_, CreateAccount>,
142142
lamports: u64,
143143
space: u64,
144144
owner: &Pubkey,
@@ -163,7 +163,7 @@ pub struct CreateAccountWithSeed {
163163
}
164164

165165
pub fn create_account_with_seed(
166-
ctx: CpiContext<'_, '_, 'static, CreateAccountWithSeed>,
166+
ctx: CpiContext<'_, '_, CreateAccountWithSeed>,
167167
seed: &str,
168168
lamports: u64,
169169
space: u64,
@@ -246,7 +246,7 @@ pub struct InitializeNonceAccount {
246246
}
247247

248248
pub fn initialize_nonce_account(
249-
ctx: CpiContext<'_, '_, 'static, InitializeNonceAccount>,
249+
ctx: CpiContext<'_, '_, InitializeNonceAccount>,
250250
authority: &Pubkey,
251251
) -> Result<()> {
252252
let instruction = system_instruction::InitializeNonceAccount {
@@ -264,7 +264,7 @@ pub struct Transfer {
264264
pub to: AccountInfo,
265265
}
266266

267-
pub fn transfer(ctx: CpiContext<'_, '_, 'static, Transfer>, lamports: u64) -> Result<()> {
267+
pub fn transfer(ctx: CpiContext<'_, '_, Transfer>, lamports: u64) -> Result<()> {
268268
let instruction = system_instruction::Transfer {
269269
from: &ctx.accounts.from,
270270
to: &ctx.accounts.to,
@@ -283,7 +283,7 @@ pub struct TransferWithSeed {
283283
}
284284

285285
pub fn transfer_with_seed(
286-
ctx: CpiContext<'_, '_, 'static, TransferWithSeed>,
286+
ctx: CpiContext<'_, '_, TransferWithSeed>,
287287
seed: &str,
288288
owner: &Pubkey,
289289
lamports: u64,
@@ -312,7 +312,7 @@ pub struct WithdrawNonceAccount {
312312
}
313313

314314
pub fn withdraw_nonce_account(
315-
ctx: CpiContext<'_, '_, 'static, WithdrawNonceAccount>,
315+
ctx: CpiContext<'_, '_, WithdrawNonceAccount>,
316316
lamports: u64,
317317
) -> Result<()> {
318318
let instruction = system_instruction::WithdrawNonceAccount {

lang/tests/account_reload.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#[path = "support/mod.rs"]
22
mod support;
33

4-
use std::cell::RefMut;
5-
64
use anchor_lang::prelude::*;
5+
use anchor_lang::RefMut;
76

87
use support::OwnedPinocchioAccount;
98

lang/tests/pinocchio_stack_smoke.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn cpi_context_create_account_triple_lifetime() {
6363
from: from_o.info,
6464
to: to_o.info,
6565
};
66-
let _ctx: CpiContext<'_, '_, 'static, CreateAccount> =
66+
let _ctx: CpiContext<'_, '_, CreateAccount> =
6767
CpiContext::new(system_program::ID, accs);
6868
}
6969

0 commit comments

Comments
 (0)