Skip to content

[queue] QueueEntry Ord tie-breaking undefined for equal net_profit_usd_cents — PartialEq/Ord consistency unverified #155

@obchain

Description

@obchain

Refs #40

File: crates/charon-core/src/queue.rs

PR branch: feat/15-profit-calc-and-queue

Problem:
BinaryHeap is ordered by net_profit_usd_cents. When two entries share equal profit, std::collections::BinaryHeap makes no ordering guarantee for the second sort key. In practice for same-profit opportunities the bot should prefer the most recently scored entry (freshest price data) or the one with the soonest TTL expiry (least time-sensitive first). Neither secondary key is encoded.

More critically: if QueueEntry derives PartialEq via #[derive] alongside a hand-written Ord, PartialEq will compare all fields while Ord compares only net_profit_usd_cents. This violates the Rust invariant that a == b implies a.cmp(b) == Equal, causing BinaryHeap to exhibit undefined behavior. Rust does not enforce this invariant at compile time — it is a silent logic error.

Fix: Implement PartialEq explicitly and consistently with Ord (equal only when net_profit_usd_cents and all secondary sort keys are equal). Add a secondary sort key (e.g., reverse inserted_at_block so fresher entries win). Add a test that pushes two entries with identical profit at different block heights and asserts which one pops first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglayer:rustRust crates (core / scanner / protocols / executor / cli)pr-reviewFindings from PR review processpriority:p2-polishNice-to-have / polishstatus:readyScoped and ready to pick up

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions