Skip to content

Conversation

@LStan
Copy link
Contributor

@LStan LStan commented May 26, 2025

This PR adds EpochRewards sysvar type. I copied it from solana-sdk. Not sure about total_points because it is u128. It will work with get, but if from_account_info is implemented it can cause UB. Probably it should be changed to [u8; 16].

Comment on lines 4 to 13
pub const HASH_BYTES: usize = 32;

#[derive(Default, Debug, Clone)]
pub struct Hash([u8; HASH_BYTES]);

impl Hash {
pub fn to_bytes(self) -> [u8; HASH_BYTES] {
self.0
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert in how things are organized in Pinocchio, but it seems strange to define the blockhash type inside sysvars::epoch_rewards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should just remove it completely and change parent_blockhash: Hash to parent_blockhash: [u8; 32] ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a dedicated type for the hashes seems quite reasonable.
Not sure what is the state of the Pinocchio coverage here.
There should be a Hash type at some point, I would think.

Comment on lines 6 to 7
#[derive(Default, Debug, Clone)]
pub struct Hash([u8; HASH_BYTES]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also be repr(C) if it is used inside a repr(C) struct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is repr(transparent) in solana-sdk

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either repr(transparent) or repr(C) would work here, I think.
repr(transparent) is more reasonable if the internal state is also public.
This way, the user can see what layout is being used in the docs, for example.

So, maybe this:

Suggested change
#[derive(Default, Debug, Clone)]
pub struct Hash([u8; HASH_BYTES]);
#[derive(Default, Debug, Clone)]
#[repr(transparent)]
pub struct Hash(pub [u8; HASH_BYTES]);

I think any 32 bytes are a valid hash. So it seems fine to expose [u8; HASH_BYTES] I guess?

The SDK folks will probably want to look at this change as well.

@joncinque You are the expert here, right?
Or you can probably get someone with the SDK expertise to double-check the approach here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We avoided exposing the hash bytes publicly for the base hash type in the sdk, which makes we wary of exposing it here: https://github.com/anza-xyz/solana-sdk/blob/965e2a8a63bcc730ee6c001a0e66e3314bb924d6/hash/src/lib.rs#L53

We should definitely go with repr(transparent) though.

@febo do you have any opinions about a generic hash type for pinocchio? Did you want to create newtype, a type alias, or eventually use solana_hash::Hash? I'm hoping we can go with the last option, but we might have to remove the solana_sanitize dependency first.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LStan I think it will be better to mimic the SDK type here – so avoid exposing the hash bytes:

[repr(transparent)]
pub struct Hash(pub(crate) [u8; HASH_BYTES]);

We eventually will use the SDK type so it will be easier to "swap" the type if they follow the same implementation.

@ilya-bobyr ilya-bobyr requested review from febo and joncinque July 14, 2025 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants