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