Skip to content

Commit

Permalink
Emit more things
Browse files Browse the repository at this point in the history
  • Loading branch information
ebatsell committed Jun 27, 2024
1 parent 7fa0f34 commit f5c08dd
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 42 deletions.
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(),
})
}
}
47 changes: 21 additions & 26 deletions programs/steward/src/state/steward_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::{
UnstakeState,
},
errors::StewardError,
score::{instant_unstake_validator, validator_score},
score::{
instant_unstake_validator, validator_score, InstantUnstakeComponents, ScoreComponents,
},
utils::{epoch_progress, get_target_lamports, stake_lamports_at_validator_list_index, U8Bool},
Config, Parameters,
};
Expand All @@ -27,31 +29,32 @@ fn invalid_state_error(_expected: String, _actual: String) -> Error {
}

#[event]
#[derive(Debug)]
pub struct StateTransition {
epoch: u64,
slot: u64,
previous_state: String,
new_state: String,
pub epoch: u64,
pub slot: u64,
pub previous_state: String,
pub new_state: String,
}

pub fn maybe_transition_and_emit(
steward_state: &mut StewardState,
clock: &Clock,
params: &Parameters,
epoch_schedule: &EpochSchedule,
) -> Result<()> {
) -> Result<Option<StateTransition>> {
let initial_state = steward_state.state_tag.to_string();
steward_state.transition(clock, params, epoch_schedule)?;

if initial_state != steward_state.state_tag.to_string() {
emit!(StateTransition {
return Ok(Some(StateTransition {
epoch: clock.epoch,
slot: clock.slot,
previous_state: initial_state,
new_state: steward_state.state_tag.to_string(),
});
}));
}
Ok(())
Ok(None)
}

/// Tracks state of the stake pool.
Expand Down Expand Up @@ -542,7 +545,7 @@ impl StewardState {
cluster: &ClusterHistory,
config: &Config,
num_pool_validators: u64,
) -> Result<()> {
) -> Result<Option<ScoreComponents>> {
if matches!(self.state_tag, StewardStateEnum::ComputeScores) {
let current_epoch = clock.epoch;
let current_slot = clock.slot;
Expand Down Expand Up @@ -576,7 +579,7 @@ impl StewardState {

// Skip scoring if already processed
if self.progress.get(index)? {
return Ok(());
return Ok(None);
}

// Skip scoring if marked for deletion
Expand All @@ -602,7 +605,7 @@ impl StewardState {

self.progress.set(index, true)?;

return Ok(());
return Ok(None);
}

// Check that latest_update_slot is within the current epoch to guarantee previous epoch data is complete
Expand All @@ -622,7 +625,6 @@ impl StewardState {
}

let score = validator_score(validator, index, cluster, config, current_epoch as u16)?;
emit!(score);

self.scores[index] = (score.score * 1_000_000_000.) as u32;
self.yield_scores[index] = (score.yield_score * 1_000_000_000.) as u32;
Expand All @@ -645,7 +647,7 @@ impl StewardState {
)?;

self.progress.set(index, true)?;
return Ok(());
return Ok(Some(score));
}
Err(invalid_state_error(
"ComputeScores".to_string(),
Expand Down Expand Up @@ -705,7 +707,7 @@ impl StewardState {
index: usize,
cluster: &ClusterHistory,
config: &Config,
) -> Result<()> {
) -> Result<Option<InstantUnstakeComponents>> {
if matches!(self.state_tag, StewardStateEnum::ComputeInstantUnstake) {
if clock.epoch >= self.next_cycle_epoch {
return Err(invalid_state_error(
Expand All @@ -722,13 +724,13 @@ impl StewardState {

// Skip if already processed
if self.progress.get(index)? {
return Ok(());
return Ok(None);
}

// Skip if marked for deletion
if self.validators_to_remove.get(index)? {
self.progress.set(index, true)?;
return Ok(());
return Ok(None);
}

let first_slot = epoch_schedule.get_first_slot_in_epoch(clock.epoch);
Expand Down Expand Up @@ -761,11 +763,11 @@ impl StewardState {
first_slot,
clock.epoch as u16,
)?;
emit!(instant_unstake_result);

self.instant_unstake
.set(index, instant_unstake_result.instant_unstake)?;
self.progress.set(index, true)?;
return Ok(());
return Ok(Some(instant_unstake_result));
}
Err(invalid_state_error(
"ComputeInstantUnstake".to_string(),
Expand Down Expand Up @@ -932,13 +934,6 @@ impl StewardState {
stake_deposit_unstake_lamports,
total_unstake_lamports,
}) => {
emit!(DecreaseComponents {
scoring_unstake_lamports,
instant_unstake_lamports,
stake_deposit_unstake_lamports,
total_unstake_lamports,
});

self.validator_lamport_balances[index] = self.validator_lamport_balances[index]
.saturating_sub(total_unstake_lamports);

Expand Down

0 comments on commit f5c08dd

Please sign in to comment.