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

Commit 67cb90f

Browse files
committed
De-tangle processor/state dependencies
1 parent b97849d commit 67cb90f

File tree

7 files changed

+123
-125
lines changed

7 files changed

+123
-125
lines changed

stake-pool/cli/src/main.rs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn command_create_pool(
120120
let pool_account_balance = config
121121
.rpc_client
122122
.get_minimum_balance_for_rent_exemption(get_packed_len::<StakePool>())?;
123-
let empty_validator_list = ValidatorList::new_with_max_validators(max_validators);
123+
let empty_validator_list = ValidatorList::new(max_validators);
124124
let validator_list_size = get_instance_packed_len(&empty_validator_list)?;
125125
let validator_list_balance = config
126126
.rpc_client
@@ -272,7 +272,7 @@ fn command_vsa_add(
272272
command_update(config, pool)?;
273273
}
274274

275-
let pool_data = get_stake_pool(&config.rpc_client, pool)?;
275+
let stake_pool = get_stake_pool(&config.rpc_client, pool)?;
276276

277277
let mut total_rent_free_balances: u64 = 0;
278278

@@ -286,7 +286,7 @@ fn command_vsa_add(
286286
&config,
287287
&token_receiver,
288288
&token_receiver_account,
289-
&pool_data.pool_mint,
289+
&stake_pool.pool_mint,
290290
&mut instructions,
291291
|balance| {
292292
signers.push(&token_receiver_account);
@@ -299,14 +299,14 @@ fn command_vsa_add(
299299
&spl_stake_pool::id(),
300300
pool,
301301
AUTHORITY_DEPOSIT,
302-
pool_data.deposit_bump_seed,
302+
stake_pool.deposit_bump_seed,
303303
)
304304
.unwrap();
305305
let pool_withdraw_authority: Pubkey = create_pool_authority_address(
306306
&spl_stake_pool::id(),
307307
pool,
308308
AUTHORITY_WITHDRAW,
309-
pool_data.withdraw_bump_seed,
309+
stake_pool.withdraw_bump_seed,
310310
)
311311
.unwrap();
312312

@@ -332,10 +332,10 @@ fn command_vsa_add(
332332
&config.owner.pubkey(),
333333
&pool_deposit_authority,
334334
&pool_withdraw_authority,
335-
&pool_data.validator_list,
335+
&stake_pool.validator_list,
336336
&stake,
337337
&token_receiver,
338-
&pool_data.pool_mint,
338+
&stake_pool.pool_mint,
339339
&spl_token::id(),
340340
)?,
341341
]);
@@ -365,12 +365,12 @@ fn command_vsa_remove(
365365
command_update(config, pool)?;
366366
}
367367

368-
let pool_data = get_stake_pool(&config.rpc_client, pool)?;
368+
let stake_pool = get_stake_pool(&config.rpc_client, pool)?;
369369
let pool_withdraw_authority: Pubkey = create_pool_authority_address(
370370
&spl_stake_pool::id(),
371371
pool,
372372
AUTHORITY_WITHDRAW,
373-
pool_data.withdraw_bump_seed,
373+
stake_pool.withdraw_bump_seed,
374374
)
375375
.unwrap();
376376

@@ -379,16 +379,16 @@ fn command_vsa_remove(
379379

380380
// Calculate amount of tokens to withdraw
381381
let stake_account = config.rpc_client.get_account(&stake)?;
382-
let tokens_to_withdraw = pool_data
382+
let tokens_to_withdraw = stake_pool
383383
.calc_pool_withdraw_amount(stake_account.lamports)
384384
.unwrap();
385385

386386
// Check balance and mint
387387
let token_account =
388-
get_token_account(&config.rpc_client, &withdraw_from, &pool_data.pool_mint)?;
388+
get_token_account(&config.rpc_client, &withdraw_from, &stake_pool.pool_mint)?;
389389

390390
if token_account.amount < tokens_to_withdraw {
391-
let pool_mint = get_token_mint(&config.rpc_client, &pool_data.pool_mint)?;
391+
let pool_mint = get_token_mint(&config.rpc_client, &stake_pool.pool_mint)?;
392392
return Err(format!(
393393
"Not enough balance to burn to remove validator stake account from the pool. {} pool tokens needed.",
394394
spl_token::amount_to_ui_amount(tokens_to_withdraw, pool_mint.decimals)
@@ -413,10 +413,10 @@ fn command_vsa_remove(
413413
&config.owner.pubkey(),
414414
&pool_withdraw_authority,
415415
&new_authority,
416-
&pool_data.validator_list,
416+
&stake_pool.validator_list,
417417
&stake,
418418
&withdraw_from,
419-
&pool_data.pool_mint,
419+
&stake_pool.pool_mint,
420420
&spl_token::id(),
421421
)?,
422422
],
@@ -490,7 +490,7 @@ fn command_deposit(
490490
command_update(config, pool)?;
491491
}
492492

493-
let pool_data = get_stake_pool(&config.rpc_client, pool)?;
493+
let stake_pool = get_stake_pool(&config.rpc_client, pool)?;
494494
let stake_state = get_stake_state(&config.rpc_client, &stake)?;
495495

496496
if config.verbose {
@@ -502,7 +502,7 @@ fn command_deposit(
502502
}?;
503503

504504
// Check if this vote account has staking account in the pool
505-
let validator_list = get_validator_list(&config.rpc_client, &pool_data.validator_list)?;
505+
let validator_list = get_validator_list(&config.rpc_client, &stake_pool.validator_list)?;
506506
if !validator_list.contains(&vote_account) {
507507
return Err("Stake account for this validator does not exist in the pool.".into());
508508
}
@@ -529,7 +529,7 @@ fn command_deposit(
529529
&config,
530530
&token_receiver,
531531
&token_receiver_account,
532-
&pool_data.pool_mint,
532+
&stake_pool.pool_mint,
533533
&mut instructions,
534534
|balance| {
535535
signers.push(&token_receiver_account);
@@ -542,14 +542,14 @@ fn command_deposit(
542542
&spl_stake_pool::id(),
543543
pool,
544544
AUTHORITY_DEPOSIT,
545-
pool_data.deposit_bump_seed,
545+
stake_pool.deposit_bump_seed,
546546
)
547547
.unwrap();
548548
let pool_withdraw_authority: Pubkey = create_pool_authority_address(
549549
&spl_stake_pool::id(),
550550
pool,
551551
AUTHORITY_WITHDRAW,
552-
pool_data.withdraw_bump_seed,
552+
stake_pool.withdraw_bump_seed,
553553
)
554554
.unwrap();
555555

@@ -572,14 +572,14 @@ fn command_deposit(
572572
spl_stake_pool::instruction::deposit(
573573
&spl_stake_pool::id(),
574574
&pool,
575-
&pool_data.validator_list,
575+
&stake_pool.validator_list,
576576
&pool_deposit_authority,
577577
&pool_withdraw_authority,
578578
&stake,
579579
&validator_stake_account,
580580
&token_receiver,
581-
&pool_data.owner_fee_account,
582-
&pool_data.pool_mint,
581+
&stake_pool.owner_fee_account,
582+
&stake_pool.pool_mint,
583583
&spl_token::id(),
584584
)?,
585585
]);
@@ -599,11 +599,11 @@ fn command_deposit(
599599
}
600600

601601
fn command_list(config: &Config, pool: &Pubkey) -> CommandResult {
602-
let pool_data = get_stake_pool(&config.rpc_client, pool)?;
602+
let stake_pool = get_stake_pool(&config.rpc_client, pool)?;
603603

604604
if config.verbose {
605605
println!("Current validator list");
606-
let validator_list = get_validator_list(&config.rpc_client, &pool_data.validator_list)?;
606+
let validator_list = get_validator_list(&config.rpc_client, &stake_pool.validator_list)?;
607607
for validator in validator_list.validators {
608608
println!(
609609
"Vote Account: {}\tBalance: {}\tEpoch: {}",
@@ -616,7 +616,7 @@ fn command_list(config: &Config, pool: &Pubkey) -> CommandResult {
616616
&spl_stake_pool::id(),
617617
pool,
618618
AUTHORITY_WITHDRAW,
619-
pool_data.withdraw_bump_seed,
619+
stake_pool.withdraw_bump_seed,
620620
)
621621
.unwrap();
622622

@@ -641,8 +641,8 @@ fn command_list(config: &Config, pool: &Pubkey) -> CommandResult {
641641
}
642642

643643
fn command_update(config: &Config, pool: &Pubkey) -> CommandResult {
644-
let pool_data = get_stake_pool(&config.rpc_client, pool)?;
645-
let validator_list = get_validator_list(&config.rpc_client, &pool_data.validator_list)?;
644+
let stake_pool = get_stake_pool(&config.rpc_client, pool)?;
645+
let validator_list = get_validator_list(&config.rpc_client, &stake_pool.validator_list)?;
646646
let epoch_info = config.rpc_client.get_epoch_info()?;
647647

648648
let accounts_to_update: Vec<Pubkey> = validator_list
@@ -667,20 +667,20 @@ fn command_update(config: &Config, pool: &Pubkey) -> CommandResult {
667667
for chunk in accounts_to_update.chunks(MAX_ACCOUNTS_TO_UPDATE) {
668668
instructions.push(spl_stake_pool::instruction::update_validator_list_balance(
669669
&spl_stake_pool::id(),
670-
&pool_data.validator_list,
670+
&stake_pool.validator_list,
671671
&chunk,
672672
)?);
673673
}
674674

675-
if instructions.is_empty() && pool_data.last_update_epoch == epoch_info.epoch {
675+
if instructions.is_empty() && stake_pool.last_update_epoch == epoch_info.epoch {
676676
println!("Stake pool balances are up to date, no update required.");
677677
Ok(())
678678
} else {
679679
println!("Updating stake pool...");
680680
instructions.push(spl_stake_pool::instruction::update_stake_pool_balance(
681681
&spl_stake_pool::id(),
682682
pool,
683-
&pool_data.validator_list,
683+
&stake_pool.validator_list,
684684
)?);
685685

686686
let mut transaction =
@@ -765,21 +765,21 @@ fn command_withdraw(
765765
command_update(config, pool)?;
766766
}
767767

768-
let pool_data = get_stake_pool(&config.rpc_client, pool)?;
769-
let pool_mint = get_token_mint(&config.rpc_client, &pool_data.pool_mint)?;
768+
let stake_pool = get_stake_pool(&config.rpc_client, pool)?;
769+
let pool_mint = get_token_mint(&config.rpc_client, &stake_pool.pool_mint)?;
770770
let pool_amount = spl_token::ui_amount_to_amount(pool_amount, pool_mint.decimals);
771771

772772
let pool_withdraw_authority: Pubkey = create_pool_authority_address(
773773
&spl_stake_pool::id(),
774774
pool,
775775
AUTHORITY_WITHDRAW,
776-
pool_data.withdraw_bump_seed,
776+
stake_pool.withdraw_bump_seed,
777777
)
778778
.unwrap();
779779

780780
// Check withdraw_from account type
781781
let token_account =
782-
get_token_account(&config.rpc_client, &withdraw_from, &pool_data.pool_mint)?;
782+
get_token_account(&config.rpc_client, &withdraw_from, &stake_pool.pool_mint)?;
783783

784784
// Check withdraw_from balance
785785
if token_account.amount < pool_amount {
@@ -794,7 +794,7 @@ fn command_withdraw(
794794
// Get the list of accounts to withdraw from
795795
let withdraw_accounts = prepare_withdraw_accounts(
796796
&config.rpc_client,
797-
&pool_data,
797+
&stake_pool,
798798
&pool_withdraw_authority,
799799
pool_amount,
800800
)?;
@@ -824,7 +824,7 @@ fn command_withdraw(
824824
// Go through prepared accounts and withdraw/claim them
825825
for withdraw_account in withdraw_accounts {
826826
// Convert pool tokens amount to lamports
827-
let sol_withdraw_amount = pool_data
827+
let sol_withdraw_amount = stake_pool
828828
.calc_lamports_withdraw_amount(withdraw_account.pool_amount)
829829
.unwrap();
830830

@@ -867,13 +867,13 @@ fn command_withdraw(
867867
instructions.push(spl_stake_pool::instruction::withdraw(
868868
&spl_stake_pool::id(),
869869
&pool,
870-
&pool_data.validator_list,
870+
&stake_pool.validator_list,
871871
&pool_withdraw_authority,
872872
&withdraw_account.address,
873873
&stake_receiver.unwrap(), // Cannot be none at this point
874874
&config.owner.pubkey(),
875875
&withdraw_from,
876-
&pool_data.pool_mint,
876+
&stake_pool.pool_mint,
877877
&spl_token::id(),
878878
withdraw_account.pool_amount,
879879
)?);
@@ -899,19 +899,20 @@ fn command_set_owner(
899899
new_owner: &Option<Pubkey>,
900900
new_fee_receiver: &Option<Pubkey>,
901901
) -> CommandResult {
902-
let pool_data = get_stake_pool(&config.rpc_client, pool)?;
902+
let stake_pool = get_stake_pool(&config.rpc_client, pool)?;
903903

904904
// If new accounts are missing in the arguments use the old ones
905905
let new_owner = match new_owner {
906-
None => pool_data.owner,
906+
None => stake_pool.owner,
907907
Some(value) => *value,
908908
};
909909
let new_fee_receiver = match new_fee_receiver {
910-
None => pool_data.owner_fee_account,
910+
None => stake_pool.owner_fee_account,
911911
Some(value) => {
912912
// Check for fee receiver being a valid token account and have to same mint as the stake pool
913-
let token_account = get_token_account(&config.rpc_client, value, &pool_data.pool_mint)?;
914-
if token_account.mint != pool_data.pool_mint {
913+
let token_account =
914+
get_token_account(&config.rpc_client, value, &stake_pool.pool_mint)?;
915+
if token_account.mint != stake_pool.pool_mint {
915916
return Err("Fee receiver account belongs to a different mint"
916917
.to_string()
917918
.into());

stake-pool/program/src/borsh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use {
55
borsh::{maybestd::io::Error, BorshDeserialize, BorshSerialize},
6-
std::io::{Result as IoResult, Write},
6+
std::io::{self, Write},
77
};
88

99
/// Deserializes something and allows for incomplete reading
@@ -20,13 +20,13 @@ struct WriteCounter {
2020
}
2121

2222
impl Write for WriteCounter {
23-
fn write(&mut self, data: &[u8]) -> IoResult<usize> {
23+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
2424
let amount = data.len();
2525
self.count += amount;
2626
Ok(amount)
2727
}
2828

29-
fn flush(&mut self) -> IoResult<()> {
29+
fn flush(&mut self) -> io::Result<()> {
3030
Ok(())
3131
}
3232
}

stake-pool/program/src/entrypoint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ fn process_instruction(
1717
if let Err(error) = Processor::process(program_id, accounts, instruction_data) {
1818
// catch the error so we can print it
1919
error.print::<StakePoolError>();
20-
return Err(error);
20+
Err(error)
21+
} else {
22+
Ok(())
2123
}
22-
Ok(())
2324
}

0 commit comments

Comments
 (0)