Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 1566564

Browse files
authored
stake-pool: Refresh blockhash more often on update tests (#3853)
* stake-pool: Refresh blockhash more often on update tests * Bump down max pool size, updated correctly in #3839 * Move withdraw test to another file * Refactor withdraw tests more * Get more blockhashes during `update_stake_pool_balance`
1 parent 7d4ab60 commit 1566564

File tree

8 files changed

+513
-463
lines changed

8 files changed

+513
-463
lines changed

stake-pool/program/tests/helpers/mod.rs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use {
1111
pubkey::Pubkey,
1212
stake, system_instruction, system_program,
1313
},
14-
solana_program_test::{processor, BanksClient, ProgramTest},
14+
solana_program_test::{processor, BanksClient, ProgramTest, ProgramTestContext},
1515
solana_sdk::{
1616
account::{Account as SolanaAccount, WritableAccount},
1717
clock::{Clock, Epoch},
@@ -2110,3 +2110,90 @@ pub fn add_token_account(
21102110
);
21112111
program_test.add_account(*account_key, fee_account);
21122112
}
2113+
2114+
pub async fn setup_for_withdraw(
2115+
token_program_id: Pubkey,
2116+
) -> (
2117+
ProgramTestContext,
2118+
StakePoolAccounts,
2119+
ValidatorStakeAccount,
2120+
DepositStakeAccount,
2121+
Keypair,
2122+
Keypair,
2123+
u64,
2124+
) {
2125+
let mut context = program_test().start_with_context().await;
2126+
let stake_pool_accounts = StakePoolAccounts::new_with_token_program(token_program_id);
2127+
stake_pool_accounts
2128+
.initialize_stake_pool(
2129+
&mut context.banks_client,
2130+
&context.payer,
2131+
&context.last_blockhash,
2132+
MINIMUM_RESERVE_LAMPORTS,
2133+
)
2134+
.await
2135+
.unwrap();
2136+
2137+
let validator_stake_account = simple_add_validator_to_pool(
2138+
&mut context.banks_client,
2139+
&context.payer,
2140+
&context.last_blockhash,
2141+
&stake_pool_accounts,
2142+
None,
2143+
)
2144+
.await;
2145+
2146+
let current_minimum_delegation = stake_pool_get_minimum_delegation(
2147+
&mut context.banks_client,
2148+
&context.payer,
2149+
&context.last_blockhash,
2150+
)
2151+
.await;
2152+
2153+
let deposit_info = simple_deposit_stake(
2154+
&mut context.banks_client,
2155+
&context.payer,
2156+
&context.last_blockhash,
2157+
&stake_pool_accounts,
2158+
&validator_stake_account,
2159+
current_minimum_delegation * 3,
2160+
)
2161+
.await
2162+
.unwrap();
2163+
2164+
let tokens_to_withdraw = deposit_info.pool_tokens;
2165+
2166+
// Delegate tokens for withdrawing
2167+
let user_transfer_authority = Keypair::new();
2168+
delegate_tokens(
2169+
&mut context.banks_client,
2170+
&context.payer,
2171+
&context.last_blockhash,
2172+
&stake_pool_accounts.token_program_id,
2173+
&deposit_info.pool_account.pubkey(),
2174+
&deposit_info.authority,
2175+
&user_transfer_authority.pubkey(),
2176+
tokens_to_withdraw,
2177+
)
2178+
.await;
2179+
2180+
// Create stake account to withdraw to
2181+
let user_stake_recipient = Keypair::new();
2182+
create_blank_stake_account(
2183+
&mut context.banks_client,
2184+
&context.payer,
2185+
&context.last_blockhash,
2186+
&user_stake_recipient,
2187+
)
2188+
.await;
2189+
2190+
(
2191+
context,
2192+
stake_pool_accounts,
2193+
validator_stake_account,
2194+
deposit_info,
2195+
user_transfer_authority,
2196+
user_stake_recipient,
2197+
tokens_to_withdraw,
2198+
)
2199+
}

stake-pool/program/tests/huge_pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use {
2020
},
2121
};
2222

23-
const HUGE_POOL_SIZE: u32 = 2_300;
23+
const HUGE_POOL_SIZE: u32 = 2_000;
2424
const STAKE_AMOUNT: u64 = 200_000_000_000;
2525

2626
async fn setup(

stake-pool/program/tests/update_stake_pool_balance.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use {
1010
},
1111
solana_program_test::*,
1212
solana_sdk::{
13+
hash::Hash,
1314
signature::{Keypair, Signer},
1415
stake,
1516
transaction::TransactionError,
@@ -24,6 +25,7 @@ async fn setup(
2425
num_validators: u64,
2526
) -> (
2627
ProgramTestContext,
28+
Hash,
2729
StakePoolAccounts,
2830
Vec<ValidatorStakeAccount>,
2931
) {
@@ -60,6 +62,12 @@ async fn setup(
6062
.await;
6163
assert!(error.is_none());
6264

65+
let mut last_blockhash = context
66+
.banks_client
67+
.get_new_latest_blockhash(&context.last_blockhash)
68+
.await
69+
.unwrap();
70+
6371
// Add several accounts
6472
let mut stake_accounts: Vec<ValidatorStakeAccount> = vec![];
6573
for i in 0..num_validators {
@@ -71,7 +79,7 @@ async fn setup(
7179
create_vote(
7280
&mut context.banks_client,
7381
&context.payer,
74-
&context.last_blockhash,
82+
&last_blockhash,
7583
&stake_account.validator,
7684
&stake_account.vote,
7785
)
@@ -81,7 +89,7 @@ async fn setup(
8189
.add_validator_to_pool(
8290
&mut context.banks_client,
8391
&context.payer,
84-
&context.last_blockhash,
92+
&last_blockhash,
8593
&stake_account.stake_account,
8694
&stake_account.vote.pubkey(),
8795
stake_account.validator_stake_seed,
@@ -95,31 +103,34 @@ async fn setup(
95103
TEST_STAKE_AMOUNT,
96104
);
97105
deposit_account
98-
.create_and_delegate(
99-
&mut context.banks_client,
100-
&context.payer,
101-
&context.last_blockhash,
102-
)
106+
.create_and_delegate(&mut context.banks_client, &context.payer, &last_blockhash)
103107
.await;
104108

105109
deposit_account
106110
.deposit_stake(
107111
&mut context.banks_client,
108112
&context.payer,
109-
&context.last_blockhash,
113+
&last_blockhash,
110114
&stake_pool_accounts,
111115
)
112116
.await;
113117

118+
last_blockhash = context
119+
.banks_client
120+
.get_new_latest_blockhash(&last_blockhash)
121+
.await
122+
.unwrap();
123+
114124
stake_accounts.push(stake_account);
115125
}
116126

117-
(context, stake_pool_accounts, stake_accounts)
127+
(context, last_blockhash, stake_pool_accounts, stake_accounts)
118128
}
119129

120130
#[tokio::test]
121131
async fn success() {
122-
let (mut context, stake_pool_accounts, stake_accounts) = setup(NUM_VALIDATORS).await;
132+
let (mut context, last_blockhash, stake_pool_accounts, stake_accounts) =
133+
setup(NUM_VALIDATORS).await;
123134

124135
let pre_fee = get_token_balance(
125136
&mut context.banks_client,
@@ -157,12 +168,18 @@ async fn success() {
157168
let slot = context.genesis_config().epoch_schedule.first_normal_slot;
158169
context.warp_to_slot(slot).unwrap();
159170

171+
let last_blockhash = context
172+
.banks_client
173+
.get_new_latest_blockhash(&last_blockhash)
174+
.await
175+
.unwrap();
176+
160177
// Update list and pool
161178
let error = stake_pool_accounts
162179
.update_all(
163180
&mut context.banks_client,
164181
&context.payer,
165-
&context.last_blockhash,
182+
&last_blockhash,
166183
stake_accounts
167184
.iter()
168185
.map(|v| v.vote.pubkey())
@@ -216,7 +233,8 @@ async fn success() {
216233

217234
#[tokio::test]
218235
async fn success_absorbing_extra_lamports() {
219-
let (mut context, stake_pool_accounts, stake_accounts) = setup(NUM_VALIDATORS).await;
236+
let (mut context, mut last_blockhash, stake_pool_accounts, stake_accounts) =
237+
setup(NUM_VALIDATORS).await;
220238

221239
let pre_balance = get_validator_list_sum(
222240
&mut context.banks_client,
@@ -244,11 +262,17 @@ async fn success_absorbing_extra_lamports() {
244262
transfer(
245263
&mut context.banks_client,
246264
&context.payer,
247-
&context.last_blockhash,
265+
&last_blockhash,
248266
&stake_account.stake_account,
249267
EXTRA_STAKE_AMOUNT,
250268
)
251269
.await;
270+
271+
last_blockhash = context
272+
.banks_client
273+
.get_new_latest_blockhash(&last_blockhash)
274+
.await
275+
.unwrap();
252276
}
253277

254278
let extra_lamports = EXTRA_STAKE_AMOUNT * stake_accounts.len() as u64;
@@ -257,13 +281,18 @@ async fn success_absorbing_extra_lamports() {
257281
// Update epoch
258282
let slot = context.genesis_config().epoch_schedule.first_normal_slot;
259283
context.warp_to_slot(slot).unwrap();
284+
let last_blockhash = context
285+
.banks_client
286+
.get_new_latest_blockhash(&last_blockhash)
287+
.await
288+
.unwrap();
260289

261290
// Update list and pool
262291
let error = stake_pool_accounts
263292
.update_all(
264293
&mut context.banks_client,
265294
&context.payer,
266-
&context.last_blockhash,
295+
&last_blockhash,
267296
stake_accounts
268297
.iter()
269298
.map(|v| v.vote.pubkey())

0 commit comments

Comments
 (0)