fix: enhance comparison logic for zero-length since - #153
Conversation
There was a problem hiding this comment.
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
PartialOrdcomparison to avoid incorrect ordering whenlength == 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.
| impl PartialOrd for EpochNumberWithFraction { | ||
| fn partial_cmp(&self, other: &EpochNumberWithFraction) -> Option<Ordering> { | ||
| let (self_index, self_len) = if self.length() == 0 { | ||
| (0, 1) | ||
| } else { |
|
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); Perhaps it would be more appropriate to put this logic in the constructor? |
|
The previous(old) implementation is acceptable. |
No description provided.