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

Commit a3d783c

Browse files
kianenigmaParity Benchmarking Bot
andauthored
Don't slash all outgoing members. (#7394)
* Don't slash all outgoing members. * One more fix * cargo run --release --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml -- benchmark --chain dev --steps 50 --repeat 20 --extrinsic * --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output ./bin/node/runtime/src/weights --header ./HEADER --pallet pallet_elections_phragmen Co-authored-by: Parity Benchmarking Bot <[email protected]>
1 parent d766e22 commit a3d783c

File tree

2 files changed

+119
-24
lines changed

2 files changed

+119
-24
lines changed
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of Substrate.
22

3-
// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
3+
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
44
// SPDX-License-Identifier: Apache-2.0
55

66
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,8 +15,10 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc6
19-
18+
//! Weights for pallet_elections_phragmen
19+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
20+
//! DATE: 2020-10-26, STEPS: [50], REPEAT: 20, LOW RANGE: [], HIGH RANGE: []
21+
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128
2022
#![allow(unused_parens)]
2123
#![allow(unused_imports)]
2224

@@ -26,65 +28,65 @@ use sp_std::marker::PhantomData;
2628
pub struct WeightInfo<T>(PhantomData<T>);
2729
impl<T: frame_system::Trait> pallet_elections_phragmen::WeightInfo for WeightInfo<T> {
2830
fn vote(v: u32, ) -> Weight {
29-
(91_489_000 as Weight)
30-
.saturating_add((199_000 as Weight).saturating_mul(v as Weight))
31+
(91_991_000 as Weight)
32+
.saturating_add((184_000 as Weight).saturating_mul(v as Weight))
3133
.saturating_add(T::DbWeight::get().reads(5 as Weight))
3234
.saturating_add(T::DbWeight::get().writes(2 as Weight))
3335
}
3436
fn vote_update(v: u32, ) -> Weight {
35-
(56_511_000 as Weight)
36-
.saturating_add((245_000 as Weight).saturating_mul(v as Weight))
37+
(56_633_000 as Weight)
38+
.saturating_add((228_000 as Weight).saturating_mul(v as Weight))
3739
.saturating_add(T::DbWeight::get().reads(5 as Weight))
3840
.saturating_add(T::DbWeight::get().writes(2 as Weight))
3941
}
4042
fn remove_voter() -> Weight {
41-
(76_714_000 as Weight)
43+
(76_890_000 as Weight)
4244
.saturating_add(T::DbWeight::get().reads(2 as Weight))
4345
.saturating_add(T::DbWeight::get().writes(2 as Weight))
4446
}
4547
fn report_defunct_voter_correct(c: u32, v: u32, ) -> Weight {
4648
(0 as Weight)
47-
.saturating_add((1_743_000 as Weight).saturating_mul(c as Weight))
48-
.saturating_add((31_750_000 as Weight).saturating_mul(v as Weight))
49+
.saturating_add((1_769_000 as Weight).saturating_mul(c as Weight))
50+
.saturating_add((32_244_000 as Weight).saturating_mul(v as Weight))
4951
.saturating_add(T::DbWeight::get().reads(7 as Weight))
5052
.saturating_add(T::DbWeight::get().writes(3 as Weight))
5153
}
5254
fn report_defunct_voter_incorrect(c: u32, v: u32, ) -> Weight {
5355
(0 as Weight)
54-
.saturating_add((1_733_000 as Weight).saturating_mul(c as Weight))
55-
.saturating_add((31_861_000 as Weight).saturating_mul(v as Weight))
56+
.saturating_add((1_777_000 as Weight).saturating_mul(c as Weight))
57+
.saturating_add((32_528_000 as Weight).saturating_mul(v as Weight))
5658
.saturating_add(T::DbWeight::get().reads(6 as Weight))
5759
.saturating_add(T::DbWeight::get().writes(2 as Weight))
5860
}
5961
fn submit_candidacy(c: u32, ) -> Weight {
60-
(74_714_000 as Weight)
61-
.saturating_add((315_000 as Weight).saturating_mul(c as Weight))
62+
(75_137_000 as Weight)
63+
.saturating_add((310_000 as Weight).saturating_mul(c as Weight))
6264
.saturating_add(T::DbWeight::get().reads(3 as Weight))
6365
.saturating_add(T::DbWeight::get().writes(1 as Weight))
6466
}
6567
fn renounce_candidacy_candidate(c: u32, ) -> Weight {
66-
(50_408_000 as Weight)
67-
.saturating_add((159_000 as Weight).saturating_mul(c as Weight))
68+
(50_071_000 as Weight)
69+
.saturating_add((184_000 as Weight).saturating_mul(c as Weight))
6870
.saturating_add(T::DbWeight::get().reads(1 as Weight))
6971
.saturating_add(T::DbWeight::get().writes(1 as Weight))
7072
}
7173
fn renounce_candidacy_members() -> Weight {
72-
(79_626_000 as Weight)
74+
(79_471_000 as Weight)
7375
.saturating_add(T::DbWeight::get().reads(3 as Weight))
7476
.saturating_add(T::DbWeight::get().writes(4 as Weight))
7577
}
7678
fn renounce_candidacy_runners_up() -> Weight {
77-
(49_715_000 as Weight)
79+
(49_740_000 as Weight)
7880
.saturating_add(T::DbWeight::get().reads(1 as Weight))
7981
.saturating_add(T::DbWeight::get().writes(1 as Weight))
8082
}
8183
fn remove_member_with_replacement() -> Weight {
82-
(76_572_000 as Weight)
84+
(76_973_000 as Weight)
8385
.saturating_add(T::DbWeight::get().reads(4 as Weight))
8486
.saturating_add(T::DbWeight::get().writes(5 as Weight))
8587
}
8688
fn remove_member_wrong_refund() -> Weight {
87-
(8_777_000 as Weight)
89+
(8_871_000 as Weight)
8890
.saturating_add(T::DbWeight::get().reads(1 as Weight))
8991
}
9092
}

frame/elections-phragmen/src/lib.rs

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -973,18 +973,29 @@ impl<T: Trait> Module<T> {
973973
);
974974
T::ChangeMembers::set_prime(prime);
975975

976-
// outgoing members lose their bond.
977-
let mut to_burn_bond = outgoing.to_vec();
976+
// outgoing members who are no longer a runner-up lose their bond.
977+
let mut to_burn_bond = outgoing
978+
.iter()
979+
.filter(|o| new_runners_up_ids_sorted.binary_search(o).is_err())
980+
.cloned()
981+
.collect::<Vec<_>>();
978982

979-
// compute the outgoing of runners up as well and append them to the `to_burn_bond`
983+
// compute the outgoing of runners up as well and append them to the `to_burn_bond`, if
984+
// they are not members.
980985
{
981986
let (_, outgoing) = T::ChangeMembers::compute_members_diff(
982987
&new_runners_up_ids_sorted,
983988
&old_runners_up_ids_sorted,
984989
);
985990
// none of the ones computed to be outgoing must still be in the list.
986991
debug_assert!(outgoing.iter().all(|o| !new_runners_up_ids_sorted.contains(o)));
987-
to_burn_bond.extend(outgoing);
992+
to_burn_bond.extend(
993+
outgoing
994+
.iter()
995+
.filter(|o| new_members_ids_sorted.binary_search(o).is_err())
996+
.cloned()
997+
.collect::<Vec<_>>()
998+
);
988999
}
9891000

9901001
// Burn loser bond. members list is sorted. O(NLogM) (N candidates, M members)
@@ -2876,4 +2887,86 @@ mod tests {
28762887
assert_eq!(balances(&3), (25, 5));
28772888
})
28782889
}
2890+
2891+
#[test]
2892+
fn member_to_runner_up_wont_slash() {
2893+
ExtBuilder::default().desired_runners_up(2).desired_members(1).build_and_execute(|| {
2894+
assert_ok!(submit_candidacy(Origin::signed(4)));
2895+
assert_ok!(submit_candidacy(Origin::signed(3)));
2896+
assert_ok!(submit_candidacy(Origin::signed(2)));
2897+
2898+
2899+
assert_ok!(vote(Origin::signed(4), vec![4], 40));
2900+
assert_ok!(vote(Origin::signed(3), vec![3], 30));
2901+
assert_ok!(vote(Origin::signed(2), vec![2], 20));
2902+
2903+
System::set_block_number(5);
2904+
Elections::end_block(System::block_number());
2905+
2906+
assert_eq!(Elections::members_ids(), vec![4]);
2907+
assert_eq!(Elections::runners_up_ids(), vec![2, 3]);
2908+
2909+
assert_eq!(balances(&4), (35, 5));
2910+
assert_eq!(balances(&3), (25, 5));
2911+
assert_eq!(balances(&2), (15, 5));
2912+
2913+
// this guy will shift everyone down.
2914+
assert_ok!(submit_candidacy(Origin::signed(5)));
2915+
assert_ok!(vote(Origin::signed(5), vec![5], 50));
2916+
2917+
System::set_block_number(10);
2918+
Elections::end_block(System::block_number());
2919+
2920+
assert_eq!(Elections::members_ids(), vec![5]);
2921+
assert_eq!(Elections::runners_up_ids(), vec![3, 4]);
2922+
2923+
// 4 went from member to runner-up -- don't slash.
2924+
assert_eq!(balances(&4), (35, 5));
2925+
// 3 stayed runner-up -- don't slash.
2926+
assert_eq!(balances(&3), (25, 5));
2927+
// 2 was removed -- slash.
2928+
assert_eq!(balances(&2), (15, 2));
2929+
});
2930+
}
2931+
2932+
#[test]
2933+
fn runner_up_to_member_wont_slash() {
2934+
ExtBuilder::default().desired_runners_up(2).desired_members(1).build_and_execute(|| {
2935+
assert_ok!(submit_candidacy(Origin::signed(4)));
2936+
assert_ok!(submit_candidacy(Origin::signed(3)));
2937+
assert_ok!(submit_candidacy(Origin::signed(2)));
2938+
2939+
2940+
assert_ok!(vote(Origin::signed(4), vec![4], 40));
2941+
assert_ok!(vote(Origin::signed(3), vec![3], 30));
2942+
assert_ok!(vote(Origin::signed(2), vec![2], 20));
2943+
2944+
System::set_block_number(5);
2945+
Elections::end_block(System::block_number());
2946+
2947+
assert_eq!(Elections::members_ids(), vec![4]);
2948+
assert_eq!(Elections::runners_up_ids(), vec![2, 3]);
2949+
2950+
assert_eq!(balances(&4), (35, 5));
2951+
assert_eq!(balances(&3), (25, 5));
2952+
assert_eq!(balances(&2), (15, 5));
2953+
2954+
// swap some votes.
2955+
assert_ok!(vote(Origin::signed(4), vec![2], 40));
2956+
assert_ok!(vote(Origin::signed(2), vec![4], 20));
2957+
2958+
System::set_block_number(10);
2959+
Elections::end_block(System::block_number());
2960+
2961+
assert_eq!(Elections::members_ids(), vec![2]);
2962+
assert_eq!(Elections::runners_up_ids(), vec![4, 3]);
2963+
2964+
// 2 went from runner to member, don't slash
2965+
assert_eq!(balances(&2), (15, 5));
2966+
// 4 went from member to runner, don't slash
2967+
assert_eq!(balances(&4), (35, 5));
2968+
// 3 stayed the same
2969+
assert_eq!(balances(&3), (25, 5));
2970+
});
2971+
}
28792972
}

0 commit comments

Comments
 (0)