-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathepoch_schedule.rs
More file actions
242 lines (225 loc) · 7.93 KB
/
epoch_schedule.rs
File metadata and controls
242 lines (225 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! Information about epoch duration.
//!
//! The _epoch schedule_ sysvar provides access to the [`EpochSchedule`] type,
//! which includes the number of slots per epoch, timing of leader schedule
//! selection, and information about epoch warm-up time.
//!
//! [`EpochSchedule`] implements [`Sysvar::get`] and can be loaded efficiently without
//! passing the sysvar account ID to the program.
//!
//! See also the Solana [documentation on the epoch schedule sysvar][sdoc].
//!
//! [sdoc]: https://docs.solanalabs.com/runtime/sysvars#epochschedule
//!
//! # Examples
//!
//! Accessing via on-chain program directly:
//!
//! ```no_run
//! # use solana_account_info::AccountInfo;
//! # use solana_epoch_schedule::EpochSchedule;
//! # use solana_msg::msg;
//! # use solana_program_error::{ProgramError, ProgramResult};
//! # use solana_pubkey::Pubkey;
//! # use solana_sdk_ids::sysvar::epoch_schedule;
//! # use solana_sysvar::Sysvar;
//! fn process_instruction(
//! program_id: &Pubkey,
//! accounts: &[AccountInfo],
//! instruction_data: &[u8],
//! ) -> ProgramResult {
//!
//! let epoch_schedule = EpochSchedule::get()?;
//! msg!("epoch_schedule: {:#?}", epoch_schedule);
//!
//! Ok(())
//! }
//! #
//! # use solana_sysvar_id::SysvarId;
//! # let p = EpochSchedule::id();
//! # let l = &mut 1120560;
//! # let d = &mut vec![0, 32, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
//! # let a = AccountInfo::new(&p, false, false, l, d, &p, false);
//! # let accounts = &[a.clone(), a];
//! # process_instruction(
//! # &Pubkey::new_unique(),
//! # accounts,
//! # &[],
//! # )?;
//! # Ok::<(), ProgramError>(())
//! ```
//!
//! Accessing via on-chain program's account parameters:
//!
//! ```
//! # use solana_account_info::{AccountInfo, next_account_info};
//! # use solana_epoch_schedule::EpochSchedule;
//! # use solana_msg::msg;
//! # use solana_program_error::{ProgramError, ProgramResult};
//! # use solana_pubkey::Pubkey;
//! # use solana_sdk_ids::sysvar::epoch_schedule;
//! # use solana_sysvar::{Sysvar, SysvarSerialize};
//! fn process_instruction(
//! program_id: &Pubkey,
//! accounts: &[AccountInfo],
//! instruction_data: &[u8],
//! ) -> ProgramResult {
//! let account_info_iter = &mut accounts.iter();
//! let epoch_schedule_account_info = next_account_info(account_info_iter)?;
//!
//! assert!(epoch_schedule::check_id(epoch_schedule_account_info.key));
//!
//! let epoch_schedule = EpochSchedule::from_account_info(epoch_schedule_account_info)?;
//! msg!("epoch_schedule: {:#?}", epoch_schedule);
//!
//! Ok(())
//! }
//! #
//! # use solana_sysvar_id::SysvarId;
//! # let p = EpochSchedule::id();
//! # let l = &mut 1120560;
//! # let d = &mut vec![0, 32, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
//! # let a = AccountInfo::new(&p, false, false, l, d, &p, false);
//! # let accounts = &[a.clone(), a];
//! # process_instruction(
//! # &Pubkey::new_unique(),
//! # accounts,
//! # &[],
//! # )?;
//! # Ok::<(), ProgramError>(())
//! ```
//!
//! Accessing via the RPC client:
//!
//! ```
//! # use solana_epoch_schedule::EpochSchedule;
//! # use solana_example_mocks::solana_account;
//! # use solana_example_mocks::solana_rpc_client;
//! # use solana_rpc_client::rpc_client::RpcClient;
//! # use solana_account::Account;
//! # use solana_sdk_ids::sysvar::epoch_schedule;
//! # use anyhow::Result;
//! #
//! fn print_sysvar_epoch_schedule(client: &RpcClient) -> Result<()> {
//! # client.set_get_account_response(epoch_schedule::ID, Account {
//! # lamports: 1120560,
//! # data: vec![0, 32, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
//! # owner: solana_sdk_ids::system_program::ID,
//! # executable: false,
//! # });
//! #
//! let epoch_schedule = client.get_account(&epoch_schedule::ID)?;
//! let data: EpochSchedule = bincode::deserialize(&epoch_schedule.data)?;
//!
//! Ok(())
//! }
//! #
//! # let client = RpcClient::new(String::new());
//! # print_sysvar_epoch_schedule(&client)?;
//! #
//! # Ok::<(), anyhow::Error>(())
//! ```
use crate::Sysvar;
#[cfg(feature = "bincode")]
use crate::SysvarSerialize;
pub use {
solana_epoch_schedule::EpochSchedule,
solana_sdk_ids::sysvar::epoch_schedule::{check_id, id, ID},
};
/// Pod (Plain Old Data) representation of [`EpochSchedule`] with no padding.
///
/// This type can be safely loaded via `sol_get_sysvar` without undefined behavior.
/// Provides performant zero-copy accessors as an alternative to the `EpochSchedule` type.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PodEpochSchedule {
slots_per_epoch: [u8; 8],
leader_schedule_slot_offset: [u8; 8],
warmup: u8,
first_normal_epoch: [u8; 8],
first_normal_slot: [u8; 8],
}
const POD_EPOCH_SCHEDULE_SIZE: usize = 33;
const _: () = assert!(core::mem::size_of::<PodEpochSchedule>() == POD_EPOCH_SCHEDULE_SIZE);
impl PodEpochSchedule {
/// Fetch the sysvar data using the `sol_get_sysvar` syscall.
/// This provides an alternative to `EpochSchedule` which provides zero-copy accessors.
pub fn fetch() -> Result<Self, solana_program_error::ProgramError> {
let mut pod = core::mem::MaybeUninit::<Self>::uninit();
// Safety: `get_sysvar_unchecked` will initialize `pod` with the sysvar data,
// and error if unsuccessful.
unsafe {
crate::get_sysvar_unchecked(
pod.as_mut_ptr() as *mut u8,
(&id()) as *const _ as *const u8,
0,
POD_EPOCH_SCHEDULE_SIZE as u64,
)?;
Ok(pod.assume_init())
}
}
pub fn slots_per_epoch(&self) -> u64 {
u64::from_le_bytes(self.slots_per_epoch)
}
pub fn leader_schedule_slot_offset(&self) -> u64 {
u64::from_le_bytes(self.leader_schedule_slot_offset)
}
pub fn warmup(&self) -> bool {
self.warmup != 0
}
pub fn first_normal_epoch(&self) -> u64 {
u64::from_le_bytes(self.first_normal_epoch)
}
pub fn first_normal_slot(&self) -> u64 {
u64::from_le_bytes(self.first_normal_slot)
}
}
impl From<PodEpochSchedule> for EpochSchedule {
fn from(pod: PodEpochSchedule) -> Self {
Self {
slots_per_epoch: pod.slots_per_epoch(),
leader_schedule_slot_offset: pod.leader_schedule_slot_offset(),
warmup: pod.warmup(),
first_normal_epoch: pod.first_normal_epoch(),
first_normal_slot: pod.first_normal_slot(),
}
}
}
impl Sysvar for EpochSchedule {
fn get() -> Result<Self, solana_program_error::ProgramError> {
Ok(Self::from(PodEpochSchedule::fetch()?))
}
}
#[cfg(feature = "bincode")]
impl SysvarSerialize for EpochSchedule {}
#[cfg(test)]
mod tests {
use {super::*, crate::Sysvar, serial_test::serial};
#[test]
fn test_pod_epoch_schedule_conversion() {
let pod = PodEpochSchedule {
slots_per_epoch: 432000u64.to_le_bytes(),
leader_schedule_slot_offset: 432000u64.to_le_bytes(),
warmup: 1,
first_normal_epoch: 14u64.to_le_bytes(),
first_normal_slot: 524256u64.to_le_bytes(),
};
let epoch_schedule = EpochSchedule::from(pod);
assert_eq!(epoch_schedule.slots_per_epoch, 432000);
assert_eq!(epoch_schedule.leader_schedule_slot_offset, 432000);
assert!(epoch_schedule.warmup);
assert_eq!(epoch_schedule.first_normal_epoch, 14);
assert_eq!(epoch_schedule.first_normal_slot, 524256);
}
#[test]
#[serial]
#[cfg(feature = "bincode")]
fn test_epoch_schedule_get() {
let expected = EpochSchedule::custom(1234, 5678, false);
let data = bincode::serialize(&expected).unwrap();
assert_eq!(data.len(), 33);
crate::tests::mock_get_sysvar_syscall(&data);
let got = EpochSchedule::get().unwrap();
assert_eq!(got, expected);
}
}