-
Notifications
You must be signed in to change notification settings - Fork 113
/
HederaTransactionState.java
333 lines (323 loc) · 12.1 KB
/
HederaTransactionState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.hedera.sdk.common;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.hedera.sdk.node.HederaNode;
import com.hederahashgraph.api.proto.java.ResponseCodeEnum;
/**
* This class exists in order to enable asynchronous handling of Transaction Receipts
* It is fully synchronised and therefore thread safe
* The class holds an internal hashmap of node and receipt, indexed by TransactionID
* Adding a transaction to the hashmap means it will be queried for a receipt when refresh() is called
* Transactions are automatically removed from the hashmap when the collected receipt means there is no reason
* to continue polling for one.
* Note: No callback has been implemented on completion of receipt acquisition.
*/
public class HederaTransactionState {
private class TransactionState {
public HederaNode node;
public HederaTransactionReceipt receipt;
}
private static Map<HederaTransactionID, TransactionState> stateMap = new HashMap<>();
/**
* Adds (or replaces) a transaction to the internal hashmap
* @param transactionID the {@link HederaTransactionID} to add
* @param node the {@link HederaNode} against which the receipt should be polled for
*/
public void setTransaction(HederaTransactionID transactionID, HederaNode node)
{
TransactionState state = new TransactionState();
state.node = node;
state.receipt = new HederaTransactionReceipt();
// Only one thread is permitted
synchronized(this)
{
stateMap.put(transactionID, state);
}
}
/**
* Adds (or replaces) a transaction to the internal hashmap
* @param transactionID the {@link HederaTransactionID} to add
* @param node the {@link HederaNode} against which the receipt should be polled for
* @param receipt the updated {@link HederaTransactionReceipt}
*/
public void setTransaction(HederaTransactionID transactionID, HederaNode node, HederaTransactionReceipt receipt)
{
TransactionState state = new TransactionState();
state.node = node;
state.receipt = receipt;
// Only one thread is permitted
synchronized(this)
{
stateMap.put(transactionID, state);
}
}
/**
* Removes a transaction from the hashmap so that it's no longer polled for
* @param transactionID the {@link HederaTransactionID} to remove
*/
public void removeTransaction(HederaTransactionID transactionID)
{
// Only one thread is permitted
synchronized(this)
{
stateMap.remove(transactionID);
}
}
/**
* Gets the receipt associated with a {@link HederaTransactionID}
* @param transactionID the transaction ID for which the receipt is requested
* @return {@link HederaTransactionReceipt} the receipt
*/
public HederaTransactionReceipt getReceipt(HederaTransactionID transactionID)
{
// Only one thread is permitted
synchronized(this)
{
return stateMap.get(transactionID).receipt;
}
}
/**
* refreshes the hashmap by querying node(s) for a receipt for each TransactionID
* Note: This implementation removes receipts once they have reached a logical conclusion
* @throws InterruptedException in the event of an error
*/
public void refresh() throws InterruptedException
{
// Only one thread is permitted
synchronized(this)
{
Iterator it = stateMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
HederaTransactionID transactionID = (HederaTransactionID)pair.getKey();
TransactionState state = (TransactionState)pair.getValue();
HederaTransactionReceipt receipt = Utilities.getReceipt(transactionID, state.node, 1, 0, 0);
it.remove(); // avoids a ConcurrentModificationException
switch (receipt.nodePrecheck) {
case ACCOUNT_UPDATE_FAILED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case BAD_ENCODING:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case BUSY:
break;
case CONTRACT_EXECUTION_EXCEPTION:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case CONTRACT_REVERT_EXECUTED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case CONTRACT_SIZE_LIMIT_EXCEEDED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case CONTRACT_UPDATE_FAILED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case DUPLICATE_TRANSACTION:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case EMPTY_TRANSACTION_BODY:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case FAIL_BALANCE:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case FAIL_FEE:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case FAIL_INVALID:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case FILE_CONTENT_EMPTY:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INSUFFICIENT_ACCOUNT_BALANCE:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INSUFFICIENT_GAS:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INSUFFICIENT_PAYER_BALANCE:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INSUFFICIENT_TX_FEE:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_ACCOUNT_AMOUNTS:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_ACCOUNT_ID:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_CONTRACT_ID:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_EXPIRATION_TIME:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_FEE_SUBMITTED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_FILE_ID:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_KEY_ENCODING:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_NODE_ACCOUNT:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_PAYER_SIGNATURE:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_QUERY_HEADER:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_RECEIVING_NODE_ACCOUNT:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_SIGNATURE:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_SOLIDITY_ADDRESS:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_SOLIDITY_ID:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_TRANSACTION:
// do nothing
break;
case INVALID_TRANSACTION_BODY:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_TRANSACTION_DURATION:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_TRANSACTION_ID:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case INVALID_TRANSACTION_START:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case KEY_NOT_PROVIDED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case KEY_REQUIRED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case LOCAL_CALL_MODIFICATION_EXCEPTION:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case MEMO_TOO_LONG:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case MISSING_QUERY_HEADER:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case NO_WACL_KEY:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case NOT_SUPPORTED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case NULL_SOLIDITY_ADDRESS:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case OK:
if (receipt.transactionStatus == ResponseCodeEnum.SUCCESS) {
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
}
break;
case PAYER_ACCOUNT_NOT_FOUND:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case RECEIPT_NOT_FOUND:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case RECORD_NOT_FOUND:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case SUCCESS:
if (receipt.transactionStatus == ResponseCodeEnum.SUCCESS) {
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
}
break;
case TRANSACTION_EXPIRED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case UNKNOWN:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
case UNRECOGNIZED:
// a callback would be implemented here to deal with the operation
stateMap.remove(transactionID);
break;
}
}
}
}
/**
* Gets the number of Transaction IDs in the hashmap
* @return the number of transaction IDs.
*/
public int getCount()
{
// Only one thread is permitted
synchronized(this)
{
return stateMap.size();
}
}
}