Skip to content

Peer's Ord is inconsistent with its Eq #2798

Closed

Description

@josecelano-bot

Summary

torrust_tracker_primitives::peer::Peer derives PartialEq, Eq and Hash over all seven fields, but implements Ord by peer_id only (PartialOrd delegates to it). For a seeder a and a copy b with left = 5, a == b is false while a.cmp(&b) is Ordering::Equal. This violates the Ord/Eq contract (cmp must be Equal if and only if ==) in a published crate.

Impact

Latent today: no production code sorts, min/maxes or puts Peer in a BTreeSet; the swarm keys by PeerId in a BTreeMap<PeerId, Arc<Peer>>. PeerList and Torrent in torrent-repository-benchmarking derive Ord and inherit the defect through Arc<Peer>, but nothing orders them either. External users of the crate who sort or deduplicate peers are exposed.

Reproduction

Reproduced with a temporary unit test: a == b: false, a.cmp(&b): Equal.

Plan

Investigate why Eq is whole-struct and Ord is id-only and classify every call site (the peer id is chosen by clients, so an implicit id-only comparison can hide bugs or collisions), then fix per the options in the spec (A remove Ord, B id-only Eq, C order by all fields with peer_id first, D document only; C is the tentative recommendation). Add a regression test proven red before the fix.

Full specification: docs/issues/drafts/peer-ord-inconsistent-with-eq/ISSUE.md on the spec branch (to be moved to docs/issues/open/ once this issue has a number).

Activity

  1. added 8 commits that reference this issue on Nov 10, 2026
  2. added 2 commits that reference this issue on Nov 10, 2026
  3. added 5 commits that reference this issue on Nov 10, 2026
  4. 1 remaining item

  5. added 10 commits that reference this issue on Nov 10, 2026
  6. added a commit that references this issue on Nov 11, 2026
  7. added a commit that references this issue on Nov 11, 2026
  8. added 2 commits that reference this issue on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions