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

FIX + TESTS (STEWARD): Update Parameters #43

Merged
merged 9 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ test-ledger
**/targets
**/credentials
**/config
**/*.env
**/*.env
/scripts
3 changes: 3 additions & 0 deletions programs/steward/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ pub const EPOCH_PROGRESS_MAX: f64 = 0.99;
pub const NUM_EPOCHS_BETWEEN_SCORING_MAX: u64 = 100;
// Cannot score validators in under 100 slots, to submit 1 instruction per validator
pub const COMPUTE_SCORE_SLOT_RANGE_MIN: usize = 100;
#[cfg(feature = "mainnet-beta")]
pub const VALIDATOR_HISTORY_FIRST_RELIABLE_EPOCH: usize = 520;
#[cfg(not(feature = "mainnet-beta"))]
pub const VALIDATOR_HISTORY_FIRST_RELIABLE_EPOCH: usize = 0;
16 changes: 14 additions & 2 deletions programs/steward/src/instructions/initialize_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anchor_lang::{prelude::*, solana_program::program::invoke};

use crate::{utils::StakePool, Config, Staker};
use crate::{utils::StakePool, Config, Staker, UpdateParametersArgs};

#[derive(Accounts)]
pub struct InitializeConfig<'info> {
Expand Down Expand Up @@ -39,11 +39,23 @@ pub struct InitializeConfig<'info> {
pub signer: Signer<'info>,
}

pub fn handler(ctx: Context<InitializeConfig>, authority: Pubkey) -> Result<()> {
pub fn handler(
ctx: Context<InitializeConfig>,
authority: Pubkey,
update_parameters_args: &UpdateParametersArgs,
) -> Result<()> {
let mut config = ctx.accounts.config.load_init()?;
config.stake_pool = ctx.accounts.stake_pool.key();
config.authority = authority;

// Set Initial Parameters
let max_slots_in_epoch = EpochSchedule::get()?.slots_per_epoch;
let current_epoch = Clock::get()?.epoch;
config
.parameters
.update(update_parameters_args, current_epoch, max_slots_in_epoch)?;

// Set the staker account
ctx.accounts.staker.bump = ctx.bumps.staker;
invoke(
&spl_stake_pool::instruction::set_staker(
Expand Down
6 changes: 4 additions & 2 deletions programs/steward/src/instructions/update_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ pub fn handler(
ctx: Context<UpdateParameters>,
update_parameters_args: &UpdateParametersArgs,
) -> Result<()> {
let mut parameters = ctx.accounts.config.load_mut()?.parameters;
let mut config = ctx.accounts.config.load_mut()?;
let max_slots_in_epoch = EpochSchedule::get()?.slots_per_epoch;
let current_epoch = Clock::get()?.epoch;
parameters.update(update_parameters_args, current_epoch, max_slots_in_epoch)?;
config
.parameters
.update(update_parameters_args, current_epoch, max_slots_in_epoch)?;
Ok(())
}
8 changes: 6 additions & 2 deletions programs/steward/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ pub mod steward {

// Initializes Config and Staker accounts. Must be called before any other instruction
// Requires Pool to be initialized
pub fn initialize_config(ctx: Context<InitializeConfig>, authority: Pubkey) -> Result<()> {
instructions::initialize_config::handler(ctx, authority)
pub fn initialize_config(
ctx: Context<InitializeConfig>,
authority: Pubkey,
update_parameters_args: UpdateParametersArgs,
) -> Result<()> {
instructions::initialize_config::handler(ctx, authority, &update_parameters_args)
}

/// Creates state account
Expand Down
2 changes: 1 addition & 1 deletion programs/steward/src/state/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
errors::StewardError,
};

#[derive(BorshSerialize, BorshDeserialize, Debug, Default)]
#[derive(BorshSerialize, BorshDeserialize, Debug, Default, Clone)]
pub struct UpdateParametersArgs {
// Scoring parameters
pub mev_commission_range: Option<u16>,
Expand Down
34 changes: 33 additions & 1 deletion tests/src/steward_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use jito_steward::{
constants::{MAX_VALIDATORS, SORTED_INDEX_DEFAULT, STAKE_POOL_WITHDRAW_SEED},
utils::StakePool,
Config, Delegation, Parameters, Staker, StewardState, StewardStateAccount, StewardStateEnum,
UpdateParametersArgs,
};
use solana_program_test::*;
use solana_sdk::{
Expand Down Expand Up @@ -231,7 +232,28 @@ impl TestFixture {
}
}

pub async fn initialize_config(&self) {
pub async fn initialize_config(&self, parameters: Option<UpdateParametersArgs>) {
// Default parameters from JIP
let update_parameters_args = parameters.unwrap_or(UpdateParametersArgs {
mev_commission_range: Some(0), // Set to pass validation, where epochs starts at 0
epoch_credits_range: Some(0), // Set to pass validation, where epochs starts at 0
commission_range: Some(0), // Set to pass validation, where epochs starts at 0
scoring_delinquency_threshold_ratio: Some(0.85),
instant_unstake_delinquency_threshold_ratio: Some(0.70),
mev_commission_bps_threshold: Some(1000),
commission_threshold: Some(5),
num_delegation_validators: Some(200),
scoring_unstake_cap_bps: Some(750),
instant_unstake_cap_bps: Some(10),
stake_deposit_unstake_cap_bps: Some(10),
instant_unstake_epoch_progress: Some(0.90),
compute_score_slot_range: Some(1000),
instant_unstake_inputs_epoch_progress: Some(0.50),
num_epochs_between_scoring: Some(10),
minimum_stake_lamports: Some(5_000_000_000),
minimum_voting_epochs: Some(0), // Set to pass validation, where epochs starts at 0
});

let instruction = Instruction {
program_id: jito_steward::id(),
accounts: jito_steward::accounts::InitializeConfig {
Expand All @@ -245,6 +267,7 @@ impl TestFixture {
.to_account_metas(None),
data: jito_steward::instruction::InitializeConfig {
authority: self.keypair.pubkey(),
update_parameters_args,
}
.data(),
};
Expand Down Expand Up @@ -579,6 +602,15 @@ pub fn new_vote_account(
}
}

pub fn closed_vote_account() -> Account {
Account {
lamports: 0,
data: vec![0; VoteState::size_of()],
owner: anchor_lang::system_program::ID, // Close the account
..Account::default()
}
}

// TODO write a function to serialize any account with T: AnchorSerialize
pub fn serialized_validator_list_account(
validator_list: ValidatorList,
Expand Down
14 changes: 7 additions & 7 deletions tests/tests/steward/test_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn test_compute_delegations() {
let fixture = TestFixture::new().await;
let ctx = &fixture.ctx;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

let clock: Clock = fixture.get_sysvar().await;
Expand Down Expand Up @@ -173,7 +173,7 @@ async fn test_compute_scores() {
let fixture = TestFixture::new().await;
let ctx = &fixture.ctx;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

let epoch_credits = vec![(0, 1, 0), (1, 2, 1), (2, 3, 2), (3, 4, 3), (4, 5, 4)];
Expand Down Expand Up @@ -419,7 +419,7 @@ async fn test_compute_instant_unstake() {
let fixture = TestFixture::new().await;
let ctx = &fixture.ctx;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

let epoch_credits = vec![(0, 1, 0), (1, 2, 1), (2, 3, 2), (3, 4, 3), (4, 5, 4)];
Expand Down Expand Up @@ -632,7 +632,7 @@ async fn test_idle() {
let fixture = TestFixture::new().await;
let ctx = &fixture.ctx;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

let clock: Clock = fixture.get_sysvar().await;
Expand Down Expand Up @@ -768,7 +768,7 @@ async fn test_rebalance_increase() {
.advance_num_epochs(epoch_schedule.first_normal_epoch - clock.epoch, 10)
.await;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

let mut steward_config: Config = fixture
Expand Down Expand Up @@ -1006,7 +1006,7 @@ async fn test_rebalance_decrease() {
.advance_num_epochs(epoch_schedule.first_normal_epoch - clock.epoch, 10)
.await;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

let mut steward_config: Config = fixture
Expand Down Expand Up @@ -1308,7 +1308,7 @@ async fn test_rebalance_other_cases() {
.advance_num_epochs(epoch_schedule.first_normal_epoch - clock.epoch, 10)
.await;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

let mut steward_config: Config = fixture
Expand Down
16 changes: 8 additions & 8 deletions tests/tests/steward/test_spl_passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ async fn test_add_validator_to_pool() {
// Set up the test fixture
let fixture = TestFixture::new().await;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

{
Expand All @@ -514,7 +514,7 @@ async fn test_remove_validator_from_pool() {
// Set up the test fixture
let fixture = TestFixture::new().await;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

// Setup the steward state
Expand Down Expand Up @@ -609,7 +609,7 @@ async fn test_set_preferred_validator() {
// Set up the test fixture
let fixture = TestFixture::new().await;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

// Assert the validator was added to the validator list
Expand Down Expand Up @@ -667,7 +667,7 @@ async fn test_increase_validator_stake() {
let fixture = TestFixture::new().await;

fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

// Assert the validator was added to the validator list
Expand All @@ -689,7 +689,7 @@ async fn test_decrease_validator_stake() {
let fixture = TestFixture::new().await;

fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

_add_test_validator(&fixture, Pubkey::new_unique()).await;
Expand Down Expand Up @@ -769,7 +769,7 @@ async fn test_increase_additional_validator_stake() {
let fixture = TestFixture::new().await;

fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

// Assert the validator was added to the validator list
Expand All @@ -792,7 +792,7 @@ async fn test_decrease_additional_validator_stake() {
let fixture = TestFixture::new().await;

fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

_add_test_validator(&fixture, Pubkey::new_unique()).await;
Expand Down Expand Up @@ -878,7 +878,7 @@ async fn test_set_staker() {
// Set up the test fixture
let fixture = TestFixture::new().await;
fixture.initialize_stake_pool().await;
fixture.initialize_config().await;
fixture.initialize_config(None).await;
fixture.initialize_steward_state().await;

let new_staker = Keypair::new();
Expand Down
Loading