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 2.7k
Ranked Collective pallet #11548
Merged
Merged
Ranked Collective pallet #11548
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1798b60
Ranked Collective pallet
gavofyork a6e2367
Fixes
gavofyork bf125a8
benchmarks
gavofyork b8f6f2c
Weights
gavofyork c44e41a
Allow class voting in rank
gavofyork 0822cd6
Index by rank, still O(1).
gavofyork 5688cb3
Custom vote weights
gavofyork 919589d
Formatting
gavofyork 7fc9867
Merge branch 'master' into gav-ranked-collective
gavofyork 4deff13
Update frame/ranked-collective/src/lib.rs
gavofyork d5a2166
Broken :(
gavofyork 15293e0
Merge branch 'gav-ranked-collective' of github.com:paritytech/substra…
gavofyork fd172b8
origin guard; cleanup uses new API
gavofyork 6b781b7
Formatting
gavofyork 0ee3e28
Promote/demote by rank
gavofyork 255420a
Formatting
gavofyork a8217fa
Use new API
gavofyork 63dd270
Remove code in another PR
gavofyork 1530149
Remove code in another PR
gavofyork 10f293b
Formatting
gavofyork b5a0aed
Remove code in another PR
gavofyork ad7d645
Docs
gavofyork bec4d6e
Docs
gavofyork ea0f6b1
Merge remote-tracking branch 'origin/master' into gav-ranked-collective
gavofyork 5168c7b
Bump
gavofyork 3a84fa2
Fixes
gavofyork f9d1ce2
Formatting
gavofyork 51f59ee
Fixes
gavofyork 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| [package] | ||
| name = "pallet-ranked-collective" | ||
| version = "4.0.0-dev" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2021" | ||
| license = "Apache-2.0" | ||
| homepage = "https://substrate.io" | ||
| repository = "https://github.com/paritytech/substrate/" | ||
| description = "Ranked collective system: Members of a set of account IDs can make their collective feelings known through dispatched calls from one of two specialized origins." | ||
| readme = "README.md" | ||
|
|
||
| [package.metadata.docs.rs] | ||
| targets = ["x86_64-unknown-linux-gnu"] | ||
|
|
||
| [dependencies] | ||
| codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } | ||
| log = { version = "0.4.16", default-features = false } | ||
| scale-info = { version = "2.0.1", default-features = false, features = ["derive"] } | ||
| frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" } | ||
| frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" } | ||
| frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" } | ||
| sp-arithmetic = { version = "5.0.0", default-features = false, path = "../../primitives/arithmetic" } | ||
| sp-core = { version = "6.0.0", default-features = false, path = "../../primitives/core" } | ||
| sp-io = { version = "6.0.0", default-features = false, path = "../../primitives/io" } | ||
| sp-runtime = { version = "6.0.0", default-features = false, path = "../../primitives/runtime" } | ||
| sp-std = { version = "4.0.0", default-features = false, path = "../../primitives/std" } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "codec/std", | ||
| "frame-benchmarking/std", | ||
| "frame-support/std", | ||
| "frame-system/std", | ||
| "log/std", | ||
| "scale-info/std", | ||
| "sp-arithmetic/std", | ||
| "sp-core/std", | ||
| "sp-io/std", | ||
| "sp-runtime/std", | ||
| "sp-std/std", | ||
| ] | ||
| runtime-benchmarks = [ | ||
| "frame-benchmarking/runtime-benchmarks", | ||
| "frame-support/runtime-benchmarks", | ||
| "frame-system/runtime-benchmarks", | ||
| "sp-runtime/runtime-benchmarks", | ||
| ] | ||
| try-runtime = ["frame-support/try-runtime"] |
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Ranked collective system. | ||
|
|
||
| This is a membership pallet providing a `Tally` implementation ready for use with polling | ||
| systems such as the Referenda pallet. Members each have a rank, with zero being the lowest. | ||
| There is no complexity limitation on either the number of members at a rank or the number of | ||
| ranks in the system thus allowing potentially public membership. A member of at least a given | ||
| rank can be selected at random in O(1) time, allowing for various games to constructed using | ||
| this as a primitive. Members may only be promoted and demoted by one rank at a time, however | ||
| all operations (save one) are O(1) in complexity. The only operation which is not O(1) is the | ||
| `remove_member` since they must be removed from all ranks from the present down to zero. | ||
|
|
||
| Different ranks have different voting power, and are able to vote in different polls. In general | ||
| rank privileges are cumulative. Higher ranks are able to vote in any polls open to lower ranks. | ||
| Similarly, higher ranks always have at least as much voting power in any given poll as lower | ||
| ranks. | ||
|
|
||
| Two `Config` trait items control these "rank privileges": `MinRankOfClass` and `VoteWeight`. | ||
| The first controls which ranks are allowed to vote on a particular class of poll. The second | ||
| controls the weight of a vote given the voters rank compared to the minimum rank of the poll. | ||
|
|
||
| An origin control, `EnsureRank`, ensures that the origin is a member of the collective of at | ||
| least a particular rank. |
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 |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) 2020-2022 Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Staking pallet benchmarking. | ||
|
|
||
| use super::*; | ||
| #[allow(unused_imports)] | ||
| use crate::Pallet as RankedCollective; | ||
|
|
||
| use frame_benchmarking::{account, benchmarks_instance_pallet, whitelisted_caller}; | ||
| use frame_support::{assert_ok, dispatch::UnfilteredDispatchable}; | ||
| use frame_system::RawOrigin as SystemOrigin; | ||
|
|
||
| const SEED: u32 = 0; | ||
|
|
||
| fn assert_last_event<T: Config<I>, I: 'static>(generic_event: <T as Config<I>>::Event) { | ||
| frame_system::Pallet::<T>::assert_last_event(generic_event.into()); | ||
| } | ||
|
|
||
| fn make_member<T: Config<I>, I: 'static>(rank: Rank) -> T::AccountId { | ||
| let who = account::<T::AccountId>("member", MemberCount::<T, I>::get(0), SEED); | ||
| assert_ok!(Pallet::<T, I>::add_member(T::PromoteOrigin::successful_origin(), who.clone())); | ||
| for _ in 0..rank { | ||
| assert_ok!(Pallet::<T, I>::promote_member( | ||
| T::PromoteOrigin::successful_origin(), | ||
| who.clone() | ||
| )); | ||
| } | ||
| who | ||
| } | ||
|
|
||
| benchmarks_instance_pallet! { | ||
| add_member { | ||
| let who = account::<T::AccountId>("member", 0, SEED); | ||
| let origin = T::PromoteOrigin::successful_origin(); | ||
| let call = Call::<T, I>::add_member { who: who.clone() }; | ||
| }: { call.dispatch_bypass_filter(origin)? } | ||
| verify { | ||
| assert_eq!(MemberCount::<T, I>::get(0), 1); | ||
| assert_last_event::<T, I>(Event::MemberAdded { who }.into()); | ||
| } | ||
|
|
||
| remove_member { | ||
| let r in 0 .. 10; | ||
| let rank = r as u16; | ||
| let first = make_member::<T, I>(rank); | ||
| let who = make_member::<T, I>(rank); | ||
| let last = make_member::<T, I>(rank); | ||
| let last_index = (0..=rank).map(|r| IdToIndex::<T, I>::get(r, &last).unwrap()).collect::<Vec<_>>(); | ||
| let origin = T::DemoteOrigin::successful_origin(); | ||
| let call = Call::<T, I>::remove_member { who: who.clone(), min_rank: rank }; | ||
| }: { call.dispatch_bypass_filter(origin)? } | ||
| verify { | ||
| for r in 0..=rank { | ||
| assert_eq!(MemberCount::<T, I>::get(r), 2); | ||
| assert_ne!(last_index[r as usize], IdToIndex::<T, I>::get(r, &last).unwrap()); | ||
| } | ||
| assert_last_event::<T, I>(Event::MemberRemoved { who, rank }.into()); | ||
| } | ||
|
|
||
| promote_member { | ||
| let r in 0 .. 10; | ||
| let rank = r as u16; | ||
| let who = make_member::<T, I>(rank); | ||
| let origin = T::PromoteOrigin::successful_origin(); | ||
| let call = Call::<T, I>::promote_member { who: who.clone() }; | ||
| }: { call.dispatch_bypass_filter(origin)? } | ||
| verify { | ||
| assert_eq!(Members::<T, I>::get(&who).unwrap().rank, rank + 1); | ||
| assert_last_event::<T, I>(Event::RankChanged { who, rank: rank + 1 }.into()); | ||
| } | ||
|
|
||
| demote_member { | ||
| let r in 0 .. 10; | ||
| let rank = r as u16; | ||
| let first = make_member::<T, I>(rank); | ||
| let who = make_member::<T, I>(rank); | ||
| let last = make_member::<T, I>(rank); | ||
| let last_index = IdToIndex::<T, I>::get(rank, &last).unwrap(); | ||
| let origin = T::DemoteOrigin::successful_origin(); | ||
| let call = Call::<T, I>::demote_member { who: who.clone() }; | ||
| }: { call.dispatch_bypass_filter(origin)? } | ||
| verify { | ||
| assert_eq!(Members::<T, I>::get(&who).map(|x| x.rank), rank.checked_sub(1)); | ||
| assert_eq!(MemberCount::<T, I>::get(rank), 2); | ||
| assert_ne!(last_index, IdToIndex::<T, I>::get(rank, &last).unwrap()); | ||
| assert_last_event::<T, I>(match rank { | ||
| 0 => Event::MemberRemoved { who, rank: 0 }, | ||
| r => Event::RankChanged { who, rank: r - 1 }, | ||
| }.into()); | ||
| } | ||
|
|
||
| vote { | ||
| let caller: T::AccountId = whitelisted_caller(); | ||
| assert_ok!(Pallet::<T, I>::add_member(T::PromoteOrigin::successful_origin(), caller.clone())); | ||
| // Create a poll | ||
| let class = T::Polls::classes().into_iter().next().unwrap(); | ||
| let rank = T::MinRankOfClass::convert(class.clone()); | ||
| for _ in 0..rank { | ||
| assert_ok!(Pallet::<T, I>::promote_member( | ||
| T::PromoteOrigin::successful_origin(), | ||
| caller.clone() | ||
| )); | ||
| } | ||
|
|
||
| let poll = T::Polls::create_ongoing(class).expect("Must always be able to create a poll for rank 0"); | ||
|
|
||
| // Vote once. | ||
| assert_ok!(Pallet::<T, I>::vote(SystemOrigin::Signed(caller.clone()).into(), poll, true)); | ||
| }: _(SystemOrigin::Signed(caller.clone()), poll, false) | ||
| verify { | ||
| let tally = Tally::from_parts(0, 0, 1); | ||
| let ev = Event::Voted { who: caller, poll, vote: VoteRecord::Nay(1), tally }; | ||
| assert_last_event::<T, I>(ev.into()); | ||
| } | ||
|
|
||
| cleanup_poll { | ||
| let n in 1 .. 100; | ||
|
|
||
| // Create a poll | ||
| let class = T::Polls::classes().into_iter().next().unwrap(); | ||
| let rank = T::MinRankOfClass::convert(class.clone()); | ||
| let poll = T::Polls::create_ongoing(class).expect("Must always be able to create a poll"); | ||
|
|
||
| // Vote in the poll by each of `n` members | ||
| for i in 0..n { | ||
| let who = make_member::<T, I>(rank); | ||
| assert_ok!(Pallet::<T, I>::vote(SystemOrigin::Signed(who).into(), poll, true)); | ||
| } | ||
|
|
||
| // End the poll. | ||
| T::Polls::end_ongoing(poll, false).expect("Must always be able to end a poll"); | ||
|
|
||
| assert_eq!(Voting::<T, I>::iter_prefix(poll).count(), n as usize); | ||
| }: _(SystemOrigin::Signed(whitelisted_caller()), poll, n) | ||
| verify { | ||
| assert_eq!(Voting::<T, I>::iter().count(), 0); | ||
| } | ||
|
|
||
| impl_benchmark_test_suite!(RankedCollective, crate::tests::new_test_ext(), crate::tests::Test); | ||
| } | ||
Oops, something went wrong.
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.