Skip to content

Commit

Permalink
More events emitted (#54)
Browse files Browse the repository at this point in the history
Puts all events except for Scoring / instant unstake response types in a
single file
  • Loading branch information
ebatsell authored Jul 1, 2024
1 parent 4facad0 commit 104f1b7
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 75 deletions.
113 changes: 113 additions & 0 deletions programs/steward/idl/steward.json
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,32 @@
}
],
"events": [
{
"name": "AutoAddValidatorEvent",
"discriminator": [
123,
65,
239,
15,
82,
216,
206,
28
]
},
{
"name": "AutoRemoveValidatorEvent",
"discriminator": [
211,
46,
52,
163,
17,
38,
197,
186
]
},
{
"name": "DecreaseComponents",
"discriminator": [
Expand All @@ -1420,6 +1446,19 @@
8
]
},
{
"name": "EpochMaintenanceEvent",
"discriminator": [
255,
149,
70,
161,
199,
176,
9,
42
]
},
{
"name": "InstantUnstakeComponents",
"discriminator": [
Expand Down Expand Up @@ -1661,6 +1700,46 @@
]
}
},
{
"name": "AutoAddValidatorEvent",
"type": {
"kind": "struct",
"fields": [
{
"name": "validator_list_index",
"type": "u64"
},
{
"name": "vote_account",
"type": "pubkey"
}
]
}
},
{
"name": "AutoRemoveValidatorEvent",
"type": {
"kind": "struct",
"fields": [
{
"name": "validator_list_index",
"type": "u64"
},
{
"name": "vote_account",
"type": "pubkey"
},
{
"name": "vote_account_closed",
"type": "bool"
},
{
"name": "stake_account_deactivated",
"type": "bool"
}
]
}
},
{
"name": "BitMask",
"docs": [
Expand Down Expand Up @@ -2037,6 +2116,40 @@
]
}
},
{
"name": "EpochMaintenanceEvent",
"type": {
"kind": "struct",
"fields": [
{
"name": "validator_index_to_remove",
"type": {
"option": "u64"
}
},
{
"name": "validator_list_length",
"type": "u64"
},
{
"name": "num_pool_validators",
"type": "u64"
},
{
"name": "validators_to_remove",
"type": "u64"
},
{
"name": "validators_to_add",
"type": "u64"
},
{
"name": "maintenance_complete",
"type": "bool"
}
]
}
},
{
"name": "InstantUnstakeComponents",
"type": {
Expand Down
12 changes: 1 addition & 11 deletions programs/steward/src/delegation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use anchor_lang::idl::*;
use anchor_lang::prelude::*;
use borsh::BorshSerialize;
use spl_stake_pool::big_vec::BigVec;

use crate::events::DecreaseComponents;
use crate::{
errors::StewardError,
utils::{get_target_lamports, stake_lamports_at_validator_list_index},
Expand All @@ -16,15 +15,6 @@ pub enum RebalanceType {
None,
}

#[event]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct DecreaseComponents {
pub scoring_unstake_lamports: u64,
pub instant_unstake_lamports: u64,
pub stake_deposit_unstake_lamports: u64,
pub total_unstake_lamports: u64,
}

/// Given a target validator, determines how much stake to remove on this validator given the constraints of unstaking caps.
/// Validators with lower yield_scores are prioritized for unstaking. We simulate unstaking movements on each validator, starting
/// from the lowest yield_score validator, until we reach the target validator. If the target validator is reached and there is still
Expand Down
93 changes: 93 additions & 0 deletions programs/steward/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use anchor_lang::idl::{
types::{IdlEnumVariant, IdlTypeDef, IdlTypeDefTy},
IdlBuild,
};
use anchor_lang::prelude::{event, AnchorDeserialize, AnchorSerialize};
use anchor_lang::solana_program::pubkey::Pubkey;
use borsh::{BorshDeserialize, BorshSerialize};

#[event]
pub struct AutoRemoveValidatorEvent {
pub validator_list_index: u64,
pub vote_account: Pubkey,
pub vote_account_closed: bool,
pub stake_account_deactivated: bool,
}

#[event]
pub struct AutoAddValidatorEvent {
pub validator_list_index: u64,
pub vote_account: Pubkey,
}

#[event]
pub struct EpochMaintenanceEvent {
pub validator_index_to_remove: Option<u64>,
pub validator_list_length: u64,
pub num_pool_validators: u64,
pub validators_to_remove: u64,
pub validators_to_add: u64,
pub maintenance_complete: bool,
}

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

#[event]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct DecreaseComponents {
pub scoring_unstake_lamports: u64,
pub instant_unstake_lamports: u64,
pub stake_deposit_unstake_lamports: u64,
pub total_unstake_lamports: u64,
}

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

#[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(),
})
}
}
15 changes: 11 additions & 4 deletions programs/steward/src/instructions/auto_add_validator_to_pool.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::constants::{MAX_VALIDATORS, STAKE_POOL_WITHDRAW_SEED};
use crate::errors::StewardError;
use crate::events::AutoAddValidatorEvent;
use crate::state::{Config, StewardStateAccount};
use crate::utils::{deserialize_stake_pool, get_stake_pool_address};
use anchor_lang::prelude::*;
Expand Down Expand Up @@ -111,13 +112,14 @@ pub fn handler(ctx: Context<AutoAddValidator>) -> Result<()> {
StewardError::EpochMaintenanceNotComplete
);

{
let validator_list_len = {
let validator_list_data = &mut ctx.accounts.validator_list.try_borrow_mut_data()?;
let (_, validator_list) = ValidatorListHeader::deserialize_vec(validator_list_data)?;

if validator_list.len().checked_add(1).unwrap() > MAX_VALIDATORS as u32 {
return Err(StewardError::MaxValidatorsReached.into());
}
validator_list.len()
};
if validator_list_len.checked_add(1).unwrap() > MAX_VALIDATORS as u32 {
return Err(StewardError::MaxValidatorsReached.into());
}

let start_epoch =
Expand Down Expand Up @@ -150,6 +152,11 @@ pub fn handler(ctx: Context<AutoAddValidator>) -> Result<()> {
// Have to drop the state account before calling the CPI
drop(state_account);

emit!(AutoAddValidatorEvent {
vote_account: ctx.accounts.vote_account.key(),
validator_list_index: validator_list_len as u64
});

invoke_signed(
&spl_stake_pool::instruction::add_validator_to_pool(
&ctx.accounts.stake_pool_program.key(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::num::NonZeroU32;

use crate::constants::STAKE_POOL_WITHDRAW_SEED;
use crate::errors::StewardError;
use crate::events::AutoRemoveValidatorEvent;
use crate::state::Config;
use crate::utils::{
deserialize_stake_pool, get_stake_pool_address, get_validator_stake_info_at_index,
Expand Down Expand Up @@ -167,6 +168,13 @@ pub fn handler(ctx: Context<AutoRemoveValidator>, validator_list_index: usize) -
state_account
.state
.mark_validator_for_removal(validator_list_index)?;

emit!(AutoRemoveValidatorEvent {
vote_account: ctx.accounts.vote_account.key(),
validator_list_index: validator_list_index as u64,
stake_account_deactivated,
vote_account_closed,
});
}

invoke_signed(
Expand Down
10 changes: 10 additions & 0 deletions programs/steward/src/instructions/epoch_maintenance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
errors::StewardError,
events::EpochMaintenanceEvent,
utils::{
check_validator_list_has_stake_status_other_than, deserialize_stake_pool,
get_stake_pool_address, get_validator_list_length,
Expand Down Expand Up @@ -94,6 +95,15 @@ pub fn handler(
state_account.state.checked_validators_removed_from_list = false.into();
state_account.state.rebalance_completed = false.into();
}

emit!(EpochMaintenanceEvent {
validator_index_to_remove: validator_index_to_remove.map(|x| x as u64),
validator_list_length: get_validator_list_length(&ctx.accounts.validator_list)? as u64,
num_pool_validators: state_account.state.num_pool_validators,
validators_to_remove: state_account.state.validators_to_remove.count() as u64,
validators_to_add: state_account.state.validators_added as u64,
maintenance_complete: okay_to_update,
});
}

Ok(())
Expand Down
Loading

0 comments on commit 104f1b7

Please sign in to comment.