Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Benchmarking for Offchain Phragmén. #4947

Closed
wants to merge 157 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
157 commits
Select commit Hold shift + click to select a range
46ecd58
Initial skeleton for offchain phragmen
kianenigma Jan 2, 2020
a830ec9
Basic compact encoding decoding for results
kianenigma Jan 6, 2020
8448ee9
add compact files
kianenigma Jan 6, 2020
5e26a68
Bring back Self::ensure_storage_upgraded();
kianenigma Jan 6, 2020
65b2df6
Make staking use compact stuff.
kianenigma Jan 7, 2020
85b726c
First seemingly working version of reduce, full of todos
kianenigma Jan 13, 2020
bf053c2
Master.into()
kianenigma Jan 13, 2020
ca68e24
Everything phragmen related works again.
kianenigma Jan 13, 2020
62e61db
Signing made easier, still issues.
kianenigma Jan 14, 2020
c4a77ac
Signing from offchain compile fine 😎
kianenigma Jan 15, 2020
31b699a
make compact work with staked asssignment
kianenigma Jan 15, 2020
c5d8c51
Evaluation basics are in place.
kianenigma Jan 15, 2020
6d4494e
Master.into()
kianenigma Jan 15, 2020
35a7872
Move reduce into crate. Document stuff
kianenigma Jan 15, 2020
0540646
move reduce into no_std
kianenigma Jan 16, 2020
da66da2
Add files
kianenigma Jan 16, 2020
d95010d
Remove other std deps. Runtime compiles
kianenigma Jan 16, 2020
3f23367
Seemingly it is al stable; cycle implemented but not integrated.
kianenigma Jan 20, 2020
3cb47fb
Add fuzzing code.
kianenigma Jan 21, 2020
a21e90b
Cleanup reduce a bit more.
kianenigma Jan 21, 2020
df7676b
a metric ton of tests for staking; wip 🔨
kianenigma Jan 22, 2020
6f4a2fe
Master.into()
kianenigma Jan 22, 2020
889b18c
Implement a lot more of the tests.
kianenigma Jan 23, 2020
50f5399
wip getting the unsigned stuff to work
kianenigma Jan 23, 2020
2bbdec4
A bit gleanup for unsigned debug
kianenigma Jan 24, 2020
c9cfcb8
Clean and finalize compact code.
kianenigma Jan 24, 2020
3633a77
Document reduce.
kianenigma Jan 27, 2020
686d263
Still problems with signing
kianenigma Jan 27, 2020
295cdd7
We officaly duct taped the transaction submission stuff. 🤓
kianenigma Jan 27, 2020
57b2b4e
Deadlock with keys again
kianenigma Jan 28, 2020
6a33fc1
Runtime builds
kianenigma Jan 28, 2020
4e951af
Unsigned test works 🙌
kianenigma Jan 28, 2020
5ab3cf0
Some cleanups
kianenigma Jan 28, 2020
c88df34
Master.into()
kianenigma Jan 28, 2020
31fd60e
Make all the tests compile and stuff
kianenigma Jan 29, 2020
2623d98
Minor cleanup
kianenigma Feb 3, 2020
c911a87
Master.into()
kianenigma Feb 3, 2020
2ed0a1d
fix more merge stuff
kianenigma Feb 3, 2020
6bef2d9
Most tests work again.
kianenigma Feb 5, 2020
201c11c
a very nasty bug in reduce
kianenigma Feb 6, 2020
eef09e5
Fix all integrations
kianenigma Feb 6, 2020
51fc865
Fix more todos
kianenigma Feb 6, 2020
aa6186c
Revamp everything and everything
kianenigma Feb 11, 2020
596bc77
Master.into()
kianenigma Feb 11, 2020
084a355
Remove bogus test
kianenigma Feb 11, 2020
817f8aa
Some review grumbles.
kianenigma Feb 11, 2020
d1e290f
Make it work with custom key type
kianenigma Feb 12, 2020
02c4e3b
Some fixes
kianenigma Feb 12, 2020
aea7ad2
Fix doc test
kianenigma Feb 12, 2020
e2210cc
loop for submission
kianenigma Feb 13, 2020
ca20033
Fix cli, keyring etc.
kianenigma Feb 13, 2020
b2c4416
Merged with the generic phragmen -- maybe not perfectly but works
kianenigma Feb 13, 2020
10e97e3
some cleanup
kianenigma Feb 14, 2020
19cf777
Master.into()
kianenigma Feb 14, 2020
54a5a35
Fix staking tests again
kianenigma Feb 14, 2020
c691d87
initial version that works again.
kianenigma Feb 14, 2020
a2859da
More or less works with the new setup.
kianenigma Feb 14, 2020
109e6d6
Better setup
kianenigma Feb 14, 2020
38e8302
Alles Ok.
kianenigma Feb 17, 2020
822c855
cleanup
kianenigma Feb 17, 2020
a1c30d2
Fix tests
kianenigma Feb 17, 2020
f38cdb5
Apply suggestions from code review
kianenigma Feb 17, 2020
d746fd4
fix per-things; bring patches from benchmarking
kianenigma Feb 18, 2020
2f75ea7
better score prediction
kianenigma Feb 18, 2020
e032650
some patches
kianenigma Feb 19, 2020
e542a5d
Merge with upstream and fix all bugs
kianenigma Feb 19, 2020
2fa064b
Add fuzzer, more patches.
kianenigma Feb 19, 2020
cddaf12
few more nits
kianenigma Feb 19, 2020
0afbde3
Some fixes
kianenigma Feb 19, 2020
16d8c64
Fix tests
kianenigma Feb 19, 2020
85f8898
Upstream.into()
kianenigma Feb 19, 2020
b8efef4
More docs
kianenigma Feb 20, 2020
e72d642
Remove unused generics
kianenigma Feb 20, 2020
9f227f9
Remove max-nominator footgun
kianenigma Feb 20, 2020
9116285
Better fuzzer
kianenigma Feb 21, 2020
36bbf4b
Master.into()
kianenigma Feb 21, 2020
44c0a6b
Disable it ❌
kianenigma Feb 21, 2020
c3769fa
Master.into()
kianenigma Feb 21, 2020
f21df59
Bump.
kianenigma Feb 21, 2020
f9f92c4
Another round of self-review
kianenigma Feb 26, 2020
4df9ed4
Master.into()
kianenigma Feb 26, 2020
037057c
Refactor a lot
kianenigma Feb 27, 2020
2b89c0e
More major fixes in perThing
kianenigma Feb 28, 2020
4e44501
Add new fuzz file
kianenigma Feb 28, 2020
5c3fc6c
Master.into()
kianenigma Feb 28, 2020
7287c2b
Update lock
kianenigma Feb 28, 2020
ac6ce78
fix fuzzing code.
kianenigma Feb 28, 2020
7be2268
Fix nominator retain test
kianenigma Mar 2, 2020
1bd7850
Add slashing check
kianenigma Mar 2, 2020
5f9844d
Update frame/staking/src/tests.rs
kianenigma Mar 2, 2020
555007f
Some formatting nits
kianenigma Mar 2, 2020
c16ad73
Merge branch 'kiz-offchain-phragmen-4' of github.com:paritytech/subst…
kianenigma Mar 2, 2020
c8cc72f
Review comments.
kianenigma Mar 3, 2020
ceef0e6
Merge branch 'master' of github.com:paritytech/substrate into kiz-off…
kianenigma Mar 3, 2020
62d4f5e
Fix cargo file
kianenigma Mar 3, 2020
fd9816a
Master.into()
kianenigma Mar 4, 2020
85f1aa4
Almost all tests work again
kianenigma Mar 4, 2020
9b8af93
Update frame/staking/src/tests.rs
kianenigma Mar 4, 2020
58fcf27
Fix review comments
kianenigma Mar 4, 2020
7f47696
Merge branch 'kiz-offchain-phragmen-4' of github.com:paritytech/subst…
kianenigma Mar 4, 2020
ef7eab2
More review stuff
kianenigma Mar 4, 2020
5f70434
Some nits
kianenigma Mar 4, 2020
4d82b4d
Merge branch 'master' of github.com:paritytech/substrate into kiz-off…
kianenigma Mar 4, 2020
58267dc
Fix new staking / session / babe relation
kianenigma Mar 5, 2020
bee2f5d
Update primitives/phragmen/src/lib.rs
kianenigma Mar 5, 2020
3def57b
Update primitives/phragmen/src/lib.rs
kianenigma Mar 5, 2020
9590986
Update primitives/phragmen/compact/src/lib.rs
kianenigma Mar 5, 2020
6566d81
Some doc updates to slashing
kianenigma Mar 5, 2020
b01000e
Merge branch 'kiz-offchain-phragmen-4' of github.com:paritytech/subst…
kianenigma Mar 5, 2020
1667f6e
Fix derive
kianenigma Mar 5, 2020
155e9a3
Master.into()
kianenigma Mar 5, 2020
e25c103
Remove imports
kianenigma Mar 5, 2020
81ddd48
Remove unimplemented tests
kianenigma Mar 6, 2020
87bb4d1
Master.into()
kianenigma Mar 9, 2020
eda82ae
nits
kianenigma Mar 9, 2020
56d1f5e
Remove dbg
kianenigma Mar 9, 2020
0c9c20f
Better fuzzing params
kianenigma Mar 9, 2020
f308817
Master.into()
kianenigma Mar 9, 2020
f014e2c
Remove unused pref map
kianenigma Mar 9, 2020
26d9329
Deferred Slashing/Offence for offchain Phragmen (#5151)
kianenigma Mar 10, 2020
ecd042e
Fix build
kianenigma Mar 10, 2020
63eb8c4
review comments
kianenigma Mar 12, 2020
3cd2c55
fix more
kianenigma Mar 12, 2020
1e4291c
Merge branch 'master' of github.com:paritytech/substrate into kiz-off…
kianenigma Mar 12, 2020
918e58e
fix build
kianenigma Mar 12, 2020
537d6ee
Some cleanups and self-reviews
kianenigma Mar 13, 2020
98f9c1b
More minor self reviews
kianenigma Mar 13, 2020
b553cf5
Master.into()
kianenigma Mar 13, 2020
199d8fb
Final nits
kianenigma Mar 13, 2020
a424a94
Merge branch 'master' of github.com:paritytech/substrate into kiz-off…
kianenigma Mar 13, 2020
42dbf06
A holy darn big upstream merge.
kianenigma Mar 19, 2020
202db20
Some merge fixes.
kianenigma Mar 19, 2020
8372453
opt comment
kianenigma Mar 20, 2020
db50363
Master.into()
kianenigma Mar 20, 2020
2692605
Fix build
kianenigma Mar 20, 2020
2cf090c
Fix build again.
kianenigma Mar 20, 2020
ffda508
Merge branch 'master' into kiz-offchain-phragmen-4
gavofyork Mar 21, 2020
4c56e36
Master.into()
kianenigma Mar 21, 2020
18558af
Update frame/staking/fuzz/fuzz_targets/submit_solution.rs
kianenigma Mar 22, 2020
6f9a2c5
Update frame/staking/src/slashing.rs
kianenigma Mar 23, 2020
cd3df80
Update frame/staking/src/offchain_election.rs
kianenigma Mar 23, 2020
1153084
Fix review comments
kianenigma Mar 23, 2020
4c274ab
Merge branch 'kiz-offchain-phragmen-4' of github.com:paritytech/subst…
kianenigma Mar 23, 2020
c75f441
Master.into()
kianenigma Mar 23, 2020
34e3d7b
fix test
kianenigma Mar 23, 2020
ee3c4cb
=== 🔑 Revamp without staking key.
kianenigma Mar 24, 2020
3c5dd95
Master.into()
kianenigma Mar 24, 2020
5a1aa2f
Master.into()
kianenigma Mar 25, 2020
03bb2a9
final round of changes.
kianenigma Mar 26, 2020
8dd2564
Master.into()
kianenigma Mar 26, 2020
4514432
Fix cargo-deny
kianenigma Mar 26, 2020
ce2ff51
Merge branch 'master' into kiz-offchain-phragmen-4
gavofyork Mar 26, 2020
9c0f42c
Master.into()
kianenigma Mar 26, 2020
94efcff
Master.into()
kianenigma Mar 26, 2020
8182947
Update frame/staking/src/lib.rs
kianenigma Mar 26, 2020
4c517f9
Bearly merged
kianenigma Mar 27, 2020
c225475
Master.into()
kianenigma Mar 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions frame/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ env_logger = "0.7.1"
hex = "0.4"

[features]
<<<<<<< HEAD
default = ["std", "runtime-benchmarks"]
=======
testing-utils = [
"std",
"pallet-indices/std",
"sp-core/std",
"rand/std",
]
default = ["std"]
>>>>>>> f7bd10a548201cb68491f72be73115279d97f160
std = [
"serde",
"codec/std",
Expand All @@ -68,6 +72,13 @@ std = [
"sp-core/std",
]
runtime-benchmarks = [
"testing-utils",
"rand_chacha",
"frame-benchmarking",
]
testing-utils = [
"std",
"pallet-indices/std",
"sp-core/std",
"rand/std",
]
92 changes: 92 additions & 0 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,100 @@ use frame_benchmarking::{benchmarks, account};

use crate::Module as Staking;
use frame_system::Module as System;
use frame_support::assert_ok;

const SEED: u32 = 0;
const USER: u32 = 10;

#[repr(u32)]
#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub enum OffchainBenchmarkingMode {
/// Initial submission. This will be rather cheap
InitialSubmission,
/// A better submission that will replace the previous ones. This is the most expensive.
StrongerSubmission,
/// A weak submission that will be rejected. This will be rather cheap.
WeakerSubmission,
}

fn setup_submit_election_solution<T: Trait>(
mode: OffchainBenchmarkingMode,
num_nominators: u32,
num_validators: u32,
edge_per_voter: u32,
to_elect: u32,
do_reduce: bool,
) -> Result<(Call<T>, RawOrigin<T::AccountId>), &'static str> {
sp_std::if_std! {
println!("++ instance with params {} / {} / {} / {:?} / {}",
num_nominators,
num_validators,
edge_per_voter,
mode,
to_elect,
);
}

if num_validators < to_elect {
return Err("invalid setup. too few candidates");
}

// stake and nominate everyone
create_validators_with_nominators_for_era::<T>(num_validators, num_nominators)?;

// create snapshot
<Module<T>>::create_stakers_snapshot();

let era = <Module<T>>::active_era().unwrap().index;
let origin = RawOrigin::Signed(create_funded_user::<T>("Alan Turing", 999));

// call data
let (winners, compact, score) = match mode {
OffchainBenchmarkingMode::InitialSubmission => {
// No need to setup anything
testing_utils::get_seq_phragmen_solution::<T>(do_reduce)
},
OffchainBenchmarkingMode::StrongerSubmission => {
let (winners, compact, score) = testing_utils::get_weak_solution::<T>(false);
assert_ok!(
<Module<T>>::submit_election_solution(
origin.clone().into(),
winners,
compact,
score,
era,
)
);
testing_utils::get_seq_phragmen_solution::<T>(do_reduce)
},
OffchainBenchmarkingMode::WeakerSubmission => {
let (winners, compact, score) = testing_utils::get_seq_phragmen_solution::<T>(do_reduce);
assert_ok!(
<Module<T>>::submit_election_solution(
origin.clone().into(),
winners,
compact,
score,
era,
)
);
testing_utils::get_weak_solution::<T>(false)
}
};

// must have chosen correct number of winners.
assert_eq!(winners.len() as u32, <Module<T>>::validator_count());

let call = crate::Call::<T>::submit_election_solution(
winners,
compact,
score,
era,
);

Ok((call, origin))
}

fn create_funded_user<T: Trait>(string: &'static str, n: u32) -> T::AccountId {
let user = account(string, n, SEED);
Expand Down
4 changes: 4 additions & 0 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@
mod mock;
#[cfg(test)]
mod tests;
<<<<<<< HEAD

=======
>>>>>>> f7bd10a548201cb68491f72be73115279d97f160
#[cfg(feature = "testing-utils")]
pub mod testing_utils;
#[cfg(any(feature = "runtime-benchmarks", test))]
Expand Down
13 changes: 6 additions & 7 deletions frame/staking/src/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ where
targets,
);
});

<Module<T>>::create_stakers_snapshot();
}

/// Build a _really bad_ but acceptable solution for election. This should always yield a solution
Expand All @@ -153,13 +151,13 @@ pub fn get_weak_solution<T: Trait>(
let mut backing_stake_of: BTreeMap<T::AccountId, BalanceOf<T>> = BTreeMap::new();

// self stake
<Validators<T>>::enumerate().for_each(|(who, _p)| {
<Validators<T>>::iter().for_each(|(who, _p)| {
*backing_stake_of.entry(who.clone()).or_insert(Zero::zero()) +=
<Module<T>>::slashable_balance_of(&who)
});

// add nominator stuff
<Nominators<T>>::enumerate().for_each(|(who, nomination)| {
<Nominators<T>>::iter().for_each(|(who, nomination)| {
nomination.targets.into_iter().for_each(|v| {
*backing_stake_of.entry(v).or_insert(Zero::zero()) +=
<Module<T>>::slashable_balance_of(&who)
Expand All @@ -176,7 +174,7 @@ pub fn get_weak_solution<T: Trait>(
.collect();

let mut staked_assignments: Vec<StakedAssignment<T::AccountId>> = Vec::new();
<Nominators<T>>::enumerate().for_each(|(who, nomination)| {
<Nominators<T>>::iter().for_each(|(who, nomination)| {
let mut dist: Vec<(T::AccountId, ExtendedBalance)> = Vec::new();
nomination.targets.into_iter().for_each(|v| {
if winners.iter().find(|&w| *w == v).is_some() {
Expand Down Expand Up @@ -320,19 +318,20 @@ pub fn get_seq_phragmen_solution<T: Trait>(
}

/// Remove all validator, nominators, votes and exposures.
/// TODO: this can be better probably.
pub fn clean<T: Trait>(era: EraIndex)
where
<T as frame_system::Trait>::AccountId: codec::EncodeLike<u32>,
u32: codec::EncodeLike<T::AccountId>,
{
<Validators<T>>::enumerate().for_each(|(k, _)| {
<Validators<T>>::iter().for_each(|(k, _)| {
let ctrl = <Module<T>>::bonded(&k).unwrap();
<Bonded<T>>::remove(&k);
<Validators<T>>::remove(&k);
<Ledger<T>>::remove(&ctrl);
<ErasStakers<T>>::remove(k, era);
});
<Nominators<T>>::enumerate().for_each(|(k, _)| <Nominators<T>>::remove(k));
<Nominators<T>>::iter().for_each(|(k, _)| <Nominators<T>>::remove(k));
<Ledger<T>>::remove_all();
<Bonded<T>>::remove_all();
<QueuedElected<T>>::kill();
Expand Down
17 changes: 17 additions & 0 deletions primitives/phragmen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,19 @@ fn self_votes_should_be_kept() {
}

#[test]
<<<<<<< HEAD
fn elect_should_never_output_zero() {
// phragmen internally uses Rational128 to keep ratios, which is _as accurate as it gets_. When
// down-casting this number to a PerThing, this might lead to `Zero::zero()`. This should simply
// be dropped as an edge.
// We also guard against this when we convert assignments into staked and visa versa, but still
// the elect must also check this.
unimplemented!();
}

#[test]
=======
>>>>>>> f7bd10a548201cb68491f72be73115279d97f160
fn assignment_convert_works() {
let staked = StakedAssignment {
who: 1 as AccountId,
Expand Down Expand Up @@ -940,7 +953,11 @@ mod compact {
);
}

<<<<<<< HEAD
#[test]
=======
#[test]
>>>>>>> f7bd10a548201cb68491f72be73115279d97f160
fn zero_target_count_is_ignored() {
let voters = vec![1 as AccountId, 2];
let targets = vec![10 as AccountId, 11];
Expand Down