Skip to content

add slot hashes sysvar #121

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

add slot hashes sysvar #121

wants to merge 3 commits into from

Conversation

Ozodimgba
Copy link
Contributor

@Ozodimgba Ozodimgba commented Apr 7, 2025

Add SlotHashes sysvar support to Pinocchio

This PR adds support for the SlotHashes sysvar to the Pinocchio library, providing on-chain programs with efficient access to historical slot hash data.

Implementation details

  • added a new slot_hashes.rs module in the sysvars directory
  • implemented a SlotHashesSysvar struct with methods to efficiently access specific entries using direct syscalls
  • used a no_std compatible approach with no dependencies on the standard library
  • Added constants for the data layout and sizes to match the actual Solana implementation

sysvar on chain methods

  • get_entry(index): Access a specific slot-hash entry by index
  • get_hash(slot): Find the hash for a specific slot
  • position(slot): Find the position of a slot in the collection
  • len(): Get the number of entries available

This implementation allows Pinocchio-based programs to access the SlotHashes sysvar in a way that's compute-efficient and memory-efficient, avoiding the need to deserialize the entire data structure.

question for @febo:

Can I add a std-featured off-chain SlotHashes struct to the implementation? I'd like to include a convenience wrapper that uses Vec for off-chain tooling when the "std" feature is enabled. This would make the API more familiar for off-chain use cases while keeping the core implementation no_std compatible. Would this approach be acceptable for the Pinocchio library's design philosophy?

let mut len_buf = [0u8; VEC_LENGTH_SIZE];

let result = unsafe {
crate::syscalls::sol_get_sysvar(
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is a bit expensive to use a syscall just to get the number of entries. Better to use the syscall to get all the data.

let len = Self::len()?;

for i in 0..len {
if let Some(entry) = Self::get_entry(i) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This approach works, but it will be expensive to use multiple syscalls to iterate over them. It will be better to follow a similar approach as PodSlotHashes and Instructions sysvar.

let len = Self::len()?;

for i in 0..len {
if let Some(entry) = Self::get_entry(i) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment here, it will be expensive to iterate using the syscall.

@febo
Copy link
Collaborator

febo commented Apr 10, 2025

Thanks for the PR. While this approach will work, it will be expensive to use it – a syscall takes 100 CUs so ideally you will have only one syscall to get all the data. Left some links for you to follow if you decide to refactor this one.

Can I add a std-featured off-chain SlotHashes struct to the implementation? I'd like to include a convenience wrapper that uses Vec for off-chain tooling when the "std" feature is enabled.

Off-chain clients can use the SDK crate right? I am not sure what would be the benefit of using this one.

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.

2 participants