feat: add admin-gated decrement_usage for over-report corrections#140
Merged
mikewheeleer merged 2 commits intoJun 29, 2026
Merged
Conversation
Contributor
|
nice — a decrement_usage for over-reported usage is handy. lgtm 🙌 |
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.
closes #83
feat: add admin-gated decrement_usage for over-report corrections
Summary
Adds a new
decrement_usage(env, agent, service_id, amount) -> u32entrypoint that lets the admin subtract an erroneous delta from a per-(agent, service_id) usage counter without discarding the legitimate remainder. Previously the only ways to lower a counter weresettle/reset_usage, which wipe it entirely to zero.Changes
contracts/escrow/src/lib.rsdecrement_usageentrypoint with validation order: paused → amount==0 → admin authsaturating_sub— clamps at zero, never underflowsamount == 0with existingEscrowError::RequestsMustBePositive(code Add a reset_usage entrypoint to drain an agent-service counter without settling #2)usage_dec(agent, service_id, amount, new_total)event for audit trailTotalUsageByAgentandTotalRequestsAllTimeare not adjusted (raw analytics preserved)contracts/escrow/src/test.rs11 new tests covering all edge cases:
test_decrement_usage_basictest_decrement_usage_past_zero_clampstest_decrement_usage_to_zerotest_decrement_usage_never_used_clampstest_decrement_usage_rejects_zerotest_decrement_usage_rejects_non_adminUnauthorizedtest_decrement_usage_rejected_while_pausedtest_decrement_usage_lifetime_counters_unchangedtest_decrement_usage_emits_eventusage_decpayloadtest_decrement_usage_paused_beats_zerotest_decrement_usage_zero_beats_noauthREADME.mdSecurity notes
saturating_sub— counter clamps at zero, never underflowsrequire_authon the stored admin addressusage_decevent with(agent, service_id, amount, new_total)payloadamount == 0is rejected withRequestsMustBePositiveto prevent no-op corrections in the audit trailTotalUsageByAgentandTotalRequestsAllTimeare untouched so analytics retain the raw reported figureFull
cargo testoutputVerification