This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Companion for EPM duplicate submissions #6115
Merged
paritytech-processbot
merged 15 commits into
master
from
kiz-comp-duplicate-submissions
Oct 19, 2022
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6474092
make it work
kianenigma b7aa0bb
add migration
kianenigma 43e498f
fix
kianenigma 9381014
Update utils/staking-miner/src/opts.rs
kianenigma 06394dc
Merge branch 'master' of github.com:paritytech/polkadot into kiz-comp…
kianenigma cb03087
Merge branch 'kiz-comp-duplicate-submissions' of github.com:paritytec…
kianenigma 455ff00
Update utils/staking-miner/src/monitor.rs
kianenigma 215976f
small tweaks
kianenigma 8651e79
Update utils/staking-miner/src/opts.rs
kianenigma 108d9d9
Master.into()
kianenigma 3293347
Merge branch 'kiz-comp-duplicate-submissions' of github.com:paritytec…
kianenigma 82e8b56
fmt
kianenigma df028d6
fix print'
kianenigma 4d0cb9b
fmt
kianenigma 6c3d5ad
update lockfile for {"substrate"}
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,7 +64,7 @@ where | |
| .map_err::<Error<T>, _>(Into::into)? | ||
| .unwrap_or_default(); | ||
|
|
||
| for (_score, idx) in indices { | ||
| for (_score, _bn, idx) in indices { | ||
| let key = StorageKey(EPM::SignedSubmissionsMap::<T>::hashed_key_for(idx)); | ||
|
|
||
| if let Some(submission) = rpc | ||
|
|
@@ -81,20 +81,37 @@ where | |
| Ok(()) | ||
| } | ||
|
|
||
| /// `true` if `our_score` should pass the onchain `best_score` with 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) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| /// Reads all current solutions and checks the scores according to the `SubmissionStrategy`. | ||
| async fn ensure_no_better_solution<T: EPM::Config, B: BlockT>( | ||
| async fn ensure_strategy_met<T: EPM::Config, B: BlockT>( | ||
| rpc: &SharedRpcClient, | ||
| at: Hash, | ||
| score: sp_npos_elections::ElectionScore, | ||
| strategy: SubmissionStrategy, | ||
| max_submissions: u32, | ||
| ) -> Result<(), Error<T>> { | ||
| let epsilon = match strategy { | ||
| // don't care about current scores. | ||
| SubmissionStrategy::Always => return Ok(()), | ||
| SubmissionStrategy::IfLeading => Perbill::zero(), | ||
| SubmissionStrategy::ClaimBetterThan(epsilon) => epsilon, | ||
| }; | ||
| // don't care about current scores. | ||
| if matches!(strategy, SubmissionStrategy::Always) { | ||
| return Ok(()) | ||
| } | ||
|
|
||
| let indices_key = StorageKey(EPM::SignedSubmissionIndices::<T>::hashed_key().to_vec()); | ||
|
|
||
|
|
@@ -104,34 +121,16 @@ async fn ensure_no_better_solution<T: EPM::Config, B: BlockT>( | |
| .map_err::<Error<T>, _>(Into::into)? | ||
| .unwrap_or_default(); | ||
|
|
||
| let mut is_best_score = true; | ||
| let mut scores = 0; | ||
|
|
||
| log::debug!(target: LOG_TARGET, "submitted solutions on chain: {:?}", indices); | ||
|
|
||
| // BTreeMap is ordered, take last to get the max score. | ||
| for (curr_max_score, _) in indices.into_iter() { | ||
| if !score.strict_threshold_better(curr_max_score, epsilon) { | ||
| log::warn!(target: LOG_TARGET, "mined score is not better; skipping to submit"); | ||
| is_best_score = false; | ||
| } | ||
|
|
||
| if score == curr_max_score { | ||
| log::warn!( | ||
| target: LOG_TARGET, | ||
| "mined score has the same score as already submitted score" | ||
| ); | ||
| } | ||
|
|
||
| // Indices can't be bigger than u32::MAX so can't overflow. | ||
| scores += 1; | ||
| // we check the queue here as well. Could be checked elsewhere. | ||
| if indices.len() as u32 >= max_submissions { | ||
| return Err(Error::<T>::QueueFull) | ||
| } | ||
|
|
||
| if scores == max_submissions { | ||
| log::warn!(target: LOG_TARGET, "The submissions queue is full"); | ||
| } | ||
| // default score is all zeros, any score is better than it. | ||
| let best_score = indices.last().map(|(score, _, _)| *score).unwrap_or_default(); | ||
| log::debug!(target: LOG_TARGET, "best onchain score is {:?}", best_score); | ||
|
|
||
| if is_best_score { | ||
| if score_passes_strategy(score, best_score, strategy) { | ||
| Ok(()) | ||
| } else { | ||
| Err(Error::StrategyNotSatisfied) | ||
|
|
@@ -314,9 +313,14 @@ macro_rules! monitor_cmd_for { ($runtime:tt) => { paste::paste! { | |
| } | ||
| }; | ||
|
|
||
| let ensure_no_better_fut = tokio::spawn(async move { | ||
| ensure_no_better_solution::<Runtime, Block>(&rpc1, latest_head, score, config.submission_strategy, | ||
| SignedMaxSubmissions::get()).await | ||
| let ensure_strategy_met_fut = tokio::spawn(async move { | ||
| ensure_strategy_met::<Runtime, Block>( | ||
| &rpc1, | ||
| latest_head, | ||
| score, | ||
| config.submission_strategy, | ||
| SignedMaxSubmissions::get() | ||
| ).await | ||
| }); | ||
|
|
||
| let ensure_signed_phase_fut = tokio::spawn(async move { | ||
|
|
@@ -325,10 +329,10 @@ macro_rules! monitor_cmd_for { ($runtime:tt) => { paste::paste! { | |
|
|
||
| // Run the calls in parallel and return once all has completed or any failed. | ||
| if let Err(err) = tokio::try_join!( | ||
| flatten(ensure_no_better_fut), | ||
| flatten(ensure_strategy_met_fut), | ||
| flatten(ensure_signed_phase_fut), | ||
| ) { | ||
| log::debug!(target: LOG_TARGET, "Skipping to submit at block {}; {}", at.number, err); | ||
| log::error!(target: LOG_TARGET, "Skipping to submit at block {}; {}", at.number, err); | ||
|
||
| return; | ||
| } | ||
|
|
||
|
|
@@ -420,3 +424,46 @@ macro_rules! monitor_cmd_for { ($runtime:tt) => { paste::paste! { | |
| monitor_cmd_for!(polkadot); | ||
| monitor_cmd_for!(kusama); | ||
| monitor_cmd_for!(westend); | ||
|
|
||
| #[cfg(test)] | ||
| pub 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))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.