Skip to content

TransactionId.compareTo() can violate Comparable contract when only one validStart is null #2740

Description

@ADITYA-CODE-SOURCE

Problem

TransactionId.compareTo() uses the wrong null flag when comparing validStart.
In sdk/src/main/java/com/hedera/hashgraph/sdk/TransactionId.java:532:

if (thisStartIsNull != otherStartIsNull) {
    return thisAccountIdIsNull ? -1 : 1;  // ❌ should use thisStartIsNull
} 

This can violate the Comparable contract when both accountId values are non-null but only one validStart is null.

Impact
Sorted collections (TreeSet, TreeMap, Collections.sort()) may behave unexpectedly if comparisons are inconsistent.

Suggested Fix
Change line 532 to:
return thisStartIsNull ? -1 : 1;

Reproduction
TransactionId a = new TransactionId(new AccountId(1, 2, 3), Instant.now());
TransactionId b = new TransactionId(new AccountId(1, 2, 3), null);
// Both a.compareTo(b) and b.compareTo(a) may return 1

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