Skip to content

Reputation Engine Historical Window Buffer Returns Uninitialized Zero Entries for New Validators #26

Description

@JamesEjembi

Problem Statement / Feature Objective

The reputation engine maintains a sliding window of historical scores for each validator (WINDOW_SIZE = 4096 epochs). The window buffer is implemented as a circular buffer of fixed length WINDOW_SIZE. When a validator is first observed, the buffer is initialized with zeroes and the write pointer starts at index 0. After exactly 4096 epochs, the write pointer wraps to index 0 and overwrites the oldest entry. However, the buffer does not track the number of valid entries written, causing a read of the full window to include uninitialized zero entries as valid historical scores, diluting the reputation average.

Technical Invariants & Bounds

  • WINDOW_SIZE: 4096 entries per validator.
  • Buffer: Vec with capacity WINDOW_SIZE, pre-filled with 0.
  • Write pointer: circular index starting at 0, wrapping at WINDOW_SIZE.
  • Read returns WINDOW_SIZE entries from (pointer - WINDOW_SIZE) to pointer.
  • Zero entries from uninitialized period dilute average by ~50% for new validators.
  • Zero entries are indistinguishable from actual scores of 0 (inactive validators).

Codebase Navigation Guide

  • src/reputation/historical-window.rs - CircularWindow struct, push_score(), window_slice().
  • src/reputation/score-engine.rs - compute_weighted_average() that consumes window.
  • tests/reputation/historical_window_test.rs - buffer behavior tests.

Implementation Blueprint

  1. In src/reputation/historical-window.rs, add a entries_written: u64 counter that tracks total pushes to the buffer.
  2. In window_slice(), return only min(entries_written, WINDOW_SIZE) entries, ignoring uninitialized positions.
  3. If entries_written < WINDOW_SIZE, the effective window is smaller and the average should be computed over the actual number of entries, not the full WINDOW_SIZE.
  4. Update compute_weighted_average() to use the actual entry count for normalization.
  5. Write a test: push 100 scores to a new validator's window and assert the window returns exactly 100 entries, not 4096.

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions