Skip to content

Commit

Permalink
Move back get_sysvar to solana-program
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Oct 28, 2024
1 parent 5a44394 commit b0ad1c4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 58 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion programs/sbf/Cargo.lock

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

31 changes: 30 additions & 1 deletion sdk/program/src/sysvar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
use crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
#[deprecated(since = "2.1.0", note = "Use `solana-sysvar-id` crate instead")]
pub use solana_sysvar_id::{
check_id, declare_deprecated_sysvar_id, declare_sysvar_id, get_sysvar, id, SysvarId, ID,
check_id, declare_deprecated_sysvar_id, declare_sysvar_id, id, SysvarId, ID,
};
#[allow(deprecated)]
pub use sysvar_ids::ALL_IDS;
Expand Down Expand Up @@ -203,6 +203,35 @@ macro_rules! impl_sysvar_get {
};
}

/// Handler for retrieving a slice of sysvar data from the `sol_get_sysvar`
/// syscall.
fn get_sysvar(
dst: &mut [u8],
sysvar_id: &Pubkey,
offset: u64,
length: u64,
) -> Result<(), ProgramError> {
// Check that the provided destination buffer is large enough to hold the
// requested data.
if dst.len() < length as usize {
return Err(ProgramError::InvalidArgument);
}

let sysvar_id = sysvar_id as *const _ as *const u8;
let var_addr = dst as *mut _ as *mut u8;

#[cfg(target_os = "solana")]
let result = unsafe { crate::syscalls::sol_get_sysvar(sysvar_id, var_addr, offset, length) };

#[cfg(not(target_os = "solana"))]
let result = crate::program_stubs::sol_get_sysvar(sysvar_id, var_addr, offset, length);

match result {
crate::entrypoint::SUCCESS => Ok(()),
e => Err(e.into()),
}
}

#[cfg(test)]
mod tests {
use {
Expand Down
5 changes: 0 additions & 5 deletions sdk/sysvar-id/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ license = { workspace = true }
edition = { workspace = true }

[dependencies]
solana-program-error = { workspace = true }
solana-pubkey = { workspace = true, default-features = false }

[dev-dependencies]
solana-program-entrypoint = { workspace = true }
static_assertions = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
all-features = true
Expand Down
48 changes: 0 additions & 48 deletions sdk/sysvar-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//!
//! [sysvardoc]: https://docs.solanalabs.com/runtime/sysvars
use solana_program_error::ProgramError;
/// Re-export types required for macros
pub use solana_pubkey::{declare_deprecated_id, declare_id, Pubkey};

Expand Down Expand Up @@ -69,52 +68,5 @@ macro_rules! declare_deprecated_sysvar_id(
)
);

const _SUCCESS: u64 = 0;
#[cfg(test)]
static_assertions::const_assert_eq!(_SUCCESS, solana_program_entrypoint::SUCCESS);

// Owner pubkey for sysvar accounts
solana_pubkey::declare_id!("Sysvar1111111111111111111111111111111111111");

/// Handler for retrieving a slice of sysvar data from the `sol_get_sysvar`
/// syscall.
pub fn get_sysvar(
dst: &mut [u8],
sysvar_id: &Pubkey,
offset: u64,
length: u64,
) -> Result<(), ProgramError> {
// Check that the provided destination buffer is large enough to hold the
// requested data.
if dst.len() < length as usize {
return Err(ProgramError::InvalidArgument);
}

get_sysvar_unchecked(dst, sysvar_id, offset, length)
}

/// Handler for retrieving a slice of sysvar data from the `sol_get_sysvar`
/// syscall, without a client-side length check
#[allow(unused_variables)]
pub fn get_sysvar_unchecked(
dst: &mut [u8],
sysvar_id: &Pubkey,
offset: u64,
length: u64,
) -> Result<(), ProgramError> {
#[cfg(target_os = "solana")]
{
let sysvar_id = sysvar_id as *const _ as *const u8;
let var_addr = dst as *mut _ as *mut u8;

let result =
unsafe { crate::syscalls::sol_get_sysvar(sysvar_id, var_addr, offset, length) };

match result {
_SUCCESS => Ok(()),
e => Err(e.into()),
}
}
#[cfg(not(target_os = "solana"))]
Err(ProgramError::UnsupportedSysvar)
}

0 comments on commit b0ad1c4

Please sign in to comment.