Skip to content

Commit 2a4c73c

Browse files
add remove_data
1 parent 800792d commit 2a4c73c

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

sdk-libs/sdk/src/account.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub struct LightAccount<
8989
owner: &'a Pubkey,
9090
pub account: A,
9191
account_info: CompressedAccountInfo,
92+
empty_data: bool,
9293
}
9394

9495
impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHasher + Default>
@@ -112,6 +113,7 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe
112113
input: None,
113114
output: Some(output_account_info),
114115
},
116+
empty_data: false,
115117
}
116118
}
117119

@@ -156,6 +158,7 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe
156158
input: Some(input_account_info),
157159
output: Some(output_account_info),
158160
},
161+
empty_data: false,
159162
})
160163
}
161164

@@ -188,6 +191,7 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe
188191
input: Some(input_account_info),
189192
output: None,
190193
},
194+
empty_data: false,
191195
})
192196
}
193197

@@ -260,7 +264,8 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe
260264
/// Remove the data from this account by setting it to default.
261265
/// This is used when decompressing to ensure the compressed account is properly zeroed.
262266
pub fn remove_data(&mut self) {
263-
self.account = A::default();
267+
self.account = A::default(); // TODO: remove
268+
self.empty_data = true;
264269
}
265270

266271
/// 1. Serializes the account data and sets the output data hash.
@@ -270,11 +275,17 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe
270275
/// that should only be called once per instruction.
271276
pub fn to_account_info(mut self) -> Result<CompressedAccountInfo, LightSdkError> {
272277
if let Some(output) = self.account_info.output.as_mut() {
273-
output.data_hash = self.account.hash::<Poseidon>()?;
274-
output.data = self
275-
.account
276-
.try_to_vec()
277-
.map_err(|_| LightSdkError::Borsh)?;
278+
if self.empty_data {
279+
// TODO: check if this is right
280+
output.data_hash = [0; 32];
281+
output.data = Vec::new();
282+
} else {
283+
output.data_hash = self.account.hash::<Poseidon>()?;
284+
output.data = self
285+
.account
286+
.try_to_vec()
287+
.map_err(|_| LightSdkError::Borsh)?;
288+
}
278289
}
279290
Ok(self.account_info)
280291
}

sdk-libs/sdk/src/compressible/decompress_idempotent.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,8 @@ where
184184
owner_program,
185185
);
186186

187-
// Add bump to seeds for signing
187+
// cpi for each pda
188188
let bump_seed = [bump];
189-
190-
// Use ArrayVec to avoid heap allocation - Solana supports max 16 seeds
191189
let mut signer_seeds = ArrayVec::<&[u8], 16>::new();
192190
for seed in seeds.iter() {
193191
signer_seeds.push(*seed);
@@ -211,8 +209,7 @@ where
211209
// Write discriminator
212210
// TODO: we don't mind the onchain account being different?
213211
// TODO: consider passing onchain account discriminator? (can be auto-derived)
214-
let discriminator = A::LIGHT_DISCRIMINATOR;
215-
pda_account.try_borrow_mut_data()?[..8].copy_from_slice(&discriminator);
212+
pda_account.try_borrow_mut_data()?[..8].copy_from_slice(&A::LIGHT_DISCRIMINATOR);
216213

217214
// Write data to PDA
218215
decompressed_pda
@@ -226,7 +223,7 @@ where
226223
compressed_accounts_for_cpi.push(compressed_account.to_account_info()?);
227224
}
228225

229-
// Make single CPI call with all compressed accounts
226+
// apply compressed account changes via cpi
230227
if !compressed_accounts_for_cpi.is_empty() {
231228
let cpi_inputs = CpiInputs::new(proof, compressed_accounts_for_cpi);
232229
cpi_inputs.invoke_light_system_program(cpi_accounts)?;

0 commit comments

Comments
 (0)