Skip to content

feat(events): add a read view for the usage-alert threshold - #435

Merged
mikewheeleer merged 2 commits into
Agentpay-Org:mainfrom
Jagadeeshftw:feature/events-01-view
Jul 29, 2026
Merged

feat(events): add a read view for the usage-alert threshold#435
mikewheeleer merged 2 commits into
Agentpay-Org:mainfrom
Jagadeeshftw:feature/events-01-view

Conversation

@Jagadeeshftw

Copy link
Copy Markdown
Contributor

Summary

DataKey::UsageAlertThreshold drives the edge-triggered usage_hi alert inside record_usage, but no entrypoint could read or write it — the slot was permanently absent, so record_usage always used the unwrap_or(0) default and the alert path was unreachable on any live deploy.

  • Adds get_usage_alert_threshold() -> u32, a pure read (defaults to 0, matching the existing unwrap_or in record_usage).
  • Adds set_usage_alert_threshold(threshold), admin-gated, following the same shape as every other scalar admin setting (set_max_requests_per_window, etc.). Emits cfg_set(alert_thr, threshold) — the tag is deliberately distinct from the usage_hi event topic itself so the two are never conflated by a listener.
  • No change to record_usage's existing crossing logic — this only makes the threshold configurable.

Tests added

  • test_get_usage_alert_threshold_defaults_to_zero
  • test_set_usage_alert_threshold_round_trip
  • test_set_usage_alert_threshold_emits_cfg_set_event
  • test_set_usage_alert_threshold_requires_admin_auth
  • test_usage_hi_fires_once_threshold_now_settable — end-to-end: below-threshold call emits nothing, the crossing call emits exactly one usage_hi with the correct payload, and a subsequent above-threshold call emits nothing (edge-triggered)
  • test_usage_hi_disabled_by_default_zero_threshold — confirms the alert stays off when the threshold is left at its default

Closes #288

Validation

  • cargo fmt --all -- --check
  • cargo clippy --all-targets -- -D warnings
  • cargo test -p escrow — 358 passed (6 new), 1 pre-existing failure unrelated to this change (test_transfer_service_ownership_genuine_transfer_emits_event, fails identically on main)

env.events().all() reflects only the most recent contract invocation, not
a running total across separate calls (consistent with every other event
test in this file). The test compared post-transfer event count against a
pre-transfer count captured after a *different* call (set_service_metadata),
expecting accumulation that never happens, so it failed on every run
regardless of contract behavior. Fixed to assert directly on this call's
own event count.
@Jagadeeshftw
Jagadeeshftw force-pushed the feature/events-01-view branch from 8d5bb5d to 2a9e081 Compare July 28, 2026 21:19
@mikewheeleer

Copy link
Copy Markdown
Contributor

nice one @Jagadeeshftw, checks pass. merging 🚀

@mikewheeleer
mikewheeleer merged commit 46929c8 into Agentpay-Org:main Jul 29, 2026
1 check passed
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 a read-only view exposing the current events state

2 participants