First draft of offchain phragmen weights (#6032)

* Fist draft of offchain weights

* Round of review feedback

* Update frame/staking/src/lib.rs

* Fix fuzzer

* Remove some redundant comment

* Weight refund for submit solution -- potentially revert.

* First version with custom trimming of the result.

* Update frame/staking/src/benchmarking.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update frame/staking/src/benchmarking.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Apply suggestions from code review

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: thiolliere <gui.thiolliere@gmail.com>

* Update frame/staking/src/benchmarking.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update frame/staking/src/benchmarking.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Some improvements

* Benchmark submit solution without phragmen (PR for First draft of offchain phragmen weights) (#6073)

* implementation of new benchmark

* address comments

* replace test

* Update frame/staking/src/lib.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* update weight

* Fix refund

* Clean and rady for final bench

* Fix line-wdith

* Fix gitlab build

* Fix line-wdith

* Fix test macro

* Update frame/staking/src/lib.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update frame/staking/src/benchmarking.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Better length check

* Update frame/staking/src/lib.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update final weight coefficients

* Update frame/staking/src/lib.rs

* Apply suggestions from code review

* Update frame/staking/src/testing_utils.rs

* Try and fix the line-width

* Revert "Try and fix the line-width"

This reverts commit b4e284727220085b9b3daf7682c4bbf29621da09.

* Try and fix the line-width the correct way

* Revert "Try and fix the line-width the correct way"

This reverts commit 04fce128e851c9584f9f0d708a5a73cae799d8c8.

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: Gavin Wood <gavin@parity.io>
This commit is contained in:
Kian Paimani
2020-05-23 20:08:42 +02:00
committed by GitHub
parent 82a832bc3a
commit 0133185c81
14 changed files with 1201 additions and 854 deletions
+80 -53
View File
@@ -2753,7 +2753,10 @@ fn remove_multi_deferred() {
mod offchain_phragmen {
use crate::*;
use codec::Encode;
use frame_support::{assert_noop, assert_ok};
use frame_support::{
assert_noop, assert_ok, assert_err_with_weight,
dispatch::DispatchResultWithPostInfo,
};
use sp_runtime::transaction_validity::TransactionSource;
use mock::*;
use parking_lot::RwLock;
@@ -2808,6 +2811,29 @@ mod offchain_phragmen {
pool_state
}
fn election_size() -> ElectionSize {
ElectionSize {
validators: Staking::snapshot_validators().unwrap().len() as ValidatorIndex,
nominators: Staking::snapshot_nominators().unwrap().len() as NominatorIndex,
}
}
fn submit_solution(
origin: Origin,
winners: Vec<ValidatorIndex>,
compact: CompactAssignments,
score: PhragmenScore,
) -> DispatchResultWithPostInfo {
Staking::submit_election_solution(
origin,
winners,
compact,
score,
current_era(),
election_size(),
)
}
#[test]
fn is_current_session_final_works() {
ExtBuilder::default()
@@ -2996,12 +3022,11 @@ mod offchain_phragmen {
assert!(Staking::snapshot_validators().is_some());
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(Staking::submit_election_solution(
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
));
let queued_result = Staking::queued_elected().unwrap();
@@ -3039,13 +3064,7 @@ mod offchain_phragmen {
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(12));
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(Staking::submit_election_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
));
assert_ok!(submit_solution(Origin::signed(10), winners, compact, score));
let queued_result = Staking::queued_elected().unwrap();
assert_eq!(queued_result.compute, ElectionCompute::Signed);
@@ -3088,15 +3107,17 @@ mod offchain_phragmen {
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
Staking::kill_stakers_snapshot();
assert_noop!(
assert_err_with_weight!(
Staking::submit_election_solution(
Origin::signed(10),
winners,
compact,
winners.clone(),
compact.clone(),
score,
current_era(),
ElectionSize::default(),
),
Error::<Test>::PhragmenEarlySubmission,
Some(<Test as frame_system::Trait>::DbWeight::get().reads(1)),
);
})
}
@@ -3115,25 +3136,24 @@ mod offchain_phragmen {
// a good solution
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(Staking::submit_election_solution(
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
));
// a bad solution
let (compact, winners, score) = horrible_phragmen_with_post_processing(false);
assert_noop!(
Staking::submit_election_solution(
assert_err_with_weight!(
submit_solution(
Origin::signed(10),
winners,
compact,
winners.clone(),
compact.clone(),
score,
current_era(),
),
Error::<Test>::PhragmenWeakSubmission,
Some(<Test as frame_system::Trait>::DbWeight::get().reads(3))
);
})
}
@@ -3152,22 +3172,20 @@ mod offchain_phragmen {
// a meeeeh solution
let (compact, winners, score) = horrible_phragmen_with_post_processing(false);
assert_ok!(Staking::submit_election_solution(
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
));
// a better solution
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(Staking::submit_election_solution(
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
));
})
}
@@ -3268,12 +3286,11 @@ mod offchain_phragmen {
run_to_block(12);
// put a good solution on-chain
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(Staking::submit_election_solution(
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),);
// now run the offchain worker in the same chain state.
@@ -3318,6 +3335,28 @@ mod offchain_phragmen {
assert_eq!(winners.len(), 3);
assert_noop!(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusWinnerCount,
);
})
}
#[test]
fn invalid_phragmen_result_solution_size() {
ExtBuilder::default()
.offchain_phragmen_ext()
.build()
.execute_with(|| {
run_to_block(12);
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_noop!(
Staking::submit_election_solution(
Origin::signed(10),
@@ -3325,8 +3364,9 @@ mod offchain_phragmen {
compact,
score,
current_era(),
ElectionSize::default(),
),
Error::<Test>::PhragmenBogusWinnerCount,
Error::<Test>::PhragmenBogusElectionSize,
);
})
}
@@ -3350,12 +3390,11 @@ mod offchain_phragmen {
assert_eq!(winners.len(), 3);
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusWinnerCount,
);
@@ -3379,12 +3418,11 @@ mod offchain_phragmen {
assert_eq!(winners.len(), 4);
// all good. We chose 4 and it works.
assert_ok!(Staking::submit_election_solution(
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),);
})
}
@@ -3410,12 +3448,11 @@ mod offchain_phragmen {
// The error type sadly cannot be more specific now.
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusCompact,
);
@@ -3443,12 +3480,11 @@ mod offchain_phragmen {
// The error type sadly cannot be more specific now.
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusCompact,
);
@@ -3475,12 +3511,11 @@ mod offchain_phragmen {
let winners = vec![0, 1, 2, 4];
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusWinner,
);
@@ -3511,12 +3546,11 @@ mod offchain_phragmen {
});
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusEdge,
);
@@ -3547,12 +3581,11 @@ mod offchain_phragmen {
});
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusSelfVote,
);
@@ -3583,12 +3616,11 @@ mod offchain_phragmen {
// This raises score issue.
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusSelfVote,
);
@@ -3618,12 +3650,11 @@ mod offchain_phragmen {
}
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusCompact,
);
@@ -3660,12 +3691,11 @@ mod offchain_phragmen {
});
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusNomination,
);
@@ -3723,12 +3753,11 @@ mod offchain_phragmen {
});
// can be submitted.
assert_ok!(Staking::submit_election_solution(
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
));
// a wrong solution.
@@ -3742,12 +3771,11 @@ mod offchain_phragmen {
// is rejected.
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenSlashedNomination,
);
@@ -3770,12 +3798,11 @@ mod offchain_phragmen {
score[0] += 1;
assert_noop!(
Staking::submit_election_solution(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
),
Error::<Test>::PhragmenBogusScore,
);