Skip to content

Commit

Permalink
reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Aug 4, 2023
1 parent 19b57f5 commit 1f05ac6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 163 deletions.
31 changes: 22 additions & 9 deletions program-test/tests/stake.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#![allow(clippy::integer_arithmetic)]

mod test_utils;
mod setup;

use {
setup::{setup_stake, setup_vote},
solana_program_test::ProgramTest,
solana_sdk::{
instruction::InstructionError,
signature::{Keypair, Signer},
stake::instruction as stake_instruction,
transaction::Transaction,
stake::{instruction as stake_instruction, instruction::StakeError},
transaction::{Transaction, TransactionError},
},
test_utils::{setup_stake, setup_vote, setup_vote_no_warp},
};

#[derive(PartialEq)]
Expand All @@ -24,6 +25,7 @@ async fn test_stake_redelegation_pending_activation(merge_flag: PendingStakeActi
let mut context = program_test.start_with_context().await;

// 1. create first vote accounts
context.warp_to_slot(100).unwrap();
let vote_address = setup_vote(&mut context).await;

// 1.1 advance to normal epoch
Expand Down Expand Up @@ -58,10 +60,13 @@ async fn test_stake_redelegation_pending_activation(merge_flag: PendingStakeActi
context.last_blockhash,
);
let r = context.banks_client.process_transaction(transaction).await;
assert!(format!("{}", r.err().unwrap()).contains("insufficient funds for instruction"));
assert_eq!(
r.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InsufficientFunds)
);

// 3. create 2nd vote account
let vote_address2 = setup_vote_no_warp(&mut context).await;
let vote_address2 = setup_vote(&mut context).await;

// 3.1 relegate 1st stake account to 2nd stake account.
let stake_keypair2 = Keypair::new();
Expand Down Expand Up @@ -132,8 +137,13 @@ async fn test_stake_redelegation_pending_activation(merge_flag: PendingStakeActi
);
let r = context.banks_client.process_transaction(transaction).await;
assert_eq!(
format!("{}", r.err().unwrap()),
"transport transaction error: Error processing Instruction 0: custom program error: 0xf"
r.unwrap_err().unwrap(),
TransactionError::InstructionError(
0,
InstructionError::Custom(
StakeError::RedelegatedStakeMustFullyActivateBeforeDeactivationIsPermitted as u32
)
)
);

// 3.4 withdraw from 2nd stake account should also fail because of pending stake activation.
Expand All @@ -150,7 +160,10 @@ async fn test_stake_redelegation_pending_activation(merge_flag: PendingStakeActi
context.last_blockhash,
);
let r = context.banks_client.process_transaction(transaction).await;
assert!(format!("{}", r.err().unwrap()).contains("insufficient funds for instruction"));
assert_eq!(
r.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InsufficientFunds)
);

// 4. advance to new epoch so that the 2nd stake account is fully activated
current_slot += slots_per_epoch;
Expand Down
150 changes: 0 additions & 150 deletions program-test/tests/test_utils.rs

This file was deleted.

52 changes: 48 additions & 4 deletions program-test/tests/warp.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#![allow(clippy::integer_arithmetic)]

mod test_utils;
mod setup;

use {
bincode::deserialize,
log::debug,
setup::{setup_stake, setup_vote},
solana_banks_client::BanksClient,
solana_program_test::{processor, ProgramTest, ProgramTestBanksClientExt, ProgramTestError},
solana_sdk::{
account::Account,
account_info::{next_account_info, AccountInfo},
clock::Clock,
entrypoint::ProgramResult,
instruction::{AccountMeta, Instruction, InstructionError},
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
signature::{Keypair, Signer},
Expand All @@ -20,16 +25,34 @@ use {
sysvar::{
clock,
stake_history::{self, StakeHistory},
Sysvar,
},
transaction::{Transaction, TransactionError},
},
solana_stake_program::stake_state,
solana_vote_program::vote_state::{self},
test_utils::{
check_credits_observed, process_instruction, setup_stake, setup_vote, WRONG_SLOT_ERROR,
},
std::convert::TryInto,
};

// Use a big number to be sure that we get the right error
const WRONG_SLOT_ERROR: u32 = 123456;

fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
input: &[u8],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let clock_info = next_account_info(account_info_iter)?;
let clock = &Clock::from_account_info(clock_info)?;
let expected_slot = u64::from_le_bytes(input.try_into().unwrap());
if clock.slot == expected_slot {
Ok(())
} else {
Err(ProgramError::Custom(WRONG_SLOT_ERROR))
}
}

#[tokio::test]
async fn clock_sysvar_updated_from_warp() {
let program_id = Pubkey::new_unique();
Expand Down Expand Up @@ -111,11 +134,30 @@ async fn clock_sysvar_updated_from_warp() {
);
}

async fn check_credits_observed(
banks_client: &mut BanksClient,
stake_address: Pubkey,
expected_credits: u64,
) {
let stake_account = banks_client
.get_account(stake_address)
.await
.unwrap()
.unwrap();
let stake_state: StakeState = deserialize(&stake_account.data).unwrap();
assert_eq!(
stake_state.stake().unwrap().credits_observed,
expected_credits
);
}

#[tokio::test]
async fn stake_rewards_from_warp() {
// Initialize and start the test network
let program_test = ProgramTest::default();
let mut context = program_test.start_with_context().await;

context.warp_to_slot(100).unwrap();
let vote_address = setup_vote(&mut context).await;

let user_keypair = Keypair::new();
Expand Down Expand Up @@ -305,6 +347,8 @@ async fn stake_rewards_filter_bench_core(num_stake_accounts: u64) {
async fn stake_merge_immediately_after_activation() {
let program_test = ProgramTest::default();
let mut context = program_test.start_with_context().await;

context.warp_to_slot(100).unwrap();
let vote_address = setup_vote(&mut context).await;
context.increment_vote_account_credits(&vote_address, 100);

Expand Down

0 comments on commit 1f05ac6

Please sign in to comment.