Skip to content

feat: add ledger-indexed balance checkpoints - #1268

Open
blvckmamba81-sketch wants to merge 1 commit into
accesslayerorg:mainfrom
blvckmamba81-sketch:freebuff/7b68e2d1f82e4ef92b31e1f6
Open

blvckmamba81-sketch wants to merge 1 commit into
accesslayerorg:mainfrom
blvckmamba81-sketch:freebuff/7b68e2d1f82e4ef92b31e1f6

Conversation

@blvckmamba81-sketch

Copy link
Copy Markdown

Summary

Implements ledger-indexed balance checkpoints to support efficient historical balance and token supply lookups.

This enhancement enables governance voting and dividend distribution to use accurate historical balances instead of relying solely on current balance information.

Changes Made

  • Introduce the Checkpoint { ledger, balance } structure for recording historical balance changes.
  • Add persistent checkpoint storage for each (holder, key_id) pair.
  • Record a checkpoint whenever a holder's balance changes.
  • Replace the latest checkpoint when multiple balance changes occur within the same ledger to prevent duplicate entries.
  • Implement balance_at(holder, key_id, ledger) using binary search to retrieve historical balances efficiently.
  • Implement supply_at(key_id, ledger) for historical total supply lookups.
  • Add compact(holder, key_id, before_ledger) to manage checkpoint history while preserving the latest checkpoint preceding the cutoff.
  • Extend storage TTL for checkpoint entries whenever they are written.
  • Integrate historical balance lookups into governance vote-weight calculations and dividend snapshots.
  • Add unit tests covering historical lookups, checkpoint updates, and compaction.

Testing

  • Historical balance lookups return the correct balance at, between, and before checkpoints.
  • Multiple balance changes within the same ledger produce a single checkpoint.
  • Historical supply lookups return the correct values.
  • Compaction preserves the latest preceding checkpoint and all checkpoints at or after the cutoff.
  • Historical lookups remain correct after compaction.
  • Governance uses historical balances for vote-weight calculations.
  • Dividend snapshots use historical balances.
  • Storage TTL extension is verified.
  • Existing tests pass.
  • Contract build succeeds and CI passes.

Security and Correctness

  • Historical lookups never return balances from a future ledger.
  • Checkpoint history remains consistent with actual balance changes.
  • Compaction preserves the information required for accurate historical queries.
  • Binary search provides efficient lookups without requiring a full checkpoint scan.

Scope

This PR focuses on historical balance tracking, historical supply queries, checkpoint compaction, and integration with governance and dividend calculations.

Closes #1261

Record `Checkpoint { ledger, balance }` vectors per (holder, key_id) and
per key so governance, dividends and gating can read the balance in force
at a past ledger. Balance and supply writes now append (or replace, when
already written this ledger) a checkpoint and extend its TTL; new
`balance_at` / `supply_at` views binary-search the history, and `compact`
/ `compact_supply` trim old entries while keeping the latest preceding one.

Governance snapshot weights, dividend distributions and holder snapshots
read through the checkpoints, with a live balance fallback for legacy
state that predates checkpoint history.

Closes accesslayerorg#1261

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@blvckmamba81-sketch Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Chucks1093

Copy link
Copy Markdown
Member

✅ PR Ready for Review

Due to a recent malware incident affecting our repositories, all PRs are required to pass automated security checks before being reviewed for merging.

This PR has passed all automated checks:

  • ✅ Security scan — no malicious code detected
  • ✅ No merge conflicts
  • ✅ CI checks passed

Next step: Please share this PR link in our Telegram group so a maintainer can review and merge it: https://t.me/c/accesslayerorg/8514

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.

Add ledger-indexed balance checkpoints with binary-search historical lookups

3 participants