Skip to content

Commit

Permalink
Validator list balance solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ebatsell committed Aug 5, 2024
1 parent fcafcb3 commit 16a0751
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 30 deletions.
1 change: 1 addition & 0 deletions programs/steward/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const MAX_VALIDATORS: usize = 5_000;
pub const BASIS_POINTS_MAX: u16 = 10_000;
pub const COMMISSION_MAX: u8 = 100;
pub const SORTED_INDEX_DEFAULT: u16 = u16::MAX;
pub const LAMPORT_BALANCE_DEFAULT: u64 = u64::MAX;
// Need at least 1% of slots remaining (4320 slots) to execute steps in state machine
pub const EPOCH_PROGRESS_MAX: f64 = 0.99;
// Cannot go more than 100 epochs without scoring
Expand Down
2 changes: 2 additions & 0 deletions programs/steward/src/delegation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anchor_lang::prelude::*;
use spl_stake_pool::big_vec::BigVec;

use crate::constants::LAMPORT_BALANCE_DEFAULT;
use crate::events::DecreaseComponents;
use crate::{
errors::StewardError,
Expand Down Expand Up @@ -250,6 +251,7 @@ impl UnstakeState {
// either to the target or to the previous balance before the deposit, whichever is lower in terms of total lamports unstaked
if current_lamports > state.validator_lamport_balances[index]
&& self.stake_deposit_unstake_total < self.stake_deposit_unstake_cap
&& state.validator_lamport_balances[index] != LAMPORT_BALANCE_DEFAULT
{
let lamports_above_target = current_lamports
.checked_sub(target_lamports)
Expand Down
2 changes: 2 additions & 0 deletions programs/steward/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod realloc_state;
pub mod rebalance;
pub mod remove_validator_from_blacklist;
pub mod reset_steward_state;
pub mod reset_validator_lamport_balances;
pub mod resume_steward;
pub mod set_new_authority;
pub mod spl_passthrough;
Expand All @@ -38,6 +39,7 @@ pub use realloc_state::*;
pub use rebalance::*;
pub use remove_validator_from_blacklist::*;
pub use reset_steward_state::*;
pub use reset_validator_lamport_balances::*;
pub use resume_steward::*;
pub use set_new_authority::*;
pub use spl_passthrough::*;
Expand Down
3 changes: 2 additions & 1 deletion programs/steward/src/instructions/realloc_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
bitmask::BitMask,
constants::{MAX_ALLOC_BYTES, MAX_VALIDATORS, SORTED_INDEX_DEFAULT},
constants::{LAMPORT_BALANCE_DEFAULT, MAX_ALLOC_BYTES, MAX_VALIDATORS, SORTED_INDEX_DEFAULT},
errors::StewardError,
state::{Config, StewardStateAccount},
utils::get_validator_list,
Expand Down Expand Up @@ -74,6 +74,7 @@ pub fn handler(ctx: Context<ReallocState>) -> Result<()> {

state_account.state.state_tag = StewardStateEnum::ComputeScores;
state_account.state.num_pool_validators = validator_list.len() as u64;
state_account.state.validator_lamport_balances = [LAMPORT_BALANCE_DEFAULT; MAX_VALIDATORS];
state_account.state.scores = [0; MAX_VALIDATORS];
state_account.state.sorted_score_indices = [SORTED_INDEX_DEFAULT; MAX_VALIDATORS];
state_account.state.yield_scores = [0; MAX_VALIDATORS];
Expand Down
3 changes: 2 additions & 1 deletion programs/steward/src/instructions/reset_steward_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
constants::{MAX_VALIDATORS, SORTED_INDEX_DEFAULT},
constants::{LAMPORT_BALANCE_DEFAULT, MAX_VALIDATORS, SORTED_INDEX_DEFAULT},
errors::StewardError,
state::{Config, StewardStateAccount},
utils::{deserialize_stake_pool, get_config_admin, get_stake_pool_address},
Expand Down Expand Up @@ -47,6 +47,7 @@ pub fn handler(ctx: Context<ResetStewardState>) -> Result<()> {

state_account.state.state_tag = StewardStateEnum::ComputeScores;
state_account.state.num_pool_validators = validator_list.len() as u64;
state_account.state.validator_lamport_balances = [LAMPORT_BALANCE_DEFAULT; MAX_VALIDATORS];
state_account.state.scores = [0; MAX_VALIDATORS];
state_account.state.sorted_score_indices = [SORTED_INDEX_DEFAULT; MAX_VALIDATORS];
state_account.state.yield_scores = [0; MAX_VALIDATORS];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{
constants::{LAMPORT_BALANCE_DEFAULT, MAX_VALIDATORS},
state::*,
utils::get_config_admin,
};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct ResetValidatorLamportBalances<'info> {
#[account(
mut,
seeds = [StewardStateAccount::SEED, config.key().as_ref()],
bump
)]
pub steward_state: AccountLoader<'info, StewardStateAccount>,

pub config: AccountLoader<'info, Config>,

#[account(address = get_config_admin(&config)?)]
pub authority: Signer<'info>,
}

pub fn handler(ctx: Context<ResetValidatorLamportBalances>) -> Result<()> {
let state_account = &mut ctx.accounts.steward_state.load_mut()?;

state_account.state.validator_lamport_balances = [LAMPORT_BALANCE_DEFAULT; MAX_VALIDATORS];

Ok(())
}
34 changes: 21 additions & 13 deletions programs/steward/src/instructions/spl_passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// is that the config, stake pool address, staker, signer, and sometimes state account match up.
// Otherwise these instructions are intented to be minimally restrictive.

use crate::constants::MAX_VALIDATORS;
use crate::constants::{LAMPORT_BALANCE_DEFAULT, MAX_VALIDATORS};
use crate::errors::StewardError;
use crate::state::Config;
use crate::utils::{
Expand Down Expand Up @@ -400,9 +400,11 @@ pub fn increase_validator_stake_handler(
.ok_or(StewardError::ValidatorIndexOutOfBounds)?;

// Set the balance
*balance = balance
.checked_add(lamports)
.ok_or(StewardError::ArithmeticError)?;
if *balance != LAMPORT_BALANCE_DEFAULT {
*balance = balance
.checked_add(lamports)
.ok_or(StewardError::ArithmeticError)?;
}
}

invoke_signed(
Expand Down Expand Up @@ -521,9 +523,11 @@ pub fn decrease_validator_stake_handler(
.ok_or(StewardError::ValidatorIndexOutOfBounds)?;

// Set the balance
*balance = balance
.checked_sub(lamports)
.ok_or(StewardError::ArithmeticError)?;
if *balance != LAMPORT_BALANCE_DEFAULT {
*balance = balance
.checked_sub(lamports)
.ok_or(StewardError::ArithmeticError)?;
}
}

invoke_signed(
Expand Down Expand Up @@ -641,9 +645,11 @@ pub fn increase_additional_validator_stake_handler(
.ok_or(StewardError::ValidatorIndexOutOfBounds)?;

// Set the balance
*balance = balance
.checked_add(lamports)
.ok_or(StewardError::ArithmeticError)?;
if *balance != LAMPORT_BALANCE_DEFAULT {
*balance = balance
.checked_add(lamports)
.ok_or(StewardError::ArithmeticError)?;
}
}

invoke_signed(
Expand Down Expand Up @@ -765,9 +771,11 @@ pub fn decrease_additional_validator_stake_handler(
.ok_or(StewardError::ValidatorIndexOutOfBounds)?;

// Set the balance
*balance = balance
.checked_sub(lamports)
.ok_or(StewardError::ArithmeticError)?;
if *balance != LAMPORT_BALANCE_DEFAULT {
*balance = balance
.checked_sub(lamports)
.ok_or(StewardError::ArithmeticError)?;
}
}

invoke_signed(
Expand Down
7 changes: 7 additions & 0 deletions programs/steward/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ pub mod steward {
)
}

/// Reset validator_lamport_balances to default
pub fn reset_validator_lamport_balances(
ctx: Context<ResetValidatorLamportBalances>,
) -> Result<()> {
instructions::reset_validator_lamport_balances::handler(ctx)
}

/// Closes Steward PDA accounts associated with a given Config (StewardStateAccount, and Staker).
/// Config is not closed as it is a Keypair, so lamports can simply be withdrawn.
/// Reclaims lamports to authority
Expand Down
16 changes: 12 additions & 4 deletions programs/steward/src/state/steward_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;

use crate::{
bitmask::BitMask,
constants::{MAX_VALIDATORS, SORTED_INDEX_DEFAULT},
constants::{LAMPORT_BALANCE_DEFAULT, MAX_VALIDATORS, SORTED_INDEX_DEFAULT},
delegation::{
decrease_stake_calculation, increase_stake_calculation, RebalanceType, UnstakeState,
},
Expand Down Expand Up @@ -894,6 +894,11 @@ impl StewardState {
In all cases where the current_lamports is now below the target or internal balance, we update the internal balance.
Otherwise, keep the internal balance the same to ensure we still see the stake deposit delta, until it can be unstaked.
*/

if self.validator_lamport_balances[index] == LAMPORT_BALANCE_DEFAULT {
self.validator_lamport_balances[index] = current_lamports;
}

self.validator_lamport_balances[index] = match (
current_lamports < self.validator_lamport_balances[index],
current_lamports < target_lamports,
Expand Down Expand Up @@ -1005,9 +1010,12 @@ impl StewardState {
}
}
RebalanceType::Increase(amount) => {
self.validator_lamport_balances[index] = self.validator_lamport_balances[index]
.checked_add(amount)
.ok_or(StewardError::ArithmeticError)?;
if self.validator_lamport_balances[index] != LAMPORT_BALANCE_DEFAULT {
self.validator_lamport_balances[index] = self.validator_lamport_balances
[index]
.checked_add(amount)
.ok_or(StewardError::ArithmeticError)?;
}
}
RebalanceType::None => {}
}
Expand Down
16 changes: 5 additions & 11 deletions tests/tests/steward/test_spl_passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anchor_lang::{
AccountDeserialize, AnchorDeserialize, InstructionData, ToAccountMetas,
};
use jito_steward::{
constants::MAX_VALIDATORS,
constants::{LAMPORT_BALANCE_DEFAULT, MAX_VALIDATORS},
derive_steward_state_address,
utils::{StakePool, ValidatorList},
Config, Delegation, StewardStateAccount, StewardStateEnum,
Expand Down Expand Up @@ -300,7 +300,7 @@ async fn _increase_and_check_stake(

let state_account_before: StewardStateAccount =
fixture.load_and_deserialize(&fixture.steward_state).await;
let lamports_before_increase = *state_account_before
let _lamports_before_increase = *state_account_before
.state
.validator_lamport_balances
.get(validator_list_index)
Expand Down Expand Up @@ -354,10 +354,7 @@ async fn _increase_and_check_stake(
.get(validator_list_index)
.expect("Lamport balance out of bounds");

assert_eq!(
lamports_after_increase,
lamports_before_increase + lamports_to_stake
);
assert_eq!(lamports_after_increase, LAMPORT_BALANCE_DEFAULT);
}

async fn _increase_and_check_additional_stake(
Expand Down Expand Up @@ -386,7 +383,7 @@ async fn _increase_and_check_additional_stake(

let state_account_before: StewardStateAccount =
fixture.load_and_deserialize(&fixture.steward_state).await;
let lamports_before_increase = *state_account_before
let _lamports_before_increase = *state_account_before
.state
.validator_lamport_balances
.get(validator_list_index)
Expand Down Expand Up @@ -447,10 +444,7 @@ async fn _increase_and_check_additional_stake(
.get(validator_list_index)
.expect("Lamport balance out of bounds");

assert_eq!(
lamports_after_increase,
lamports_before_increase + lamports_to_stake
);
assert_eq!(lamports_after_increase, LAMPORT_BALANCE_DEFAULT);
}

pub async fn _set_staker(fixture: &TestFixture, new_staker: Pubkey) {
Expand Down

0 comments on commit 16a0751

Please sign in to comment.