feat: add atomic_close_period sealing snapshot, accrual, and report (#461)#492
Open
morelucks wants to merge 1 commit into
Open
feat: add atomic_close_period sealing snapshot, accrual, and report (#461)#492morelucks wants to merge 1 commit into
morelucks wants to merge 1 commit into
Conversation
Closes RevoraOrg#461 Add atomic_close_period(issuer, namespace, token, period_id) that finalises the snapshot content_hash, commits the accrual index, and seals the report window in a single state-changing call so indexers see one boundary event (period_sealed) rather than three separate calls. ## What changed src/lib.rs - Add EVENT_PERIOD_SEALED constant (symbol "per_seald") - Add atomic_close_period() public function src/test_close_period.rs - Add tests: happy path, hash mismatch, missing report, snapshot disabled edge cases (all referencing atomic_close_period) ## Design ### Three steps, one transaction 1. Snapshot finalisation — recomputes SHA-256(index_xdr || holder_xdr || shares_bps_xdr) over every applied holder slot and asserts it matches the committed content_hash. Skipped if snapshot was already finalised by a prior finalize_snapshot call (idempotent). 2. Accrual index commit — derives accrual_hash = SHA-256(revenue_xdr || payout_asset_xdr) so indexers can audit the revenue figure without reading raw storage. 3. Period seal — writes ClosedPeriod key, blocking all future overrides for this period_id. ### Single boundary event topics: ("per_seald", issuer, namespace, token) data: (period_id, content_hash, accrual_hash, closed_at) One event instead of three lets indexers process period boundaries with a single subscription. ### Atomicity guarantee All writes happen after all validation. Any error (SnapshotHashMismatch, MissingReportForOverride, PeriodAlreadyClosed, etc.) returns before any storage is mutated, leaving state unchanged. ### Ordering invariants - commit_snapshot + apply_snapshot_shares must precede this call - report_revenue must have been called for period_id - period_id must be > 0
|
@morelucks 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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #461
Adds
atomic_close_period— a single entry-point that finalises the snapshot, commits the accrual index, and seals the report window in one transaction, so indexers see oneperiod_sealedboundary event instead of three separate calls.What changed
src/lib.rsEVENT_PERIOD_SEALEDconstant +atomic_close_period()functionsrc/test_close_period.rsDesign
Three steps, one transaction
Step 1 — Snapshot finalisation
Recomputes
SHA-256(index_xdr ‖ holder_xdr ‖ shares_bps_xdr)over every applied holder slot and asserts it matches the committedcontent_hash. Skipped (idempotent) if the snapshot was already finalised by a priorfinalize_snapshotcall.Step 2 — Accrual index commit
Derives
accrual_hash = SHA-256(revenue_amount_xdr ‖ payout_asset_xdr)so indexers can audit the revenue figure without reading raw storage.Step 3 — Period seal
Writes the
ClosedPeriodkey, blocking all future report overrides for thisperiod_id.Single boundary event
One event instead of three lets indexers process period boundaries with a single subscription.
Atomicity guarantee
All writes happen after all validation. Any error (
SnapshotHashMismatch,MissingReportForOverride,PeriodAlreadyClosed, etc.) returns before any storage mutation, leaving state unchanged.Ordering invariants
commit_snapshot+apply_snapshot_sharesmust precede this call forsnapshot_ref == period_idreport_revenuemust have been called forperiod_idperiod_idmust be > 0Error table
ContractFrozen/ContractPausedOfferingNotFoundInvalidPeriodIdperiod_id == 0SnapshotNotEnabledOutdatedSnapshotperiod_idSnapshotHashMismatchcontent_hashMissingReportForOverrideperiod_idPeriodAlreadyClosed