-
Notifications
You must be signed in to change notification settings - Fork 5
fix(hyperliquid): dedup userFills to prevent position/PnL double-count #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jenish-25
wants to merge
3
commits into
bulletxyz:master
Choose a base branch
from
jenish-25:fix/hl-fill-dedup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| //! Bounded set of recently-seen ids with FIFO eviction. Used by exchange | ||
| //! adapters to drop fills replayed across a WS reconnect — emitting a fill | ||
| //! twice would double-count the position — without growing memory unbounded. | ||
|
|
||
| use std::collections::{HashSet, VecDeque}; | ||
|
|
||
| /// Bounded FIFO dedup set. `insert` returns `true` the first time an id is | ||
| /// seen and `false` for a repeat, evicting the oldest id once `cap` is | ||
| /// exceeded so memory stays bounded. | ||
| #[derive(Debug, Clone)] | ||
| pub struct RecentIds { | ||
| set: HashSet<String>, | ||
| order: VecDeque<String>, | ||
| cap: usize, | ||
| } | ||
|
|
||
| impl RecentIds { | ||
| /// `cap` is clamped to at least 1. A zero capacity would evict every id | ||
| /// the instant it is inserted — silently disabling dedup and reintroducing | ||
| /// the double-count it exists to prevent — so we coerce it, matching the | ||
| /// `.max(1)` guard `TickFeed` and `Volatility` use for degenerate sizes. | ||
| #[must_use] | ||
| pub fn new(cap: usize) -> Self { | ||
| Self { set: HashSet::new(), order: VecDeque::new(), cap: cap.max(1) } | ||
| } | ||
|
|
||
| /// Record `id`; returns `true` if it's new, `false` if already seen. | ||
| pub fn insert(&mut self, id: &str) -> bool { | ||
| if !self.set.insert(id.to_string()) { | ||
| return false; | ||
| } | ||
| self.order.push_back(id.to_string()); | ||
| if self.order.len() > self.cap | ||
| && let Some(evicted) = self.order.pop_front() | ||
| { | ||
| self.set.remove(&evicted); | ||
| } | ||
| true | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::RecentIds; | ||
|
|
||
| #[test] | ||
| fn recent_ids_dedups_exact_repeats() { | ||
| let mut seen = RecentIds::new(4); | ||
| assert!(seen.insert("a"), "first sighting is new"); | ||
| assert!(!seen.insert("a"), "exact repeat is a duplicate"); | ||
| assert!(seen.insert("b"), "different id is new"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recent_ids_zero_cap_is_clamped_and_still_dedups() { | ||
| // A zero capacity must not silently disable dedup — it's clamped to 1. | ||
| let mut seen = RecentIds::new(0); | ||
| assert!(seen.insert("a"), "first sighting is new"); | ||
| assert!(!seen.insert("a"), "immediate repeat is still caught"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recent_ids_evicts_oldest_past_cap() { | ||
| let mut seen = RecentIds::new(2); | ||
| seen.insert("a"); | ||
| seen.insert("b"); | ||
| seen.insert("c"); // evicts "a" | ||
| assert!(seen.insert("a"), "evicted id is treated as new again"); | ||
| // memory stays bounded at the cap | ||
| assert!(seen.set.len() <= 2); | ||
| } | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.