Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read active stake rather than validator list balance #69

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion programs/steward/src/instructions/rebalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anchor_lang::{
},
};
use borsh::BorshDeserialize;
use spl_pod::solana_program::stake::state::StakeStateV2;
use spl_pod::solana_program::{borsh1::try_from_slice_unchecked, stake::state::StakeStateV2};
use spl_stake_pool::{
find_stake_program_address, find_transient_stake_program_address, minimum_delegation,
state::ValidatorListHeader,
Expand Down Expand Up @@ -179,6 +179,13 @@ pub fn handler(ctx: Context<Rebalance>, validator_list_index: usize) -> Result<(

transient_seed = u64::from(validator_stake_info.transient_seed_suffix);

let stake_account_data = &mut ctx.accounts.stake_account.data.borrow();
let stake_state = try_from_slice_unchecked::<StakeStateV2>(stake_account_data)?;
let stake_account_active_lamports = match stake_state {
StakeStateV2::Stake(_meta, stake, _stake_flags) => stake.delegation.stake,
_ => return Err(StewardError::StakeStateIsNotStake.into()),
};

let minimum_delegation = minimum_delegation(get_minimum_delegation()?);
let stake_rent = Rent::get()?.minimum_balance(StakeStateV2::size_of());

Expand All @@ -196,6 +203,7 @@ pub fn handler(ctx: Context<Rebalance>, validator_list_index: usize) -> Result<(
&validator_list,
stake_pool_lamports_with_fixed_cost,
reserve_lamports_with_rent,
stake_account_active_lamports,
minimum_delegation,
stake_rent,
&config.parameters,
Expand Down
6 changes: 4 additions & 2 deletions programs/steward/src/state/steward_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ impl StewardState {
validator_list: &BigVec<'_>,
stake_pool_lamports: u64,
reserve_lamports: u64,
stake_account_current_lamports: u64,
minimum_delegation: u64,
stake_rent: u64,
parameters: &Parameters,
Expand Down Expand Up @@ -885,10 +886,11 @@ impl StewardState {
let target_lamports =
get_target_lamports(&self.delegations[index], stake_pool_lamports)?;

let (mut current_lamports, some_transient_lamports) =
let (_, some_transient_lamports) =
stake_lamports_at_validator_list_index(validator_list, index)?;

current_lamports = current_lamports.saturating_sub(base_lamport_balance);
let current_lamports =
stake_account_current_lamports.saturating_sub(minimum_delegation);

if !some_transient_lamports {
/* This field is used to determine the amount of stake deposits this validator has gotten which push it over the target.
Expand Down
7 changes: 7 additions & 0 deletions tests/tests/steward/test_state_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ fn test_rebalance() {
&validator_list_bigvec,
4000 * LAMPORTS_PER_SOL,
1000 * LAMPORTS_PER_SOL,
u64::from(fixtures.validator_list[0].active_stake_lamports),
0,
0,
&fixtures.config.parameters,
Expand Down Expand Up @@ -557,6 +558,7 @@ fn test_rebalance() {
&validator_list_bigvec,
4000 * LAMPORTS_PER_SOL,
1000 * LAMPORTS_PER_SOL,
u64::from(fixtures.validator_list[1].active_stake_lamports),
0,
0,
&fixtures.config.parameters,
Expand Down Expand Up @@ -595,6 +597,7 @@ fn test_rebalance() {
&validator_list_bigvec,
4000 * LAMPORTS_PER_SOL,
1000 * LAMPORTS_PER_SOL,
u64::from(fixtures.validator_list[1].active_stake_lamports),
0,
0,
&fixtures.config.parameters,
Expand Down Expand Up @@ -631,6 +634,7 @@ fn test_rebalance() {
&validator_list_bigvec,
4000 * LAMPORTS_PER_SOL,
1000 * LAMPORTS_PER_SOL,
u64::from(fixtures.validator_list[1].active_stake_lamports),
0,
0,
&fixtures.config.parameters,
Expand Down Expand Up @@ -683,6 +687,7 @@ fn test_rebalance() {
&validator_list_bigvec,
4000 * LAMPORTS_PER_SOL,
1000 * LAMPORTS_PER_SOL,
u64::from(fixtures.validator_list[0].active_stake_lamports),
0,
0,
&fixtures.config.parameters,
Expand Down Expand Up @@ -715,6 +720,7 @@ fn test_rebalance() {
&validator_list_bigvec,
3000 * LAMPORTS_PER_SOL,
0,
u64::from(fixtures.validator_list[0].active_stake_lamports),
0,
0,
&fixtures.config.parameters,
Expand All @@ -733,6 +739,7 @@ fn test_rebalance() {
&validator_list_bigvec,
3000 * LAMPORTS_PER_SOL,
0,
u64::from(fixtures.validator_list[0].active_stake_lamports),
0,
0,
&fixtures.config.parameters,
Expand Down
Loading