|
1 | 1 | package com.openelements.hiero.test; |
2 | 2 |
|
3 | 3 | import com.hedera.hashgraph.sdk.Status; |
| 4 | +import com.hedera.hashgraph.sdk.TopicId; |
4 | 5 | import com.hedera.hashgraph.sdk.TransactionId; |
5 | 6 | import com.openelements.hiero.base.HieroException; |
6 | 7 | import com.openelements.hiero.base.mirrornode.MirrorNodeClient; |
| 8 | +import com.openelements.hiero.base.mirrornode.TopicRepository; |
7 | 9 | import com.openelements.hiero.base.protocol.ProtocolLayerClient; |
8 | 10 | import com.openelements.hiero.base.protocol.TransactionListener; |
9 | 11 | import com.openelements.hiero.base.protocol.data.TransactionType; |
10 | 12 | import java.io.Serializable; |
11 | 13 | import java.time.LocalDateTime; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.Optional; |
12 | 16 | import java.util.concurrent.atomic.AtomicReference; |
13 | 17 | import org.slf4j.Logger; |
14 | 18 | import org.slf4j.LoggerFactory; |
15 | 19 |
|
16 | 20 | public class HieroTestUtils implements Serializable { |
17 | 21 |
|
18 | | - private final static Logger log = LoggerFactory.getLogger(HieroTestUtils.class); |
| 22 | + private static final Logger log = LoggerFactory.getLogger(HieroTestUtils.class); |
19 | 23 |
|
20 | 24 | private final MirrorNodeClient mirrorNodeClient; |
21 | 25 |
|
22 | 26 | private final ProtocolLayerClient protocolLayerClient; |
23 | 27 |
|
24 | 28 | private final AtomicReference<TransactionId> transactionIdRef = new AtomicReference<>(); |
25 | 29 |
|
26 | | - public HieroTestUtils() { |
27 | | - throw new UnsupportedOperationException("No-args constructor not supported"); |
28 | | - } |
29 | | - |
30 | 30 | 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"); |
33 | 33 |
|
34 | 34 | protocolLayerClient.addTransactionListener(new TransactionListener() { |
35 | 35 | @Override |
36 | 36 | public void transactionSubmitted(TransactionType transactionType, TransactionId transactionId) { |
| 37 | + log.debug("Transaction submitted: type={}, id={}", transactionType, transactionId); |
37 | 38 | transactionIdRef.set(transactionId); |
38 | 39 | } |
39 | 40 |
|
40 | 41 | @Override |
41 | 42 | public void transactionHandled(TransactionType transactionType, TransactionId transactionId, |
42 | | - Status transactionStatus) { |
| 43 | + Status transactionStatus) { |
| 44 | + log.debug("Transaction handled: type={}, id={}, status={}", transactionType, transactionId, transactionStatus); |
43 | 45 | } |
44 | 46 | }); |
45 | 47 | } |
46 | 48 |
|
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; |
71 | 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); |
72 | 74 | } |
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) { |
75 | 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; |
| 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 | + } |
76 | 129 | } |
77 | 130 | } |
78 | 131 | } |
0 commit comments