Skip to content

Commit

Permalink
fix: DUPLICATE_TRANSACTION error for Queries (#2190)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <[email protected]>
  • Loading branch information
0xivanov authored Feb 4, 2025
1 parent 6eef60a commit 49eb915
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 286 deletions.
5 changes: 4 additions & 1 deletion sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ tasks.withType<Test>().configureEach {
systemProperty("OPERATOR_KEY", providers.gradleProperty("OPERATOR_KEY").getOrElse(""))
}

tasks.testIntegration { failFast = true }
tasks.testIntegration {
maxParallelForks = (Runtime.getRuntime().availableProcessors()).coerceAtLeast(1)
failFast = true
}

tasks.withType<JavaCompile>().configureEach { options.compilerArgs.add("-Xlint:-exports,-dep-ann") }

Expand Down
25 changes: 9 additions & 16 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,22 +364,15 @@ private void initWithNodeIds(Client client) {
* @return the transaction
*/
Transaction getPaymentTransaction(int index) {
var paymentTx = Objects.requireNonNull(paymentTransactions).get(index);
if (paymentTx != null) {
return paymentTx;
} else {
if (paymentTransactionId == null) {
paymentTransactionId = TransactionId.generate(Objects.requireNonNull(paymentOperator).accountId);
}

var newPaymentTx = makePaymentTransaction(
paymentTransactionId,
nodeAccountIds.get(index),
paymentOperator,
Objects.requireNonNull(chosenQueryPayment));
paymentTransactions.set(index, newPaymentTx);
return newPaymentTx;
}
paymentTransactionId = TransactionId.generate(Objects.requireNonNull(paymentOperator).accountId);

var newPaymentTx = makePaymentTransaction(
paymentTransactionId,
nodeAccountIds.get(index),
paymentOperator,
Objects.requireNonNull(chosenQueryPayment));
paymentTransactions.set(index, newPaymentTx);
return newPaymentTx;
}

@Override
Expand Down
12 changes: 11 additions & 1 deletion sdk/src/main/java/com/hedera/hashgraph/sdk/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -1313,8 +1313,9 @@ TransactionResponse mapResponse(
com.hedera.hashgraph.sdk.proto.Transaction request) {
var transactionId = Objects.requireNonNull(getTransactionIdInternal());
var hash = hash(request.getSignedTransactionBytes().toByteArray());
// advance is needed for chunked transactions
transactionIds.advance();
return new TransactionResponse(nodeId, transactionId, hash, null);
return new TransactionResponse(nodeId, transactionId, hash, null, this);
}

@Override
Expand Down Expand Up @@ -1375,6 +1376,15 @@ ExecutionState getExecutionState(Status status, com.hedera.hashgraph.sdk.proto.T
return super.getExecutionState(status, response);
}

Transaction regenerateTransactionId(Client client) {
Objects.requireNonNull(client.getOperatorAccountId());
transactionIds.setLocked(false);
var newTransactionID = TransactionId.generate(client.getOperatorAccountId());
transactionIds.set(transactionIds.getIndex(), newTransactionID);
transactionIds.setLocked(true);
return this;
}

@Override
@SuppressWarnings("LiteProtoToString")
public String toString() {
Expand Down
139 changes: 83 additions & 56 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/TransactionResponse.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ TransactionResponse mapResponse(
new AccountId(3),
TransactionId.withValidStart(new AccountId(3), now),
new byte[] {1, 2, 3},
null,
null)
.setValidateStatus(true);
}
Expand Down Expand Up @@ -299,6 +300,7 @@ TransactionResponse mapResponse(
new AccountId(4),
TransactionId.withValidStart(new AccountId(4), now),
new byte[] {1, 2, 3},
null,
null);
}
};
Expand Down Expand Up @@ -352,6 +354,7 @@ TransactionResponse mapResponse(
new AccountId(3),
TransactionId.withValidStart(new AccountId(3), now),
new byte[] {1, 2, 3},
null,
null);
}
};
Expand Down Expand Up @@ -423,6 +426,7 @@ void testChannelFailedToConnectTimeout() {
new AccountId(3),
TransactionId.withValidStart(new AccountId(3), java.time.Instant.now()),
new byte[] {1, 2, 3},
null,
null);
var tx = new DummyTransaction();

Expand Down
Loading

0 comments on commit 49eb915

Please sign in to comment.