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
@@ -16,17 +16,18 @@
// limitations under the License.
//! Fuzzing for staking pallet.
//!
//! HFUZZ_RUN_ARGS="-n 8" cargo hfuzz run submit_solution
use honggfuzz::fuzz;
use mock::Test;
use pallet_staking::testing_utils::{
USER, get_seq_phragmen_solution, get_weak_solution, setup_chain_stakers,
set_validator_count, signed_account,
};
use pallet_staking::testing_utils::*;
use frame_support::{assert_ok, storage::StorageValue};
use frame_system::RawOrigin;
use sp_runtime::{traits::Dispatchable, DispatchError};
use sp_core::offchain::{testing::TestOffchainExt, OffchainExt};
use pallet_staking::{EraElectionStatus, ElectionStatus, Module as Staking, Call as StakingCall};
mod mock;
@@ -88,47 +89,52 @@ fn main() {
ext.execute_with(|| {
// initial setup
set_validator_count::<Test>(to_elect);
pallet_staking::testing_utils::init_active_era();
setup_chain_stakers::<Test>(
init_active_era();
assert_ok!(create_validators_with_nominators_for_era::<Test>(
num_validators,
num_nominators,
edge_per_voter,
);
<pallet_staking::EraElectionStatus<Test>>::put(pallet_staking::ElectionStatus::Open(1));
edge_per_voter as usize,
true,
None,
));
<EraElectionStatus<Test>>::put(ElectionStatus::Open(1));
assert!(<Staking<Test>>::create_stakers_snapshot().0);
let origin = RawOrigin::Signed(create_funded_user::<Test>("fuzzer", 0, 100));
println!("++ Chain setup done.");
// stuff to submit
let (winners, compact, score) = match mode {
let (winners, compact, score, size) = match mode {
Mode::InitialSubmission => {
/* No need to setup anything */
get_seq_phragmen_solution::<Test>(do_reduce)
},
Mode::StrongerSubmission => {
let (winners, compact, score) = get_weak_solution::<Test>(false);
let (winners, compact, score, size) = get_weak_solution::<Test>(false);
println!("Weak on chain score = {:?}", score);
assert_ok!(
<pallet_staking::Module<Test>>::submit_election_solution(
signed_account::<Test>(USER),
<Staking<Test>>::submit_election_solution(
origin.clone().into(),
winners,
compact,
score,
pallet_staking::testing_utils::active_era::<Test>(),
current_era::<Test>(),
size,
)
);
get_seq_phragmen_solution::<Test>(do_reduce)
},
Mode::WeakerSubmission => {
let (winners, compact, score) = get_seq_phragmen_solution::<Test>(do_reduce);
let (winners, compact, score, size) = get_seq_phragmen_solution::<Test>(do_reduce);
println!("Strong on chain score = {:?}", score);
assert_ok!(
<pallet_staking::Module<Test>>::submit_election_solution(
signed_account::<Test>(USER),
<Staking<Test>>::submit_election_solution(
origin.clone().into(),
winners,
compact,
score,
pallet_staking::testing_utils::active_era::<Test>(),
current_era::<Test>(),
size,
)
);
get_weak_solution::<Test>(false)
@@ -138,27 +144,34 @@ fn main() {
println!("++ Submission ready. Score = {:?}", score);
// must have chosen correct number of winners.
assert_eq!(winners.len() as u32, <pallet_staking::Module<Test>>::validator_count());
assert_eq!(winners.len() as u32, <Staking<Test>>::validator_count());
// final call and origin
let call = pallet_staking::Call::<Test>::submit_election_solution(
let call = StakingCall::<Test>::submit_election_solution(
winners,
compact,
score,
pallet_staking::testing_utils::active_era::<Test>(),
current_era::<Test>(),
size,
);
let caller = signed_account::<Test>(USER);
// actually submit
match mode {
Mode::WeakerSubmission => {
assert_eq!(
call.dispatch(caller.into()).unwrap_err().error,
DispatchError::Module { index: 0, error: 16, message: Some("PhragmenWeakSubmission") },
call.dispatch(origin.clone().into()).unwrap_err().error,
DispatchError::Module {
index: 0,
error: 16,
message: Some("PhragmenWeakSubmission"),
},
);
},
// NOTE: so exhaustive pattern doesn't work here.. maybe some rust issue? or due to `#[repr(u32)]`?
Mode::InitialSubmission | Mode::StrongerSubmission => assert!(call.dispatch(caller.into()).is_ok()),
// NOTE: so exhaustive pattern doesn't work here.. maybe some rust issue?
// or due to `#[repr(u32)]`?
Mode::InitialSubmission | Mode::StrongerSubmission => {
assert_ok!(call.dispatch(origin.into()));
}
};
})
});