Return sysvars via syscalls (#16422)

This commit is contained in:
Jack May
2021-04-12 16:04:57 -07:00
committed by GitHub
parent 2229b70c4e
commit fa83f3bd73
18 changed files with 683 additions and 28 deletions

View File

@ -82,7 +82,7 @@ pub type UnixTimestamp = i64;
/// as the network progresses).
///
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
#[derive(Serialize, Clone, Deserialize, Debug, Default, PartialEq)]
pub struct Clock {
/// the current network/bank Slot
pub slot: Slot,

View File

@ -2,7 +2,10 @@
#![cfg(not(target_arch = "bpf"))]
use crate::{account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction};
use crate::{
account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction,
program_error::UNSUPPORTED_SYSVAR,
};
use std::sync::{Arc, RwLock};
lazy_static::lazy_static! {
@ -31,6 +34,19 @@ pub trait SyscallStubs: Sync + Send {
sol_log("SyscallStubs: sol_invoke_signed() not available");
Ok(())
}
fn sol_get_clock_sysvar(&self, _var_addr: *mut u8) -> u64 {
UNSUPPORTED_SYSVAR
}
fn sol_get_epoch_schedule_sysvar(&self, _var_addr: *mut u8) -> u64 {
UNSUPPORTED_SYSVAR
}
fn sol_get_fees_sysvar(&self, _var_addr: *mut u8) -> u64 {
UNSUPPORTED_SYSVAR
}
fn sol_get_rent_sysvar(&self, _var_addr: *mut u8) -> u64 {
UNSUPPORTED_SYSVAR
}
}
struct DefaultSyscallStubs {}
@ -61,3 +77,22 @@ pub(crate) fn sol_invoke_signed(
.unwrap()
.sol_invoke_signed(instruction, account_infos, signers_seeds)
}
pub(crate) fn sol_get_clock_sysvar(var_addr: *mut u8) -> u64 {
SYSCALL_STUBS.read().unwrap().sol_get_clock_sysvar(var_addr)
}
pub(crate) fn sol_get_epoch_schedule_sysvar(var_addr: *mut u8) -> u64 {
SYSCALL_STUBS
.read()
.unwrap()
.sol_get_epoch_schedule_sysvar(var_addr)
}
pub(crate) fn sol_get_fees_sysvar(var_addr: *mut u8) -> u64 {
SYSCALL_STUBS.read().unwrap().sol_get_fees_sysvar(var_addr)
}
pub(crate) fn sol_get_rent_sysvar(var_addr: *mut u8) -> u64 {
SYSCALL_STUBS.read().unwrap().sol_get_rent_sysvar(var_addr)
}

View File

@ -2,8 +2,10 @@
//!
pub use crate::clock::Clock;
use crate::sysvar::Sysvar;
use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
crate::declare_sysvar_id!("SysvarC1ock11111111111111111111111111111111", Clock);
impl Sysvar for Clock {}
impl Sysvar for Clock {
impl_sysvar_get!(sol_get_clock_sysvar);
}

View File

@ -1,8 +1,11 @@
//! This account contains the current cluster rent
//!
pub use crate::epoch_schedule::EpochSchedule;
use crate::sysvar::Sysvar;
use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
crate::declare_sysvar_id!("SysvarEpochSchedu1e111111111111111111111111", EpochSchedule);
impl Sysvar for EpochSchedule {}
impl Sysvar for EpochSchedule {
impl_sysvar_get!(sol_get_epoch_schedule_sysvar);
}

View File

@ -1,11 +1,13 @@
//! This account contains the current cluster fees
//!
use crate::{fee_calculator::FeeCalculator, sysvar::Sysvar};
use crate::{
fee_calculator::FeeCalculator, impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar,
};
crate::declare_sysvar_id!("SysvarFees111111111111111111111111111111111", Fees);
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
pub struct Fees {
pub fee_calculator: FeeCalculator,
}
@ -17,4 +19,6 @@ impl Fees {
}
}
impl Sysvar for Fees {}
impl Sysvar for Fees {
impl_sysvar_get!(sol_get_fees_sysvar);
}

View File

@ -47,14 +47,14 @@ macro_rules! declare_sysvar_id(
)
);
// owner pubkey for sysvar accounts
// Owner pubkey for sysvar accounts
crate::declare_id!("Sysvar1111111111111111111111111111111111111");
pub trait SysvarId {
fn check_id(pubkey: &Pubkey) -> bool;
}
// utilities for moving into and out of Accounts
// Sysvar utilities
pub trait Sysvar:
SysvarId + Default + Sized + serde::Serialize + serde::de::DeserializeOwned
{
@ -70,6 +70,34 @@ pub trait Sysvar:
fn to_account_info(&self, account_info: &mut AccountInfo) -> Option<()> {
bincode::serialize_into(&mut account_info.data.borrow_mut()[..], self).ok()
}
fn get() -> Result<Self, ProgramError> {
Err(ProgramError::UnsupportedSysvar)
}
}
#[macro_export]
macro_rules! impl_sysvar_get {
($syscall_name:ident) => {
fn get() -> Result<Self, ProgramError> {
let mut var = Self::default();
let var_addr = &mut var as *mut _ as *mut u8;
#[cfg(target_arch = "bpf")]
let result = unsafe {
extern "C" {
fn $syscall_name(var_addr: *mut u8) -> u64;
}
$syscall_name(var_addr)
};
#[cfg(not(target_arch = "bpf"))]
let result = crate::program_stubs::$syscall_name(var_addr);
match result {
crate::entrypoint::SUCCESS => Ok(var),
e => Err(e.into()),
}
}
};
}
#[cfg(test)]

View File

@ -2,8 +2,10 @@
//!
pub use crate::rent::Rent;
use crate::sysvar::Sysvar;
use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
crate::declare_sysvar_id!("SysvarRent111111111111111111111111111111111", Rent);
impl Sysvar for Rent {}
impl Sysvar for Rent {
impl_sysvar_get!(sol_get_rent_sysvar);
}