Skip to content

Commit f8d441d

Browse files
committed
factor out SysvarGet from Sysvar
1 parent 6589b7e commit f8d441d

13 files changed

+83
-52
lines changed

sysvar/src/clock.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,16 @@
123123
//! ```
124124
125125
#[cfg(feature = "bincode")]
126-
use crate::{impl_sysvar_get, Sysvar};
126+
use crate::Sysvar;
127+
use crate::{impl_sysvar_get, SysvarGet};
127128
pub use {
128129
solana_clock::Clock,
129130
solana_sdk_ids::sysvar::clock::{check_id, id, ID},
130131
};
131132

132-
#[cfg(feature = "bincode")]
133-
impl Sysvar for Clock {
133+
impl SysvarGet for Clock {
134134
impl_sysvar_get!(sol_get_clock_sysvar);
135135
}
136+
137+
#[cfg(feature = "bincode")]
138+
impl Sysvar for Clock {}

sysvar/src/epoch_rewards.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,16 @@
156156
//! ```
157157
158158
#[cfg(feature = "bincode")]
159-
use crate::{impl_sysvar_get, Sysvar};
159+
use crate::Sysvar;
160+
use crate::{impl_sysvar_get, SysvarGet};
160161
pub use {
161162
solana_epoch_rewards::EpochRewards,
162163
solana_sdk_ids::sysvar::epoch_rewards::{check_id, id, ID},
163164
};
164165

165-
#[cfg(feature = "bincode")]
166-
impl Sysvar for EpochRewards {
166+
impl SysvarGet for EpochRewards {
167167
impl_sysvar_get!(sol_get_epoch_rewards_sysvar);
168168
}
169+
170+
#[cfg(feature = "bincode")]
171+
impl Sysvar for EpochRewards {}

sysvar/src/epoch_schedule.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,16 @@
121121
//! # Ok::<(), anyhow::Error>(())
122122
//! ```
123123
#[cfg(feature = "bincode")]
124-
use crate::{impl_sysvar_get, Sysvar};
124+
use crate::Sysvar;
125+
use crate::{impl_sysvar_get, SysvarGet};
125126
pub use {
126127
solana_epoch_schedule::EpochSchedule,
127128
solana_sdk_ids::sysvar::epoch_schedule::{check_id, id, ID},
128129
};
129130

130-
#[cfg(feature = "bincode")]
131-
impl Sysvar for EpochSchedule {
131+
impl SysvarGet for EpochSchedule {
132132
impl_sysvar_get!(sol_get_epoch_schedule_sysvar);
133133
}
134+
135+
#[cfg(feature = "bincode")]
136+
impl Sysvar for EpochSchedule {}

sysvar/src/fees.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
#![allow(deprecated)]
2222

2323
#[cfg(feature = "bincode")]
24-
use crate::{impl_sysvar_get, Sysvar};
24+
use crate::Sysvar;
2525
#[cfg(feature = "serde")]
2626
use serde_derive::{Deserialize, Serialize};
2727
pub use solana_sdk_ids::sysvar::fees::{check_id, id, ID};
2828
use {
29-
solana_fee_calculator::FeeCalculator, solana_sdk_macro::CloneZeroed,
29+
crate::{impl_sysvar_get, SysvarGet},
30+
solana_fee_calculator::FeeCalculator,
31+
solana_sdk_macro::CloneZeroed,
3032
solana_sysvar_id::impl_deprecated_sysvar_id,
3133
};
3234

@@ -53,11 +55,13 @@ impl Fees {
5355
}
5456
}
5557

56-
#[cfg(feature = "bincode")]
57-
impl Sysvar for Fees {
58+
impl SysvarGet for Fees {
5859
impl_sysvar_get!(sol_get_fees_sysvar);
5960
}
6061

62+
#[cfg(feature = "bincode")]
63+
impl Sysvar for Fees {}
64+
6165
#[cfg(test)]
6266
mod tests {
6367
use super::*;

sysvar/src/last_restart_slot.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@
3737
//! ```
3838
//!
3939
#[cfg(feature = "bincode")]
40-
use crate::{impl_sysvar_get, Sysvar};
40+
use crate::Sysvar;
41+
use crate::{impl_sysvar_get, SysvarGet};
4142
pub use {
4243
solana_last_restart_slot::LastRestartSlot,
4344
solana_sdk_ids::sysvar::last_restart_slot::{check_id, id, ID},
4445
};
4546

46-
#[cfg(feature = "bincode")]
47-
impl Sysvar for LastRestartSlot {
47+
impl SysvarGet for LastRestartSlot {
4848
impl_sysvar_get!(sol_get_last_restart_slot);
4949
}
50+
51+
#[cfg(feature = "bincode")]
52+
impl Sysvar for LastRestartSlot {}

sysvar/src/lib.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ pub mod __private {
8383
pub use solana_define_syscall::definitions;
8484
pub use {solana_program_entrypoint::SUCCESS, solana_program_error::ProgramError};
8585
}
86-
use solana_pubkey::Pubkey;
8786
#[allow(deprecated)]
8887
#[doc(inline)]
8988
#[deprecated(
@@ -92,10 +91,8 @@ use solana_pubkey::Pubkey;
9291
)]
9392
pub use sysvar_ids::ALL_IDS;
9493
#[cfg(feature = "bincode")]
95-
use {
96-
solana_account_info::AccountInfo, solana_program_error::ProgramError,
97-
solana_sysvar_id::SysvarId,
98-
};
94+
use {solana_account_info::AccountInfo, solana_sysvar_id::SysvarId};
95+
use {solana_program_error::ProgramError, solana_pubkey::Pubkey};
9996

10097
pub mod clock;
10198
pub mod epoch_rewards;
@@ -145,10 +142,25 @@ pub fn is_sysvar_id(id: &Pubkey) -> bool {
145142
ALL_IDS.iter().any(|key| key == id)
146143
}
147144

145+
/// Interface for loading a sysvar.
146+
pub trait SysvarGet: Sized {
147+
/// Load the sysvar directly from the runtime.
148+
///
149+
/// This is the preferred way to load a sysvar. Calling this method does not
150+
/// incur any deserialization overhead, and does not require the sysvar
151+
/// account to be passed to the program.
152+
///
153+
/// Not all sysvars support this method. If not, it returns
154+
/// [`ProgramError::UnsupportedSysvar`].
155+
fn get() -> Result<Self, ProgramError> {
156+
Err(ProgramError::UnsupportedSysvar)
157+
}
158+
}
159+
148160
#[cfg(feature = "bincode")]
149161
/// A type that holds sysvar data.
150162
pub trait Sysvar:
151-
SysvarId + Default + Sized + serde::Serialize + serde::de::DeserializeOwned
163+
SysvarGet + SysvarId + Default + serde::Serialize + serde::de::DeserializeOwned
152164
{
153165
/// The size in bytes of the sysvar as serialized account data.
154166
fn size_of() -> usize {
@@ -177,16 +189,9 @@ pub trait Sysvar:
177189
bincode::serialize_into(&mut account_info.data.borrow_mut()[..], self).ok()
178190
}
179191

180-
/// Load the sysvar directly from the runtime.
181-
///
182-
/// This is the preferred way to load a sysvar. Calling this method does not
183-
/// incur any deserialization overhead, and does not require the sysvar
184-
/// account to be passed to the program.
185-
///
186-
/// Not all sysvars support this method. If not, it returns
187-
/// [`ProgramError::UnsupportedSysvar`].
192+
/// Calls [`SysvarGet::get`].
188193
fn get() -> Result<Self, ProgramError> {
189-
Err(ProgramError::UnsupportedSysvar)
194+
<Self as SysvarGet>::get()
190195
}
191196
}
192197

@@ -271,6 +276,7 @@ mod tests {
271276
check_id(pubkey)
272277
}
273278
}
279+
impl SysvarGet for TestSysvar {}
274280
impl Sysvar for TestSysvar {}
275281

276282
// NOTE tests that use this mock MUST carry the #[serial] attribute

sysvar/src/program_stubs.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,25 @@ pub(crate) fn sol_get_sysvar(
155155
.sol_get_sysvar(sysvar_id_addr, var_addr, offset, length)
156156
}
157157

158-
#[cfg(feature = "bincode")]
159158
pub(crate) fn sol_get_clock_sysvar(var_addr: *mut u8) -> u64 {
160159
SYSCALL_STUBS.read().unwrap().sol_get_clock_sysvar(var_addr)
161160
}
162161

163-
#[cfg(feature = "bincode")]
164162
pub(crate) fn sol_get_epoch_schedule_sysvar(var_addr: *mut u8) -> u64 {
165163
SYSCALL_STUBS
166164
.read()
167165
.unwrap()
168166
.sol_get_epoch_schedule_sysvar(var_addr)
169167
}
170168

171-
#[cfg(feature = "bincode")]
172169
pub(crate) fn sol_get_fees_sysvar(var_addr: *mut u8) -> u64 {
173170
SYSCALL_STUBS.read().unwrap().sol_get_fees_sysvar(var_addr)
174171
}
175172

176-
#[cfg(feature = "bincode")]
177173
pub(crate) fn sol_get_rent_sysvar(var_addr: *mut u8) -> u64 {
178174
SYSCALL_STUBS.read().unwrap().sol_get_rent_sysvar(var_addr)
179175
}
180176

181-
#[cfg(feature = "bincode")]
182177
pub(crate) fn sol_get_last_restart_slot(var_addr: *mut u8) -> u64 {
183178
SYSCALL_STUBS
184179
.read()
@@ -216,7 +211,6 @@ pub fn sol_get_stack_height() -> u64 {
216211
SYSCALL_STUBS.read().unwrap().sol_get_stack_height()
217212
}
218213

219-
#[cfg(feature = "bincode")]
220214
pub(crate) fn sol_get_epoch_rewards_sysvar(var_addr: *mut u8) -> u64 {
221215
SYSCALL_STUBS
222216
.read()

sysvar/src/recent_blockhashes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::Sysvar;
2424
use serde_derive::{Deserialize, Serialize};
2525
pub use solana_sdk_ids::sysvar::recent_blockhashes::{check_id, id, ID};
2626
use {
27+
crate::SysvarGet,
2728
solana_fee_calculator::FeeCalculator,
2829
solana_hash::Hash,
2930
solana_sysvar_id::impl_sysvar_id,
@@ -150,6 +151,8 @@ impl<T: Ord> Iterator for IntoIterSorted<T> {
150151
}
151152
}
152153

154+
impl SysvarGet for RecentBlockhashes {}
155+
153156
#[cfg(feature = "bincode")]
154157
impl Sysvar for RecentBlockhashes {
155158
fn size_of() -> usize {

sysvar/src/rent.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,15 @@
123123
//! # Ok::<(), anyhow::Error>(())
124124
//! ```
125125
#[cfg(feature = "bincode")]
126-
use crate::{impl_sysvar_get, Sysvar};
126+
use crate::Sysvar;
127+
use crate::{impl_sysvar_get, SysvarGet};
127128
pub use {
128129
solana_rent::Rent,
129130
solana_sdk_ids::sysvar::rent::{check_id, id, ID},
130131
};
131-
132-
#[cfg(feature = "bincode")]
133-
impl Sysvar for Rent {
132+
impl SysvarGet for Rent {
134133
impl_sysvar_get!(sol_get_rent_sysvar);
135134
}
135+
136+
#[cfg(feature = "bincode")]
137+
impl Sysvar for Rent {}

sysvar/src/rewards.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::Sysvar;
44
#[cfg(feature = "serde")]
55
use serde_derive::{Deserialize, Serialize};
66
pub use solana_sdk_ids::sysvar::rewards::{check_id, id, ID};
7-
use solana_sysvar_id::impl_sysvar_id;
7+
use {crate::SysvarGet, solana_sysvar_id::impl_sysvar_id};
88

99
impl_sysvar_id!(Rewards);
1010

@@ -23,5 +23,6 @@ impl Rewards {
2323
}
2424
}
2525
}
26+
impl SysvarGet for Rewards {}
2627
#[cfg(feature = "bincode")]
2728
impl Sysvar for Rewards {}

sysvar/src/slot_hashes.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@
4545
//! #
4646
//! # Ok::<(), anyhow::Error>(())
4747
//! ```
48-
4948
#[cfg(feature = "bytemuck")]
5049
use bytemuck_derive::{Pod, Zeroable};
5150
#[cfg(feature = "bincode")]
5251
use {crate::Sysvar, solana_account_info::AccountInfo};
53-
use {solana_clock::Slot, solana_hash::Hash};
52+
use {crate::SysvarGet, solana_clock::Slot, solana_hash::Hash};
5453

55-
#[cfg(all(feature = "bincode", feature = "bytemuck"))]
54+
#[cfg(feature = "bytemuck")]
5655
const U64_SIZE: usize = std::mem::size_of::<u64>();
5756

5857
pub use {
@@ -61,12 +60,17 @@ pub use {
6160
solana_sysvar_id::SysvarId,
6261
};
6362

63+
// hard-coded so that we don't have to construct an empty.
64+
// golden, update if MAX_ENTRIES changes
65+
#[cfg(any(feature = "bincode", feature = "bytemuck"))]
66+
const SLOT_HASHES_SIZE: usize = 20_488;
67+
68+
impl SysvarGet for SlotHashes {}
6469
#[cfg(feature = "bincode")]
6570
impl Sysvar for SlotHashes {
6671
// override
6772
fn size_of() -> usize {
68-
// hard-coded so that we don't have to construct an empty
69-
20_488 // golden, update if MAX_ENTRIES changes
73+
SLOT_HASHES_SIZE
7074
}
7175
fn from_account_info(
7276
_account_info: &AccountInfo,
@@ -102,7 +106,7 @@ impl PodSlotHashes {
102106
/// Fetch all of the raw sysvar data using the `sol_get_sysvar` syscall.
103107
pub fn fetch() -> Result<Self, solana_program_error::ProgramError> {
104108
// Allocate an uninitialized buffer for the raw sysvar data.
105-
let sysvar_len = SlotHashes::size_of();
109+
let sysvar_len = SLOT_HASHES_SIZE;
106110
let mut data = vec![0; sysvar_len];
107111

108112
// Ensure the created buffer is aligned to 8.
@@ -206,7 +210,7 @@ impl SlotHashesSysvar {
206210
}
207211
}
208212

209-
#[cfg(feature = "bytemuck")]
213+
#[cfg(all(feature = "bincode", feature = "bytemuck"))]
210214
fn get_pod_slot_hashes() -> Result<Vec<PodSlotHash>, solana_program_error::ProgramError> {
211215
let mut pod_hashes = vec![PodSlotHash::default(); solana_slot_hashes::MAX_ENTRIES];
212216
{
@@ -219,7 +223,7 @@ fn get_pod_slot_hashes() -> Result<Vec<PodSlotHash>, solana_program_error::Progr
219223
}
220224

221225
let offset = 8; // Vector length as `u64`.
222-
let length = (SlotHashes::size_of() as u64).saturating_sub(offset);
226+
let length = (SLOT_HASHES_SIZE as u64).saturating_sub(offset);
223227
crate::get_sysvar(data, &SlotHashes::id(), offset, length)?;
224228
}
225229
Ok(pod_hashes)

sysvar/src/slot_history.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@
5050
5151
#[cfg(feature = "bincode")]
5252
use crate::Sysvar;
53+
use crate::SysvarGet;
5354
pub use {
5455
solana_account_info::AccountInfo,
5556
solana_program_error::ProgramError,
5657
solana_sdk_ids::sysvar::slot_history::{check_id, id, ID},
5758
solana_slot_history::SlotHistory,
5859
};
59-
60+
impl SysvarGet for SlotHistory {}
6061
#[cfg(feature = "bincode")]
6162
impl Sysvar for SlotHistory {
6263
// override

sysvar/src/stake_history.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ pub use solana_sdk_ids::sysvar::stake_history::{check_id, id, ID};
5656
pub use solana_stake_interface::stake_history::{
5757
StakeHistory, StakeHistoryEntry, StakeHistoryGetEntry, MAX_ENTRIES,
5858
};
59-
use {crate::get_sysvar, solana_clock::Epoch};
59+
use {
60+
crate::{get_sysvar, SysvarGet},
61+
solana_clock::Epoch,
62+
};
6063

64+
impl SysvarGet for StakeHistory {}
6165
#[cfg(feature = "bincode")]
6266
impl Sysvar for StakeHistory {
6367
// override

0 commit comments

Comments
 (0)