Skip to content

Commit 6b12c7a

Browse files
committed
refactor: light-sdks detach account metas from account infos
1 parent 0a12148 commit 6b12c7a

17 files changed

Lines changed: 225 additions & 69 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/anchor/token-escrow/src/escrow_with_compressed_pda/escrow.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ fn cpi_compressed_pda_transfer<'info>(
133133
.clone(),
134134
];
135135
system_accounts.extend_from_slice(ctx.remaining_accounts);
136-
let light_accounts = CpiAccounts::new_with_config(
136+
let light_accounts = CpiAccounts::try_new_with_config(
137137
ctx.accounts.signer.as_ref(),
138138
&system_accounts,
139139
CpiAccountsConfig::new_with_cpi_context(crate::LIGHT_CPI_SIGNER),
140-
);
140+
)
141+
.unwrap();
141142

142143
verify_borsh(light_accounts, &inputs_struct).map_err(ProgramError::from)?;
143144

examples/anchor/token-escrow/src/escrow_with_compressed_pda/withdrawal.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,12 @@ fn cpi_compressed_pda_withdrawal<'info>(
157157
.clone(),
158158
];
159159
system_accounts.extend_from_slice(ctx.remaining_accounts);
160-
let light_accounts = CpiAccounts::new_with_config(
160+
let light_accounts = CpiAccounts::try_new_with_config(
161161
ctx.accounts.signer.as_ref(),
162162
&system_accounts,
163163
CpiAccountsConfig::new_with_cpi_context(crate::LIGHT_CPI_SIGNER),
164-
);
164+
)
165+
.unwrap();
165166
verify_borsh(light_accounts, &inputs_struct).unwrap();
166167

167168
Ok(())

program-libs/account-checks/src/account_info/account_info_trait.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
use core::ops::{Deref, DerefMut};
1+
use core::{
2+
fmt::Debug,
3+
ops::{Deref, DerefMut},
4+
};
25

36
use crate::error::AccountError;
47

58
/// Trait to abstract over different AccountInfo implementations (pinocchio vs solana)
69
pub trait AccountInfoTrait {
7-
type Pubkey: Copy + Clone;
10+
type Pubkey: Copy + Clone + Debug;
811
type DataRef<'a>: Deref<Target = [u8]>
912
where
1013
Self: 'a;

program-tests/create-address-test-program/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use light_compressed_account::instruction_data::{
1717
use light_sdk::{
1818
constants::LIGHT_SYSTEM_PROGRAM_ID,
1919
cpi::{
20-
invoke_light_system_program, to_account_metas, to_account_metas_small, CpiAccountsConfig,
20+
get_account_metas_from_config, invoke_light_system_program, to_account_metas_small,
21+
CpiAccountsConfig, CpiInstructionConfig,
2122
},
2223
};
2324

@@ -77,15 +78,17 @@ pub mod system_cpi_test {
7778
} else {
7879
use light_sdk::cpi::CpiAccounts;
7980
let cpi_accounts =
80-
CpiAccounts::new_with_config(&fee_payer, ctx.remaining_accounts, config);
81+
CpiAccounts::try_new_with_config(&fee_payer, ctx.remaining_accounts, config)
82+
.unwrap();
8183
let account_infos = cpi_accounts
8284
.to_account_infos()
8385
.into_iter()
8486
.cloned()
8587
.collect::<Vec<_>>();
8688

87-
let account_metas =
88-
to_account_metas(cpi_accounts).map_err(|_| ErrorCode::AccountNotEnoughKeys)?;
89+
let config = CpiInstructionConfig::try_from(&cpi_accounts)
90+
.map_err(|_| ErrorCode::AccountNotEnoughKeys)?;
91+
let account_metas = get_account_metas_from_config(config);
8992
(account_infos, account_metas)
9093
};
9194
let instruction = Instruction {

program-tests/sdk-pinocchio-test/src/create_pda.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ pub fn create_pda<const BATCHED: bool>(
2121
let instruction_data = CreatePdaInstructionData::deserialize(&mut instruction_data)
2222
.map_err(|_| LightSdkError::Borsh)?;
2323
let config = CpiAccountsConfig::new(crate::LIGHT_CPI_SIGNER);
24-
let cpi_accounts = CpiAccounts::new_with_config(
24+
let cpi_accounts = CpiAccounts::try_new_with_config(
2525
&accounts[0],
2626
&accounts[instruction_data.system_accounts_offset as usize..],
2727
config,
28-
);
28+
)?;
2929

3030
let address_tree_info = instruction_data.address_tree_info;
3131
let (address, address_seed) = if BATCHED {

program-tests/sdk-pinocchio-test/src/update_pda.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ pub fn update_pda<const BATCHED: bool>(
3737

3838
let config = CpiAccountsConfig::new(crate::LIGHT_CPI_SIGNER);
3939
sol_log_compute_units();
40-
let cpi_accounts = CpiAccounts::new_with_config(
40+
let cpi_accounts = CpiAccounts::try_new_with_config(
4141
&accounts[0],
4242
&accounts[instruction_data.system_accounts_offset as usize..],
4343
config,
44-
);
44+
)?;
4545
sol_log_compute_units();
4646
let cpi_inputs = CpiInputs::new(
4747
instruction_data.proof,

program-tests/sdk-test/src/create_pda.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ pub fn create_pda<const BATCHED: bool>(
2121
let instruction_data = CreatePdaInstructionData::deserialize(&mut instruction_data)
2222
.map_err(|_| LightSdkError::Borsh)?;
2323
let config = CpiAccountsConfig::new(crate::LIGHT_CPI_SIGNER);
24-
let cpi_accounts = CpiAccounts::new_with_config(
24+
let cpi_accounts = CpiAccounts::try_new_with_config(
2525
&accounts[0],
2626
&accounts[instruction_data.system_accounts_offset as usize..],
2727
config,
28-
);
28+
)
29+
.unwrap();
2930

3031
let address_tree_info = instruction_data.address_tree_info;
3132
let (address, address_seed) = if BATCHED {

program-tests/sdk-test/src/update_pda.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ pub fn update_pda<const BATCHED: bool>(
3535

3636
let config = CpiAccountsConfig::new(crate::LIGHT_CPI_SIGNER);
3737
sol_log_compute_units();
38-
let cpi_accounts = CpiAccounts::new_with_config(
38+
let cpi_accounts = CpiAccounts::try_new_with_config(
3939
&accounts[0],
4040
&accounts[instruction_data.system_accounts_offset as usize..],
4141
config,
42-
);
42+
)?;
4343
sol_log_compute_units();
4444
let cpi_inputs = CpiInputs::new(
4545
instruction_data.proof,

sdk-libs/sdk-pinocchio/src/error.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use light_account_checks::error::AccountError;
12
use light_hasher::HasherError;
23
pub use light_sdk_types::error::LightSdkTypesError;
34
use light_zero_copy::errors::ZeroCopyError;
@@ -68,12 +69,20 @@ pub enum LightSdkError {
6869
MetaCloseInputIsNone,
6970
#[error("CPI accounts index out of bounds: {0}")]
7071
CpiAccountsIndexOutOfBounds(usize),
72+
#[error("Invalid CPI context account")]
73+
InvalidCpiContextAccount,
74+
#[error("Invalid sol pool pda account")]
75+
InvalidSolPoolPdaAccount,
76+
#[error("CpigAccounts accounts slice starts with an invalid account. It should start with LightSystemProgram SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7.")]
77+
InvalidCpiAccountsOffset,
7178
#[error(transparent)]
7279
Hasher(#[from] HasherError),
7380
#[error(transparent)]
7481
ZeroCopy(#[from] ZeroCopyError),
7582
#[error("Program error: {0:?}")]
7683
ProgramError(ProgramError),
84+
#[error(transparent)]
85+
AccountError(#[from] AccountError),
7786
}
7887

7988
impl From<ProgramError> for LightSdkError {
@@ -111,6 +120,10 @@ impl From<LightSdkTypesError> for LightSdkError {
111120
LightSdkTypesError::CpiAccountsIndexOutOfBounds(index) => {
112121
LightSdkError::CpiAccountsIndexOutOfBounds(index)
113122
}
123+
LightSdkTypesError::InvalidCpiContextAccount => LightSdkError::InvalidCpiContextAccount,
124+
LightSdkTypesError::InvalidSolPoolPdaAccount => LightSdkError::InvalidSolPoolPdaAccount,
125+
LightSdkTypesError::AccountError(e) => LightSdkError::AccountError(e),
126+
LightSdkTypesError::InvalidCpiAccountsOffset => LightSdkError::InvalidCpiAccountsOffset,
114127
}
115128
}
116129
}
@@ -148,9 +161,13 @@ impl From<LightSdkError> for u32 {
148161
LightSdkError::MetaCloseAddressIsNone => 16028,
149162
LightSdkError::MetaCloseInputIsNone => 16029,
150163
LightSdkError::CpiAccountsIndexOutOfBounds(_) => 16031,
164+
LightSdkError::InvalidCpiContextAccount => 16032,
165+
LightSdkError::InvalidSolPoolPdaAccount => 16033,
166+
LightSdkError::InvalidCpiAccountsOffset => 16034,
151167
LightSdkError::Hasher(e) => e.into(),
152168
LightSdkError::ZeroCopy(e) => e.into(),
153169
LightSdkError::ProgramError(e) => u64::from(e) as u32,
170+
LightSdkError::AccountError(e) => e.into(),
154171
}
155172
}
156173
}

0 commit comments

Comments
 (0)