-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsteward_fixtures.rs
1934 lines (1779 loc) · 66.4 KB
/
steward_fixtures.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![allow(clippy::await_holding_refcell_ref)]
use std::{cell::RefCell, collections::HashMap, rc::Rc, str::FromStr, vec};
use crate::spl_stake_pool_cli;
use anchor_lang::{
prelude::SolanaSysvar,
solana_program::{
clock::Clock,
pubkey::Pubkey,
vote::state::{VoteInit, VoteState, VoteStateVersions},
},
AccountSerialize, AnchorSerialize, Discriminator, InstructionData, ToAccountMetas,
};
use jito_steward::{
bitmask::BitMask,
constants::{MAX_VALIDATORS, SORTED_INDEX_DEFAULT, STAKE_POOL_WITHDRAW_SEED},
utils::StakePool,
utils::ValidatorList,
Config, Delegation, LargeBitMask, Parameters, StewardState, StewardStateAccount,
StewardStateEnum, UpdateParametersArgs, STATE_PADDING_0_SIZE,
};
use solana_program_test::*;
use solana_sdk::{
account::Account,
compute_budget::ComputeBudgetInstruction,
epoch_schedule::EpochSchedule,
hash::Hash,
instruction::Instruction,
native_token::LAMPORTS_PER_SOL,
rent::Rent,
signature::Keypair,
signer::Signer,
stake::{
self,
state::{Lockup, StakeStateV2},
},
system_program, sysvar,
transaction::Transaction,
};
use spl_stake_pool::{
find_stake_program_address, find_transient_stake_program_address, minimum_delegation,
state::{
AccountType, Fee, FutureEpoch, StakeStatus, ValidatorList as SPLValidatorList,
ValidatorStakeInfo,
},
};
use validator_history::{
self,
constants::{MAX_ALLOC_BYTES, TVC_MULTIPLIER},
CircBuf, CircBufCluster, ClusterHistory, ClusterHistoryEntry, ValidatorHistory,
ValidatorHistoryEntry,
};
pub struct StakePoolMetadata {
pub stake_pool_keypair: Keypair,
pub stake_pool: Pubkey,
pub validator_list_keypair: Keypair,
pub validator_list: Pubkey,
pub reserve_keypair: Keypair,
pub reserve: Pubkey,
}
impl Default for StakePoolMetadata {
fn default() -> Self {
let stake_pool_keypair = Keypair::new();
let stake_pool = stake_pool_keypair.pubkey();
let validator_list_keypair = Keypair::new();
let validator_list = validator_list_keypair.pubkey();
let reserve_keypair = Keypair::new();
let reserve = reserve_keypair.pubkey();
Self {
stake_pool_keypair,
stake_pool,
validator_list_keypair,
validator_list,
reserve_keypair,
reserve,
}
}
}
pub struct TestFixture {
pub ctx: Rc<RefCell<ProgramTestContext>>,
pub stake_pool_meta: StakePoolMetadata,
pub steward_config: Keypair,
pub steward_state: Pubkey,
pub cluster_history_account: Pubkey,
pub validator_history_config: Pubkey,
pub keypair: Keypair,
}
impl TestFixture {
pub async fn new() -> Self {
/*
Initializes test context with Steward and Stake Pool programs loaded, as well as
a vote account and a system account for signing transactions.
Returns a fixture with relevant account addresses and keypairs.
*/
let mut program = match std::env::var("SBF_OUT_DIR") {
Ok(_) | Err(_) => {
let mut program = ProgramTest::new("jito_steward", jito_steward::ID, None);
program.add_program("spl_stake_pool", spl_stake_pool::id(), None);
program
} // Err(_) => {
// let mut program = ProgramTest::new(
// "jito-steward",
// jito_steward::ID,
// processor!(jito_steward::entry),
// );
// program.add_program(
// "spl-stake-pool",
// spl_stake_pool::id(),
// processor!(spl_stake_pool::processor::Processor::process),
// );
// program
// }
};
let stake_pool_meta = StakePoolMetadata::default();
let steward_config = Keypair::new();
let steward_state = Pubkey::find_program_address(
&[StewardStateAccount::SEED, steward_config.pubkey().as_ref()],
&jito_steward::id(),
)
.0;
let cluster_history_account =
Pubkey::find_program_address(&[ClusterHistory::SEED], &validator_history::id()).0;
let (validator_history_config, vhc_bump) = Pubkey::find_program_address(
&[validator_history::state::Config::SEED],
&validator_history::id(),
);
let keypair = Keypair::new();
program.add_account(keypair.pubkey(), system_account(100_000_000_000));
program.add_account(steward_config.pubkey(), system_account(100_000_000_000));
program.add_account(
validator_history_config,
validator_history_config_account(vhc_bump, 1),
);
program.deactivate_feature(
Pubkey::from_str("9onWzzvCzNC2jfhxxeqRgs5q7nFAAKpCUvkj6T6GJK9i").unwrap(),
);
let ctx = Rc::new(RefCell::new(program.start_with_context().await));
Self {
ctx,
stake_pool_meta,
steward_state,
steward_config,
validator_history_config,
cluster_history_account,
keypair,
}
}
pub async fn new_from_accounts(
accounts_fixture: FixtureDefaultAccounts,
additional_accounts: HashMap<Pubkey, Account>,
) -> Self {
let mut program = ProgramTest::new("jito_steward", jito_steward::ID, None);
program.add_program("validator_history", validator_history::id(), None);
program.add_program("spl_stake_pool", spl_stake_pool::id(), None);
for (key, account) in accounts_fixture.to_accounts_vec() {
// Skip keys that are overriden by additional_accounts
if !additional_accounts.contains_key(&key) {
program.add_account(key, account);
}
}
for (key, account) in additional_accounts {
program.add_account(key, account);
}
program.deactivate_feature(
Pubkey::from_str("9onWzzvCzNC2jfhxxeqRgs5q7nFAAKpCUvkj6T6GJK9i").unwrap(),
);
let ctx = Rc::new(RefCell::new(program.start_with_context().await));
let steward_config_address = accounts_fixture.steward_config_keypair.pubkey();
Self {
ctx,
stake_pool_meta: accounts_fixture.stake_pool_meta,
steward_config: accounts_fixture.steward_config_keypair,
steward_state: Pubkey::find_program_address(
&[StewardStateAccount::SEED, steward_config_address.as_ref()],
&jito_steward::id(),
)
.0,
cluster_history_account: Pubkey::find_program_address(
&[ClusterHistory::SEED],
&validator_history::id(),
)
.0,
validator_history_config: Pubkey::find_program_address(
&[validator_history::state::Config::SEED],
&validator_history::id(),
)
.0,
keypair: accounts_fixture.keypair,
}
}
pub async fn load_and_deserialize<T: anchor_lang::AccountDeserialize>(
&self,
address: &Pubkey,
) -> T {
let ai = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client.get_account(*address).await.unwrap().unwrap()
};
T::try_deserialize(&mut ai.data.as_slice()).unwrap()
}
pub async fn get_sysvar<T: SolanaSysvar>(&self) -> T {
let sysvar = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client.get_sysvar().await.unwrap()
};
sysvar
}
pub async fn get_account(&self, address: &Pubkey) -> Account {
let account = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client.get_account(*address).await.unwrap().unwrap()
};
account
}
pub async fn simulate_stake_pool_update(&self) {
let stake_pool: StakePool = self
.load_and_deserialize(&self.stake_pool_meta.stake_pool)
.await;
let mut stake_pool_spl = stake_pool.as_ref().clone();
let current_epoch = self
.ctx
.borrow_mut()
.banks_client
.get_sysvar::<Clock>()
.await
.unwrap()
.epoch;
stake_pool_spl.last_update_epoch = current_epoch;
self.ctx.borrow_mut().set_account(
&self.stake_pool_meta.stake_pool,
&serialized_stake_pool_account(stake_pool_spl, std::mem::size_of::<StakePool>()).into(),
);
}
pub async fn initialize_stake_pool(&self) {
// Call command_create_pool and execute transactions responded
let mint = Keypair::new();
let cli_config = spl_stake_pool_cli::CliConfig {
manager: self.keypair.insecure_clone(),
staker: self.keypair.insecure_clone(),
funding_authority: None,
token_owner: self.keypair.insecure_clone(),
fee_payer: self.keypair.insecure_clone(),
dry_run: false,
no_update: false,
};
let epoch_fee = Fee {
numerator: 1,
denominator: 100,
};
let withdrawal_fee = Fee {
numerator: 1,
denominator: 100,
};
let deposit_fee = Fee {
numerator: 1,
denominator: 100,
};
let transactions_and_signers = spl_stake_pool_cli::command_create_pool(
&cli_config,
None,
epoch_fee,
withdrawal_fee,
deposit_fee,
0,
MAX_VALIDATORS as u32,
self.stake_pool_meta.stake_pool_keypair.insecure_clone(),
self.stake_pool_meta.validator_list_keypair.insecure_clone(),
mint,
self.stake_pool_meta.reserve_keypair.insecure_clone(),
true,
spl_stake_pool::id(),
)
.expect("failed to create pool initialization instructions");
for (instructions, signers) in transactions_and_signers {
let signers = signers.iter().map(|s| s as &dyn Signer).collect::<Vec<_>>();
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&self.keypair.pubkey()),
&signers,
self.ctx.borrow().last_blockhash,
);
self.submit_transaction_assert_success(transaction).await;
}
}
pub async fn initialize_steward(&self, parameters: Option<UpdateParametersArgs>) {
// Default parameters from JIP
let update_parameters_args = parameters.unwrap_or(UpdateParametersArgs {
mev_commission_range: Some(0), // Set to pass validation, where epochs starts at 0
epoch_credits_range: Some(0), // Set to pass validation, where epochs starts at 0
commission_range: Some(0), // Set to pass validation, where epochs starts at 0
scoring_delinquency_threshold_ratio: Some(0.85),
instant_unstake_delinquency_threshold_ratio: Some(0.70),
mev_commission_bps_threshold: Some(1000),
commission_threshold: Some(5),
historical_commission_threshold: Some(50),
num_delegation_validators: Some(200),
scoring_unstake_cap_bps: Some(750),
instant_unstake_cap_bps: Some(10),
stake_deposit_unstake_cap_bps: Some(10),
instant_unstake_epoch_progress: Some(0.90),
compute_score_slot_range: Some(1000),
instant_unstake_inputs_epoch_progress: Some(0.50),
num_epochs_between_scoring: Some(10),
minimum_stake_lamports: Some(5_000_000_000),
minimum_voting_epochs: Some(0), // Set to pass validation, where epochs starts at 0
});
let instruction = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::InitializeSteward {
config: self.steward_config.pubkey(),
stake_pool: self.stake_pool_meta.stake_pool,
state_account: self.steward_state,
stake_pool_program: spl_stake_pool::id(),
system_program: anchor_lang::solana_program::system_program::id(),
current_staker: self.keypair.pubkey(),
}
.to_account_metas(None),
data: jito_steward::instruction::InitializeSteward {
update_parameters_args,
}
.data(),
};
let transaction = Transaction::new_signed_with_payer(
&[instruction],
Some(&self.keypair.pubkey()),
&[&self.keypair, &self.steward_config],
self.ctx.borrow().last_blockhash,
);
self.submit_transaction_assert_success(transaction).await;
}
pub async fn get_latest_blockhash(&self) -> Hash {
let blockhash = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client
.get_new_latest_blockhash(&Hash::default())
.await
.unwrap()
};
blockhash
}
pub async fn realloc_steward_state(&self) {
// Realloc validator history account
let mut num_reallocs = (StewardStateAccount::SIZE - MAX_ALLOC_BYTES) / MAX_ALLOC_BYTES + 1;
let mut ixs = vec![];
while num_reallocs > 0 {
ixs.extend(vec![
Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::ReallocState {
state_account: self.steward_state,
config: self.steward_config.pubkey(),
validator_list: self.stake_pool_meta.validator_list,
system_program: anchor_lang::solana_program::system_program::id(),
signer: self.keypair.pubkey(),
}
.to_account_metas(None),
data: jito_steward::instruction::ReallocState {}.data(),
};
num_reallocs.min(10)
]);
let blockhash = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client
.get_new_latest_blockhash(&Hash::default())
.await
.unwrap()
};
let transaction = Transaction::new_signed_with_payer(
&ixs,
Some(&self.keypair.pubkey()),
&[&self.keypair],
blockhash,
);
self.submit_transaction_assert_success(transaction).await;
num_reallocs -= num_reallocs.min(10);
ixs = vec![];
}
}
pub async fn initialize_validator_history_config(&self) {
let instruction = Instruction {
program_id: validator_history::id(),
accounts: validator_history::accounts::InitializeConfig {
config: self.validator_history_config,
system_program: anchor_lang::solana_program::system_program::id(),
signer: self.keypair.pubkey(),
}
.to_account_metas(None),
data: validator_history::instruction::InitializeConfig {
authority: self.keypair.pubkey(),
}
.data(),
};
let transaction = Transaction::new_signed_with_payer(
&[instruction],
Some(&self.keypair.pubkey()),
&[&self.keypair],
self.ctx.borrow().last_blockhash,
);
self.submit_transaction_assert_success(transaction).await;
}
pub async fn initialize_validator_list(&self, num_validators: usize) {
let stake_program_minimum = self.fetch_minimum_delegation().await;
let pool_minimum_delegation = minimum_delegation(stake_program_minimum);
let stake_rent = self.fetch_stake_rent().await;
let minimum_active_stake_with_rent = pool_minimum_delegation + stake_rent;
let validator_list_account_info =
self.get_account(&self.stake_pool_meta.validator_list).await;
let validator_list: ValidatorList = self
.load_and_deserialize(&self.stake_pool_meta.validator_list)
.await;
let mut spl_validator_list = validator_list.as_ref().clone();
for _ in 0..num_validators {
spl_validator_list.validators.push(ValidatorStakeInfo {
active_stake_lamports: minimum_active_stake_with_rent.into(),
vote_account_address: Pubkey::new_unique(),
..ValidatorStakeInfo::default()
});
}
self.ctx.borrow_mut().set_account(
&self.stake_pool_meta.validator_list,
&serialized_validator_list_account(
spl_validator_list.clone(),
Some(validator_list_account_info.data.len()),
)
.into(),
);
}
// Turn this into a fixture creator
pub async fn initialize_cluster_history_account(&self) -> ClusterHistory {
todo!()
}
pub fn initialize_validator_history_with_credits(
&self,
vote_account: Pubkey,
index: usize,
) -> Pubkey {
let mut validator_history = validator_history_default(vote_account, index as u32);
let validator_history_address = Pubkey::find_program_address(
&[ValidatorHistory::SEED, vote_account.as_ref()],
&validator_history::id(),
)
.0;
for i in 0..20 {
validator_history.history.push(ValidatorHistoryEntry {
epoch: i,
epoch_credits: 400000,
activated_stake_lamports: 100_000_000_000_000,
..ValidatorHistoryEntry::default()
});
}
let epoch_credits = vec![(0, 1, 0), (1, 2, 1), (2, 3, 2), (3, 4, 3), (4, 5, 4)];
self.ctx.borrow_mut().set_account(
&vote_account,
&new_vote_account(Pubkey::new_unique(), vote_account, 1, Some(epoch_credits)).into(),
);
self.ctx.borrow_mut().set_account(
&validator_history_address,
&serialized_validator_history_account(validator_history).into(),
);
validator_history_address
}
pub async fn stake_accounts_for_validator(
&self,
vote_account: Pubkey,
) -> (Pubkey, Pubkey, Pubkey) {
let stake_pool: StakePool = self
.load_and_deserialize(&self.stake_pool_meta.stake_pool)
.await;
let withdraw_authority = Pubkey::create_program_address(
&[
self.stake_pool_meta.stake_pool.as_ref(),
STAKE_POOL_WITHDRAW_SEED,
&[stake_pool.as_ref().stake_withdraw_bump_seed],
],
&spl_stake_pool::id(),
)
.unwrap();
// stake account
let stake_account_address = find_stake_program_address(
&spl_stake_pool::id(),
&vote_account,
&self.stake_pool_meta.stake_pool,
None,
)
.0;
// transient stake account
let (transient_stake_account_address, _transient_seed) =
find_transient_stake_program_address(
&spl_stake_pool::id(),
&vote_account,
&self.stake_pool_meta.stake_pool,
0,
);
(
stake_account_address,
transient_stake_account_address,
withdraw_authority,
)
}
pub async fn fetch_minimum_delegation(&self) -> u64 {
let ix = solana_program::stake::instruction::get_minimum_delegation();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&self.keypair.pubkey()),
&[&self.keypair],
self.ctx.borrow_mut().last_blockhash,
);
let process_tx_result = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client.process_transaction_with_metadata(tx).await
};
let result = process_tx_result.unwrap();
assert!(result.result.is_ok());
let metadata = result.metadata.unwrap();
let mut bytes = [0u8; 8];
bytes.copy_from_slice(&metadata.return_data.clone().unwrap().data[..8]);
u64::from_le_bytes(bytes)
}
pub async fn fetch_stake_rent(&self) -> u64 {
let rent: Rent = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client.get_sysvar().await.expect("Failed to get rent")
};
rent.minimum_balance(StakeStateV2::size_of())
}
pub async fn advance_num_epochs(&self, num_epochs: u64, additional_slots: u64) {
let clock: Clock = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client
.get_sysvar()
.await
.expect("Failed getting clock")
};
let epoch_schedule: EpochSchedule = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client
.get_sysvar()
.await
.expect("Failed getting epoch schedule")
};
let target_epoch = clock.epoch + num_epochs;
let target_slot = epoch_schedule.get_first_slot_in_epoch(target_epoch) + additional_slots;
self.ctx
.borrow_mut()
.warp_to_slot(target_slot)
.expect("Failed warping to future epoch");
}
pub async fn submit_transaction_assert_success(&self, transaction: Transaction) {
let process_transaction_result = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client
.process_transaction_with_preflight(transaction)
.await
};
if let Err(e) = process_transaction_result {
panic!("Error: {}", e);
}
}
pub async fn submit_transaction_assert_error(
&self,
transaction: Transaction,
error_message: &str,
) {
let process_transaction_result = {
let mut banks_client = self.ctx.borrow_mut().banks_client.clone();
banks_client
.process_transaction_with_preflight(transaction)
.await
};
if let Err(e) = process_transaction_result {
if !e.to_string().contains(error_message) {
panic!("Error: {}\n\nDoes not match {}", e, error_message);
}
assert!(e.to_string().contains(error_message));
} else {
panic!("Error: Transaction succeeded. Expected {}", error_message);
}
}
}
pub struct ExtraValidatorAccounts {
pub vote_account: Pubkey,
pub validator_history_address: Pubkey,
pub stake_account_address: Pubkey,
pub transient_stake_account_address: Pubkey,
pub withdraw_authority: Pubkey,
}
pub async fn crank_stake_pool(fixture: &TestFixture) {
let stake_pool: StakePool = fixture
.load_and_deserialize(&fixture.stake_pool_meta.stake_pool)
.await;
let validator_list: ValidatorList = fixture
.load_and_deserialize(&fixture.stake_pool_meta.validator_list)
.await;
let (initial_ixs, final_ixs) = spl_stake_pool::instruction::update_stake_pool(
&spl_stake_pool::id(),
stake_pool.as_ref(),
validator_list.as_ref(),
&fixture.stake_pool_meta.stake_pool,
false,
);
let tx = Transaction::new_signed_with_payer(
&initial_ixs,
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
fixture
.ctx
.borrow_mut()
.get_new_latest_blockhash()
.await
.unwrap(),
);
fixture.submit_transaction_assert_success(tx).await;
let tx = Transaction::new_signed_with_payer(
&final_ixs,
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
fixture
.ctx
.borrow_mut()
.get_new_latest_blockhash()
.await
.unwrap(),
);
fixture.submit_transaction_assert_success(tx).await;
}
pub async fn crank_epoch_maintenance(fixture: &TestFixture, remove_indices: Option<&[usize]>) {
let ctx = &fixture.ctx;
// Epoch Maintenence
if let Some(indices) = remove_indices {
for i in indices {
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::EpochMaintenance {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
validator_list: fixture.stake_pool_meta.validator_list,
stake_pool: fixture.stake_pool_meta.stake_pool,
}
.to_account_metas(None),
data: jito_steward::instruction::EpochMaintenance {
validator_index_to_remove: Some(*i as u64),
}
.data(),
};
let blockhash = ctx.borrow_mut().get_new_latest_blockhash().await.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
} else {
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::EpochMaintenance {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
validator_list: fixture.stake_pool_meta.validator_list,
stake_pool: fixture.stake_pool_meta.stake_pool,
}
.to_account_metas(None),
data: jito_steward::instruction::EpochMaintenance {
validator_index_to_remove: None,
}
.data(),
};
let blockhash = ctx.borrow_mut().get_new_latest_blockhash().await.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
}
pub async fn auto_add_validator(fixture: &TestFixture, extra_accounts: &ExtraValidatorAccounts) {
let ctx = &fixture.ctx;
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::AutoAddValidator {
validator_history_account: extra_accounts.validator_history_address,
steward_state: fixture.steward_state,
config: fixture.steward_config.pubkey(),
stake_pool_program: spl_stake_pool::id(),
stake_pool: fixture.stake_pool_meta.stake_pool,
reserve_stake: fixture.stake_pool_meta.reserve,
withdraw_authority: extra_accounts.withdraw_authority,
validator_list: fixture.stake_pool_meta.validator_list,
stake_account: extra_accounts.stake_account_address,
vote_account: extra_accounts.vote_account,
rent: solana_sdk::sysvar::rent::id(),
clock: solana_sdk::sysvar::clock::id(),
stake_history: solana_sdk::sysvar::stake_history::id(),
stake_config: stake::config::ID,
system_program: system_program::id(),
stake_program: stake::program::id(),
}
.to_account_metas(None),
data: jito_steward::instruction::AutoAddValidatorToPool {}.data(),
};
let blockhash = ctx.borrow_mut().get_new_latest_blockhash().await.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
pub async fn auto_remove_validator(
fixture: &TestFixture,
extra_accounts: &ExtraValidatorAccounts,
index: u64,
) {
let ctx = &fixture.ctx;
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::AutoRemoveValidator {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
validator_list: fixture.stake_pool_meta.validator_list,
stake_pool: fixture.stake_pool_meta.stake_pool,
stake_account: extra_accounts.stake_account_address,
withdraw_authority: extra_accounts.withdraw_authority,
validator_history_account: extra_accounts.validator_history_address,
reserve_stake: fixture.stake_pool_meta.reserve,
transient_stake_account: extra_accounts.transient_stake_account_address,
vote_account: extra_accounts.vote_account,
stake_history: solana_sdk::sysvar::stake_history::id(),
stake_config: stake::config::ID,
stake_program: stake::program::id(),
stake_pool_program: spl_stake_pool::id(),
system_program: system_program::id(),
rent: solana_sdk::sysvar::rent::id(),
clock: solana_sdk::sysvar::clock::id(),
}
.to_account_metas(None),
data: jito_steward::instruction::AutoRemoveValidatorFromPool {
validator_list_index: index,
}
.data(),
};
let blockhash = ctx.borrow_mut().get_new_latest_blockhash().await.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
pub async fn instant_remove_validator(fixture: &TestFixture, index: usize) {
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::InstantRemoveValidator {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
validator_list: fixture.stake_pool_meta.validator_list,
stake_pool: fixture.stake_pool_meta.stake_pool,
}
.to_account_metas(None),
data: jito_steward::instruction::InstantRemoveValidator {
validator_index_to_remove: index as u64,
}
.data(),
};
let blockhash = fixture
.ctx
.borrow_mut()
.get_new_latest_blockhash()
.await
.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
pub async fn manual_remove_validator(
fixture: &TestFixture,
index: usize,
mark_for_removal: bool,
immediate: bool,
) {
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::AdminMarkForRemoval {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
authority: fixture.keypair.pubkey(),
}
.to_account_metas(None),
data: jito_steward::instruction::AdminMarkForRemoval {
validator_list_index: index as u64,
mark_for_removal: if mark_for_removal { 1 } else { 0 },
immediate: if immediate { 1 } else { 0 },
}
.data(),
};
let blockhash = fixture
.ctx
.borrow_mut()
.get_new_latest_blockhash()
.await
.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
pub async fn crank_compute_score(
fixture: &TestFixture,
_unit_test_fixtures: &StateMachineFixtures,
extra_validator_accounts: &[ExtraValidatorAccounts],
indices: &[usize],
) {
let ctx = &fixture.ctx;
for &i in indices {
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::ComputeScore {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
validator_list: fixture.stake_pool_meta.validator_list,
validator_history: extra_validator_accounts[i].validator_history_address,
cluster_history: fixture.cluster_history_account,
}
.to_account_metas(None),
data: jito_steward::instruction::ComputeScore {
validator_list_index: i as u64,
}
.data(),
};
let blockhash = ctx.borrow_mut().get_new_latest_blockhash().await.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
}
pub async fn crank_compute_delegations(fixture: &TestFixture) {
let ctx = &fixture.ctx;
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::ComputeDelegations {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
validator_list: fixture.stake_pool_meta.validator_list,
}
.to_account_metas(None),
data: jito_steward::instruction::ComputeDelegations {}.data(),
};
let blockhash = ctx.borrow_mut().get_new_latest_blockhash().await.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
pub async fn crank_idle(fixture: &TestFixture) {
let ctx = &fixture.ctx;
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::Idle {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
validator_list: fixture.stake_pool_meta.validator_list,
}
.to_account_metas(None),
data: jito_steward::instruction::Idle {}.data(),
};
let blockhash = ctx.borrow_mut().get_new_latest_blockhash().await.unwrap();
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&fixture.keypair.pubkey()),
&[&fixture.keypair],
blockhash,
);
fixture.submit_transaction_assert_success(tx).await;
}
pub async fn crank_compute_instant_unstake(
fixture: &TestFixture,
_unit_test_fixtures: &StateMachineFixtures,
extra_validator_accounts: &[ExtraValidatorAccounts],
indices: &[usize],
) {
let ctx = &fixture.ctx;
for &i in indices {
let ix = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::ComputeInstantUnstake {
config: fixture.steward_config.pubkey(),
state_account: fixture.steward_state,
validator_history: extra_validator_accounts[i].validator_history_address,
validator_list: fixture.stake_pool_meta.validator_list,
cluster_history: fixture.cluster_history_account,
}