Module routines

Source
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 -> RotateAffidavitKey

Each 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 Finalized offchain 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:

Finality is governed by FinalizedPolicy using:

§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 LogLevel for 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.