Skip to content

Commit 5c021ed

Browse files
committed
StakeTestContext to reduce boilerplate
1 parent c584c4f commit 5c021ed

File tree

11 files changed

+763
-987
lines changed

11 files changed

+763
-987
lines changed

program/tests/authorize.rs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ mod helpers;
55
use {
66
helpers::{
77
add_sysvars, initialize_stake_account, parse_stake_account,
8-
process_instruction_after_testing_missing_signers,
8+
process_instruction_after_testing_missing_signers, StakeTestContext,
99
},
10-
mollusk_svm::{result::Check, Mollusk},
10+
mollusk_svm::result::Check,
1111
solana_account::AccountSharedData,
1212
solana_program_error::ProgramError,
1313
solana_pubkey::Pubkey,
@@ -18,15 +18,9 @@ use {
1818
solana_stake_program::id,
1919
};
2020

21-
fn mollusk_bpf() -> Mollusk {
22-
Mollusk::new(&id(), "solana_stake_program")
23-
}
24-
2521
#[test]
2622
fn test_authorize() {
27-
let mollusk = mollusk_bpf();
28-
29-
let rent_exempt_reserve = helpers::STAKE_RENT_EXEMPTION;
23+
let ctx = StakeTestContext::new();
3024

3125
let staker1 = Pubkey::new_unique();
3226
let staker2 = Pubkey::new_unique();
@@ -38,7 +32,7 @@ fn test_authorize() {
3832

3933
let stake = Pubkey::new_unique();
4034
let stake_account = AccountSharedData::new_data_with_space(
41-
rent_exempt_reserve,
35+
ctx.rent_exempt_reserve,
4236
&StakeStateV2::Uninitialized,
4337
StakeStateV2::size_of(),
4438
&id(),
@@ -48,9 +42,9 @@ fn test_authorize() {
4842
// Authorize uninitialized fails for staker
4943
let instruction = ixn::authorize(&stake, &stake, &staker1, StakeAuthorize::Staker, None);
5044
let accounts = vec![(stake, stake_account.clone())];
51-
let accounts = add_sysvars(&mollusk, &instruction, accounts);
45+
let accounts = add_sysvars(&ctx.mollusk, &instruction, accounts);
5246

53-
mollusk.process_and_validate_instruction(
47+
ctx.mollusk.process_and_validate_instruction(
5448
&instruction,
5549
&accounts,
5650
&[Check::err(ProgramError::InvalidAccountData)],
@@ -65,18 +59,18 @@ fn test_authorize() {
6559
None,
6660
);
6761
let accounts = vec![(stake, stake_account.clone())];
68-
let accounts = add_sysvars(&mollusk, &instruction, accounts);
62+
let accounts = add_sysvars(&ctx.mollusk, &instruction, accounts);
6963

70-
mollusk.process_and_validate_instruction(
64+
ctx.mollusk.process_and_validate_instruction(
7165
&instruction,
7266
&accounts,
7367
&[Check::err(ProgramError::InvalidAccountData)],
7468
);
7569

7670
let mut stake_account = initialize_stake_account(
77-
&mollusk,
71+
&ctx.mollusk,
7872
&stake,
79-
rent_exempt_reserve,
73+
ctx.rent_exempt_reserve,
8074
&Authorized {
8175
staker: staker1,
8276
withdrawer: withdrawer1,
@@ -90,14 +84,14 @@ fn test_authorize() {
9084

9185
// Test that removing any signer causes failure, then verify success
9286
let result = process_instruction_after_testing_missing_signers(
93-
&mollusk,
87+
&ctx.mollusk,
9488
&instruction,
9589
&accounts,
9690
&[
9791
Check::success(),
9892
Check::all_rent_exempt(),
9993
Check::account(&stake)
100-
.lamports(rent_exempt_reserve)
94+
.lamports(ctx.rent_exempt_reserve)
10195
.owner(&id())
10296
.space(StakeStateV2::size_of())
10397
.build(),
@@ -120,14 +114,14 @@ fn test_authorize() {
120114

121115
// Test that removing any signer causes failure, then verify success
122116
let result = process_instruction_after_testing_missing_signers(
123-
&mollusk,
117+
&ctx.mollusk,
124118
&instruction,
125119
&accounts,
126120
&[
127121
Check::success(),
128122
Check::all_rent_exempt(),
129123
Check::account(&stake)
130-
.lamports(rent_exempt_reserve)
124+
.lamports(ctx.rent_exempt_reserve)
131125
.owner(&id())
132126
.space(StakeStateV2::size_of())
133127
.build(),
@@ -148,8 +142,8 @@ fn test_authorize() {
148142
);
149143
let accounts = vec![(stake, stake_account.clone())];
150144

151-
let accounts = add_sysvars(&mollusk, &instruction, accounts);
152-
mollusk.process_and_validate_instruction(
145+
let accounts = add_sysvars(&ctx.mollusk, &instruction, accounts);
146+
ctx.mollusk.process_and_validate_instruction(
153147
&instruction,
154148
&accounts,
155149
&[Check::err(ProgramError::MissingRequiredSignature)],
@@ -165,8 +159,8 @@ fn test_authorize() {
165159
);
166160
let accounts = vec![(stake, stake_account.clone())];
167161

168-
let accounts = add_sysvars(&mollusk, &instruction, accounts);
169-
mollusk.process_and_validate_instruction(
162+
let accounts = add_sysvars(&ctx.mollusk, &instruction, accounts);
163+
ctx.mollusk.process_and_validate_instruction(
170164
&instruction,
171165
&accounts,
172166
&[Check::err(ProgramError::MissingRequiredSignature)],
@@ -178,14 +172,14 @@ fn test_authorize() {
178172

179173
// Test that removing any signer causes failure, then verify success
180174
let result = process_instruction_after_testing_missing_signers(
181-
&mollusk,
175+
&ctx.mollusk,
182176
&instruction,
183177
&accounts,
184178
&[
185179
Check::success(),
186180
Check::all_rent_exempt(),
187181
Check::account(&stake)
188-
.lamports(rent_exempt_reserve)
182+
.lamports(ctx.rent_exempt_reserve)
189183
.owner(&id())
190184
.space(StakeStateV2::size_of())
191185
.build(),
@@ -208,14 +202,14 @@ fn test_authorize() {
208202

209203
// Test that removing any signer causes failure, then verify success
210204
let result = process_instruction_after_testing_missing_signers(
211-
&mollusk,
205+
&ctx.mollusk,
212206
&instruction,
213207
&accounts,
214208
&[
215209
Check::success(),
216210
Check::all_rent_exempt(),
217211
Check::account(&stake)
218-
.lamports(rent_exempt_reserve)
212+
.lamports(ctx.rent_exempt_reserve)
219213
.owner(&id())
220214
.space(StakeStateV2::size_of())
221215
.build(),
@@ -236,8 +230,8 @@ fn test_authorize() {
236230
);
237231
let accounts = vec![(stake, stake_account.clone())];
238232

239-
let accounts = add_sysvars(&mollusk, &instruction, accounts);
240-
mollusk.process_and_validate_instruction(
233+
let accounts = add_sysvars(&ctx.mollusk, &instruction, accounts);
234+
ctx.mollusk.process_and_validate_instruction(
241235
&instruction,
242236
&accounts,
243237
&[Check::err(ProgramError::MissingRequiredSignature)],
@@ -249,14 +243,14 @@ fn test_authorize() {
249243

250244
// Test that removing any signer causes failure, then verify success
251245
let result = process_instruction_after_testing_missing_signers(
252-
&mollusk,
246+
&ctx.mollusk,
253247
&instruction,
254248
&accounts,
255249
&[
256250
Check::success(),
257251
Check::all_rent_exempt(),
258252
Check::account(&stake)
259-
.lamports(rent_exempt_reserve)
253+
.lamports(ctx.rent_exempt_reserve)
260254
.owner(&id())
261255
.space(StakeStateV2::size_of())
262256
.build(),
@@ -270,14 +264,14 @@ fn test_authorize() {
270264
// Withdraw using staker fails - test all three stakers to ensure none can withdraw
271265
for staker in [staker1, staker2, staker3] {
272266
let recipient = Pubkey::new_unique();
273-
let instruction = ixn::withdraw(&stake, &staker, &recipient, rent_exempt_reserve, None);
267+
let instruction = ixn::withdraw(&stake, &staker, &recipient, ctx.rent_exempt_reserve, None);
274268
let accounts = vec![
275269
(stake, stake_account.clone()),
276270
(recipient, AccountSharedData::default()),
277271
];
278272

279-
let accounts = add_sysvars(&mollusk, &instruction, accounts);
280-
mollusk.process_and_validate_instruction(
273+
let accounts = add_sysvars(&ctx.mollusk, &instruction, accounts);
274+
ctx.mollusk.process_and_validate_instruction(
281275
&instruction,
282276
&accounts,
283277
&[Check::err(ProgramError::MissingRequiredSignature)],

0 commit comments

Comments
 (0)