Skip to content

Commit 839d855

Browse files
authored
fix: include nonce in TransactionId equals() method (#2555)
Signed-off-by: emiliyank <e.kadiyski@gmail.com>
1 parent fc39259 commit 839d855

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

sdk/src/main/java/com/hedera/hashgraph/sdk/TransactionId.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,10 @@ public boolean equals(Object object) {
495495
var id = (TransactionId) object;
496496

497497
if (accountId != null && validStart != null && id.accountId != null && id.validStart != null) {
498-
return id.accountId.equals(accountId) && id.validStart.equals(validStart) && scheduled == id.scheduled;
498+
return id.accountId.equals(accountId)
499+
&& id.validStart.equals(validStart)
500+
&& scheduled == id.scheduled
501+
&& Objects.equals(nonce, id.nonce);
499502
} else {
500503
return false;
501504
}

sdk/src/test/java/com/hedera/hashgraph/sdk/TransactionIdTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import static org.assertj.core.api.Assertions.assertThat;
55
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
67

78
import com.google.protobuf.InvalidProtocolBufferException;
89
import io.github.jsonSnapshot.SnapshotMatcher;
@@ -170,4 +171,19 @@ void shouldAddTrailingZeroesToNanoseconds() {
170171
var txId = TransactionId.fromString(txIdString);
171172
assertThat(txId).hasToString(txIdString);
172173
}
174+
175+
@Test
176+
void equalsHashCodeContractWithNonce() {
177+
AccountId accountId = new AccountId(0, 0, 1000);
178+
Instant now = Instant.now();
179+
180+
TransactionId txnId1 = TransactionId.withValidStart(accountId, now);
181+
TransactionId txnId2 = TransactionId.withValidStart(accountId, now);
182+
183+
txnId2.setNonce(0);
184+
185+
assertFalse(
186+
txnId1.equals(txnId2) && txnId1.hashCode() != txnId2.hashCode(),
187+
"equals/hashCode contract violation: equal objects must have same hashCode");
188+
}
173189
}

0 commit comments

Comments
 (0)