warp.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #![allow(clippy::arithmetic_side_effects)]
  2. mod setup;
  3. use {
  4. bincode::deserialize,
  5. log::debug,
  6. setup::{setup_stake, setup_vote},
  7. solana_account::Account,
  8. solana_account_info::{next_account_info, AccountInfo},
  9. solana_banks_client::BanksClient,
  10. solana_clock::Clock,
  11. solana_instruction::{error::InstructionError, AccountMeta, Instruction},
  12. solana_keypair::Keypair,
  13. solana_program_error::{ProgramError, ProgramResult},
  14. solana_program_test::{processor, ProgramTest, ProgramTestBanksClientExt, ProgramTestError},
  15. solana_pubkey::Pubkey,
  16. solana_rent::Rent,
  17. solana_signer::Signer,
  18. solana_stake_interface::{
  19. instruction as stake_instruction,
  20. state::{StakeActivationStatus, StakeStateV2},
  21. },
  22. solana_stake_program::stake_state,
  23. solana_sysvar::{
  24. clock,
  25. stake_history::{self, StakeHistory},
  26. Sysvar,
  27. },
  28. solana_transaction::Transaction,
  29. solana_transaction_error::TransactionError,
  30. solana_vote_program::vote_state,
  31. std::convert::TryInto,
  32. };
  33. // Use a big number to be sure that we get the right error
  34. const WRONG_SLOT_ERROR: u32 = 123456;
  35. fn process_instruction(
  36. _program_id: &Pubkey,
  37. accounts: &[AccountInfo],
  38. input: &[u8],
  39. ) -> ProgramResult {
  40. let account_info_iter = &mut accounts.iter();
  41. let clock_info = next_account_info(account_info_iter)?;
  42. let clock = &Clock::from_account_info(clock_info)?;
  43. let expected_slot = u64::from_le_bytes(input.try_into().unwrap());
  44. if clock.slot == expected_slot {
  45. Ok(())
  46. } else {
  47. Err(ProgramError::Custom(WRONG_SLOT_ERROR))
  48. }
  49. }
  50. #[tokio::test]
  51. async fn clock_sysvar_updated_from_warp() {
  52. let program_id = Pubkey::new_unique();
  53. // Initialize and start the test network
  54. let program_test = ProgramTest::new(
  55. "program-test-warp",
  56. program_id,
  57. processor!(process_instruction),
  58. );
  59. let mut context = program_test.start_with_context().await;
  60. let mut expected_slot = 100_000;
  61. let instruction = Instruction::new_with_bincode(
  62. program_id,
  63. &expected_slot,
  64. vec![AccountMeta::new_readonly(clock::id(), false)],
  65. );
  66. // Fail transaction
  67. let transaction = Transaction::new_signed_with_payer(
  68. &[instruction.clone()],
  69. Some(&context.payer.pubkey()),
  70. &[&context.payer],
  71. context.last_blockhash,
  72. );
  73. assert_eq!(
  74. context
  75. .banks_client
  76. .process_transaction(transaction)
  77. .await
  78. .unwrap_err()
  79. .unwrap(),
  80. TransactionError::InstructionError(0, InstructionError::Custom(WRONG_SLOT_ERROR))
  81. );
  82. // Warp to success!
  83. context.warp_to_slot(expected_slot).unwrap();
  84. let instruction = Instruction::new_with_bincode(
  85. program_id,
  86. &expected_slot,
  87. vec![AccountMeta::new_readonly(clock::id(), false)],
  88. );
  89. let transaction = Transaction::new_signed_with_payer(
  90. &[instruction],
  91. Some(&context.payer.pubkey()),
  92. &[&context.payer],
  93. context.last_blockhash,
  94. );
  95. context
  96. .banks_client
  97. .process_transaction(transaction)
  98. .await
  99. .unwrap();
  100. // Try warping ahead one slot (corner case in warp logic)
  101. expected_slot += 1;
  102. assert!(context.warp_to_slot(expected_slot).is_ok());
  103. let instruction = Instruction::new_with_bincode(
  104. program_id,
  105. &expected_slot,
  106. vec![AccountMeta::new_readonly(clock::id(), false)],
  107. );
  108. let transaction = Transaction::new_signed_with_payer(
  109. &[instruction],
  110. Some(&context.payer.pubkey()),
  111. &[&context.payer],
  112. context.last_blockhash,
  113. );
  114. context
  115. .banks_client
  116. .process_transaction(transaction)
  117. .await
  118. .unwrap();
  119. // Try warping again to the same slot
  120. assert_eq!(
  121. context.warp_to_slot(expected_slot).unwrap_err(),
  122. ProgramTestError::InvalidWarpSlot,
  123. );
  124. }
  125. #[tokio::test]
  126. async fn stake_rewards_from_warp() {
  127. // Initialize and start the test network
  128. let program_test = ProgramTest::default();
  129. let mut context = program_test.start_with_context().await;
  130. context.warp_to_slot(100).unwrap();
  131. let vote_address = setup_vote(&mut context).await;
  132. let user_keypair = Keypair::new();
  133. let stake_lamports = 1_000_000_000_000;
  134. let stake_address =
  135. setup_stake(&mut context, &user_keypair, &vote_address, stake_lamports).await;
  136. let account = context
  137. .banks_client
  138. .get_account(stake_address)
  139. .await
  140. .expect("account exists")
  141. .unwrap();
  142. assert_eq!(account.lamports, stake_lamports);
  143. // warp one epoch forward for normal inflation, no rewards collected
  144. let first_normal_slot = context.genesis_config().epoch_schedule.first_normal_slot;
  145. context.warp_to_slot(first_normal_slot).unwrap();
  146. let account = context
  147. .banks_client
  148. .get_account(stake_address)
  149. .await
  150. .expect("account exists")
  151. .unwrap();
  152. assert_eq!(account.lamports, stake_lamports);
  153. context.increment_vote_account_credits(&vote_address, 100);
  154. // go forward and see that rewards have been distributed
  155. let slots_per_epoch = context.genesis_config().epoch_schedule.slots_per_epoch;
  156. context
  157. .warp_to_slot(first_normal_slot + slots_per_epoch + 1) // when partitioned rewards are enabled, the rewards are paid at 1 slot after the first slot of the epoch
  158. .unwrap();
  159. let account = context
  160. .banks_client
  161. .get_account(stake_address)
  162. .await
  163. .expect("account exists")
  164. .unwrap();
  165. assert!(account.lamports > stake_lamports);
  166. // check that stake is fully active
  167. let stake_history_account = context
  168. .banks_client
  169. .get_account(stake_history::id())
  170. .await
  171. .expect("account exists")
  172. .unwrap();
  173. let clock_account = context
  174. .banks_client
  175. .get_account(clock::id())
  176. .await
  177. .expect("account exists")
  178. .unwrap();
  179. let stake_state: StakeStateV2 = deserialize(&account.data).unwrap();
  180. let stake_history: StakeHistory = deserialize(&stake_history_account.data).unwrap();
  181. let clock: Clock = deserialize(&clock_account.data).unwrap();
  182. let stake = stake_state.stake().unwrap();
  183. assert_eq!(
  184. stake
  185. .delegation
  186. .stake_activating_and_deactivating(clock.epoch, &stake_history, None),
  187. StakeActivationStatus::with_effective(stake.delegation.stake),
  188. );
  189. }
  190. #[tokio::test]
  191. async fn stake_rewards_filter_bench_100() {
  192. stake_rewards_filter_bench_core(100).await;
  193. }
  194. async fn stake_rewards_filter_bench_core(num_stake_accounts: u64) {
  195. // Initialize and start the test network
  196. let mut program_test = ProgramTest::default();
  197. // create vote account
  198. let vote_address = Pubkey::new_unique();
  199. let node_address = Pubkey::new_unique();
  200. let vote_account = vote_state::create_account(&vote_address, &node_address, 0, 1_000_000_000);
  201. program_test.add_account(vote_address, vote_account.clone().into());
  202. // create stake accounts with 0.9 sol to test min-stake filtering
  203. const TEST_FILTER_STAKE: u64 = 900_000_000; // 0.9 sol
  204. let mut to_filter = vec![];
  205. for i in 0..num_stake_accounts {
  206. let stake_pubkey = Pubkey::new_unique();
  207. let stake_account = Account::from(stake_state::create_account(
  208. &stake_pubkey,
  209. &vote_address,
  210. &vote_account,
  211. &Rent::default(),
  212. TEST_FILTER_STAKE,
  213. ));
  214. program_test.add_account(stake_pubkey, stake_account);
  215. to_filter.push(stake_pubkey);
  216. if i % 100 == 0 {
  217. debug!("create stake account {i} {stake_pubkey}");
  218. }
  219. }
  220. let mut context = program_test.start_with_context().await;
  221. let stake_lamports = 2_000_000_000_000;
  222. let user_keypair = Keypair::new();
  223. let stake_address =
  224. setup_stake(&mut context, &user_keypair, &vote_address, stake_lamports).await;
  225. let account = context
  226. .banks_client
  227. .get_account(stake_address)
  228. .await
  229. .expect("account exists")
  230. .unwrap();
  231. assert_eq!(account.lamports, stake_lamports);
  232. // warp one epoch forward for normal inflation, no rewards collected
  233. let first_normal_slot = context.genesis_config().epoch_schedule.first_normal_slot;
  234. context.warp_to_slot(first_normal_slot).unwrap();
  235. let account = context
  236. .banks_client
  237. .get_account(stake_address)
  238. .await
  239. .expect("account exists")
  240. .unwrap();
  241. assert_eq!(account.lamports, stake_lamports);
  242. context.increment_vote_account_credits(&vote_address, 100);
  243. // go forward and see that rewards have been distributed
  244. let slots_per_epoch = context.genesis_config().epoch_schedule.slots_per_epoch;
  245. context
  246. .warp_to_slot(first_normal_slot + slots_per_epoch + 1) // when partitioned rewards are enabled, the rewards are paid at 1 slot after the first slot of the epoch
  247. .unwrap();
  248. let account = context
  249. .banks_client
  250. .get_account(stake_address)
  251. .await
  252. .expect("account exists")
  253. .unwrap();
  254. assert!(account.lamports > stake_lamports);
  255. // check that filtered stake accounts are excluded from receiving epoch rewards
  256. for stake_address in to_filter {
  257. let account = context
  258. .banks_client
  259. .get_account(stake_address)
  260. .await
  261. .expect("account exists")
  262. .unwrap();
  263. assert_eq!(account.lamports, TEST_FILTER_STAKE);
  264. }
  265. // check that stake is fully active
  266. let stake_history_account = context
  267. .banks_client
  268. .get_account(stake_history::id())
  269. .await
  270. .expect("account exists")
  271. .unwrap();
  272. let clock_account = context
  273. .banks_client
  274. .get_account(clock::id())
  275. .await
  276. .expect("account exists")
  277. .unwrap();
  278. let stake_state: StakeStateV2 = deserialize(&account.data).unwrap();
  279. let stake_history: StakeHistory = deserialize(&stake_history_account.data).unwrap();
  280. let clock: Clock = deserialize(&clock_account.data).unwrap();
  281. let stake = stake_state.stake().unwrap();
  282. assert_eq!(
  283. stake
  284. .delegation
  285. .stake_activating_and_deactivating(clock.epoch, &stake_history, None),
  286. StakeActivationStatus::with_effective(stake.delegation.stake),
  287. );
  288. }
  289. async fn check_credits_observed(
  290. banks_client: &mut BanksClient,
  291. stake_address: Pubkey,
  292. expected_credits: u64,
  293. ) {
  294. let stake_account = banks_client
  295. .get_account(stake_address)
  296. .await
  297. .unwrap()
  298. .unwrap();
  299. let stake_state: StakeStateV2 = deserialize(&stake_account.data).unwrap();
  300. assert_eq!(
  301. stake_state.stake().unwrap().credits_observed,
  302. expected_credits
  303. );
  304. }
  305. #[tokio::test]
  306. async fn stake_merge_immediately_after_activation() {
  307. let program_test = ProgramTest::default();
  308. let mut context = program_test.start_with_context().await;
  309. context.warp_to_slot(100).unwrap();
  310. let vote_address = setup_vote(&mut context).await;
  311. context.increment_vote_account_credits(&vote_address, 100);
  312. let first_normal_slot = context.genesis_config().epoch_schedule.first_normal_slot;
  313. let slots_per_epoch = context.genesis_config().epoch_schedule.slots_per_epoch;
  314. let mut current_slot = first_normal_slot + slots_per_epoch;
  315. context.warp_to_slot(current_slot).unwrap();
  316. context.warp_forward_force_reward_interval_end().unwrap();
  317. // this is annoying, but if no stake has earned rewards, the bank won't
  318. // iterate through the stakes at all, which means we can only test the
  319. // behavior of advancing credits observed if another stake is earning rewards
  320. // make a base stake which receives rewards
  321. let user_keypair = Keypair::new();
  322. let stake_lamports = 1_000_000_000_000;
  323. let base_stake_address =
  324. setup_stake(&mut context, &user_keypair, &vote_address, stake_lamports).await;
  325. check_credits_observed(&mut context.banks_client, base_stake_address, 100).await;
  326. context.increment_vote_account_credits(&vote_address, 100);
  327. let clock_account = context
  328. .banks_client
  329. .get_account(clock::id())
  330. .await
  331. .expect("account exists")
  332. .unwrap();
  333. let clock: Clock = deserialize(&clock_account.data).unwrap();
  334. context.warp_to_epoch(clock.epoch + 1).unwrap();
  335. current_slot += slots_per_epoch;
  336. context.warp_forward_force_reward_interval_end().unwrap();
  337. // make another stake which will just have its credits observed advanced
  338. let absorbed_stake_address =
  339. setup_stake(&mut context, &user_keypair, &vote_address, stake_lamports).await;
  340. // the new stake is at the right value
  341. check_credits_observed(&mut context.banks_client, absorbed_stake_address, 200).await;
  342. // the base stake hasn't been moved forward because no rewards were earned
  343. check_credits_observed(&mut context.banks_client, base_stake_address, 100).await;
  344. context.increment_vote_account_credits(&vote_address, 100);
  345. current_slot += slots_per_epoch;
  346. context.warp_to_slot(current_slot).unwrap();
  347. context.warp_forward_force_reward_interval_end().unwrap();
  348. // check that base stake has earned rewards and credits moved forward
  349. let stake_account = context
  350. .banks_client
  351. .get_account(base_stake_address)
  352. .await
  353. .unwrap()
  354. .unwrap();
  355. let stake_state: StakeStateV2 = deserialize(&stake_account.data).unwrap();
  356. assert_eq!(stake_state.stake().unwrap().credits_observed, 300);
  357. assert!(stake_account.lamports > stake_lamports);
  358. // check that new stake hasn't earned rewards, but that credits_observed have been advanced
  359. let stake_account = context
  360. .banks_client
  361. .get_account(absorbed_stake_address)
  362. .await
  363. .unwrap()
  364. .unwrap();
  365. let stake_state: StakeStateV2 = deserialize(&stake_account.data).unwrap();
  366. assert_eq!(stake_state.stake().unwrap().credits_observed, 300);
  367. assert_eq!(stake_account.lamports, stake_lamports);
  368. // sanity-check that the activation epoch was actually last epoch
  369. let clock_account = context
  370. .banks_client
  371. .get_account(clock::id())
  372. .await
  373. .unwrap()
  374. .unwrap();
  375. let clock: Clock = deserialize(&clock_account.data).unwrap();
  376. assert_eq!(
  377. clock.epoch,
  378. stake_state.delegation().unwrap().activation_epoch + 1
  379. );
  380. // sanity-check that it's possible to merge the just-activated stake with the older stake!
  381. let transaction = Transaction::new_signed_with_payer(
  382. &stake_instruction::merge(
  383. &base_stake_address,
  384. &absorbed_stake_address,
  385. &user_keypair.pubkey(),
  386. ),
  387. Some(&context.payer.pubkey()),
  388. &vec![&context.payer, &user_keypair],
  389. context.last_blockhash,
  390. );
  391. context
  392. .banks_client
  393. .process_transaction(transaction)
  394. .await
  395. .unwrap();
  396. }
  397. #[tokio::test]
  398. async fn get_blockhash_post_warp() {
  399. let program_test = ProgramTest::default();
  400. let mut context = program_test.start_with_context().await;
  401. let new_blockhash = context
  402. .banks_client
  403. .get_new_latest_blockhash(&context.last_blockhash)
  404. .await
  405. .unwrap();
  406. let mut tx = Transaction::new_with_payer(&[], Some(&context.payer.pubkey()));
  407. tx.sign(&[&context.payer], new_blockhash);
  408. context.banks_client.process_transaction(tx).await.unwrap();
  409. context.warp_to_slot(10).unwrap();
  410. let new_blockhash = context
  411. .banks_client
  412. .get_new_latest_blockhash(&context.last_blockhash)
  413. .await
  414. .unwrap();
  415. let mut tx = Transaction::new_with_payer(&[], Some(&context.payer.pubkey()));
  416. tx.sign(&[&context.payer], new_blockhash);
  417. context.banks_client.process_transaction(tx).await.unwrap();
  418. }