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
Decouple Staking and Election #7319
Closed
Closed
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit
Hold shift + click to select a range
25bddc9
mockup
kianenigma 7bef52f
Add files
kianenigma 4f3dfa4
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma bc30ce1
More mockup
kianenigma 9e52fbc
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma 14ebc95
Lots more changes and tests. Initial design of signed is forseable now.
kianenigma 0587fc9
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma 233d4e1
More changes for the signed phase, now going to the cluster-fu*k of g…
kianenigma 9c880f3
Finally decoupled solution from the pallet.. such a PITA.
kianenigma 0cb35eb
Much simplifications and cleanups.
kianenigma c1ff3ec
A skeleton for the unsigned version.
kianenigma 85a346e
Master.into()
kianenigma 6a2542d
Staking builds and tests compile, some tests fail
kianenigma c361911
All staking tests work
kianenigma 218db10
Unsigned almost done, primitive crate added.
kianenigma 5e21a79
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma 25c1bc3
Use decl_error
kianenigma d53785c
Add basic benchmark setup, needs more work
kianenigma 525f6e0
Comment.
kianenigma 3026316
Checkpoint.
kianenigma 9841e3b
node-runtime builds.
kianenigma 16c0550
Builds and all good?
kianenigma f197b6b
Undo some formatting mistakes
kianenigma 5005281
Working on benchmarks'
kianenigma bd0d331
Fix bench
kianenigma 67dd4d1
Strange merge -- everything builds.
kianenigma eac4571
Everything compiles and good -- need to fix benches.
kianenigma a7c4841
Some Staking stuff left to do.
kianenigma 2ca3dcf
Master.into()
kianenigma 4922897
Split the snapshot; type for CompactSolution and MaxNominations
kianenigma ef593a7
Fix a bunch more issues.
kianenigma 8661814
check new design
kianenigma 57f7294
Everything within the module works and compiles.
kianenigma af304d1
Stkaing also builds
kianenigma 80e7450
Everything builds with the new scheme.
kianenigma 4b4712a
Master.into()
kianenigma 206933a
Simplify elect API.
kianenigma af9b97d
Fix all other tests.
kianenigma 9663fea
make the staking api better and well tested.
kianenigma 7f4f5b7
Almost done with conversion
kianenigma 8fb11f1
Move to pallet V2 + construct_runtime
kianenigma bd6d4d9
Master.into()
kianenigma 65f5ac1
Revert staking changes;
kianenigma 3860885
Revert Staking fuzzer.
kianenigma 392f35a
Fix fuzzers
kianenigma 1337e1b
Update frame/election-providers/src/two_phase/mod.rs
kianenigma 4f6ec0d
a round of self review
kianenigma b3113f7
A large number of bugfixes after local test and self-review
kianenigma abca039
Upstream.into()
kianenigma 6bc578a
Master.into()
kianenigma 876f666
Apply suggestions from code review
kianenigma 81df1df
Update frame/election-providers/src/two_phase/benchmarking.rs
kianenigma e3862ce
Fix some review grumbles.
kianenigma ca06f8e
Fix some docs
kianenigma 6206c32
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma 95d7a3c
Master.into()
kianenigma 63776ff
Maybe relocalte the code?
kianenigma 4732854
Master.into()
kianenigma a922f0b
Witness -> SoltionSize
kianenigma 6d8247a
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
e9f4437
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
02069e1
Update frame/session/src/lib.rs
kianenigma 000ecdc
Revamp macros
kianenigma cb6f156
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma df04ef3
Merge remote-tracking branch 'origin/master' into kiz-election-provider
ee2a316
Master.into()
kianenigma 0d92ed5
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma 88a96a0
Fix build
kianenigma 6480ce1
Merge remote-tracking branch 'origin/master' into kiz-election-provider
8a68915
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
f4e2c03
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma c41ecb0
fix all the tests.
kianenigma b42336a
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma f5e23f9
Fix the strange issue with cargo lock
kianenigma 56bb93c
revamp closures.
kianenigma 257795c
Update frame/two-phase-election-provider/src/benchmarking.rs
kianenigma 9c114bc
Merge remote-tracking branch 'origin/master' into kiz-election-provider
1ac97f5
Some review grumbles.
kianenigma 366b15b
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma afa5507
Upstream.into()
kianenigma 5d206ab
Fix build
kianenigma ea193c7
Bring back benches
kianenigma 60e57d7
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
77f53a3
Add a bunch of more fixes: signed weight check, tests etc.
kianenigma 9ce9d6b
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma b30dfb3
Fix build
kianenigma 6001d7d
Master.into()
kianenigma a066e4a
Last small tweaks
kianenigma 10f6e7b
Fix all tests.
kianenigma 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
Everything within the module works and compiles.
- Loading branch information
commit 57f7294d1a9fbe19183440956cf8107587663774
There are no files selected for viewing
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 |
|---|---|---|
| @@ -1,16 +1,38 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) 2020 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. | ||
|
|
||
| //! An implementation of [`sp_election_providers::ElectionProvider`] that does an on-chain | ||
| //! sequential phragmen. | ||
|
|
||
| use sp_arithmetic::PerThing; | ||
| use sp_election_providers::ElectionProvider; | ||
| use sp_election_providers::{ElectionDataProvider, ElectionProvider}; | ||
| use sp_npos_elections::{ | ||
| ElectionResult, ExtendedBalance, IdentifierT, PerThing128, Supports, VoteWeight, | ||
| }; | ||
| use sp_runtime::RuntimeDebug; | ||
| use sp_std::{collections::btree_map::BTreeMap, prelude::*}; | ||
| use sp_std::{collections::btree_map::BTreeMap, marker::PhantomData, prelude::*}; | ||
|
|
||
| /// Errors of the on-chain election. | ||
| #[derive(RuntimeDebug, Eq, PartialEq)] | ||
| pub enum Error { | ||
| /// An internal error in the NPoS elections crate. | ||
| NposElections(sp_npos_elections::Error), | ||
| /// An assignment failed to pass the feasibility check | ||
| Feasibility(&'static str), | ||
| } | ||
|
|
||
| impl From<sp_npos_elections::Error> for Error { | ||
|
|
@@ -26,47 +48,139 @@ impl From<sp_npos_elections::Error> for Error { | |
| /// ### Warning | ||
| /// | ||
| /// This can be very expensive to run frequently on-chain. Use with care. | ||
| pub struct OnChainSequentialPhragmen; | ||
| pub struct OnChainSequentialPhragmen<T: Config>(PhantomData<T>); | ||
|
|
||
| impl<AccountId: IdentifierT> ElectionProvider<AccountId> for OnChainSequentialPhragmen { | ||
| /// Configuration trait of [`OnChainSequentialPhragmen`]. | ||
| /// | ||
| /// Note that this is similar to a pallet traits, but [`OnChainSequentialPhragmen`] is not a pallet. | ||
| pub trait Config { | ||
| /// The account identifier type. | ||
| type AccountId: IdentifierT; | ||
| /// The block number type. | ||
| type BlockNumber; | ||
| /// Something that provides the data for election. | ||
| type ElectionDataProvider: ElectionDataProvider<Self::AccountId, Self::BlockNumber>; | ||
| } | ||
|
|
||
| impl<T: Config> ElectionProvider<T::AccountId> for OnChainSequentialPhragmen<T> { | ||
| type Error = Error; | ||
| const NEEDS_ELECT_DATA: bool = true; | ||
|
|
||
| fn elect<P: PerThing128>( | ||
| to_elect: usize, | ||
| targets: Vec<AccountId>, | ||
| voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>, | ||
| ) -> Result<Supports<AccountId>, Self::Error> | ||
| fn elect<P: PerThing128>() -> Result<Supports<T::AccountId>, Self::Error> | ||
| where | ||
| ExtendedBalance: From<<P as PerThing>::Inner>, | ||
| { | ||
| let mut stake_map: BTreeMap<AccountId, VoteWeight> = BTreeMap::new(); | ||
| let voters = T::ElectionDataProvider::voters(); | ||
| let targets = T::ElectionDataProvider::targets(); | ||
| let desired_targets = T::ElectionDataProvider::desired_targets() as usize; | ||
|
|
||
| let mut stake_map: BTreeMap<T::AccountId, VoteWeight> = BTreeMap::new(); | ||
|
|
||
| voters.iter().for_each(|(v, s, _)| { | ||
| stake_map.insert(v.clone(), *s); | ||
| }); | ||
| let stake_of = Box::new(|w: &AccountId| -> VoteWeight { | ||
|
|
||
| let stake_of = Box::new(|w: &T::AccountId| -> VoteWeight { | ||
| stake_map.get(w).cloned().unwrap_or_default() | ||
| }); | ||
|
|
||
| sp_npos_elections::seq_phragmen::<_, P>(to_elect, targets, voters, None) | ||
| .and_then(|e| { | ||
| // these could use potential simplifications. | ||
| let ElectionResult { | ||
| winners, | ||
| assignments, | ||
| } = e; | ||
| let staked = sp_npos_elections::assignment_ratio_to_staked_normalized( | ||
| assignments, | ||
| &stake_of, | ||
| )?; | ||
| let winners = sp_npos_elections::to_without_backing(winners); | ||
|
|
||
| sp_npos_elections::to_supports(&winners, &staked) | ||
| }) | ||
| .map_err(From::from) | ||
| let ElectionResult { | ||
| winners, | ||
| assignments, | ||
| } = sp_npos_elections::seq_phragmen::<_, P>(desired_targets, targets, voters, None) | ||
| .map_err(Error::from)?; | ||
|
|
||
| // check all assignments for feasibility, based on election data provider. | ||
| assignments | ||
| .iter() | ||
| .map(T::ElectionDataProvider::feasibility_check_assignment) | ||
| .collect::<Result<_, _>>() | ||
| .map_err(|e| Error::Feasibility(e))?; | ||
|
|
||
| let staked = | ||
| sp_npos_elections::assignment_ratio_to_staked_normalized(assignments, &stake_of)?; | ||
| let winners = sp_npos_elections::to_without_backing(winners); | ||
|
|
||
| sp_npos_elections::to_supports(&winners, &staked).map_err(Error::from) | ||
| } | ||
|
|
||
| fn ongoing() -> bool { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This API is added to cope with deferring slashes during election. I believe now we can do that as election no longer uses live staking data, but rather a snapshot thereof. Although it is harmless to keep around as well and remove it later as it would require some changes in offences pallet as well. |
||
| false | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use sp_election_providers::{Assignment, VoteWeight}; | ||
| use sp_npos_elections::Support; | ||
|
|
||
| type AccountId = u64; | ||
| type BlockNumber = u32; | ||
| type Accuracy = sp_runtime::Perbill; | ||
|
|
||
| struct Runtime; | ||
| impl Config for Runtime { | ||
| type AccountId = AccountId; | ||
| type BlockNumber = BlockNumber; | ||
| type ElectionDataProvider = mock_data_provider::DataProvider; | ||
| } | ||
|
|
||
| type OnChainPhragmen = OnChainSequentialPhragmen<Runtime>; | ||
|
|
||
| mod mock_data_provider { | ||
| use super::*; | ||
|
|
||
| pub struct DataProvider; | ||
|
|
||
| impl ElectionDataProvider<AccountId, BlockNumber> for DataProvider { | ||
| fn voters() -> Vec<(AccountId, VoteWeight, Vec<AccountId>)> { | ||
| vec![ | ||
| (1, 10, vec![10, 20]), | ||
| (2, 20, vec![30, 20]), | ||
| (3, 30, vec![10, 30]), | ||
| ] | ||
| } | ||
|
|
||
| fn targets() -> Vec<AccountId> { | ||
| vec![10, 20, 30] | ||
| } | ||
|
|
||
| fn desired_targets() -> u32 { | ||
| 2 | ||
| } | ||
|
|
||
| fn feasibility_check_assignment<P: PerThing>( | ||
| _: &Assignment<AccountId, P>, | ||
| ) -> Result<(), &'static str> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn next_election_prediction(_: BlockNumber) -> BlockNumber { | ||
| 0 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn onchain_seq_phragmen_works() { | ||
| assert_eq!( | ||
| OnChainPhragmen::elect::<Accuracy>().unwrap(), | ||
| vec![ | ||
| ( | ||
| 10, | ||
| Support { | ||
| total: 25, | ||
| voters: vec![(1, 10), (3, 15)] | ||
| } | ||
| ), | ||
| ( | ||
| 30, | ||
| Support { | ||
| total: 35, | ||
| voters: vec![(2, 20), (3, 15)] | ||
| } | ||
| ) | ||
| ] | ||
| ); | ||
| } | ||
| } | ||
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
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.