Skip to content

selection: tie-break tier 2 ranks Some(0) stake above None, contradicting its own comment #1459

Closed

Description

@thiras

Split out of the review on #1456. Pre-existing and currently dormant, but it becomes live exactly when on-chain stake lookup is wired — so it is worth deciding deliberately now rather than discovering it then.

What

crates/node/src/selection.rs, tie-break tier 2:

// Tier 2: higher stake wins. `None` is treated as the lowest possible stake ...
let max_stake = pool.iter()
    .filter_map(|i| group.get(*i).and_then(|r| r.candidate.stake))
    .max();
if let Some(top) = max_stake {
    pool.retain(|i| group.get(*i).and_then(|r| r.candidate.stake) == Some(top));
}

The comment says None is treated as the lowest possible stake. It is not — filter_map drops None entirely, so None is treated as strictly lower than Some(0). Given a tie group with one Some(0) and one None, max_stake == Some(0) and retain keeps only the Some(0) candidate.

A provably-zero-stake node beats an unknown-stake node.

Why it is dormant, and when it stops being

All three construction sites pass None today (node_origin.rs:1182, node_origin.rs:1258, and the test helper), so tier 2 is a uniform no-op and the bug cannot fire.

It becomes reachable the moment stake lookup is partially wired — and an on-chain read is precisely the kind of lookup that succeeds for some peers and fails for others. That mixed population is the trigger, and it is the normal steady state for a chain read, not an edge case.

Root cause is the type

Candidate.stake: Option<u64> conflates two orthogonal axes — "we don't know" and "we know it's zero" — which is what makes the tier's intended semantics undecidable from the signature alone. The current code does neither of the two coherent things.

When the lookup lands, pick one deliberately:

  • Make the field u64 and resolve unknown→0 at a single explicit place in the lookup layer. Simple, and matches the comment's stated intent.
  • Keep Option and have tier 2 skip entirely when any candidate in the group is None — unknown poisons the tier rather than losing it. Safer against a partial-read scenario being silently interpreted as "this peer has no stake."

Either way, fix the comment to match. Blocked on nothing; just shouldn't be decided incidentally.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions