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

Logs for rebalance debugging #50

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 4 additions & 2 deletions programs/steward/src/delegation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anchor_lang::idl::*;
use anchor_lang::prelude::*;
use borsh::BorshSerialize;
use spl_stake_pool::big_vec::BigVec;

use crate::{
Expand All @@ -7,15 +9,15 @@ use crate::{
StewardState,
};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum RebalanceType {
Increase(u64),
Decrease(DecreaseComponents),
None,
}

#[event]
#[derive(Debug, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct DecreaseComponents {
pub scoring_unstake_lamports: u64,
pub instant_unstake_lamports: u64,
Expand Down
6 changes: 4 additions & 2 deletions programs/steward/src/instructions/compute_delegations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ pub fn handler(ctx: Context<ComputeDelegations>) -> Result<()> {
.state
.compute_delegations(clock.epoch, &config)?;

maybe_transition_and_emit(
if let Some(event) = maybe_transition_and_emit(
&mut state_account.state,
&clock,
&config.parameters,
&epoch_schedule,
)?;
)? {
emit!(event);
}

Ok(())
}
12 changes: 8 additions & 4 deletions programs/steward/src/instructions/compute_instant_unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,25 @@ pub fn handler(ctx: Context<ComputeInstantUnstake>, validator_list_index: usize)
return Err(StewardError::StateMachinePaused.into());
}

state_account.state.compute_instant_unstake(
if let Some(instant_unstake) = state_account.state.compute_instant_unstake(
&clock,
&epoch_schedule,
&validator_history,
validator_list_index,
&cluster,
&config,
)?;
)? {
emit!(instant_unstake);
}

maybe_transition_and_emit(
if let Some(event) = maybe_transition_and_emit(
&mut state_account.state,
&clock,
&config.parameters,
&epoch_schedule,
)?;
)? {
emit!(event);
}

Ok(())
}
12 changes: 8 additions & 4 deletions programs/steward/src/instructions/compute_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,26 @@ pub fn handler(ctx: Context<ComputeScore>, validator_list_index: usize) -> Resul
StewardError::InvalidState
);

state_account.state.compute_score(
if let Some(score) = state_account.state.compute_score(
&clock,
&epoch_schedule,
&validator_history,
validator_list_index,
&cluster_history,
&config,
num_pool_validators as u64,
)?;
)? {
emit!(score);
}

maybe_transition_and_emit(
if let Some(event) = maybe_transition_and_emit(
&mut state_account.state,
&clock,
&config.parameters,
&epoch_schedule,
)?;
)? {
emit!(event);
}

Ok(())
}
93 changes: 89 additions & 4 deletions programs/steward/src/instructions/rebalance.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::num::NonZeroU32;

use anchor_lang::{
idl::types::*,
idl::*,
prelude::*,
solana_program::{
program::invoke_signed,
stake::{self, tools::get_minimum_delegation},
system_program, sysvar, vote,
},
};
use borsh::{BorshDeserialize, BorshSerialize};
use spl_pod::solana_program::stake::state::StakeStateV2;
use spl_stake_pool::{
find_stake_program_address, find_transient_stake_program_address, minimum_delegation,
Expand All @@ -17,7 +20,7 @@ use validator_history::ValidatorHistory;

use crate::{
constants::STAKE_POOL_WITHDRAW_SEED,
delegation::RebalanceType,
delegation::{DecreaseComponents, RebalanceType},
errors::StewardError,
maybe_transition_and_emit,
utils::{deserialize_stake_pool, get_stake_pool_address, get_validator_stake_info_at_index},
Expand Down Expand Up @@ -193,7 +196,7 @@ pub fn handler(ctx: Context<Rebalance>, validator_list_index: usize) -> Result<(
)?
};

match result {
match result.clone() {
RebalanceType::Decrease(decrease_components) => {
invoke_signed(
&spl_stake_pool::instruction::decrease_validator_stake_with_reserve(
Expand Down Expand Up @@ -270,12 +273,94 @@ pub fn handler(ctx: Context<Rebalance>, validator_list_index: usize) -> Result<(
RebalanceType::None => {}
}

maybe_transition_and_emit(
emit!(rebalance_to_event(
ctx.accounts.vote_account.key(),
clock.epoch as u16,
result
));

if let Some(event) = maybe_transition_and_emit(
&mut state_account.state,
&clock,
&config.parameters,
&epoch_schedule,
)?;
)? {
emit!(event);
}

Ok(())
}

#[event]
pub struct RebalanceEvent {
pub vote_account: Pubkey,
pub epoch: u16,
pub rebalance_type_tag: RebalanceTypeTag,
pub increase_lamports: u64,
pub decrease_components: DecreaseComponents,
}

fn rebalance_to_event(
vote_account: Pubkey,
epoch: u16,
rebalance_type: RebalanceType,
) -> RebalanceEvent {
match rebalance_type {
RebalanceType::None => RebalanceEvent {
vote_account,
epoch,
rebalance_type_tag: RebalanceTypeTag::None,
increase_lamports: 0,
decrease_components: DecreaseComponents::default(),
},
RebalanceType::Increase(lamports) => RebalanceEvent {
vote_account,
epoch,
rebalance_type_tag: RebalanceTypeTag::Increase,
increase_lamports: lamports,
decrease_components: DecreaseComponents::default(),
},
RebalanceType::Decrease(decrease_components) => RebalanceEvent {
vote_account,
epoch,
rebalance_type_tag: RebalanceTypeTag::Decrease,
increase_lamports: 0,
decrease_components: decrease_components,
},
}
}

#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub enum RebalanceTypeTag {
None,
Increase,
Decrease,
}

impl IdlBuild for RebalanceTypeTag {
fn create_type() -> Option<IdlTypeDef> {
Some(IdlTypeDef {
name: "RebalanceTypeTag".to_string(),
ty: IdlTypeDefTy::Enum {
variants: vec![
IdlEnumVariant {
name: "None".to_string(),
fields: None,
},
IdlEnumVariant {
name: "Increase".to_string(),
fields: None,
},
IdlEnumVariant {
name: "Decrease".to_string(),
fields: None,
},
],
},
docs: Default::default(),
generics: Default::default(),
serialization: Default::default(),
repr: Default::default(),
})
}
}
Loading