Skip to content

Commit 368f9f0

Browse files
committed
refactor: use account_info_init instead of manually setting the discriminator
1 parent 6fb4811 commit 368f9f0

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

programs/system/src/accounts/init_context_account.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use borsh::BorshSerialize;
22
use light_account_checks::{
3-
checks::{check_data_is_zeroed, check_owner, check_signer},
3+
checks::{account_info_init, check_owner, check_signer},
44
discriminator::Discriminator,
55
};
66
use light_batched_merkle_tree::merkle_tree::BatchedMerkleTreeAccount;
@@ -9,10 +9,7 @@ use light_compressed_account::constants::{
99
};
1010
use pinocchio::{account_info::AccountInfo, program_error::ProgramError};
1111

12-
use crate::{
13-
errors::SystemProgramError, invoke_cpi::account::CpiContextAccount, Result,
14-
CPI_CONTEXT_ACCOUNT_DISCRIMINATOR,
15-
};
12+
use crate::{errors::SystemProgramError, invoke_cpi::account::CpiContextAccount, Result};
1613
pub struct InitializeCpiContextAccount<'info> {
1714
pub fee_payer: &'info AccountInfo,
1815
pub cpi_context_account: &'info AccountInfo,
@@ -55,12 +52,11 @@ pub fn init_cpi_context_account(accounts: &[AccountInfo]) -> Result<()> {
5552
// Check that Merkle tree is initialized.
5653
let ctx = InitializeCpiContextAccount::from_account_infos(accounts)?;
5754

58-
let mut cpi_context_account_data = ctx.cpi_context_account.try_borrow_mut_data()?;
59-
// Check account is not initialized.
60-
check_data_is_zeroed::<8>(&cpi_context_account_data[..]).map_err(ProgramError::from)?;
61-
// Initialize account with discriminator.
62-
cpi_context_account_data[..8].copy_from_slice(&CPI_CONTEXT_ACCOUNT_DISCRIMINATOR);
55+
// 1. Check discriminator bytes are zeroed.
56+
// 2. Set discriminator.
57+
account_info_init::<CpiContextAccount>(ctx.cpi_context_account)?;
6358

59+
let mut cpi_context_account_data = ctx.cpi_context_account.try_borrow_mut_data()?;
6460
let cpi_context_account = CpiContextAccount {
6561
associated_merkle_tree: *ctx.associated_merkle_tree.key(),
6662
..Default::default()

programs/system/src/invoke_cpi/account.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use light_compressed_account::instruction_data::{
1313
use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError, slice::ZeroCopySliceBorsh};
1414
use pinocchio::{account_info::AccountInfo, pubkey::Pubkey};
1515
use zerocopy::{little_endian::U32, Ref};
16+
17+
use crate::CPI_CONTEXT_ACCOUNT_DISCRIMINATOR;
18+
1619
/// Collects instruction data without executing a compressed transaction.
1720
/// Signer checks are performed on instruction data.
1821
/// Collected instruction data is combined with the instruction data of the executing cpi,
@@ -30,7 +33,7 @@ pub struct CpiContextAccount {
3033
}
3134

3235
impl Discriminator for CpiContextAccount {
33-
const DISCRIMINATOR: [u8; 8] = [22, 20, 149, 218, 74, 204, 128, 166];
36+
const DISCRIMINATOR: [u8; 8] = CPI_CONTEXT_ACCOUNT_DISCRIMINATOR;
3437
const DISCRIMINATOR_SLICE: &'static [u8] = &Self::DISCRIMINATOR;
3538
}
3639

0 commit comments

Comments
 (0)