Skip to content

feat(contracts): implement Time-Weighted Average Reputation (TWAR) segment tree (#295) - #340

Closed
s6pa1rta3n-lab wants to merge 1 commit into
LynxXProtocol:mainfrom
s6pa1rta3n-lab:fix-issue-295
Closed

s6pa1rta3n-lab wants to merge 1 commit into
LynxXProtocol:mainfrom
s6pa1rta3n-lab:fix-issue-295

Conversation

@s6pa1rta3n-lab

Copy link
Copy Markdown

Summary of Changes

Addresses #295 by implementing the Time-Weighted Average Reputation (TWAR) Segment Tree for the Soroban registry contract.

Technical Implementation

  1. Packed Ledger Storage:

    • Implemented TWARSegmentTree backed by a contiguous 1,024-byte layout in contracts/registry/src/storage.rs.
    • Node storage packed into fixed-size 16-byte slots (weighted_score: i64, anchor_ledger: u32, event_count: u32) to eliminate per-entry overhead and reduce Soroban footprint costs.
    • Slot 0 stores header metadata (magic, half_life_ledgers, last_ledger, total_updates).
    • Persisted via Soroban instance storage as Bytes under TwarStorageKey::TwarTree(Address) with automatic TTL extension.
  2. Mathematical Decay Function:

    • Implemented monotonic Q32 fixed-point half-life time-decay function (decay_weight_q32, half_life_decay) without floating point operations.
    • Exact power-of-two halving at integer multiples of half-life, with 3-point quadratic polynomial interpolation for fractional ledger deltas.
    • Symmetrically decays positive credits and negative breach penalties over ledger advances.
  3. Segment Tree Aggregation:

    • Supports $O(\log n)$ point updates (update_point), dispute retractions (retract_point), and inclusive range aggregation queries (query_range).
    • Supports $O(1)$ decayed total score evaluation (query_total) and normalized 0..=100 TWAR scoring (twar_score).
  4. Gas & Metering Benchmarks:

    • Update execution consumes well under 150,000 Soroban CPU instructions.
    • Verified via unit test test_twar_update_cpu_instruction_budget_under_150k.
  5. Exhaustive Unit Tests:

    • Mathematical properties & monotonicity across ledger deltas.
    • Instance storage roundtrip and TTL management.
    • Sequential updates across ring capacity.
    • Multi-year ledger jumps decaying to baseline (50) without overflow or panic.
    • Saturating arithmetic boundaries at i64::MAX and i64::MIN.

Acceptance Criteria Checklist

  • Implement TWARSegmentTree struct supporting point updates and range aggregation queries.
  • Implement half-life decay calculation based on ledger sequence delta.
  • Gas benchmarks demonstrating < 150,000 Soroban CPU instructions per update.
  • Unit tests covering edge cases: sequential updates, multi-year ledger jumps, and counter overflow/underflow.

Payout Routing

  • EVM (Base/Arbitrum/Polygon/ETH): 0xF46C9F6d70C50BF81ef3588AB523a90a594a2F89
  • Stellar: GCL6OXAMLD75BMTINA6EMRUDWK5THQUSHMYNLSNBCJAPZJHNYJTUNIBC

@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@amankoli09 amankoli09 closed this Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants