Skip to content

Commit

Permalink
added verify_delegate_mining_address for change_delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
rwwwx committed Aug 26, 2024
1 parent 92eea0e commit 109786a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
7 changes: 6 additions & 1 deletion programs/rewards/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub enum RewardsInstruction {
ChangeDelegate {
/// Amount of staked tokens
staked_amount: u64,
new_delegate: Pubkey,
},
}

Expand Down Expand Up @@ -409,6 +410,7 @@ pub fn change_delegate(
mining_owner: &Pubkey,
old_delegate_mining: &Pubkey,
new_delegate_mining: &Pubkey,
new_delegate: &Pubkey,
staked_amount: u64,
) -> Instruction {
let accounts = vec![
Expand All @@ -422,7 +424,10 @@ pub fn change_delegate(

Instruction::new_with_borsh(
*program_id,
&RewardsInstruction::ChangeDelegate { staked_amount },
&RewardsInstruction::ChangeDelegate {
staked_amount,
new_delegate: *new_delegate,
},
accounts,
)
}
12 changes: 11 additions & 1 deletion programs/rewards/src/instructions/change_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::{
asserts::assert_and_get_pool_and_mining,
error::MplxRewardsError,
utils::{get_delegate_mining, AccountLoader},
utils::{get_delegate_mining, verify_delegate_mining_address, AccountLoader},
};
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};

pub fn process_change_delegate<'a>(
program_id: &Pubkey,
accounts: &'a [AccountInfo<'a>],
staked_amount: u64,
new_delegate: &Pubkey
) -> ProgramResult {
let account_info_iter = &mut accounts.iter().enumerate();

Expand Down Expand Up @@ -37,6 +38,15 @@ pub fn process_change_delegate<'a>(
)?;

let new_delegate_mining = get_delegate_mining(new_delegate_mining, mining)?;
if let Some(new_delegate_mining) = new_delegate_mining {
verify_delegate_mining_address(
program_id,
new_delegate_mining,
new_delegate,
reward_pool.key,
)?
}

let old_delegate_mining = get_delegate_mining(old_delegate_mining, mining)?;

wrapped_reward_pool.change_delegate(
Expand Down
12 changes: 10 additions & 2 deletions programs/rewards/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,17 @@ pub fn process_instruction<'a>(
msg!("RewardsInstruction: CloseAccount");
process_close_mining(program_id, accounts)
}
RewardsInstruction::ChangeDelegate { staked_amount } => {
RewardsInstruction::ChangeDelegate {
staked_amount,
new_delegate,
} => {
msg!("RewardsInstruction: ChangeDelegate");
process_change_delegate(program_id, accounts, staked_amount)
process_change_delegate(
program_id,
accounts,
staked_amount,
&new_delegate,
)
}
}
}
2 changes: 2 additions & 0 deletions programs/rewards/tests/rewards/change_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async fn change_delegate_to_the_same() {
&user_a,
&user_mining_a,
&user_mining_a,
&user_a.pubkey(),
6_000_000,
)
.await
Expand Down Expand Up @@ -121,6 +122,7 @@ async fn change_delegate_then_claim() {
&user_a,
&delegate_mining,
&user_mining_a,
&delegate.pubkey(),
1_000_000,
)
.await
Expand Down
2 changes: 2 additions & 0 deletions programs/rewards/tests/rewards/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl TestRewards {
mining_owner: &Keypair,
new_delegate_mining: &Pubkey,
old_delegate_mining: &Pubkey,
new_delegate: &Pubkey,
amount: u64,
) -> BanksClientResult<()> {
let tx = Transaction::new_signed_with_payer(
Expand All @@ -137,6 +138,7 @@ impl TestRewards {
&mining_owner.pubkey(),
old_delegate_mining,
new_delegate_mining,
new_delegate,
amount,
)],
Some(&context.payer.pubkey()),
Expand Down

0 comments on commit 109786a

Please sign in to comment.