Skip to content

Commit

Permalink
Checked sub
Browse files Browse the repository at this point in the history
  • Loading branch information
ebatsell committed Sep 11, 2024
1 parent 1309e52 commit adc5422
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions programs/steward/src/score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct ScoreComponentsV2 {

pub epoch: u16,

/// Details about why a given score was calculated
pub details: ScoreDetails,
}

Expand Down Expand Up @@ -140,14 +141,14 @@ pub fn validator_score(
&commission_window,
current_epoch,
params.commission_threshold,
);
)?;

let (historical_commission_score, max_historical_commission, max_historical_commission_epoch) =
calculate_historical_commission(
validator,
current_epoch,
params.historical_commission_threshold,
);
)?;

let (superminority_score, superminority_epoch) =
calculate_superminority(validator, current_epoch, params.commission_range)?;
Expand Down Expand Up @@ -281,40 +282,44 @@ fn calculate_commission(
commission_window: &[Option<u8>],
current_epoch: u16,
commission_threshold: u8,
) -> (f64, u8, u16) {
) -> Result<(f64, u8, u16)> {
/////// Commission ///////
let (max_commission, max_commission_epoch) = commission_window
.iter()
.rev()
.enumerate()
.filter_map(|(i, &commission)| commission.map(|c| (c, current_epoch - i as u16)))
.filter_map(|(i, &commission)| commission.map(|c| (c, current_epoch.checked_sub(i as u16))))
.max_by_key(|&(commission, _)| commission)
.unwrap_or((0, current_epoch));
.unwrap_or((0, Some(current_epoch)));

let max_commission_epoch = max_commission_epoch.ok_or(StewardError::ArithmeticError)?;

let commission_score = if max_commission <= commission_threshold {
1.0
} else {
0.0
};

(commission_score, max_commission, max_commission_epoch)
Ok((commission_score, max_commission, max_commission_epoch))
}

/// Checks if validator has commission above a threshold in any epoch in their history
fn calculate_historical_commission(
validator: &ValidatorHistory,
current_epoch: u16,
historical_commission_threshold: u8,
) -> (f64, u8, u16) {
) -> Result<(f64, u8, u16)> {
let (max_historical_commission, max_historical_commission_epoch) = validator
.history
.commission_range(VALIDATOR_HISTORY_FIRST_RELIABLE_EPOCH as u16, current_epoch)
.iter()

Check warning on line 315 in programs/steward/src/score.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/stakenet/stakenet/programs/steward/src/score.rs
.rev()
.enumerate()
.filter_map(|(i, &commission)| commission.map(|c| (c, current_epoch - i as u16)))
.filter_map(|(i, &commission)| commission.map(|c| (c, current_epoch.checked_sub( i as u16))))
.max_by_key(|&(commission, _)| commission)
.unwrap_or((0, VALIDATOR_HISTORY_FIRST_RELIABLE_EPOCH as u16));
.unwrap_or((0, Some(VALIDATOR_HISTORY_FIRST_RELIABLE_EPOCH as u16)));

let max_historical_commission_epoch = max_historical_commission_epoch.ok_or(StewardError::ArithmeticError)?;

Check warning on line 322 in programs/steward/src/score.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/stakenet/stakenet/programs/steward/src/score.rs

let historical_commission_score =
if max_historical_commission <= historical_commission_threshold {
Expand All @@ -323,11 +328,11 @@ fn calculate_historical_commission(
0.0
};

(
Ok((
historical_commission_score,
max_historical_commission,
max_historical_commission_epoch,
)
))
}

/// Checks if validator is in the top 1/3 of validators by stake for the current epoch
Expand Down Expand Up @@ -410,6 +415,7 @@ pub struct InstantUnstakeComponentsV2 {

pub epoch: u16,

/// Details about why a given check was calculated
pub details: InstantUnstakeDetails,
}

Expand Down

0 comments on commit adc5422

Please sign in to comment.