I want to implement a simple PoS consensus mechanism where validator sets are chosen proportional to their stake. Do I need to write my own pallet implementing ElectionProviders trait or is there a ready-made solution?
2 Answers
As it stands now, you have two implementations in substrate:
pallet-election-provider-multi-phase
: This is a generic, asynchronous election pallet that can provide a wide range of elections to the staking pallet. This pallet is suitable for any election algorithm that is too heavy to compute onchain. It being too heavy or not is something that you need to answer based on your chain configuration. This pallet needs an underlyingSolver
. By default, substrate provides multiple algorithms here, but you can customize this to be anything you want.SequentialPhragmen
is simply a struct that implementsElectionProvider
by choosing the best validators based on the Phragmen's method, and does the entire work on the fly. This type should only be used on networks that have small number of validators/nominators.
You can find a full list of implementations of ElectionProvider
here.
Note that both of these approaches are slightly sub-optimal if your election is too simple, as-in, there is no election algorithm involved. This is because substrate assumes the existence of an election algorithm from the point of view of pallet-staking
, so instead of pallet-staking
trying to keep track of top x
validators on the fly, the entire nomination graph is passed down to an ElectionProvider
at some point for an election algorithm to compute it. Nonetheless, you are able to implement any
Also, please note, that if you use the latest version of substrate, there are 2 newly introduced structs onchain::BoundedExecution
, and onchain::UnboundedExecution
.
As its name implies, UnboundedExecution
is not statically bounded, while BoundedExecution
is. For on chain use, it's better to use BoundedExecution
to make sure it does not go out of hand.
UnboundedExecution
is generic over onchain::Config
which can be used to configure the Solver
and the DataProvider
.
BoundedExecution
is generic over onchain::BoundedConfig
that offers the extra bounds to bound the execution.
You can see examples of their usage in the testing section or in the pallet-election-provider-multi-phase
.
Please bear in mind that this is work in progress, and implementation may differ in the future, however this can be a handy wrapper to use for elections.