Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit ddbb3d8

Browse files
[spl-record] Remove Data type from RecordData type (#6062)
1 parent e4fdb74 commit ddbb3d8

File tree

4 files changed

+88
-121
lines changed

4 files changed

+88
-121
lines changed

record/program/src/instruction.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ pub fn close_account(record_account: &Pubkey, signer: &Pubkey, receiver: &Pubkey
162162

163163
#[cfg(test)]
164164
mod tests {
165-
use {
166-
super::*, crate::state::tests::TEST_DATA, solana_program::program_error::ProgramError,
167-
spl_pod::bytemuck::pod_bytes_of,
168-
};
165+
use {super::*, crate::state::tests::TEST_BYTES, solana_program::program_error::ProgramError};
169166

170167
#[test]
171168
fn serialize_initialize() {
@@ -177,7 +174,7 @@ mod tests {
177174

178175
#[test]
179176
fn serialize_write() {
180-
let data = pod_bytes_of(&TEST_DATA);
177+
let data = &TEST_BYTES;
181178
let offset = 0u64;
182179
let instruction = RecordInstruction::Write { offset: 0, data };
183180
let mut expected = vec![1];
@@ -207,7 +204,7 @@ mod tests {
207204
#[test]
208205
fn deserialize_invalid_instruction() {
209206
let mut expected = vec![12];
210-
expected.append(&mut pod_bytes_of(&TEST_DATA).to_vec());
207+
expected.extend_from_slice(&TEST_BYTES);
211208
let err: ProgramError = RecordInstruction::unpack(&expected).unwrap_err();
212209
assert_eq!(err, ProgramError::InvalidInstructionData);
213210
}

record/program/src/processor.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
//! Program state processor
22
33
use {
4-
crate::{
5-
error::RecordError,
6-
instruction::RecordInstruction,
7-
state::{Data, RecordData},
8-
},
4+
crate::{error::RecordError, instruction::RecordInstruction, state::RecordData},
95
solana_program::{
106
account_info::{next_account_info, AccountInfo},
117
entrypoint::ProgramResult,
@@ -46,7 +42,13 @@ pub fn process_instruction(
4642
let authority_info = next_account_info(account_info_iter)?;
4743

4844
let raw_data = &mut data_info.data.borrow_mut();
49-
let account_data = pod_from_bytes_mut::<RecordData>(raw_data)?;
45+
if raw_data.len() < RecordData::WRITABLE_START_INDEX {
46+
return Err(ProgramError::InvalidAccountData);
47+
}
48+
49+
let account_data = pod_from_bytes_mut::<RecordData>(
50+
&mut raw_data[..RecordData::WRITABLE_START_INDEX],
51+
)?;
5052
if account_data.is_initialized() {
5153
msg!("Record account already initialized");
5254
return Err(ProgramError::AccountAlreadyInitialized);
@@ -63,7 +65,11 @@ pub fn process_instruction(
6365
let authority_info = next_account_info(account_info_iter)?;
6466
{
6567
let raw_data = &data_info.data.borrow();
66-
let account_data = pod_from_bytes::<RecordData>(raw_data)?;
68+
if raw_data.len() < RecordData::WRITABLE_START_INDEX {
69+
return Err(ProgramError::InvalidAccountData);
70+
}
71+
let account_data =
72+
pod_from_bytes::<RecordData>(&raw_data[..RecordData::WRITABLE_START_INDEX])?;
6773
if !account_data.is_initialized() {
6874
msg!("Record account not initialized");
6975
return Err(ProgramError::UninitializedAccount);
@@ -86,7 +92,12 @@ pub fn process_instruction(
8692
let authority_info = next_account_info(account_info_iter)?;
8793
let new_authority_info = next_account_info(account_info_iter)?;
8894
let raw_data = &mut data_info.data.borrow_mut();
89-
let account_data = pod_from_bytes_mut::<RecordData>(raw_data)?;
95+
if raw_data.len() < RecordData::WRITABLE_START_INDEX {
96+
return Err(ProgramError::InvalidAccountData);
97+
}
98+
let account_data = pod_from_bytes_mut::<RecordData>(
99+
&mut raw_data[..RecordData::WRITABLE_START_INDEX],
100+
)?;
90101
if !account_data.is_initialized() {
91102
msg!("Record account not initialized");
92103
return Err(ProgramError::UninitializedAccount);
@@ -102,7 +113,12 @@ pub fn process_instruction(
102113
let authority_info = next_account_info(account_info_iter)?;
103114
let destination_info = next_account_info(account_info_iter)?;
104115
let raw_data = &mut data_info.data.borrow_mut();
105-
let account_data = pod_from_bytes_mut::<RecordData>(raw_data)?;
116+
if raw_data.len() < RecordData::WRITABLE_START_INDEX {
117+
return Err(ProgramError::InvalidAccountData);
118+
}
119+
let account_data = pod_from_bytes_mut::<RecordData>(
120+
&mut raw_data[..RecordData::WRITABLE_START_INDEX],
121+
)?;
106122
if !account_data.is_initialized() {
107123
msg!("Record not initialized");
108124
return Err(ProgramError::UninitializedAccount);
@@ -114,7 +130,6 @@ pub fn process_instruction(
114130
**destination_info.lamports.borrow_mut() = destination_starting_lamports
115131
.checked_add(data_lamports)
116132
.ok_or(RecordError::Overflow)?;
117-
account_data.data = Data::default();
118133
Ok(())
119134
}
120135
}

record/program/src/state.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use {
44
solana_program::{program_pack::IsInitialized, pubkey::Pubkey},
55
};
66

7-
/// Struct wrapping data and providing metadata
7+
/// Header type for recorded account data
88
#[repr(C)]
99
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
1010
pub struct RecordData {
@@ -13,25 +13,6 @@ pub struct RecordData {
1313

1414
/// The account allowed to update the data
1515
pub authority: Pubkey,
16-
17-
/// The data contained by the account, could be anything serializable
18-
pub data: Data,
19-
}
20-
21-
/// The length of the data contained in the account for testing.
22-
const DATA_SIZE: usize = 8;
23-
24-
/// Struct just for data
25-
#[repr(C)]
26-
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
27-
pub struct Data {
28-
/// The data contained by the account, could be anything or serializable
29-
pub bytes: [u8; DATA_SIZE],
30-
}
31-
32-
impl Data {
33-
/// very small data for easy testing
34-
pub const DATA_SIZE: usize = 8;
3516
}
3617

3718
impl RecordData {
@@ -62,21 +43,17 @@ pub mod tests {
6243
/// Pubkey for tests
6344
pub const TEST_PUBKEY: Pubkey = Pubkey::new_from_array([100; 32]);
6445
/// Bytes for tests
65-
pub const TEST_BYTES: [u8; Data::DATA_SIZE] = [42; Data::DATA_SIZE];
66-
/// Data for tests
67-
pub const TEST_DATA: Data = Data { bytes: TEST_BYTES };
46+
pub const TEST_BYTES: [u8; 8] = [42; 8];
6847
/// RecordData for tests
6948
pub const TEST_RECORD_DATA: RecordData = RecordData {
7049
version: TEST_VERSION,
7150
authority: TEST_PUBKEY,
72-
data: TEST_DATA,
7351
};
7452

7553
#[test]
7654
fn serialize_data() {
7755
let mut expected = vec![TEST_VERSION];
7856
expected.extend_from_slice(&TEST_PUBKEY.to_bytes());
79-
expected.extend_from_slice(&TEST_DATA.bytes);
8057
assert_eq!(pod_bytes_of(&TEST_RECORD_DATA), expected);
8158
assert_eq!(
8259
*pod_from_bytes::<RecordData>(&expected).unwrap(),
@@ -86,10 +63,9 @@ pub mod tests {
8663

8764
#[test]
8865
fn deserialize_invalid_slice() {
89-
let data = [200; Data::DATA_SIZE - 1];
9066
let mut expected = vec![TEST_VERSION];
9167
expected.extend_from_slice(&TEST_PUBKEY.to_bytes());
92-
expected.extend_from_slice(&data);
68+
expected.extend_from_slice(&TEST_BYTES);
9369
let err: ProgramError = pod_from_bytes::<RecordData>(&expected).unwrap_err();
9470
assert_eq!(err, ProgramError::InvalidArgument);
9571
}

0 commit comments

Comments
 (0)