Skip to content

Commit

Permalink
feat: allow pub to opt out (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
guibescos authored Sep 10, 2024
1 parent c797b1a commit 3481eef
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
4 changes: 2 additions & 2 deletions staking/integration-tests/src/integrity_pool/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ pub fn set_publisher_stake_account(
signer: &Keypair,
publisher: Pubkey,
current_stake_account_positions_option: Option<Pubkey>,
new_stake_account_positions: Pubkey,
new_stake_account_positions_option: Option<Pubkey>,
) -> TransactionResult {
let pool_config = get_pool_config_address();
let pool_data: Pubkey = fetch_account_data::<PoolConfig>(svm, &pool_config).pool_data;
Expand All @@ -465,7 +465,7 @@ pub fn set_publisher_stake_account(
publisher,
pool_data,
current_stake_account_positions_option,
new_stake_account_positions,
new_stake_account_positions_option,
pool_config,
};

Expand Down
2 changes: 1 addition & 1 deletion staking/integration-tests/tests/advance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn test_reward_events_with_delegation_fee() {
&publisher_keypair,
publisher_keypair.pubkey(),
None,
publisher_stake_account_positions,
Some(publisher_stake_account_positions),
)
.unwrap();

Expand Down
40 changes: 29 additions & 11 deletions staking/integration-tests/tests/set_publisher_stake_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn test_set_publisher_stake_account() {
&payer,
publisher_keypair.pubkey(),
None,
stake_account_positions,
Some(stake_account_positions),
),
IntegrityPoolError::PublisherOrRewardAuthorityNeedsToSign,
0
Expand All @@ -82,7 +82,7 @@ fn test_set_publisher_stake_account() {
&publisher_keypair,
publisher_keypair.pubkey(),
None,
stake_account_positions,
Some(stake_account_positions),
)
.unwrap();

Expand All @@ -101,7 +101,7 @@ fn test_set_publisher_stake_account() {
&publisher_keypair,
publisher_keypair.pubkey(),
Some(stake_account_positions),
stake_account_positions_2,
Some(stake_account_positions_2),
),
IntegrityPoolError::StakeAccountOwnerNeedsToSign,
0
Expand All @@ -115,7 +115,7 @@ fn test_set_publisher_stake_account() {
&payer,
publisher_keypair.pubkey(),
None,
stake_account_positions_2,
Some(stake_account_positions_2),
),
ErrorCode::AccountNotEnoughKeys,
0
Expand All @@ -129,7 +129,7 @@ fn test_set_publisher_stake_account() {
&payer,
publisher_keypair.pubkey(),
Some(stake_account_positions_2),
stake_account_positions_2,
Some(stake_account_positions_2),
),
IntegrityPoolError::PublisherStakeAccountMismatch,
0
Expand All @@ -153,7 +153,7 @@ fn test_set_publisher_stake_account() {
&payer,
publisher_keypair.pubkey(),
Some(stake_account_positions),
stake_account_positions_3,
Some(stake_account_positions_3),
),
IntegrityPoolError::NewStakeAccountShouldBeUndelegated,
0
Expand All @@ -167,7 +167,7 @@ fn test_set_publisher_stake_account() {
&payer,
publisher_keypair.pubkey(),
Some(stake_account_positions),
stake_account_positions_2,
Some(stake_account_positions_2),
)
.unwrap();

Expand Down Expand Up @@ -196,7 +196,7 @@ fn test_set_publisher_stake_account() {
&payer,
publisher_keypair.pubkey(),
Some(stake_account_positions_2),
stake_account_positions_3,
Some(stake_account_positions_3),
),
IntegrityPoolError::CurrentStakeAccountShouldBeUndelegated,
0
Expand All @@ -210,7 +210,7 @@ fn test_set_publisher_stake_account() {
&payer,
Pubkey::new_unique(),
Some(stake_account_positions),
stake_account_positions_2,
Some(stake_account_positions_2),
),
IntegrityPoolError::PublisherNotFound,
0
Expand Down Expand Up @@ -402,7 +402,7 @@ fn test_set_publisher_stake_account_reward_authority() {
&reward_program_authority,
publisher_keypair.pubkey(),
None,
stake_account_positions,
Some(stake_account_positions),
)
.unwrap();

Expand All @@ -421,9 +421,27 @@ fn test_set_publisher_stake_account_reward_authority() {
&reward_program_authority,
publisher_keypair.pubkey(),
Some(stake_account_positions),
stake_account_positions,
Some(stake_account_positions),
),
IntegrityPoolError::StakeAccountOwnerNeedsToSign,
0
);

// publisher can opt out
set_publisher_stake_account(
&mut svm,
&payer,
&payer,
publisher_keypair.pubkey(),
Some(stake_account_positions),
None,
)
.unwrap();

let pool_data: PoolData = fetch_account_data_bytemuck(&mut svm, &pool_data_pubkey);

assert_eq!(
pool_data.publisher_stake_accounts[publisher_index],
Pubkey::default(),
);
}
3 changes: 2 additions & 1 deletion staking/programs/integrity-pool/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ pub struct SetPublisherStakeAccount<'info> {
pub pool_data: AccountLoader<'info, PoolData>,
#[account(seeds = [POOL_CONFIG.as_bytes()], bump, has_one = pool_data)]
pub pool_config: Account<'info, PoolConfig>,
pub new_stake_account_positions: AccountLoader<'info, staking::state::positions::PositionData>,
pub new_stake_account_positions_option:
Option<AccountLoader<'info, staking::state::positions::PositionData>>,
pub current_stake_account_positions_option:
Option<AccountLoader<'info, staking::state::positions::PositionData>>,
}
Expand Down
22 changes: 13 additions & 9 deletions staking/programs/integrity-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ pub mod integrity_pool {
let publisher = &ctx.accounts.publisher;
let pool_data = &mut ctx.accounts.pool_data.load_mut()?;
let pool_config = &ctx.accounts.pool_config;
let new_stake_account =
DynamicPositionArray::load(&ctx.accounts.new_stake_account_positions)?;

let publisher_target = TargetWithParameters::IntegrityPool {
publisher: publisher.key(),
Expand Down Expand Up @@ -269,15 +267,21 @@ pub mod integrity_pool {
return Err(ErrorCode::AccountNotEnoughKeys.into());
}

if let Some(new_stake_account_positions) = &ctx.accounts.new_stake_account_positions_option
{
let new_stake_account = DynamicPositionArray::load(new_stake_account_positions)?;

// new stake account should be undelegated
require!(
!new_stake_account.has_target_with_parameters_exposure(publisher_target)?,
IntegrityPoolError::NewStakeAccountShouldBeUndelegated
);
// new stake account should be undelegated
require!(
!new_stake_account.has_target_with_parameters_exposure(publisher_target)?,
IntegrityPoolError::NewStakeAccountShouldBeUndelegated
);

pool_data.publisher_stake_accounts[publisher_index] = new_stake_account_positions.key();
} else {
pool_data.publisher_stake_accounts[publisher_index] = Pubkey::default();
}

pool_data.publisher_stake_accounts[publisher_index] =
ctx.accounts.new_stake_account_positions.key();

Ok(())
}
Expand Down

0 comments on commit 3481eef

Please sign in to comment.