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

Commit 4658815

Browse files
authored
stake-pool: Force pools to only use the SPL token program (#2521)
1 parent 7f0278c commit 4658815

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

stake-pool/program/src/processor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,15 @@ impl Processor {
565565
return Err(StakePoolError::FeeTooHigh.into());
566566
}
567567

568+
if *token_program_info.key != spl_token::id() {
569+
msg!(
570+
"Only the SPL token program is currently supported, expected {}, received {}",
571+
spl_token::id(),
572+
*token_program_info.key
573+
);
574+
return Err(ProgramError::IncorrectProgramId);
575+
}
576+
568577
if manager_fee_info.owner != token_program_info.key {
569578
return Err(ProgramError::IncorrectProgramId);
570579
}

stake-pool/program/tests/initialize.rs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,107 @@ async fn fail_with_wrong_token_program_id() {
454454
.await
455455
.unwrap();
456456

457+
create_token_account(
458+
&mut banks_client,
459+
&payer,
460+
&recent_blockhash,
461+
&stake_pool_accounts.pool_fee_account,
462+
&stake_pool_accounts.pool_mint.pubkey(),
463+
&stake_pool_accounts.manager.pubkey(),
464+
)
465+
.await
466+
.unwrap();
467+
457468
let rent = banks_client.get_rent().await.unwrap();
469+
let rent_stake_pool = rent.minimum_balance(get_packed_len::<state::StakePool>());
470+
let validator_list_size = get_instance_packed_len(&state::ValidatorList::new(
471+
stake_pool_accounts.max_validators,
472+
))
473+
.unwrap();
474+
let rent_validator_list = rent.minimum_balance(validator_list_size);
458475

459-
let account_rent = rent.minimum_balance(spl_token::state::Account::LEN);
460476
let mut transaction = Transaction::new_with_payer(
477+
&[
478+
system_instruction::create_account(
479+
&payer.pubkey(),
480+
&stake_pool_accounts.stake_pool.pubkey(),
481+
rent_stake_pool,
482+
get_packed_len::<state::StakePool>() as u64,
483+
&id(),
484+
),
485+
system_instruction::create_account(
486+
&payer.pubkey(),
487+
&stake_pool_accounts.validator_list.pubkey(),
488+
rent_validator_list,
489+
validator_list_size as u64,
490+
&id(),
491+
),
492+
instruction::initialize(
493+
&id(),
494+
&stake_pool_accounts.stake_pool.pubkey(),
495+
&stake_pool_accounts.manager.pubkey(),
496+
&stake_pool_accounts.staker.pubkey(),
497+
&stake_pool_accounts.validator_list.pubkey(),
498+
&stake_pool_accounts.reserve_stake.pubkey(),
499+
&stake_pool_accounts.pool_mint.pubkey(),
500+
&stake_pool_accounts.pool_fee_account.pubkey(),
501+
&wrong_token_program.pubkey(),
502+
None,
503+
stake_pool_accounts.epoch_fee,
504+
stake_pool_accounts.withdrawal_fee,
505+
stake_pool_accounts.deposit_fee,
506+
stake_pool_accounts.referral_fee,
507+
stake_pool_accounts.max_validators,
508+
),
509+
],
510+
Some(&payer.pubkey()),
511+
);
512+
transaction.sign(
513+
&[
514+
&payer,
515+
&stake_pool_accounts.stake_pool,
516+
&stake_pool_accounts.validator_list,
517+
&stake_pool_accounts.manager,
518+
],
519+
recent_blockhash,
520+
);
521+
let transaction_error = banks_client
522+
.process_transaction(transaction)
523+
.await
524+
.err()
525+
.unwrap();
526+
527+
match transaction_error {
528+
TransportError::TransactionError(TransactionError::InstructionError(_, error)) => {
529+
assert_eq!(error, InstructionError::IncorrectProgramId);
530+
}
531+
_ => panic!(
532+
"Wrong error occurs while try to initialize stake pool with wrong token program ID"
533+
),
534+
}
535+
}
536+
537+
#[tokio::test]
538+
async fn fail_with_fee_owned_by_wrong_token_program_id() {
539+
let (mut banks_client, payer, recent_blockhash) = program_test().start().await;
540+
let stake_pool_accounts = StakePoolAccounts::new();
541+
542+
let wrong_token_program = Keypair::new();
543+
544+
create_mint(
545+
&mut banks_client,
546+
&payer,
547+
&recent_blockhash,
548+
&stake_pool_accounts.pool_mint,
549+
&stake_pool_accounts.withdraw_authority,
550+
)
551+
.await
552+
.unwrap();
553+
554+
let rent = banks_client.get_rent().await.unwrap();
555+
556+
let account_rent = rent.minimum_balance(spl_token::state::Account::LEN);
557+
let transaction = Transaction::new_signed_with_payer(
461558
&[system_instruction::create_account(
462559
&payer.pubkey(),
463560
&stake_pool_accounts.pool_fee_account.pubkey(),
@@ -466,8 +563,6 @@ async fn fail_with_wrong_token_program_id() {
466563
&wrong_token_program.pubkey(),
467564
)],
468565
Some(&payer.pubkey()),
469-
);
470-
transaction.sign(
471566
&[&payer, &stake_pool_accounts.pool_fee_account],
472567
recent_blockhash,
473568
);

0 commit comments

Comments
 (0)