Trait ElectionManager

Source
pub trait ElectionManager<Candidate>: InspectWeight<Candidate, Self::ElectionWeightOf>
where Candidate: Keyed,
{ type ElectionWeight: Sortable; type ElectionWeightOf: Buffer<Self::ElectionWeight> + Ord + Storable; type Params: Buffer<(Candidate, Self::ElectionWeightOf)>; type Elected: Buffer<Candidate>; type ElectionModel: PurePluginModel<Self::Params, <Self::ElectionContext as ModelContext>::Context, Self::Elected> + Default; type ElectionContext: ModelContext; // Required methods fn store(_elects: &Self::Elected) -> DispatchResult; fn reveal() -> Option<Self::Elected>; fn remove(who: &Candidate); fn is_candidate(who: &Candidate) -> DispatchResult; fn can_prepare(from: &Self::Params) -> DispatchResult; // Provided methods fn prepare(from: Self::Params) -> DispatchResult { ... } fn run_model(input: Self::Params) -> Self::Elected { ... } fn on_prepare_success(_elects: &Self::Elected) { ... } fn on_prepare_fail(_err: DispatchError) { ... } }
Expand description

A trait for managing elections of candidates with associated weights.

This trait is designed to be highly generic and plugin-driven, enabling multiple election models to be used without modifying the trait itself.

Elections are inherently pluggable and diverse:

  • Different elections may use distinct weighting rules or selection algorithms.
  • Inputs consist of candidates paired with their backing weights.
  • Outputs may represent either a single winner or multiple winners.
  • A plugin-based design allows flexible implementations while preserving type safety and runtime configurability.

§Type Parameters

  • Candidate: The type representing a candidate in the election.

Required Associated Types§

Source

type ElectionWeight: Sortable

Represents a single vote or its associated weight.

This can either:

  • Store a numeric weight for a vote, or
  • Represent a single vote implicitly.

Must implement Ord to allow comparison and sorting within Self::ElectionWeightOf.

Source

type ElectionWeightOf: Buffer<Self::ElectionWeight> + Ord + Storable

Collection type for storing candidate weights.

This allows flexibility in the underlying container, such as:

  • Vec
  • Arrays
  • Custom buffer types

Must support:

Source

type Params: Buffer<(Candidate, Self::ElectionWeightOf)>

Input type for election computation.

Each entry maps a candidate to a collection of associated weights.

This abstraction ensures that plugin implementations only accept well-structured and compatible input formats.

Source

type Elected: Buffer<Candidate>

Output type representing elected candidates.

This can represent:

  • Multiple winners (e.g., committee selection), or
  • A single winner (as a single-element collection)

If the output implies ranking, the elements are expected to be ordered by priority.

If truncation occurs (e.g., selecting top-N winners), the ordering must reflect the final priority of elected candidates.

Source

type ElectionModel: PurePluginModel<Self::Params, <Self::ElectionContext as ModelContext>::Context, Self::Elected> + Default

The plugin responsible for computing election results.

This model defines the election strategy by:

This abstraction allows multiple election strategies to be plugged in safely and interchangeably.

If the resulting Self::Elected is truncated (e.g., selecting top-N), the candidates must be ordered by priority.

Source

type ElectionContext: ModelContext

Provides runtime configuration for the Self::ElectionModel computation.

This context supplies dynamic parameters such as:

  • Thresholds
  • Weights
  • Other runtime-specific settings

It enables flexible behavior without requiring hardcoded values.

Required Methods§

Source

fn store(_elects: &Self::Elected) -> DispatchResult

Persist the election results. Must be implemented by the consumer.

Source

fn reveal() -> Option<Self::Elected>

Retrieve currently elected candidates.

Source

fn remove(who: &Candidate)

Remove a candidate from the elected pool.

Source

fn is_candidate(who: &Candidate) -> DispatchResult

Check if a candidate exist in the elected pool.

Source

fn can_prepare(from: &Self::Params) -> DispatchResult

Check if election preparation is possible with the given parameters.

Provided Methods§

Source

fn prepare(from: Self::Params) -> DispatchResult

Executes the election process and persists the results.

This function:

  1. Runs the election model using Self::run_model
  2. Stores the resulting elected candidates via Self::store
Source

fn run_model(input: Self::Params) -> Self::Elected

Self::ElectionModel plugin output function.

Utilizes the plugin model’s context Self::ElectionContext

Source

fn on_prepare_success(_elects: &Self::Elected)

Hook called after a successful election preparation. Default is no-op.

Source

fn on_prepare_fail(_err: DispatchError)

Hook called after an election preparation failure. Default is no-op.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§