Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

pod: Add in-place stake history deserialization #6106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libraries/pod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ solana-zk-token-sdk = "1.17.6"
spl-program-error = { version = "0.3", path = "../program-error" }

[dev-dependencies]
bincode = "1"
serde_json = "1.0.111"

[lib]
Expand Down
5 changes: 5 additions & 0 deletions libraries/pod/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ macro_rules! impl_int_conversion {
Self::from_le_bytes(pod.0)
}
}
impl Into<usize> for $P {
fn into(self) -> usize {
usize::try_from(<$I>::from_le_bytes(self.0)).unwrap()
}
}
};
}

Expand Down
87 changes: 74 additions & 13 deletions libraries/pod/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,37 @@ use {

const LENGTH_SIZE: usize = std::mem::size_of::<PodU32>();
/// Special type for using a slice of `Pod`s in a zero-copy way
pub struct PodSlice<'data, T: Pod> {
length: &'data PodU32,
pub struct PodSlice<'data, T: Pod, S: Pod + Into<usize>> {
length: &'data S,
data: &'data [T],
}
impl<'data, T: Pod> PodSlice<'data, T> {
impl<'data, T: Pod, S: Pod + Into<usize>> PodSlice<'data, T, S> {
/// Unpack the buffer into a slice
pub fn unpack<'a>(data: &'a [u8]) -> Result<Self, ProgramError>
where
'a: 'data,
{
if data.len() < LENGTH_SIZE {
if data.len() < std::mem::size_of::<S>() {
return Err(PodSliceError::BufferTooSmall.into());
}
let (length, data) = data.split_at(LENGTH_SIZE);
let length = pod_from_bytes::<PodU32>(length)?;
let (length, data) = data.split_at(std::mem::size_of::<S>());
let length = pod_from_bytes::<S>(length)?;
let _max_length = max_len_for_type::<T>(data.len())?;
let data = pod_slice_from_bytes(data)?;
Ok(Self { length, data })
}

/// Get the slice data
pub fn data(&self) -> &[T] {
let length = u32::from(*self.length) as usize;
let length = (*self.length).into();
&self.data[..length]
}

/// Get the amount of bytes used by `num_items`
pub fn size_of(num_items: usize) -> Result<usize, ProgramError> {
std::mem::size_of::<T>()
.checked_mul(num_items)
.and_then(|len| len.checked_add(LENGTH_SIZE))
.and_then(|len| len.checked_add(std::mem::size_of::<S>()))
.ok_or_else(|| PodSliceError::CalculationFailure.into())
}
}
Expand Down Expand Up @@ -129,7 +129,14 @@ fn max_len_for_type<T>(data_len: usize) -> Result<usize, ProgramError> {

#[cfg(test)]
mod tests {
use {super::*, crate::bytemuck::pod_slice_to_bytes, bytemuck::Zeroable};
use {
super::*,
crate::{bytemuck::pod_slice_to_bytes, primitives::PodU64},
bincode::serialize,
bytemuck::Zeroable,
solana_program::stake_history::{StakeHistory, StakeHistoryEntry, MAX_ENTRIES},
std::ops::Deref,
};

#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
Expand All @@ -155,22 +162,22 @@ mod tests {
pod_slice_bytes[0..4].copy_from_slice(&len_bytes);
pod_slice_bytes[4..70].copy_from_slice(&data_bytes);

let pod_slice = PodSlice::<TestStruct>::unpack(&pod_slice_bytes).unwrap();
let pod_slice = PodSlice::<TestStruct, PodU32>::unpack(&pod_slice_bytes).unwrap();
let pod_slice_data = pod_slice.data();

assert_eq!(*pod_slice.length, PodU32::from(2));
assert_eq!(pod_slice_to_bytes(pod_slice.data()), data_bytes);
assert_eq!(pod_slice_data[0].test_field, test_field_bytes[0]);
assert_eq!(pod_slice_data[0].test_pubkey, test_pubkey_bytes);
assert_eq!(PodSlice::<TestStruct>::size_of(1).unwrap(), 37);
assert_eq!(PodSlice::<TestStruct, PodU32>::size_of(1).unwrap(), 37);
}

#[test]
fn test_pod_slice_buffer_too_large() {
// 1 `TestStruct` + length = 37 bytes
// we pass 38 to trigger BufferTooLarge
let pod_slice_bytes = [1; 38];
let err = PodSlice::<TestStruct>::unpack(&pod_slice_bytes)
let err = PodSlice::<TestStruct, PodU32>::unpack(&pod_slice_bytes)
.err()
.unwrap();
assert_eq!(
Expand All @@ -185,7 +192,7 @@ mod tests {
// 1 `TestStruct` + length = 37 bytes
// we pass 36 to trigger BufferTooSmall
let pod_slice_bytes = [1; 36];
let err = PodSlice::<TestStruct>::unpack(&pod_slice_bytes)
let err = PodSlice::<TestStruct, PodU32>::unpack(&pod_slice_bytes)
.err()
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -213,4 +220,58 @@ mod tests {
.expect_err("Expected an `PodSliceError::BufferTooSmall` error");
assert_eq!(err, PodSliceError::BufferTooSmall.into());
}

#[repr(C)]
#[derive(Debug, PartialEq, Default, Clone, Copy, Pod, Zeroable)]
pub struct EpochAndStakeHistoryEntry {
pub epoch: PodU64,
pub entry: PodStakeHistoryEntry,
}

#[repr(C)]
#[derive(Debug, PartialEq, Default, Clone, Copy, Pod, Zeroable)]
pub struct PodStakeHistoryEntry {
pub effective: PodU64, // effective stake at this epoch
pub activating: PodU64, // sum of portion of stakes not fully warmed up
pub deactivating: PodU64, // requested to be cooled down, not fully deactivated yet
}

impl From<PodStakeHistoryEntry> for StakeHistoryEntry {
fn from(item: PodStakeHistoryEntry) -> Self {
Self {
effective: item.effective.into(),
activating: item.activating.into(),
deactivating: item.deactivating.into(),
}
}
}

fn test_stake_history() -> StakeHistory {
let mut stake_history = StakeHistory::default();
for i in 0..MAX_ENTRIES as u64 + 1 {
stake_history.add(
i,
StakeHistoryEntry {
activating: i,
..StakeHistoryEntry::default()
},
);
}
stake_history
}

#[test]
fn test_serde() {
let stake_history = test_stake_history();
let serialized = serialize(&stake_history).unwrap();
let pod_slice = PodSlice::<EpochAndStakeHistoryEntry, PodU64>::unpack(&serialized).unwrap();
assert_eq!(
&pod_slice
.data()
.iter()
.map(|x| (u64::from(x.epoch), x.entry.into()))
.collect::<Vec<_>>(),
stake_history.deref()
);
}
}
Loading