Skip to content

feat(cketh): TimedSizedMap bounded, time-expiring map#10705

Open
gregorydemay wants to merge 8 commits into
masterfrom
ic_DEFI-2921_bounded-map
Open

feat(cketh): TimedSizedMap bounded, time-expiring map#10705
gregorydemay wants to merge 8 commits into
masterfrom
ic_DEFI-2921_bounded-map

Conversation

@gregorydemay

@gregorydemay gregorydemay commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Adds a generic in-heap map bounded by both a hard entry cap and a per-entry time-to-live, which will be used to store addresses that needs to be watch over for deposits.

On insertion it first evicts entries that have outlived their TTL; it then rejects rather than evicts a live entry — returning AtCapacity if the map is full of live entries, or AlreadyPresent if the key already has a live entry (a caller cannot refresh/extend an entry by re-inserting it; re-admission is only possible once it has expired). Time is supplied by the caller (a Timestamp newtype), so the structure keeps no internal clock and is deterministic to test.
Modeled on the bounded cache in dfinity/canhttp (not a workspace dependency, hence a small local structure).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a new TimedSizedMap<K, V> utility in the ckETH minter crate: an in-heap, deterministic (caller-supplied time) map bounded by both a maximum entry count and a per-entry TTL, with eviction order of “expired first, then oldest”.

Changes:

  • Added TimedSizedMap + Timestamp newtype implementing TTL-based expiration and capacity eviction.
  • Added unit tests covering eviction/refresh/TTL edge cases and internal index consistency.
  • Exported the new module from lib.rs for reuse by later F3 state/endpoint wiring.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
rs/ethereum/cketh/minter/src/timed_sized_map.rs New bounded, time-expiring map implementation (TimedSizedMap) with time index for eviction.
rs/ethereum/cketh/minter/src/timed_sized_map/tests.rs Unit tests validating expiration, capacity eviction, refresh semantics, and index consistency.
rs/ethereum/cketh/minter/src/lib.rs Exposes the new timed_sized_map module publicly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rs/ethereum/cketh/minter/src/timed_sized_map.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread rs/ethereum/cketh/minter/src/timed_sized_map.rs Outdated
Comment thread rs/ethereum/cketh/minter/src/timed_sized_map.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comment thread rs/ethereum/cketh/minter/src/timed_sized_map.rs Outdated
Comment thread rs/ethereum/cketh/minter/src/timed_sized_map/mod.rs
Comment thread rs/ethereum/cketh/minter/src/timed_sized_map/mod.rs Outdated
Comment thread rs/ethereum/cketh/minter/src/timed_sized_map.rs Outdated
Base automatically changed from ic_DEFI-2920_deposit-address-derivation to master July 9, 2026 16:06
gregorydemay and others added 3 commits July 10, 2026 08:36
Add a generic in-heap map bounded by both a hard capacity and a per-entry
time-to-live, evicting expired entries first and then the oldest to make
room. Time is caller-supplied (Timestamp newtype) so the structure holds no
clock and is deterministic to test.

This is the standalone data structure that F3's per-account deposit-address
registry will use to bound the active scanning set (DEFI-2921); wiring it
into the minter state and the deposit_erc20 endpoint follows in later PRs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
remove_from_time_index is only called after an entry was removed from the
primary map, so the timestamp bucket and the key must exist. Replace the
silent no-op guards with expect("BUG: ...") so an inconsistent index fails
loudly instead of drifting and panicking later in eviction.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…emantics

- Remove the existing key before evicting expired entries, so re-inserting a
  key never reports its own previous value as an eviction (previously the
  behavior depended on whether that value had expired).
- Replace the capacity loop's silent `break` with expect("BUG: ..."): an
  empty time index while entries is non-empty is an index-corruption bug, not
  a reason to insert past the hard capacity.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@gregorydemay gregorydemay force-pushed the ic_DEFI-2921_bounded-map branch from aaf656e to 4411504 Compare July 10, 2026 08:36
Comment thread rs/ethereum/cketh/minter/src/timed_sized_map.rs Outdated
gregorydemay and others added 3 commits July 10, 2026 09:08
A still-armed deposit address must never be silently dropped from the
watchlist: it may already have received funds. So TimedSizedMap::insert now
evicts only expired entries and, if the map is still at capacity with only
live entries, rejects a new key with an AtCapacity error rather than evicting
a live one. Re-inserting an existing key still refreshes it (and is never
rejected or reported as an eviction). Removes the now-unused evict_oldest.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Relocate the file-module src/timed_sized_map.rs to src/timed_sized_map/mod.rs,
alongside its tests submodule. No code change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Removing an existing key already drops the map below capacity (len <= capacity
is invariant), so the capacity check alone is sufficient; the separate
refreshed flag never changed the outcome.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment thread rs/ethereum/cketh/minter/src/timed_sized_map/tests.rs Outdated
Comment thread rs/ethereum/cketh/minter/src/timed_sized_map/mod.rs Outdated
… test

- Expire an entry only once its age strictly exceeds `ttl` (now - inserted_at
  > ttl), so an entry lives for the full ttl. Replaces the inclusive
  saturating_add comparison with a checked duration.
- Derive Clone/Eq/PartialEq on TimedSizedMap and assert the whole map is
  unchanged after a rejected insertion (clone-before, compare-after).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment thread rs/ethereum/cketh/minter/src/timed_sized_map/tests.rs Outdated
A caller must not be able to refresh (extend) a still-live entry by
re-inserting it, otherwise it could hold a slot indefinitely by repeatedly
calling. insert now returns InsertError::AlreadyPresent for a live key;
re-admission is only possible once the entry has expired. The error type
becomes an enum (AlreadyPresent | AtCapacity), and the now-unused by-key
remove helpers are dropped.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@gregorydemay gregorydemay marked this pull request as ready for review July 10, 2026 11:15
@gregorydemay gregorydemay requested a review from a team as a code owner July 10, 2026 11:15
@github-actions github-actions Bot added the @defi label Jul 10, 2026
@zeropath-ai

zeropath-ai Bot commented Jul 10, 2026

Copy link
Copy Markdown

No security or compliance issues detected. Reviewed everything up to f9e944d.

Security Overview
Detected Code Changes
Change Type Relevant files
Enhancement ► rs/ethereum/cketh/minter/src/lib.rs
    pub mod timed_sized_map;
Enhancement ► rs/ethereum/cketh/minter/src/timed_sized_map/mod.rs
    New module implementing TimedSizedMap with eviction and TTL logic
Enhancement ► rs/ethereum/cketh/minter/src/timed_sized_map/tests.rs
    Add tests for TimedSizedMap behavior and invariants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants