Skip to content

Extract _walk_replay_events for shared crown derivation#320

Closed
LandynDev wants to merge 2 commits into
testfrom
feat/miner-dashboard
Closed

Extract _walk_replay_events for shared crown derivation#320
LandynDev wants to merge 2 commits into
testfrom
feat/miner-dashboard

Conversation

@LandynDev
Copy link
Copy Markdown
Collaborator

@LandynDev LandynDev commented May 12, 2026

Problem → Solution

Problem — dashboard sync needs per-block crown derivation, but the logic lives inside an aggregation loop

  • replay_crown_time_window returns {hotkey: blocks} totals. The dashboard's per-block grid needs (block, holders, rate) per block, and re-implementing the qualification rule in two places guarantees drift.
  • Extract _walk_replay_events — a generator yielding one record per uniform-state interval (start, end, holders, best_rate). Both the validator aggregate (replay_crown_time_window) and the new display path (replay_crown_time_window_iter) are thin wrappers around it. Single source of truth for the qualification rule (active, not busy, has rate > 0, lower-rate-wins-when-reversed).

Problem — external readers can't open the validator's SQLite without risking a write

  • The alw-utils sync utility needs to tail state.db from another process.
  • ValidatorStateStore(readonly=True) opens via SQLite URI mode (file:...?mode=ro), skips DDL + journal_mode=WAL, surfaces writes as OperationalError. Plus get_rate_events_since_id(cursor) so readers can tail the autoincrement log.

Validator behaviour unchanged

The refactor is a pure rearrangement: existing replay_crown_time_window callsites in score_and_reward_miners keep producing identical numbers. Aggregating the iter wrapper's records also reproduces the totals — covered by tests/validator/test_scoring_iter.py::TestIterAggregateParity.

Test plan

  • pytest tests/test_scoring_v1.py tests/validator/test_scoring_iter.py — 58/58 passing locally (47 existing + 11 new)
  • ruff check allways/validator/scoring.py allways/validator/state_store.py tests/validator/ — clean
  • Iter/aggregate parity test asserts SUM(credit) over the iter path equals replay_crown_time_window totals
  • After merge: validator forward step latency unchanged in staging

Related PRs

Part of the miner-dashboard rollout — merge in dependency order:

  1. allways-db#15 — schema
  2. allways (this PR) — scoring refactor
  3. alw-utils#56 — sync utility
  4. das-allways#17 — API
  5. allways-ui#90 — page + components

Miner-dashboard groundwork: split the aggregate replay into a generator (_walk_replay_events) and two thin wrappers — replay_crown_time_window (validator scoring, unchanged numbers) and replay_crown_time_window_iter (per-block display path used by the alw-utils sync utility). Both consumers now share the qualification rule and state machine. Also opens ValidatorStateStore for read-only callers via a readonly URI mode + a get_rate_events_since_id tail-by-id accessor.
@xiao-xiao-mao xiao-xiao-mao Bot added the refactor Restructures code without changing behavior label May 12, 2026
@LandynDev
Copy link
Copy Markdown
Collaborator Author

Closing — going with Option A: porting the crown-derivation logic into the alw-utils sync utility instead, so the validator's consensus code stays untouched. The dashboard accepts the drift cost (display-only, not consensus-critical). Tracking in alw-utils#56.

@LandynDev LandynDev closed this May 12, 2026
@LandynDev LandynDev deleted the feat/miner-dashboard branch May 12, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Restructures code without changing behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant