rialo_s_program_runtime/
sysvar_cache.rs

1use rialo_s_clock::Clock;
2use rialo_s_epoch_schedule::EpochSchedule;
3use rialo_s_instruction::error::InstructionError;
4use rialo_s_pubkey::Pubkey;
5use rialo_s_rent::Rent;
6use rialo_s_sdk_ids::sysvar;
7#[allow(deprecated)]
8use rialo_s_sysvar::recent_blockhashes::RecentBlockhashes;
9use rialo_s_sysvar::Sysvar;
10use rialo_s_sysvar_id::SysvarId;
11use rialo_s_transaction_context::{IndexOfAccount, InstructionContext, TransactionContext};
12use rialo_s_type_overrides::sync::Arc;
13use serde::de::DeserializeOwned;
14
15use crate::invoke_context::InvokeContext;
16
17#[cfg(feature = "frozen-abi")]
18impl ::rialo_frozen_abi::abi_example::AbiExample for SysvarCache {
19    fn example() -> Self {
20        // SysvarCache is not Serialize so just rely on Default.
21        SysvarCache::default()
22    }
23}
24
25#[derive(Default, Clone, Debug)]
26pub struct SysvarCache {
27    // full account data as provided by bank, including any trailing zero bytes
28    clock: Option<Vec<u8>>,
29    epoch_schedule: Option<Vec<u8>>,
30    rent: Option<Vec<u8>>,
31
32    #[allow(deprecated)]
33    recent_blockhashes: Option<RecentBlockhashes>,
34}
35
36const RECENT_BLOCKHASHES_ID: Pubkey =
37    Pubkey::from_str_const("SysvarRecentB1ockHashes11111111111111111111");
38
39impl SysvarCache {
40    /// Overwrite a sysvar. For testing purposes only.
41    #[allow(deprecated)]
42    pub fn set_sysvar_for_tests<T: Sysvar + SysvarId>(&mut self, sysvar: &T) {
43        let data = bincode::serialize(sysvar).expect("Failed to serialize sysvar.");
44        let sysvar_id = T::id();
45        match sysvar_id {
46            sysvar::clock::ID => {
47                self.clock = Some(data);
48            }
49            sysvar::epoch_schedule::ID => {
50                self.epoch_schedule = Some(data);
51            }
52            RECENT_BLOCKHASHES_ID => {
53                let recent_blockhashes: RecentBlockhashes = bincode::deserialize(&data)
54                    .expect("Failed to deserialize RecentBlockhashes sysvar.");
55                self.recent_blockhashes = Some(recent_blockhashes);
56            }
57            sysvar::rent::ID => {
58                self.rent = Some(data);
59            }
60            _ => panic!("Unrecognized Sysvar ID: {sysvar_id}"),
61        }
62    }
63
64    // this is exposed for SyscallGetSysvar and should not otherwise be used
65    pub fn sysvar_id_to_buffer(&self, sysvar_id: &Pubkey) -> &Option<Vec<u8>> {
66        if Clock::check_id(sysvar_id) {
67            &self.clock
68        } else if EpochSchedule::check_id(sysvar_id) {
69            &self.epoch_schedule
70        } else if Rent::check_id(sysvar_id) {
71            &self.rent
72        } else {
73            &None
74        }
75    }
76
77    // most if not all of the obj getter functions can be removed once builtins transition to bpf
78    // the Arc<T> wrapper is to preserve the existing public interface
79    fn get_sysvar_obj<T: DeserializeOwned>(
80        &self,
81        sysvar_id: &Pubkey,
82    ) -> Result<Arc<T>, InstructionError> {
83        if let Some(ref sysvar_buf) = self.sysvar_id_to_buffer(sysvar_id) {
84            bincode::deserialize(sysvar_buf)
85                .map(Arc::new)
86                .map_err(|_| InstructionError::UnsupportedSysvar)
87        } else {
88            Err(InstructionError::UnsupportedSysvar)
89        }
90    }
91
92    pub fn get_clock(&self) -> Result<Arc<Clock>, InstructionError> {
93        self.get_sysvar_obj(&Clock::id())
94    }
95
96    pub fn get_epoch_schedule(&self) -> Result<Arc<EpochSchedule>, InstructionError> {
97        self.get_sysvar_obj(&EpochSchedule::id())
98    }
99
100    pub fn get_rent(&self) -> Result<Arc<Rent>, InstructionError> {
101        self.get_sysvar_obj(&Rent::id())
102    }
103
104    #[deprecated]
105    #[allow(deprecated)]
106    pub fn get_recent_blockhashes(&self) -> Result<Arc<RecentBlockhashes>, InstructionError> {
107        self.recent_blockhashes
108            .clone()
109            .ok_or(InstructionError::UnsupportedSysvar)
110            .map(Arc::new)
111    }
112
113    pub fn fill_missing_entries<F: FnMut(&Pubkey, &mut dyn FnMut(&[u8]))>(
114        &mut self,
115        mut get_account_data: F,
116    ) {
117        if self.clock.is_none() {
118            get_account_data(&Clock::id(), &mut |data: &[u8]| {
119                if bincode::deserialize::<Clock>(data).is_ok() {
120                    self.clock = Some(data.to_vec());
121                }
122            });
123        }
124
125        if self.epoch_schedule.is_none() {
126            get_account_data(&EpochSchedule::id(), &mut |data: &[u8]| {
127                if bincode::deserialize::<EpochSchedule>(data).is_ok() {
128                    self.epoch_schedule = Some(data.to_vec());
129                }
130            });
131        }
132
133        if self.rent.is_none() {
134            get_account_data(&Rent::id(), &mut |data: &[u8]| {
135                if bincode::deserialize::<Rent>(data).is_ok() {
136                    self.rent = Some(data.to_vec());
137                }
138            });
139        }
140
141        #[allow(deprecated)]
142        if self.recent_blockhashes.is_none() {
143            get_account_data(&RecentBlockhashes::id(), &mut |data: &[u8]| {
144                if let Ok(recent_blockhashes) = bincode::deserialize(data) {
145                    self.recent_blockhashes = Some(recent_blockhashes);
146                }
147            });
148        }
149    }
150
151    pub fn reset(&mut self) {
152        *self = Self::default();
153    }
154}
155
156/// These methods facilitate a transition from fetching sysvars from keyed
157/// accounts to fetching from the sysvar cache without breaking consensus. In
158/// order to keep consistent behavior, they continue to enforce the same checks
159/// as `rialo_s_sdk::keyed_account::from_keyed_account` despite dynamically
160/// loading them instead of deserializing from account data.
161pub mod get_sysvar_with_account_check {
162    use super::*;
163
164    fn check_sysvar_account<S: Sysvar>(
165        transaction_context: &TransactionContext,
166        instruction_context: &InstructionContext,
167        instruction_account_index: IndexOfAccount,
168    ) -> Result<(), InstructionError> {
169        let index_in_transaction = instruction_context
170            .get_index_of_instruction_account_in_transaction(instruction_account_index)?;
171        if !S::check_id(transaction_context.get_key_of_account_at_index(index_in_transaction)?) {
172            return Err(InstructionError::InvalidArgument);
173        }
174        Ok(())
175    }
176
177    pub fn clock(
178        invoke_context: &InvokeContext,
179        instruction_context: &InstructionContext,
180        instruction_account_index: IndexOfAccount,
181    ) -> Result<Arc<Clock>, InstructionError> {
182        check_sysvar_account::<Clock>(
183            invoke_context.transaction_context,
184            instruction_context,
185            instruction_account_index,
186        )?;
187        invoke_context.get_sysvar_cache().get_clock()
188    }
189
190    pub fn rent(
191        invoke_context: &InvokeContext,
192        instruction_context: &InstructionContext,
193        instruction_account_index: IndexOfAccount,
194    ) -> Result<Arc<Rent>, InstructionError> {
195        check_sysvar_account::<Rent>(
196            invoke_context.transaction_context,
197            instruction_context,
198            instruction_account_index,
199        )?;
200        invoke_context.get_sysvar_cache().get_rent()
201    }
202
203    #[allow(deprecated)]
204    pub fn recent_blockhashes(
205        invoke_context: &InvokeContext,
206        instruction_context: &InstructionContext,
207        instruction_account_index: IndexOfAccount,
208    ) -> Result<Arc<RecentBlockhashes>, InstructionError> {
209        check_sysvar_account::<RecentBlockhashes>(
210            invoke_context.transaction_context,
211            instruction_context,
212            instruction_account_index,
213        )?;
214        invoke_context.get_sysvar_cache().get_recent_blockhashes()
215    }
216}
217
218#[cfg(test)]
219mod tests {
220    use test_case::test_case;
221
222    use super::*;
223
224    // sysvar cache provides the full account data of a sysvar
225    // the setters MUST NOT be changed to serialize an object representation
226    // it is required that the syscall be able to access the full buffer as it exists onchain
227    // this is meant to cover the cases:
228    // * account data is larger than struct sysvar
229    // * vector sysvar has fewer than its maximum entries
230    // if at any point the data is roundtripped through bincode, the vector will shrink
231    #[test_case(Clock::default(); "clock")]
232    #[test_case(EpochSchedule::default(); "epoch_schedule")]
233    #[test_case(Rent::default(); "rent")]
234    fn test_sysvar_cache_preserves_bytes<T: Sysvar>(_: T) {
235        let id = T::id();
236        let size = T::size_of().saturating_mul(2);
237        let in_buf = vec![0; size];
238
239        let mut sysvar_cache = SysvarCache::default();
240        sysvar_cache.fill_missing_entries(|pubkey, callback| {
241            if *pubkey == id {
242                callback(&in_buf)
243            }
244        });
245        let sysvar_cache = sysvar_cache;
246
247        let out_buf = sysvar_cache.sysvar_id_to_buffer(&id).clone().unwrap();
248
249        assert_eq!(out_buf, in_buf);
250    }
251}