Skip to content

Commit b7b7cc6

Browse files
ericnordelo0xNeshiclaudebidzyyysqalisander
authored
[N-01] Core Math Audit (#357)
* feat: Implement Rate Limiter module (#315) * feat: add rate limiter implementation * docs: document invariants * fix: enforce missing invariants * test: add more tests * feat: cooldown supports capacity/used * ref: assume monotonic Clock * fix: new_bucket accepts initial tokens (remove new_bucket_with_tokens) * ref: track cooldown_end instead of last_used * fix: cooldown used increases by 1 on consume * ref: use 'available' design instead of 'used' * ref: inline roll_window * test: add cooldown_reconfigure_rearms_when_drained_and_deadline_elapsed * docs: mention operator's responsibility for cooldown_ms * ref: rename q to steps_to_full in bucket_accrue * docs: update invariants * fix: remove capacity + refill_amount assertion in assert_bucket_config * docs: add missing / update docs * feat: add variety of errors * docs: add missing / update docs * test: remove invariant class mentions * ref: apply Code Quality Checklist (openzeppelin_utils) * test: replace cleanup with 'abort EUnreachable' in expected-failure tests * test: use abort 0 instead of error * feat: remove 'Ms' suffix from Error names * docs: error docs don't reference internal variables * docs: rephrase rate_limiter error messages without internal field names Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * docs: update README * docs: update README * docs: align invariants.md to be sharing-ready * docs: improve wording & clarity for invariants.md * feat: cooldown now decrements available by amount * test: change abort 0 to 100 (avoids confusing with ERateLimited) * test: change all destructor patterns into abortions * docs: invariant rewording * docs: update format for invariants.md * docs: update format for invariants.md and add missing invariants * docs: align invariants format * docs: update outdated invariant references * docs: remove rate limiter temp. artifacts * chore: simplify changelog * ref: convert assert macros into funs * docs: document private funs * test(fix): add proper teardown in happy tests * ci: add contracts/utils to CI test job dir matrix * test: add regression tests for bucket_accrue * fix: bucket_accrue double spend bug * docs: add empty line before enum variant docs Co-authored-by: Daniel Bigos <daniel.bigos96@gmail.com> * Revert "docs: add empty line before enum variant docs" This reverts commit 21a9076. * feat: inline config validation assertions * docs: add more info on rate limiter types in README * docs: add clarifying comments for apparent overflow suspects * docs: mention library being access control-agnostic * docs: reword library to module * feat: add initial_available to all limiter types * ref: put capacity as first cooldown type field * ref: use spread op instead of ignoring cooldown_ms field * docs: show identical API for all types in README * docs: for fixed window update wording that it resets to full capacity * fix: reconfigure sets last_refill to now + forbid 0 initial available for cooldown * docs: remove post-core artifact * test: add bucket_reconfigure_to_faster_rate_discards_old_subinterval * docs: add SAFETY before overflow-related comments * test: verify available always returns up-to-date accrual state * test: add missing zero-config reconfigure tests * test: add try_consume_with_zero_amount for all limiter types * test: add try_consume_of_available_aborts_when_drained * docs: note that try_consume(0) aborts after available() == 0 * docs: note RateLimiter enum is not upgrade-compatible Adding variants or fields to a deployed public enum would break BCS deserialization for integrator objects that already stored the prior shape. Document this constraint at the module level. * test: add edge case test cases * docs: remove llm artifacts * ref: reorder fileds in new_cooldown * test: remove Pub.local.toml * ref: in reconfigure_fixed_window move let now into match * ref: in reconfigure_bucket move let now into match * docs: remove mention of reconfigure_cooldown clamping available to 0 * fix: align cooldown clamp behavior on reconfigure * fix: try_consume updates state only on success * docs: align * ref: fixedwindow now shares bucket's internal logic * ref: update capacity prior to accrual on reconfigure_bucket * docs: add comment * Revert "docs: add comment" This reverts commit ecb0145. * Revert "ref: update capacity prior to accrual on reconfigure_bucket" This reverts commit b545b6d. * ref: Rate Limiter: Reconfiguration via Rich `new_*` Functions (#326) * impl * simplify further * fix: expose last_refill_ms for bucket * tests: add missing * docs: update utils README * docs: fix new_cooldown accepted combos * feat: project bucket-shaped anchor getters `last_refill_ms` and `window_start_ms` now take `&Clock` and return the anchor advanced by every whole interval elapsed since the last commit, so the snapshot `(anchor, available(clock))` is internally coherent. `cooldown_end_ms` stays a plain getter — a cooldown deadline does not evolve with time. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: error numbering * fix: error messages no longer reference private fields * docs: move EBucketAnchorInFuture comment next to the error * add spec * update spec * tests: add missing invariant tests * feat: add is_type discriminator getters --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: Eric Nordelo <eric.nordelo39@gmail.com> Co-authored-by: Daniel Bigos <daniel.bigos96@gmail.com> * tests: fix formatting in rate limiter tests (#334) * feat: add median implementation (#135) * add separate quick_sort implementations for each type * remove duplicated qsort implementations * add in-place qsort implementation * add partition function * rewrite qsort without recursion * convert quick_sort to macro * add comprehensive test cases for qsort * update qsort documentation * add review fixes * remove --trace flag from testing pipeline * Revert "remove --trace flag from testing pipeline" This reverts commit 9bf7065. * split macro module * inline partition macro * add median implementation * add more edge case test cases * fix fmt * remove quick_sort duplicate * feat: add median * docs: update PR * Route median through u256 workhorse fun (#325) * fix: fmt * fix: apply CR comments * test: add missing test * fix: macro * feat: use quickselect * feat: improve CHANGELOG * feat: apply review updates * feat: update comment --------- Co-authored-by: Daniel Bigos <daniel.bigos@openzeppelin.com> Co-authored-by: Daniel Bigos <daniel.bigos@icloud.com> Co-authored-by: immrsd <103599616+immrsd@users.noreply.github.com> Co-authored-by: Eric Nordelo <eric.nordelo39@gmail.com> * feat: update check * feat: update version --------- Co-authored-by: Nenad <nenad.misic@openzeppelin.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: Daniel Bigos <daniel.bigos96@gmail.com> Co-authored-by: Alisander Qoshqosh <37006439+qalisander@users.noreply.github.com> Co-authored-by: Daniel Bigos <daniel.bigos@openzeppelin.com> Co-authored-by: Daniel Bigos <daniel.bigos@icloud.com> Co-authored-by: immrsd <103599616+immrsd@users.noreply.github.com>
1 parent 24c5e22 commit b7b7cc6

4 files changed

Lines changed: 4 additions & 4 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ permissions:
1616
pull-requests: write
1717

1818
env:
19-
SUI_VERSION: "1.72.2"
19+
SUI_VERSION: "1.72.5"
2020

2121
jobs:
2222
setup:

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
1212

1313
#### Added
1414

15-
- Added `vector::median!` macro for unsigned integer vectors with rounding for even lengths. Includes `vector::median_u256`. (#362)
15+
- Added `vector::median!` macro for unsigned integer vectors with rounding for even lengths. (#362)
1616

1717
#### Changed
1818

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Sui smart contracts are written in Move leveraging the Sui Move framework.
2323

2424
Follow the installation guide in the [Sui documentation](https://docs.sui.io/guides/developer/getting-started/sui-install).
2525

26-
**Required version**: Sui CLI [1.72.2](https://github.com/MystenLabs/sui/releases/tag/mainnet-v1.72.2).
26+
**Required version**: Sui CLI [1.72.5](https://github.com/MystenLabs/sui/releases/tag/mainnet-v1.72.5).
2727

2828
## Docs
2929

math/core/sources/vector.move

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public macro fun quick_sort_by<$T>($vec: &mut vector<$T>, $le: |&$T, &$T| -> boo
122122

123123
// Choose median-of-three (start, mid, pivot_index) as a pivot
124124
// and place it on the last position.
125-
let mid = (start + end) / 2;
125+
let mid = start + (end - start) / 2;
126126
if ($le(&vec[mid], &vec[start])) {
127127
vec.swap(start, mid);
128128
};

0 commit comments

Comments
 (0)