warp.rs 16 KB

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