pub trait RoleManager<Candidate> {
type Status: RuntimeEnum + Delimited + VariantCount;
type Meta: Delimited;
type Asset: Asset;
type TimeStamp: Time;
Show 18 methods
// Required methods
fn role_exists(who: &Candidate) -> DispatchResult;
fn can_enroll(who: &Candidate, collateral: Self::Asset) -> DispatchResult;
fn can_resign(who: &Candidate) -> DispatchResult;
fn is_available(who: &Candidate) -> DispatchResult;
fn get_meta(who: &Candidate) -> Result<Self::Meta, DispatchError>;
fn get_collateral(who: &Candidate) -> Result<Self::Asset, DispatchError>;
fn total_collateral() -> Self::Asset;
fn enroll_since(who: &Candidate) -> Result<Self::TimeStamp, DispatchError>;
fn get_status(who: &Candidate) -> Result<Self::Status, DispatchError>;
fn status_since(who: &Candidate) -> Result<Self::TimeStamp, DispatchError>;
fn set_status(who: &Candidate, status: Self::Status) -> DispatchResult;
fn enroll(
who: &Candidate,
collateral: Self::Asset,
force: Fortitude,
) -> Result<Self::Asset, DispatchError>;
fn resign(who: &Candidate) -> Result<Self::Asset, DispatchError>;
fn add_collateral(
who: &Candidate,
collateral: Self::Asset,
force: Fortitude,
) -> Result<Self::Asset, DispatchError>;
// Provided methods
fn on_enroll(_who: &Candidate, _collateral: Self::Asset) { ... }
fn on_resign(_who: &Candidate, _released: Self::Asset) { ... }
fn on_status_update(_who: &Candidate, _status: &Self::Status) { ... }
fn on_add_collateral(_who: &Candidate, _raised: Self::Asset) { ... }
}Expand description
A universal abstraction for managing roles within a runtime context.
This trait defines the full lifecycle of a role-bearing entity - from enrollment and collateral management to status transitions and resignation.
It is intended to be the foundational interface for any module that wishes to assign and track roles such as:
- Validators - entities providing consensus participation and staking collateral.
- Council Members / Governance Actors - accounts participating in decision-making.
- Oracle Operators - off-chain data feeders bonded with collateral.
- Bounty Curators / Auditors / Relayers - specialized economic actors.
By providing a unified API for checking role existence, managing enrollment conditions, and handling collateral and lifecycle events, this trait enables role composability and modular runtime design.
§Type Parameters
Candidate: The identifier type representing an account or entity that can assume a role. Typically this is anAccountId, but it can also be a multi-entity struct or hash ID.
§Invariants
- Collateral associated with a
Candidatemust remain locked while the role is active. - Status transitions must be atomic and consistent - i.e.,
onceSelf::set_statussucceeds,Self::get_statusshould immediately reflect the new state.
§Example Implementations
§Example 1: Validator Role
A validator role might involve participants who are responsible for block production. In this context:
Statuscould includePending,Active,Slashed, orResigned.- Candidates are required to provide a minimum collateral to enroll.
- The system checks whether a candidate is already enrolled before allowing enrollment.
- The trait implementation manages enrollment, resignation, collateral tracking, and status updates.
§Example 2: Governance Council Member
A council member role represents participants in a governance body:
Statuscould includeCandidate,Active,Expelled, orRetired.- Enrollment may not require collateral but must still enforce eligibility rules.
- Resignation or removal is validated against membership in the council.
- The trait implementation allows tracking of active members, status changes, and role lifecycle events.
These examples illustrate how the RoleManager trait can be applied to different
kinds of roles, demonstrating the flexibility of the trait without tying it to a
specific runtime storage or pallet.
§Usage
- This trait is not tied to a specific module/pallet. It can be implemented by multiple
role-managing pallets (e.g.
pallet_validators,pallet_council, etc.). - Generic logic (e.g. in governance or reputation systems) can rely on
RoleManagerto interact with roles abstractly without hardcoding pallet internals.
Required Associated Types§
Sourcetype Status: RuntimeEnum + Delimited + VariantCount
type Status: RuntimeEnum + Delimited + VariantCount
Represents the discrete state (status) of the role (e.g. Active, Pending, Resigned, etc).
Must implement [VariantCount] for easy introspection of status variants.
Required Methods§
Sourcefn role_exists(who: &Candidate) -> DispatchResult
fn role_exists(who: &Candidate) -> DispatchResult
Checks whether a given Candidate currently has a registered role
in the system.
Returns Ok(()) if the role exists, otherwise DispatchError.
Sourcefn can_enroll(who: &Candidate, collateral: Self::Asset) -> DispatchResult
fn can_enroll(who: &Candidate, collateral: Self::Asset) -> DispatchResult
Validates whether the Candidate is eligible to enroll for a role,
given a specific collateral amount.
This function only performs validation and not mutates state.
Returns Ok(()) if the candidate can enroll, or a DispatchError if not.
Sourcefn can_resign(who: &Candidate) -> DispatchResult
fn can_resign(who: &Candidate) -> DispatchResult
Verifies whether the Candidate can safely resign their role.
This typically ensures there are no pending obligations or locked collateral.
Sourcefn is_available(who: &Candidate) -> DispatchResult
fn is_available(who: &Candidate) -> DispatchResult
Returns Ok(()) if the Candidate is considered not defaulted i.e., available.
A defaulted state represents a breach of expected behavior such as under-collateralization, missed performance targets, or protocol violations.
Depending on implementation, a defaulted candidate may be:
- Temporarily suspended - can later recover or be reinstated once obligations are met.
- Permanently defaulted - considered resigned, fired, or barred from reactivation.
Sourcefn get_meta(who: &Candidate) -> Result<Self::Meta, DispatchError>
fn get_meta(who: &Candidate) -> Result<Self::Meta, DispatchError>
Retrieves the meta-data of the Candidate.
DispatchError otherwise.
Sourcefn get_collateral(who: &Candidate) -> Result<Self::Asset, DispatchError>
fn get_collateral(who: &Candidate) -> Result<Self::Asset, DispatchError>
Retrieves the amount of collateral currently locked by the Candidate.
Returns a Result containing the collateral value or a [DispatchError].
Sourcefn total_collateral() -> Self::Asset
fn total_collateral() -> Self::Asset
Retrieves the real-time amount of collateral currently locked by all
the Candidates.
Sourcefn enroll_since(who: &Candidate) -> Result<Self::TimeStamp, DispatchError>
fn enroll_since(who: &Candidate) -> Result<Self::TimeStamp, DispatchError>
Returns the timestamp (or block number) when the Candidate enrolled
in the role.
Sourcefn get_status(who: &Candidate) -> Result<Self::Status, DispatchError>
fn get_status(who: &Candidate) -> Result<Self::Status, DispatchError>
Returns the current Status of the Candidate.
Sourcefn status_since(who: &Candidate) -> Result<Self::TimeStamp, DispatchError>
fn status_since(who: &Candidate) -> Result<Self::TimeStamp, DispatchError>
Returns the timestamp of the last status change for the Candidate.
Sourcefn set_status(who: &Candidate, status: Self::Status) -> DispatchResult
fn set_status(who: &Candidate, status: Self::Status) -> DispatchResult
Updates the role status of the Candidate to a new Status value.
Sourcefn enroll(
who: &Candidate,
collateral: Self::Asset,
force: Fortitude,
) -> Result<Self::Asset, DispatchError>
fn enroll( who: &Candidate, collateral: Self::Asset, force: Fortitude, ) -> Result<Self::Asset, DispatchError>
Enrolls a new Candidate with the specified collateral amount.
Should handle collateral reservation and emit necessary side effects.
The force parameter determines the privilege with which the operation is conducted.
Returns the actual amount of collateral accepted or a DispatchError.
Sourcefn resign(who: &Candidate) -> Result<Self::Asset, DispatchError>
fn resign(who: &Candidate) -> Result<Self::Asset, DispatchError>
Resigns the Candidate from their role and releases any associated collateral.
Should reverse the effects of enroll.
Returns the released collateral amount.
Sourcefn add_collateral(
who: &Candidate,
collateral: Self::Asset,
force: Fortitude,
) -> Result<Self::Asset, DispatchError>
fn add_collateral( who: &Candidate, collateral: Self::Asset, force: Fortitude, ) -> Result<Self::Asset, DispatchError>
Increases a Candidate’s collateral. Required if the implementation enforces
variable collateral.
The force parameter determines the privilege with which the operation is conducted.
Returns the actual amount of collateral added or a DispatchError.
Provided Methods§
Sourcefn on_enroll(_who: &Candidate, _collateral: Self::Asset)
fn on_enroll(_who: &Candidate, _collateral: Self::Asset)
Hook triggered after a successful enrollment along with its collateral.
Used for post-processing actions such as emitting events or initializing role data.
Default is no-op.
Sourcefn on_resign(_who: &Candidate, _released: Self::Asset)
fn on_resign(_who: &Candidate, _released: Self::Asset)
Hook triggered after a role resignation along with regained (unlocked) collateral.
Typically used to perform cleanup or emit resignation events.
Default is no-op.
Sourcefn on_status_update(_who: &Candidate, _status: &Self::Status)
fn on_status_update(_who: &Candidate, _status: &Self::Status)
Hook triggered when a Candidate’s status is mutated.
Allows implementing pallets to react to state transitions (e.g. Active -> Inactive).
Default is no-op.
Sourcefn on_add_collateral(_who: &Candidate, _raised: Self::Asset)
fn on_add_collateral(_who: &Candidate, _raised: Self::Asset)
Hook triggered when the collateral value of a role is incremented.
Can be used to adjust dependent metrics or notify external systems.
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.