sysvar_last_restart_slot.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. use {
  2. solana_account_info::AccountInfo,
  3. solana_clock::Slot,
  4. solana_instruction::{AccountMeta, Instruction},
  5. solana_msg::msg,
  6. solana_program_error::ProgramResult,
  7. solana_program_test::{processor, ProgramTest, ProgramTestContext},
  8. solana_pubkey::Pubkey,
  9. solana_signer::Signer,
  10. solana_sysvar::{
  11. last_restart_slot, last_restart_slot::LastRestartSlot, Sysvar, SysvarSerialize,
  12. },
  13. solana_transaction::Transaction,
  14. };
  15. // program to check both syscall and sysvar
  16. fn sysvar_last_restart_slot_process_instruction(
  17. _program_id: &Pubkey,
  18. accounts: &[AccountInfo],
  19. input: &[u8],
  20. ) -> ProgramResult {
  21. msg!("sysvar_last_restart_slot");
  22. assert_eq!(input.len(), 8);
  23. let expected_last_hardfork_slot = u64::from_le_bytes(input[0..8].try_into().unwrap());
  24. let last_restart_slot = LastRestartSlot::get();
  25. msg!("last restart slot: {:?}", last_restart_slot);
  26. assert_eq!(
  27. last_restart_slot,
  28. Ok(LastRestartSlot {
  29. last_restart_slot: expected_last_hardfork_slot
  30. })
  31. );
  32. let last_restart_slot_account = &accounts[0];
  33. let slot_via_account = LastRestartSlot::from_account_info(last_restart_slot_account)?;
  34. msg!("slot via account: {:?}", slot_via_account);
  35. assert_eq!(
  36. slot_via_account,
  37. LastRestartSlot {
  38. last_restart_slot: expected_last_hardfork_slot
  39. }
  40. );
  41. Ok(())
  42. }
  43. async fn check_with_program(
  44. context: &mut ProgramTestContext,
  45. program_id: Pubkey,
  46. expected_last_restart_slot: u64,
  47. ) {
  48. let instructions = vec![Instruction::new_with_bincode(
  49. program_id,
  50. &expected_last_restart_slot.to_le_bytes(),
  51. vec![AccountMeta::new(last_restart_slot::id(), false)],
  52. )];
  53. let transaction = Transaction::new_signed_with_payer(
  54. &instructions,
  55. Some(&context.payer.pubkey()),
  56. &[&context.payer],
  57. context.last_blockhash,
  58. );
  59. context
  60. .banks_client
  61. .process_transaction(transaction)
  62. .await
  63. .unwrap();
  64. }
  65. #[tokio::test]
  66. async fn get_sysvar_last_restart_slot() {
  67. let program_id = Pubkey::new_unique();
  68. let program_test = ProgramTest::new(
  69. "sysvar_last_restart_slot_process",
  70. program_id,
  71. processor!(sysvar_last_restart_slot_process_instruction),
  72. );
  73. let mut context = program_test.start_with_context().await;
  74. check_with_program(&mut context, program_id, 0).await;
  75. context.warp_to_slot(40).unwrap();
  76. context.register_hard_fork(41 as Slot);
  77. check_with_program(&mut context, program_id, 0).await;
  78. context.warp_to_slot(41).unwrap();
  79. check_with_program(&mut context, program_id, 41).await;
  80. // check for value lower than previous hardfork
  81. context.register_hard_fork(40 as Slot);
  82. context.warp_to_slot(45).unwrap();
  83. check_with_program(&mut context, program_id, 41).await;
  84. context.register_hard_fork(47 as Slot);
  85. context.register_hard_fork(48 as Slot);
  86. context.warp_to_slot(46).unwrap();
  87. check_with_program(&mut context, program_id, 41).await;
  88. context.register_hard_fork(50 as Slot);
  89. context.warp_to_slot(48).unwrap();
  90. check_with_program(&mut context, program_id, 48).await;
  91. context.warp_to_slot(50).unwrap();
  92. check_with_program(&mut context, program_id, 50).await;
  93. }