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

Tracking issue for next generation pallet-staking and elections #6242

Closed
4 of 5 tasks
kianenigma opened this issue Jun 4, 2020 · 8 comments
Closed
4 of 5 tasks

Tracking issue for next generation pallet-staking and elections #6242

kianenigma opened this issue Jun 4, 2020 · 8 comments
Labels
J1-meta A specific issue for grouping tasks or bugs of a specific category. U2-some_time_soon Issue is worth doing soon.

Comments

@kianenigma
Copy link
Contributor

kianenigma commented Jun 4, 2020

I have had lots of ideas about how to improve staking, its election algorithm and its configurability lately. Here is a digest and a tracking issue about it.

Current State of Affairs

pallet-staking is currently at its most sophisticated state. Aside from basic staking ops ([un]bonding, setting intentions, maintaining eras and exposures), it handles rewards, slashing, and election, each of which being quite a complex piece of machinery. The problem is, this sophistication also translates to the code being bloated. At the time of this writing, staking's main lib.rs is ~3700 LoC. It worth noting the fact that most of the logic related to slashing and offchain workers are already placed in their own modules. Moreover, it has more than a hundred unit tests, some of which have been reported to be outdated already (#5838).

There are a few major problems with this code and how it is evolving.

  1. Sooner or later, this monolith piece of code will accumulate too much technical debt and become increasingly hard to maintain.
  2. Frame currently provides one and only one staking pallet which has all the aforementioned complexities that I named. It would be a major drawback not to have any configurability over this. I really don't think all the chains that get built by substrate need for instance the offchain worker logic of staking. Same could be arguably said about slashing and rewards as well.

That being said, there is a way to switch off the offchain workers, but the code is still very complicated and will be shipped to everyone who doesn't need it. Since there is a runtime check that disables the offchain machinery, the compiler will also have now way to know this and this will increase the wasm size by a small amount.

  1. I am increasingly worried if the unit tests are sound and will prevent all the basic errors, specially at the boundaries of these logical components: i.e. slashing <-> election.

Aside from aesthetic refactoring, we also need a bunch more election algorithms. Indeed, they are no longer Phragmén-related in any sense, hence we also need to clean and rename the sp-phragmen crate into something more general. The current pipeline is:

Offchain: seq_phragmen() -> random iteration of balancing() (aka. equalise()) -> reduce() -> compact() -> submit to pool.
Onchain: un_compact() -> validity_checks()*.

The next generation pipeline will be:

Offchain: balanced_heuristic()* -> reduce() -> compact() -> submit to pool.
Onchain: un_compact() -> validity_checks() -> PJR_check().

We also need a PJR_enabler() implementation, only to be used if we reach the end of the era with no good submissions. In that case we run seq_phragmen() -> PJR_enabler().

  • Validity checks are those that only make sure a solution is valid, and better than the best solution that we have had before. If we have no solutions for some reason, we accept any piece of sh*t solution as well.
  • New election scheme developed by Web3 Foundation. see here for more details.

The Plan Ahead

Here are the steps that I think are necessary:

// Staking trait
trait Trait {
	type Election: ElectionProvider;
	// ...
}

impl<T: Trait> Module<T> {
	fn new_session(n: BlockNumber) -> Option<T::AccountId> {
		if somehow_time_to_trigger_new_era {
			// prepare inputs
			let validates = <Validators<T>>::iter().collect();
			let nominators = <Nominators<T>>::iter().collect();
			let inp = ElectionInput { validators, nominators };
			let let maybe_new_election_output = T::Election::elect(inp).map(|output| {
				// do some processing with output, such as updating exposures etc.

				// extract only the winners Vec<T::AccountId>
				output.winners
			})
		} else {
			None
		}
	}
}

/// Something that can elect a new set of validators at the end of an era.
trait ElectionProvider {
	/// Elect new validator set.
	// The SUPER TRICKY part here is to chose a worthwhile API for this to cover all the cases.
	// A bonus not to be forgotten is that I want to use this for elections-phragmen pallet as well.
	fn elect(input: ElectionInput) -> Option<ElectionOutput>;
}

// To have an OnChain phragmen only, we just need a simple type to implement this.
struct OnChainSeqPhragmen;

impl ElectionProvider for OnChainSeqPhragmen {
	fn elect(input: ElectionInput) -> Option<ElectionOutput> {
		// just proxy a call to the primitives crates with the elections. No further call should be
		// needed.
	}
}

// To implement the current offchain machinery we will need a new pallet named:
// OffChainElectionProvider.
// /frame/offchain-election-provider/lib.rs

// this module might need to depend on staking to check solutions. We need access to staking
trait Trait: system::Trait /* + staking::Trait */ {
	type ElectionLookahead: Get<Self::BlockNumber>;
	type Call: Dispatchable + From<Call<Self>> + IsSubType<Module<Self>, Self> + Clone;
	type MaxIterations: Get<u32>;
	type MinSolutionScoreBump: Get<Perbill>;
	type UnsignedPriority: Get<TransactionPriority>;
}

// all the storage items that are only for staking
decl_storage! {
	pub SnapshotValidators get(fn snapshot_validators): Option<Vec<T::AccountId>>;
	pub SnapshotNominators get(fn snapshot_nominators): Option<Vec<T::AccountId>>;

	pub QueuedElected get(fn queued_elected): Option<ElectionResult<T::AccountId, BalanceOf<T>>>;
	pub QueuedScore get(fn queued_score): Option<ElectionScore>;

	pub EraElectionStatus get(fn era_election_status): ElectionStatus<T::BlockNumber>;

	pub IsCurrentSessionFinal get(fn is_current_session_final): bool = false;
}

decl_module! {
	fn submit_election_solution()  {}
	fn submit_election_solution_unsigned() {}
}

impl<T: Trait> ElectionProvider for Module<T> {
	fn elect(input: ElectionInput) -> Option<ElectionOutput> {
		// same logic as we have now: return best queued solution, else fallback to onchain staking. 
		<QueuedElected<T>>::take().or_else(||
			Self::fallback_seq_phragmen()
		);
	}
}

Notable challenges here are:

  • The offchain-election-provider module will need to read staking's storage to check values, unless if we pass every data that it needs to it. Not worth IMO.

  • The offchain-election-provider will need to somehow still force staking to lock itself at some points in time.

  • 5. Once this is done, we could optionally investigate stripping down rewards and slashing from staking as well. Ideally, I would like to have a core staking modules that only does, as mentioned above:

[un]bonding, setting intentions, maintaining eras and exposures

And the rest can be plugged to it as additional modules.


I the first 3 steps of the issue are somewhat mandatory to be done very soon in my opinion. The new election is a great security improvement (since we will check for PJR property) and the test cleanup has been due for a long time.

As for the refactor, if it is feasible to do, it would be much better to do it sooner than later, since it will require a complicated migration in storage. Perhaps if it can be done prior to Polkadot's staking enablement, it would be much easier to do the migration while there is still a sudo key at hand. Nonetheless, it is not a big issue as we have to ship this to Kusama first anyhow.

@kianenigma kianenigma added J1-meta A specific issue for grouping tasks or bugs of a specific category. U2-some_time_soon Issue is worth doing soon. labels Jun 4, 2020
@kianenigma
Copy link
Contributor Author

Also a good one to keep in mind: #3544

@kianenigma
Copy link
Contributor Author

Screenshot 2020-08-25 at 15 48 03

cc @thiolliere @shawntabrizi

@kianenigma
Copy link
Contributor Author

Screenshot 2020-08-25 at 15 56 34

@kianenigma
Copy link
Contributor Author

As shown in the pictures above, we had some recent discussions about staking and npos with web3 foundation as well. Here I will write a small digest of this event and the conclusions.

The content here will overlap a bit with the original issue comment but I prefer to keep them separate for better visibility. Anyone reading the main issue should also continue until here to get the full picture.

Roadmap

As seen in the roadmap, PhragMMS #6685 is the last implementation that has been added. Aside from that, we have multiple tracks of improvements. The red ones are urgent, blues are sometime soon and the grey ones are more or less just an idea.

PJR-Check Track

This is the most important track that needs to be worked upon. It needs one PR to add the PJR_check function, potentially also the PJR enabler. This must then be integrated into the current offchain phragmen v1 pipeline and we expect to see no tangible changes here.

This must definitely added prior to the miner track, because we first want to have the PJR check in place for added security and then encourage external submissions.

Miner Track

I am working on this in parallel at the moment and I expect it to be not too much work.

  1. Add MMS algorithm. Hopefully PhragMMS election. #6685 is merged by then and then this implementation will be rather trivial to add. This is a super slow algorithm that is only sensible to use offchain.
  2. Audit the feasability-check() code.
  3. put some small code on-chain that rewards the solutions.
  4. Document and announce a Rust and a JS miner. Rust will be faster but I expect JS to also be useful.

ElectionProvider Track

This is more of a refactor, and has already been explained above. One change to this is that once staking and eleciton are decoupled, we can refactor the current OffchainElectionProvider to be TwoPhaseOffchainElectionProvier, as explained in the picture above.

@kianenigma
Copy link
Contributor Author

kianenigma commented Aug 31, 2020

Some other low hanging fruit in staking are the following concerns. These are generally easy to implement but it is still uncertain if we want them or not.

1. Slashing and Self Stake

The current system barely makes any difference between nominator stake and self stake. In fact, the distinction is only made when we build the Exposure where if a backing is coming from self, we add it to the own field rather than adding a new (AccountId, Balance) pair to others, which saves us a meager few bytes. As for slashing, self stake and nominator stake is essentially the same and makes not much difference.

We are looking for a way to allow validators to put forward some risk factor by having more self stake, and slashing first from the self stake and then from the pro-rate stake.

2. Events that should chill

  1. There's already a rather ongoing dispute about keeping or chilling nominators when a non-zero slash happens here Full Slash Reversals #6835.

  2. Now there's another case as well:

There is a bug in the current system around self stake: you can change it without loosing your nominations. We actually do need to fix that one sometime.

(cc @burdges)

  1. Finally, it is arguable to request some chilling to happen when other parameters also change. What if a validator suddenly increase their reward precent to 100%, effectively ditching all nominators. Should we inform the nominator in this case?

@burdges
Copy link

burdges commented Aug 31, 2020

If we've some suppression scheme, then we could suppress nominations by the factor the reward increased. This suppression continues until the nominator touches their span.

I've no particular justification for this, except that validators can then only increase their reward percentage when they've enough excess nominations, and nominators need not configure anything.

I'm also afraid that this suppression notion (a) sucks for phragmen, and (b) does not quite match the suppression notion suggested for slashing, but maybe some common ground exists.

@stale
Copy link

stale bot commented Jul 7, 2021

Hey, is anyone still working on this? Due to the inactivity this issue has been automatically marked as stale. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the A5-stale Pull request did not receive any updates in a long time. No review needed at this stage. Close it. label Jul 7, 2021
@kianenigma
Copy link
Contributor Author

I am closing this with pretty much the last update about the state of staking, and will open a new tracking issue for the 2021 and onwards. The last note-worthy events:

@stale stale bot removed the A5-stale Pull request did not receive any updates in a long time. No review needed at this stage. Close it. label Aug 6, 2021
kianenigma added a commit to kianenigma/seeding that referenced this issue Sep 27, 2022
# Membership Request 

Hi, I am Kian Paimani, known as @kianenigma. I have been working on Polkadot/Kusama through Parity since February 2019 and I can categorize my main contributions to Polkadot's ecosystem as follows: 

1. Maintaining and developing the staking sub-system.
2. General FRAME development, especially testing and quality assurance. 
3. Polkadot-native side-projects. 
4. Education 

> My first contribution to Polkadot is also indeed related to staking: paritytech/substrate#1915

### Staking system

I joke as the Polkadot staking to be both my blessing and my curse over the years. I started working on it since the first days that I joined this ecosystem and the work [is ongoing ever since](https://github.com/orgs/paritytech/projects/33/views/9). In the past, I focused on making sure that the staking system is secure and to some extent scalable. More recently, I coordinated the (imminent) launch of Nomination Pools. Nowadays I also put an extra effort on making sure that this sub-system of Polkadot is *sustainable*, through code refactor and educating other core developers. 

Lastly, I have been the main author of the [Polkadot staking newsletter](https://gist.github.com/kianenigma/aa835946455b9a3f167821b9d05ba376), which is my main attempt at making the entire complexity and development of this part of the protocol transparent to the end-users.

I expect myself to contribute *directly* to the staking system for at least another ~12, if not more, and afterwards having the role of an advisor. 

Some notable contributions: 

- paritytech/substrate#4517
- paritytech/substrate#7910
- paritytech/substrate#6242
- paritytech/substrate#9415
- paritytech/polkadot#3141
- paritytech/substrate#11212
- paritytech/substrate#12129

### FRAME 

Historically, I have contributed a variety of domains in FRAME, namely: 

- Early version of the weight system paritytech/substrate#3816 paritytech/substrate#3157
- Early version of the transaction fee system
- Primitive arithmetic types paritytech/substrate#3456
- Council election pallet paritytech/substrate#3364

Many of which were, admittedly, a PoC at most, if not considered "poor". I am happy that nowadays many of the above have been refactored and are being maintained by new domain experts. 

These days, I put most of my FRAME focus on testing and quality assurance. Through my work in the staking system, I have had to deal with the high sensitivity and liveness requirement of protocol development first hand (I believe I had to do among the [very first storage migrations](paritytech/substrate#3948) in Kusama) and consequently I felt the need to make better testing facilities, all of which have been formulated in https://forum.polkadot.network/t/testing-complex-frame-pallets-discussion-tools/356. Some relevant PRs:

- paritytech/substrate#8038
- paritytech/substrate#9788
- paritytech/substrate#10174

Regardless of wearing the staking hat, I plan to remain a direct contributor to FRAME, namely because I consider it to be an important requirements of successfully delivering more features to Polkadot's ecosystem. 

### Polkadot-Native Side Projects

I have started multiple small, mostly non-RUST projects in the polkadot ecosystem that I am very happy about, and I plan to continue doing so. I have not yet found the time to make a "polished product" out of any of these, but I hope that I can help foster our community such that someday a team will do so. I consider my role, for the time being, to *put ideas out there* through these side projects. 

- https://github.com/substrate-portfolio/polkadot-portfolio/
- https://github.com/kianenigma/polkadot-basic-notification/
- https://github.com/paritytech/polkadot-scripts/
- https://github.com/paritytech/substrate-debug-kit/

### Education 

Lastly, aside from having had a number of educational talks over the years (all of which [are listed](https://hello.kianenigma.nl/talks/) in my personal website), I am a big enthusiast of the newly formed Polkadot Blockchain Academy. I have [been an instructor](https://singular.app/collectibles/statemine/16/2) in the first cohort, and continue to contribute for as long and as much as I can, whilst still attending to the former 3 duties. 

---

With all of that being said and done, I consider myself at the beginning of the path to Dan 4, but happy to start at a lower one as well.
bkchr added a commit to polkadot-fellows/seeding that referenced this issue Sep 27, 2022
# Membership Request 

Hi, I am Kian Paimani, known as @kianenigma. I have been working on Polkadot/Kusama through Parity since February 2019 and I can categorize my main contributions to Polkadot's ecosystem as follows: 

1. Maintaining and developing the staking sub-system.
2. General FRAME development, especially testing and quality assurance. 
3. Polkadot-native side-projects. 
4. Education 

> My first contribution to Polkadot is also indeed related to staking: paritytech/substrate#1915

### Staking system

I joke as the Polkadot staking to be both my blessing and my curse over the years. I started working on it since the first days that I joined this ecosystem and the work [is ongoing ever since](https://github.com/orgs/paritytech/projects/33/views/9). In the past, I focused on making sure that the staking system is secure and to some extent scalable. More recently, I coordinated the (imminent) launch of Nomination Pools. Nowadays I also put an extra effort on making sure that this sub-system of Polkadot is *sustainable*, through code refactor and educating other core developers. 

Lastly, I have been the main author of the [Polkadot staking newsletter](https://gist.github.com/kianenigma/aa835946455b9a3f167821b9d05ba376), which is my main attempt at making the entire complexity and development of this part of the protocol transparent to the end-users.

I expect myself to contribute *directly* to the staking system for at least another ~12, if not more, and afterwards having the role of an advisor. 

Some notable contributions: 

- paritytech/substrate#4517
- paritytech/substrate#7910
- paritytech/substrate#6242
- paritytech/substrate#9415
- paritytech/polkadot#3141
- paritytech/substrate#11212
- paritytech/substrate#12129

### FRAME 

Historically, I have contributed a variety of domains in FRAME, namely: 

- Early version of the weight system paritytech/substrate#3816 paritytech/substrate#3157
- Early version of the transaction fee system
- Primitive arithmetic types paritytech/substrate#3456
- Council election pallet paritytech/substrate#3364

Many of which were, admittedly, a PoC at most, if not considered "poor". I am happy that nowadays many of the above have been refactored and are being maintained by new domain experts. 

These days, I put most of my FRAME focus on testing and quality assurance. Through my work in the staking system, I have had to deal with the high sensitivity and liveness requirement of protocol development first hand (I believe I had to do among the [very first storage migrations](paritytech/substrate#3948) in Kusama) and consequently I felt the need to make better testing facilities, all of which have been formulated in https://forum.polkadot.network/t/testing-complex-frame-pallets-discussion-tools/356. Some relevant PRs:

- paritytech/substrate#8038
- paritytech/substrate#9788
- paritytech/substrate#10174

Regardless of wearing the staking hat, I plan to remain a direct contributor to FRAME, namely because I consider it to be an important requirements of successfully delivering more features to Polkadot's ecosystem. 

### Polkadot-Native Side Projects

I have started multiple small, mostly non-RUST projects in the polkadot ecosystem that I am very happy about, and I plan to continue doing so. I have not yet found the time to make a "polished product" out of any of these, but I hope that I can help foster our community such that someday a team will do so. I consider my role, for the time being, to *put ideas out there* through these side projects. 

- https://github.com/substrate-portfolio/polkadot-portfolio/
- https://github.com/kianenigma/polkadot-basic-notification/
- https://github.com/paritytech/polkadot-scripts/
- https://github.com/paritytech/substrate-debug-kit/

### Education 

Lastly, aside from having had a number of educational talks over the years (all of which [are listed](https://hello.kianenigma.nl/talks/) in my personal website), I am a big enthusiast of the newly formed Polkadot Blockchain Academy. I have [been an instructor](https://singular.app/collectibles/statemine/16/2) in the first cohort, and continue to contribute for as long and as much as I can, whilst still attending to the former 3 duties. 

---

With all of that being said and done, I consider myself at the beginning of the path to Dan 4, but happy to start at a lower one as well.

Co-authored-by: Bastian Köcher <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
J1-meta A specific issue for grouping tasks or bugs of a specific category. U2-some_time_soon Issue is worth doing soon.
Projects
Status: Done
Development

No branches or pull requests

2 participants