|
11 | 11 | clock::Clock, epoch_schedule::EpochSchedule, rent::Rent, slot_hashes::SlotHashes,
|
12 | 12 | stake_history::StakeHistory, Sysvar, SysvarId,
|
13 | 13 | },
|
| 14 | + transaction_context::{InstructionContext, TransactionContext}, |
14 | 15 | },
|
15 | 16 | std::sync::Arc,
|
16 | 17 | };
|
@@ -231,3 +232,86 @@ pub mod get_sysvar_with_account_check {
|
231 | 232 | invoke_context.get_sysvar_cache().get_stake_history()
|
232 | 233 | }
|
233 | 234 | }
|
| 235 | + |
| 236 | +pub mod get_sysvar_with_account_check2 { |
| 237 | + use super::*; |
| 238 | + |
| 239 | + fn check_sysvar_account<S: Sysvar>( |
| 240 | + transaction_context: &TransactionContext, |
| 241 | + instruction_context: &InstructionContext, |
| 242 | + index_in_instruction: usize, |
| 243 | + ) -> Result<(), InstructionError> { |
| 244 | + let index_in_transaction = |
| 245 | + instruction_context.get_index_in_transaction(index_in_instruction)?; |
| 246 | + if !S::check_id(transaction_context.get_key_of_account_at_index(index_in_transaction)?) { |
| 247 | + return Err(InstructionError::InvalidArgument); |
| 248 | + } |
| 249 | + Ok(()) |
| 250 | + } |
| 251 | + |
| 252 | + pub fn clock( |
| 253 | + invoke_context: &InvokeContext, |
| 254 | + instruction_context: &InstructionContext, |
| 255 | + index_in_instruction: usize, |
| 256 | + ) -> Result<Arc<Clock>, InstructionError> { |
| 257 | + check_sysvar_account::<Clock>( |
| 258 | + invoke_context.transaction_context, |
| 259 | + instruction_context, |
| 260 | + index_in_instruction, |
| 261 | + )?; |
| 262 | + invoke_context.get_sysvar_cache().get_clock() |
| 263 | + } |
| 264 | + |
| 265 | + pub fn rent( |
| 266 | + invoke_context: &InvokeContext, |
| 267 | + instruction_context: &InstructionContext, |
| 268 | + index_in_instruction: usize, |
| 269 | + ) -> Result<Arc<Rent>, InstructionError> { |
| 270 | + check_sysvar_account::<Rent>( |
| 271 | + invoke_context.transaction_context, |
| 272 | + instruction_context, |
| 273 | + index_in_instruction, |
| 274 | + )?; |
| 275 | + invoke_context.get_sysvar_cache().get_rent() |
| 276 | + } |
| 277 | + |
| 278 | + pub fn slot_hashes( |
| 279 | + invoke_context: &InvokeContext, |
| 280 | + instruction_context: &InstructionContext, |
| 281 | + index_in_instruction: usize, |
| 282 | + ) -> Result<Arc<SlotHashes>, InstructionError> { |
| 283 | + check_sysvar_account::<SlotHashes>( |
| 284 | + invoke_context.transaction_context, |
| 285 | + instruction_context, |
| 286 | + index_in_instruction, |
| 287 | + )?; |
| 288 | + invoke_context.get_sysvar_cache().get_slot_hashes() |
| 289 | + } |
| 290 | + |
| 291 | + #[allow(deprecated)] |
| 292 | + pub fn recent_blockhashes( |
| 293 | + invoke_context: &InvokeContext, |
| 294 | + instruction_context: &InstructionContext, |
| 295 | + index_in_instruction: usize, |
| 296 | + ) -> Result<Arc<RecentBlockhashes>, InstructionError> { |
| 297 | + check_sysvar_account::<RecentBlockhashes>( |
| 298 | + invoke_context.transaction_context, |
| 299 | + instruction_context, |
| 300 | + index_in_instruction, |
| 301 | + )?; |
| 302 | + invoke_context.get_sysvar_cache().get_recent_blockhashes() |
| 303 | + } |
| 304 | + |
| 305 | + pub fn stake_history( |
| 306 | + invoke_context: &InvokeContext, |
| 307 | + instruction_context: &InstructionContext, |
| 308 | + index_in_instruction: usize, |
| 309 | + ) -> Result<Arc<StakeHistory>, InstructionError> { |
| 310 | + check_sysvar_account::<StakeHistory>( |
| 311 | + invoke_context.transaction_context, |
| 312 | + instruction_context, |
| 313 | + index_in_instruction, |
| 314 | + )?; |
| 315 | + invoke_context.get_sysvar_cache().get_stake_history() |
| 316 | + } |
| 317 | +} |
0 commit comments