-
Notifications
You must be signed in to change notification settings - Fork 33
StakeStakeV2 zero copy api [wincode]
#222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
9c65372 to
e0471c2
Compare
e0471c2 to
3b7d1b1
Compare
| } | ||
| } | ||
|
|
||
| pub fn from_bytes(bytes: &[u8]) -> Result<Self, StakeStateError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be zero-copy if you do a transmute instead of StakeStateV2Tag::deserialize, but not sure if there is any benefit in doing that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we lose some safety though? What do you think about this instead?
pub fn from_bytes(bytes: &[u8]) -> Result<Self, StakeStateError> {
if bytes.len() < 4 {
return Err(StakeStateError::UnexpectedEof);
}
let raw = u32::from_le_bytes(bytes[..4].try_into().unwrap());
Self::from_u32(raw)
}| #[repr(transparent)] | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq, Default, SchemaWrite, SchemaRead)] | ||
| #[wincode(assert_zero_copy)] | ||
| pub struct PodAddress(pub [u8; 32]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add wincode support to Address, then this type is not needed.
| }; | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum StakeStateV2View<'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a breaking change, could we stop using the enum? Seems like all we need is already in StakeStateV2Layout, so that one could become the state "account". One benefit is that we won't need to have the *View and *Write type. The fields not applicable for a variant can be optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Discussed offline)
Going to revise the API, but ensure a private-field pattern to ensure only methods can make state transitions to prevent invalidate state.
Adds a new zero‑copy state API backed by wincode.
Key Changes
p-stake-interfacecrate (#![no_std]compatible)PodU32,PodU64,PodI64,PodAddress) for safe zero-copy fromunaligned slices
StakeStateV2:StakeStateV2::from_bytes(&[u8]) -> Result<&StakeStateV2, StakeStateError>for read-only accessStakeStateV2::from_bytes_mut(&mut [u8]) -> Result<&mut StakeStateV2, StakeStateError>formutations
meta(),stake(),meta_mut(),stake_mut()initialize(),delegate()Motivation
Deserialization via bincode/borsh is computationally expensive for programs that only need to access specific fields or verify the state. This zero-copy approach allows for significantly lower compute unit (CU) usage when interacting with stake accounts.