Expand description
Offchain routines orchestrating the affidavit lifecycle and election execution.
This module implements a coordinated set of Offchain Worker (OCW) routines that drive the lifecycle of authors from affidavit key initialization to election participation and key rotation.
§Lifecycle Pipeline
The system operates as a continuous OCW-driven pipeline:
InitAffidavitKey -> TryElection -> DeclareAffidavit -> RotateAffidavitKeyEach stage is independently executable and relies on repeated OCW execution across blocks to eventually converge to a consistent state.
§Responsibilities
§1. Affidavit Key Initialization (InitAffidavitKey)
- Generates an ephemeral affidavit key pair using application crypto.
- Persists the public identifier in
Finalizedoffchain storage. - Ensures exactly one active affidavit key exists per node.
§2. Election Execution (TryElection)
- Opportunistically attempts to run the election for the upcoming session.
- Uses the currently active affidavit key for authorization.
- Ensures at-most-once execution per author per session.
- Designed to be non-blocking and retry-safe.
§3. Affidavit Declaration (DeclareAffidavit)
- Submits a signed affidavit to signal participation in the next session’s election.
- Prepares and finalizes the next affidavit key for rotation.
- Ensures eligibility and timing constraints via runtime checks.
§4. Key Rotation (RotateAffidavitKey)
- Finalizes transition from next -> active affidavit key.
- Confirms successful affidavit submission via runtime state.
- Performs cleanup and ensures lifecycle continuity.
§Storage & Finality
Uses layered offchain storage:
ForkAware: fork-safe speculative statePersistent: durable observation ledgerFinalized: stable values viaConfidence
Finality is governed by FinalizedPolicy using:
- time delay (
FinalityAfter) - observation count (
FinalityTicks)
§Execution Model
- Idempotent: All routines can run repeatedly without side effects.
- Non-blocking: Routines exit early when prerequisites are unmet.
- Opportunistic: Actions may be attempted before full readiness.
- Eventually consistent: Correct state is reached through repetition.
§Security Model
- Uses ephemeral affidavit keys instead of long-term authority keys.
- Enforces key rotation per lifecycle.
- Limits signing scope to specific operations (affidavit/election).
- Reduces attack surface and key exposure risk.
§Failure Handling
- Storage inconsistencies trigger hard stops to prevent unsafe execution.
- Failed extrinsics are logged and retried in future OCW runs.
- Missing runtime reflection (e.g. failed affidavit) triggers state reset.
This ensures the system never remains in a partially inconsistent state.
Constants§
- ACTIVE_
AFDT_ KEY - Offchain key identifier for the active affidavit key.
- EMOJI_
DEBUG 🔒 - Emoji indicator mapped to each
LogLevelfor visual log scanning. - EMOJI_
ERROR 🔒 - EMOJI_
INFO 🔒 - EMOJI_
WARN 🔒 - LOG_
TARGET_ AFDT - Log target (classifier) for affidavit logging.
- LOG_
TARGET_ ELEC - Log target (classifier) for elections logging.
- NEXT_
AFDT_ KEY - Offchain storage key identifying the next affidavit key.
Functions§
- level_
emoji 🔒 - Returns the emoji indicator associated with a given
LogLevel. - std_fmt
- Standard log formatter for OCW routines.