Skip to content

fix: enhance comparison logic for zero-length since - #153

Closed
XuJiandong wants to merge 1 commit into
nervosnetwork:masterfrom
XuJiandong:improve-since
Closed

fix: enhance comparison logic for zero-length since#153
XuJiandong wants to merge 1 commit into
nervosnetwork:masterfrom
XuJiandong:improve-since

Conversation

@XuJiandong

Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates EpochNumberWithFraction comparison semantics to behave safely and consistently when an epoch is constructed with a zero length (e.g., via unchecked construction or malformed on-chain data), and adds unit tests covering the comparison behavior.

Changes:

  • Normalize zero-length epochs during PartialOrd comparison to avoid incorrect ordering when length == 0.
  • Adjust cross-multiplication logic to use the normalized (index, length) pair.
  • Add unit tests for epoch comparisons, including zero-length edge cases.

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

Comment thread src/since.rs
Comment on lines 250 to +254
impl PartialOrd for EpochNumberWithFraction {
fn partial_cmp(&self, other: &EpochNumberWithFraction) -> Option<Ordering> {
let (self_index, self_len) = if self.length() == 0 {
(0, 1)
} else {
@mohanson

mohanson commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

It appears we checked if length was 0 when constructing EpochNumberWithFraction, but this only works in debug mode.

debug_assert!(number < Self::NUMBER_MAXIMUM_VALUE);
debug_assert!(index < Self::INDEX_MAXIMUM_VALUE);
debug_assert!(length < Self::LENGTH_MAXIMUM_VALUE);
debug_assert!(length > 0);
debug_assert!(index < length);

Perhaps it would be more appropriate to put this logic in the constructor?

@XuJiandong

XuJiandong commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator Author

The previous(old) implementation is acceptable. from_full_value should handle length = 0, as it may originate from a transaction. It should disallow explicitly setting length = 0.

@XuJiandong XuJiandong closed this Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants