Skip to content

Commit

Permalink
Revert "companion for the recent epm changes (#402)"
Browse files Browse the repository at this point in the history
This reverts commit fbe5a55.
  • Loading branch information
niklasad1 committed Nov 1, 2022
1 parent 964c233 commit 7532fb8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 88 deletions.
Binary file modified artifacts/metadata.scale
Binary file not shown.
91 changes: 15 additions & 76 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ async fn mine_and_submit_solution<T>(
return
}

match ensure_solution_passes_strategy(&api, best_head, score, config.submission_strategy)
match ensure_no_better_solution(&api, best_head, score, config.submission_strategy)
.timed()
.await
{
Expand Down Expand Up @@ -321,7 +321,7 @@ where
let addr = runtime::storage().election_provider_multi_phase().signed_submission_indices();
let indices = api.storage().fetch_or_default(&addr, Some(at)).await?;

for (_score, _, idx) in indices.0 {
for (_score, idx) in indices.0 {
let submission = epm::signed_submission_at::<T>(idx, at, api).await?;

if let Some(submission) = submission {
Expand All @@ -334,31 +334,31 @@ where
Ok(())
}

async fn ensure_solution_passes_strategy(
async fn ensure_no_better_solution(
api: &SubxtClient,
at: Hash,
score: sp_npos_elections::ElectionScore,
strategy: SubmissionStrategy,
) -> Result<(), Error> {
// don't care about current scores.
if matches!(strategy, SubmissionStrategy::Always) {
return Ok(())
}
let epsilon = match strategy {
// don't care about current scores.
SubmissionStrategy::Always => return Ok(()),
SubmissionStrategy::IfLeading => Perbill::zero(),
SubmissionStrategy::ClaimBetterThan(epsilon) => epsilon,
};

let addr = runtime::storage().election_provider_multi_phase().signed_submission_indices();
let indices = api.storage().fetch_or_default(&addr, Some(at)).await?;

log::debug!(target: LOG_TARGET, "submitted solutions: {:?}", indices.0);

if indices
.0
.last()
.map_or(true, |(best_score, _, _)| score_passes_strategy(score, *best_score, strategy))
{
Ok(())
} else {
Err(Error::BetterScoreExist)
for (other_score, _) in indices.0 {
if !score.strict_threshold_better(other_score, epsilon) {
return Err(Error::BetterScoreExist)
}
}

Ok(())
}

async fn submit_and_watch_solution<T: MinerConfig + Send + Sync + 'static>(
Expand Down Expand Up @@ -443,64 +443,3 @@ async fn get_latest_head(api: &SubxtClient, listen: Listen) -> Result<Hash, Erro
Listen::Finalized => api.rpc().finalized_head().await.map_err(Into::into),
}
}

/// Returns `true` if `our_score` better the onchain `best_score` according the given strategy.
pub(crate) fn score_passes_strategy(
our_score: sp_npos_elections::ElectionScore,
best_score: sp_npos_elections::ElectionScore,
strategy: SubmissionStrategy,
) -> bool {
match strategy {
SubmissionStrategy::Always => true,
SubmissionStrategy::IfLeading =>
our_score == best_score ||
our_score.strict_threshold_better(best_score, Perbill::zero()),
SubmissionStrategy::ClaimBetterThan(epsilon) =>
our_score.strict_threshold_better(best_score, epsilon),
SubmissionStrategy::ClaimNoWorseThan(epsilon) =>
!best_score.strict_threshold_better(our_score, epsilon),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn score_passes_strategy_works() {
let s = |x| sp_npos_elections::ElectionScore { minimal_stake: x, ..Default::default() };
let two = Perbill::from_percent(2);

// anything passes Always
assert!(score_passes_strategy(s(0), s(0), SubmissionStrategy::Always));
assert!(score_passes_strategy(s(5), s(0), SubmissionStrategy::Always));
assert!(score_passes_strategy(s(5), s(10), SubmissionStrategy::Always));

// if leading
assert!(score_passes_strategy(s(0), s(0), SubmissionStrategy::IfLeading));
assert!(score_passes_strategy(s(1), s(0), SubmissionStrategy::IfLeading));
assert!(score_passes_strategy(s(2), s(0), SubmissionStrategy::IfLeading));
assert!(!score_passes_strategy(s(5), s(10), SubmissionStrategy::IfLeading));
assert!(!score_passes_strategy(s(9), s(10), SubmissionStrategy::IfLeading));
assert!(score_passes_strategy(s(10), s(10), SubmissionStrategy::IfLeading));

// if better by 2%
assert!(!score_passes_strategy(s(50), s(100), SubmissionStrategy::ClaimBetterThan(two)));
assert!(!score_passes_strategy(s(100), s(100), SubmissionStrategy::ClaimBetterThan(two)));
assert!(!score_passes_strategy(s(101), s(100), SubmissionStrategy::ClaimBetterThan(two)));
assert!(!score_passes_strategy(s(102), s(100), SubmissionStrategy::ClaimBetterThan(two)));
assert!(score_passes_strategy(s(103), s(100), SubmissionStrategy::ClaimBetterThan(two)));
assert!(score_passes_strategy(s(150), s(100), SubmissionStrategy::ClaimBetterThan(two)));

// if no less than 2% worse
assert!(!score_passes_strategy(s(50), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
assert!(!score_passes_strategy(s(97), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
assert!(score_passes_strategy(s(98), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
assert!(score_passes_strategy(s(99), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
assert!(score_passes_strategy(s(100), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
assert!(score_passes_strategy(s(101), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
assert!(score_passes_strategy(s(102), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
assert!(score_passes_strategy(s(103), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
assert!(score_passes_strategy(s(150), s(100), SubmissionStrategy::ClaimNoWorseThan(two)));
}
}
15 changes: 3 additions & 12 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,12 @@ macro_rules! any_runtime {
#[derive(Debug, Copy, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum SubmissionStrategy {
/// Always submit.
// Only submit if at the time, we are the best.
IfLeading,
// Always submit.
Always,
// Submit if we are leading, or if the solution that's leading is more that the given `Perbill`
// better than us. This helps detect obviously fake solutions and still combat them.
/// Only submit if at the time, we are the best (or equal to it).
IfLeading,
/// Submit if we are no worse than `Perbill` worse than the best.
ClaimNoWorseThan(Perbill),
/// Submit if we are leading, or if the solution that's leading is more that the given `Perbill`
/// better than us. This helps detect obviously fake solutions and still combat them.
ClaimBetterThan(Perbill),
}

Expand All @@ -102,9 +98,6 @@ impl FromStr for SubmissionStrategy {
Self::IfLeading
} else if s == "always" {
Self::Always
} else if let Some(percent) = s.strip_prefix("no-worse-than ") {
let percent: u32 = percent.parse().map_err(|e| format!("{:?}", e))?;
Self::ClaimNoWorseThan(Perbill::from_percent(percent))
} else if let Some(percent) = s.strip_prefix("percent-better ") {
let percent: u32 = percent.parse().map_err(|e| format!("{:?}", e))?;
Self::ClaimBetterThan(Perbill::from_percent(percent))
Expand Down Expand Up @@ -209,8 +202,6 @@ pub struct MonitorConfig {
/// `--submission-strategy always`: always submit.
///
/// `--submission-strategy "percent-better <percent>"`: submit if the submission is `n` percent better.
///
/// `--submission-strategy "no-worse-than <percent>"`: submit if submission is no more than `n` percent worse.
#[clap(long, parse(try_from_str), default_value = "if-leading")]
pub submission_strategy: SubmissionStrategy,

Expand Down

0 comments on commit 7532fb8

Please sign in to comment.