solana_program/sysvar/epoch_schedule.rs
1//! Information about epoch duration.
2//!
3//! The _epoch schedule_ sysvar provides access to the [`EpochSchedule`] type,
4//! which includes the number of slots per epoch, timing of leader schedule
5//! selection, and information about epoch warm-up time.
6//!
7//! [`EpochSchedule`] implements [`Sysvar::get`] and can be loaded efficiently without
8//! passing the sysvar account ID to the program.
9//!
10//! See also the Solana [documentation on the epoch schedule sysvar][sdoc].
11//!
12//! [sdoc]: https://docs.solanalabs.com/runtime/sysvars#epochschedule
13//!
14//! # Examples
15//!
16//! Accessing via on-chain program directly:
17//!
18//! ```no_run
19//! # use solana_program::{
20//! # account_info::{AccountInfo, next_account_info},
21//! # entrypoint::ProgramResult,
22//! # msg,
23//! # pubkey::Pubkey,
24//! # sysvar::epoch_schedule::{self, EpochSchedule},
25//! # sysvar::Sysvar,
26//! # };
27//! # use solana_program::program_error::ProgramError;
28//! #
29//! fn process_instruction(
30//! program_id: &Pubkey,
31//! accounts: &[AccountInfo],
32//! instruction_data: &[u8],
33//! ) -> ProgramResult {
34//!
35//! let epoch_schedule = EpochSchedule::get()?;
36//! msg!("epoch_schedule: {:#?}", epoch_schedule);
37//!
38//! Ok(())
39//! }
40//! #
41//! # use solana_sysvar_id::SysvarId;
42//! # let p = EpochSchedule::id();
43//! # let l = &mut 1120560;
44//! # 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];
45//! # let a = AccountInfo::new(&p, false, false, l, d, &p, false, 0);
46//! # let accounts = &[a.clone(), a];
47//! # process_instruction(
48//! # &Pubkey::new_unique(),
49//! # accounts,
50//! # &[],
51//! # )?;
52//! # Ok::<(), ProgramError>(())
53//! ```
54//!
55//! Accessing via on-chain program's account parameters:
56//!
57//! ```
58//! # use solana_program::{
59//! # account_info::{AccountInfo, next_account_info},
60//! # entrypoint::ProgramResult,
61//! # msg,
62//! # pubkey::Pubkey,
63//! # sysvar::epoch_schedule::{self, EpochSchedule},
64//! # sysvar::Sysvar,
65//! # };
66//! # use solana_program::program_error::ProgramError;
67//! #
68//! fn process_instruction(
69//! program_id: &Pubkey,
70//! accounts: &[AccountInfo],
71//! instruction_data: &[u8],
72//! ) -> ProgramResult {
73//! let account_info_iter = &mut accounts.iter();
74//! let epoch_schedule_account_info = next_account_info(account_info_iter)?;
75//!
76//! assert!(epoch_schedule::check_id(epoch_schedule_account_info.key));
77//!
78//! let epoch_schedule = EpochSchedule::from_account_info(epoch_schedule_account_info)?;
79//! msg!("epoch_schedule: {:#?}", epoch_schedule);
80//!
81//! Ok(())
82//! }
83//! #
84//! # use solana_sysvar_id::SysvarId;
85//! # let p = EpochSchedule::id();
86//! # let l = &mut 1120560;
87//! # 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];
88//! # let a = AccountInfo::new(&p, false, false, l, d, &p, false, 0);
89//! # let accounts = &[a.clone(), a];
90//! # process_instruction(
91//! # &Pubkey::new_unique(),
92//! # accounts,
93//! # &[],
94//! # )?;
95//! # Ok::<(), ProgramError>(())
96//! ```
97//!
98//! Accessing via the RPC client:
99//!
100//! ```
101//! # use solana_program::example_mocks::solana_sdk;
102//! # use solana_program::example_mocks::solana_rpc_client;
103//! # use solana_sdk::account::Account;
104//! # use solana_rpc_client::rpc_client::RpcClient;
105//! # use solana_sdk::sysvar::epoch_schedule::{self, EpochSchedule};
106//! # use anyhow::Result;
107//! #
108//! fn print_sysvar_epoch_schedule(client: &RpcClient) -> Result<()> {
109//! # client.set_get_account_response(epoch_schedule::ID, Account {
110//! # lamports: 1120560,
111//! # 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],
112//! # owner: solana_sdk::system_program::ID,
113//! # executable: false,
114//! # rent_epoch: 307,
115//! # });
116//! #
117//! let epoch_schedule = client.get_account(&epoch_schedule::ID)?;
118//! let data: EpochSchedule = bincode::deserialize(&epoch_schedule.data)?;
119//!
120//! Ok(())
121//! }
122//! #
123//! # let client = RpcClient::new(String::new());
124//! # print_sysvar_epoch_schedule(&client)?;
125//! #
126//! # Ok::<(), anyhow::Error>(())
127//! ```
128use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
129pub use solana_epoch_schedule::{
130 sysvar::{check_id, id, ID},
131 EpochSchedule,
132};
133
134impl Sysvar for EpochSchedule {
135 impl_sysvar_get!(sol_get_epoch_schedule_sysvar);
136}