Skip to content

Commit a98d413

Browse files
Fix up
Signed-off-by: Ndacyayisenga-droid <[email protected]>
1 parent ca9673c commit a98d413

File tree

1 file changed

+34
-87
lines changed

1 file changed

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

33
import com.hedera.hashgraph.sdk.Status;
4-
import com.hedera.hashgraph.sdk.TopicId;
54
import com.hedera.hashgraph.sdk.TransactionId;
65
import com.openelements.hiero.base.HieroException;
76
import com.openelements.hiero.base.mirrornode.MirrorNodeClient;
8-
import com.openelements.hiero.base.mirrornode.TopicRepository;
97
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
108
import com.openelements.hiero.base.protocol.TransactionListener;
119
import com.openelements.hiero.base.protocol.data.TransactionType;
1210
import java.io.Serializable;
1311
import java.time.LocalDateTime;
14-
import java.util.Objects;
15-
import java.util.Optional;
1612
import java.util.concurrent.atomic.AtomicReference;
1713
import org.slf4j.Logger;
1814
import org.slf4j.LoggerFactory;
1915

2016
public class HieroTestUtils implements Serializable {
2117

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

2420
private final MirrorNodeClient mirrorNodeClient;
2521

2622
private final ProtocolLayerClient protocolLayerClient;
2723

2824
private final AtomicReference<TransactionId> transactionIdRef = new AtomicReference<>();
2925

26+
public HieroTestUtils() {
27+
throw new UnsupportedOperationException("No-args constructor not supported");
28+
}
29+
3030
public HieroTestUtils(MirrorNodeClient mirrorNodeClient, ProtocolLayerClient protocolLayerClient) {
31-
this.mirrorNodeClient = Objects.requireNonNull(mirrorNodeClient, "MirrorNodeClient cannot be null");
32-
this.protocolLayerClient = Objects.requireNonNull(protocolLayerClient, "ProtocolLayerClient cannot be null");
31+
this.mirrorNodeClient = mirrorNodeClient;
32+
this.protocolLayerClient = protocolLayerClient;
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);
3837
transactionIdRef.set(transactionId);
3938
}
4039

4140
@Override
4241
public void transactionHandled(TransactionType transactionType, TransactionId transactionId,
43-
Status transactionStatus) {
44-
log.debug("Transaction handled: type={}, id={}, status={}", transactionType, transactionId, transactionStatus);
42+
Status transactionStatus) {
4543
}
4644
});
4745
}
4846

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;
70-
}
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);
74-
}
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 {
47+
public void waitForMirrorNodeRecords() {
9548
final TransactionId transactionId = transactionIdRef.get();
96-
if (transactionId == null) {
97-
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;
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+
}
11371
}
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);
12872
}
73+
log.debug("Transaction '{}' is available at mirror node", transactionId);
74+
} else {
75+
log.debug("No transaction to wait for");
12976
}
13077
}
13178
}

0 commit comments

Comments
 (0)