feat: Detect High-Frequency Contract Calls#210
Merged
mijinummi merged 1 commit intoJul 21, 2026
Merged
Conversation
Adds src/modules/detection/contracts/ with HighFrequencyContractCallsService, which flags abnormal per-contract call volume via two independent rules: a configurable absolute cap per sliding window, and a rolling-baseline comparison (current window vs. a multiple of the average of recent closed windows). Thresholds are configurable globally (constructor default) and per contract (setThreshold/getThreshold). Closes MD-Creative-Production#150
Collaborator
|
LGTM |
3 tasks
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.
What
Implements high-frequency contract call detection requested in #150, under
src/modules/detection/contracts/.Why
Exploits often generate abnormal transaction volume against a contract before or during an attack. This gives Sentinel a way to flag that volume in near real time.
Implementation
HighFrequencyContractCallsServiceruns two independent detection rules on every recorded call:recordCall()tracks calls per contract in a sliding, tumbling window (getCallCount()/getWindowHistory()expose the current and recently-closed window counts).baselineWindowCountclosed windows (getBaselineAverage()). If the current window's count exceeds that average bybaselineSpikeMultiplierx, abaseline_spikealert fires — this catches sustained-but-abnormal activity even when it stays under the absolute cap.onAlert()lets subscribers observeFrequencyAlerts (with an unsubscribe function), each carrying the triggering reason, call count, threshold, and (for baseline alerts) the baseline average.FrequencyThresholdConfig(window size, absolute cap, baseline window count, spike multiplier) can be set via the constructor, and overridden per contract withsetThreshold()/getThreshold().clearContract()resets all tracked state for a contract.Follows the same plain-class-plus-interfaces style already used by
src/modules/detection/malicious-addresses.Testing
15 new unit tests in
high-frequency-calls.service.spec.ts, using explicit event timestamps (no real-time waiting or fake timers needed) covering: per-contract threshold configuration and overrides, window rolling/history capping, absolute-threshold alerts, baseline-spike alerts, no-alert-on-normal-activity, subscriber notification/unsubscription, and state reset viaclearContract.Verified locally:
npm run lint— cleannpm run format:check— clean for these filesnpm run build— passesnpx jest --config jest.backend.config.js src/modules/detection/contracts— 15/15 passingsrc/modulessuite — 70/70 passing (no regressions)Acceptance Criteria
Closes #150