Skip to content

Commit 3069bb3

Browse files
Update HieroTestUtils.java
1 parent 100f343 commit 3069bb3

File tree

1 file changed

+87
-34
lines changed

1 file changed

+87
-34
lines changed
Lines changed: 87 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,131 @@
11
package com.openelements.hiero.test;
22

33
import com.hedera.hashgraph.sdk.Status;
4+
import com.hedera.hashgraph.sdk.TopicId;
45
import com.hedera.hashgraph.sdk.TransactionId;
56
import com.openelements.hiero.base.HieroException;
67
import com.openelements.hiero.base.mirrornode.MirrorNodeClient;
8+
import com.openelements.hiero.base.mirrornode.TopicRepository;
79
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
810
import com.openelements.hiero.base.protocol.TransactionListener;
911
import com.openelements.hiero.base.protocol.data.TransactionType;
1012
import java.io.Serializable;
1113
import java.time.LocalDateTime;
14+
import java.util.Objects;
15+
import java.util.Optional;
1216
import java.util.concurrent.atomic.AtomicReference;
1317
import org.slf4j.Logger;
1418
import org.slf4j.LoggerFactory;
1519

1620
public class HieroTestUtils implements Serializable {
1721

18-
private final static Logger log = LoggerFactory.getLogger(HieroTestUtils.class);
22+
private static final Logger log = LoggerFactory.getLogger(HieroTestUtils.class);
1923

2024
private final MirrorNodeClient mirrorNodeClient;
2125

2226
private final ProtocolLayerClient protocolLayerClient;
2327

2428
private final AtomicReference<TransactionId> transactionIdRef = new AtomicReference<>();
2529

26-
public HieroTestUtils() {
27-
throw new UnsupportedOperationException("No-args constructor not supported");
28-
}
29-
3030
public HieroTestUtils(MirrorNodeClient mirrorNodeClient, ProtocolLayerClient protocolLayerClient) {
31-
this.mirrorNodeClient = mirrorNodeClient;
32-
this.protocolLayerClient = protocolLayerClient;
31+
this.mirrorNodeClient = Objects.requireNonNull(mirrorNodeClient, "MirrorNodeClient cannot be null");
32+
this.protocolLayerClient = Objects.requireNonNull(protocolLayerClient, "ProtocolLayerClient cannot be null");
3333

3434
protocolLayerClient.addTransactionListener(new TransactionListener() {
3535
@Override
3636
public void transactionSubmitted(TransactionType transactionType, TransactionId transactionId) {
37+
log.debug("Transaction submitted: type={}, id={}", transactionType, transactionId);
3738
transactionIdRef.set(transactionId);
3839
}
3940

4041
@Override
4142
public void transactionHandled(TransactionType transactionType, TransactionId transactionId,
42-
Status transactionStatus) {
43+
Status transactionStatus) {
44+
log.debug("Transaction handled: type={}, id={}, status={}", transactionType, transactionId, transactionStatus);
4345
}
4446
});
4547
}
4648

47-
public void waitForMirrorNodeRecords() {
48-
final TransactionId transactionId = transactionIdRef.get();
49-
if (transactionId != null) {
50-
log.debug("Waiting for transaction '{}' available at mirror node", transactionId);
51-
final LocalDateTime start = LocalDateTime.now();
52-
boolean done = false;
53-
while (!done) {
54-
String transactionIdString =
55-
transactionId.accountId.toString() + "-" + transactionId.validStart.getEpochSecond() + "-"
56-
+ String.format("%09d", transactionId.validStart.getNano());
57-
try {
58-
done = mirrorNodeClient.queryTransaction(transactionIdString).isPresent();
59-
} catch (HieroException e) {
60-
throw new RuntimeException("Error in mirror node query!", e);
61-
}
62-
if (!done) {
63-
if (LocalDateTime.now().isAfter(start.plusSeconds(30))) {
64-
throw new RuntimeException("Timeout waiting for transaction");
65-
}
66-
try {
67-
Thread.sleep(100);
68-
} catch (InterruptedException e) {
69-
throw new RuntimeException("Interrupted while waiting for transaction", e);
70-
}
49+
/**
50+
* Waits for a topic to be available on the mirror node.
51+
*
52+
* @param topicId The TopicId to check for on the mirror node.
53+
* @param topicRepository The TopicRepository to query the mirror node.
54+
* @throws HieroException If the topic is not found within the timeout period or an error occurs.
55+
*/
56+
public void waitForMirrorNodeRecords(TopicId topicId, TopicRepository topicRepository) throws HieroException {
57+
Objects.requireNonNull(topicId, "TopicId cannot be null");
58+
Objects.requireNonNull(topicRepository, "TopicRepository cannot be null");
59+
60+
log.debug("Waiting for topic '{}' to be available on mirror node", topicId);
61+
final LocalDateTime start = LocalDateTime.now();
62+
final long timeoutSeconds = 30;
63+
64+
while (true) {
65+
try {
66+
Optional<?> topic = topicRepository.findTopicById(topicId);
67+
if (topic.isPresent()) {
68+
log.debug("Topic '{}' is available on mirror node", topicId);
69+
return;
7170
}
71+
} catch (HieroException e) {
72+
log.error("Error querying mirror node for topic {}: {}", topicId, e.getMessage());
73+
throw new HieroException("Failed to query mirror node for topic " + topicId, e);
7274
}
73-
log.debug("Transaction '{}' is available at mirror node", transactionId);
74-
} else {
75+
76+
if (LocalDateTime.now().isAfter(start.plusSeconds(timeoutSeconds))) {
77+
throw new HieroException("Timeout waiting for topic " + topicId + " to appear on mirror node after " + timeoutSeconds + " seconds");
78+
}
79+
80+
try {
81+
Thread.sleep(1000); // Wait 1 second before retrying
82+
} catch (InterruptedException e) {
83+
Thread.currentThread().interrupt();
84+
throw new HieroException("Interrupted while waiting for topic " + topicId, e);
85+
}
86+
}
87+
}
88+
89+
/**
90+
* Waits for a transaction to be available on the mirror node (legacy method).
91+
*
92+
* @throws HieroException If the transaction is not found within the timeout period or an error occurs.
93+
*/
94+
public void waitForMirrorNodeRecords() throws HieroException {
95+
final TransactionId transactionId = transactionIdRef.get();
96+
if (transactionId == null) {
7597
log.debug("No transaction to wait for");
98+
return;
99+
}
100+
101+
log.debug("Waiting for transaction '{}' to be available on mirror node", transactionId);
102+
final LocalDateTime start = LocalDateTime.now();
103+
final long timeoutSeconds = 30;
104+
105+
String transactionIdString = transactionId.accountId.toString() + "@" + transactionId.validStart.getEpochSecond() + "."
106+
+ String.format("%09d", transactionId.validStart.getNano());
107+
108+
while (true) {
109+
try {
110+
if (mirrorNodeClient.queryTransaction(transactionIdString).isPresent()) {
111+
log.debug("Transaction '{}' is available on mirror node", transactionId);
112+
return;
113+
}
114+
} catch (HieroException e) {
115+
log.error("Error querying mirror node for transaction {}: {}", transactionIdString, e.getMessage());
116+
throw new HieroException("Failed to query mirror node for transaction " + transactionIdString, e);
117+
}
118+
119+
if (LocalDateTime.now().isAfter(start.plusSeconds(timeoutSeconds))) {
120+
throw new HieroException("Timeout waiting for transaction " + transactionIdString + " after " + timeoutSeconds + " seconds");
121+
}
122+
123+
try {
124+
Thread.sleep(100); // Wait 100ms before retrying
125+
} catch (InterruptedException e) {
126+
Thread.currentThread().interrupt();
127+
throw new HieroException("Interrupted while waiting for transaction " + transactionIdString, e);
128+
}
76129
}
77130
}
78131
}

0 commit comments

Comments
 (0)