forked from ApolloFoundation/Apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeClient.java
More file actions
408 lines (364 loc) · 19.1 KB
/
NodeClient.java
File metadata and controls
408 lines (364 loc) · 19.1 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package test;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.util.StringContentProvider;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.slf4j.LoggerFactory.getLogger;
import static test.TestUtil.createURI;
import static test.TestUtil.getMAPPER;
public class NodeClient {
public static final Long DEFAULT_FEE = 100_000_000L; //1 APL
public static final Long DEFAULT_AMOUNT = 100_000_000L; //1 APL
public static final Long DEFAULT_DEADLINE = 60L; //1 hour
private static final Logger LOG = getLogger(NodeClientTestMainnet.class);
private static final int REQUEST_TIMEOUT = 15;
private static final HttpClient CLIENT = new HttpClient(new SslContextFactory());
private static final ObjectMapper MAPPER = getMAPPER();
private static final JSONParser PARSER = new JSONParser();
static {
try {
CLIENT.setConnectTimeout(15_000);
CLIENT.start();
}
catch (Exception e) {
LOG.error("Http CLIENT could not initialized.", e);
System.exit(0);
}
}
public String getJson(URI uri) {
return getJson(uri, null);
}
public String getJson(URI uri, Map<String, String> params) {
try {
Request request = CLIENT.newRequest(uri)
.method(HttpMethod.GET)
.timeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
if (params != null) {
params.forEach(request::param);
}
ContentResponse response = request.send();
if (response.getStatus() != HttpStatus.OK_200) {
throw new RuntimeException("Request is not successful! Status= " + response.getStatus());
}
return response.getContentAsString();
}
catch (Exception e) {
LOG.error("Cannot perform getRequest to " + uri.toString(), e);
}
return null;
}
public String postJson(URI uri, Map<String, String> parameters, String bodyJson) {
try {
Request request = CLIENT.newRequest(uri)
.method(HttpMethod.POST)
.timeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.content(new StringContentProvider(bodyJson, Charset.forName("utf-8")));
parameters.forEach(request::param);
ContentResponse response = request.send();
if (response.getStatus() != HttpStatus.OK_200) {
throw new RuntimeException("Request is not successful! Status= " + response.getStatus());
}
return response.getContentAsString();
}
catch (Exception e) {
LOG.error("Cannot perform postRequest to " + uri.toString() + "/" + uri.getPath(), e);
}
return null;
}
/**
* curl -X POST -d "requestType=sendMoney&secretPhrase=dont know secret phrase hungry&recipient=APL-R3YT-LZ35-PS5X-3NMHR&amountNQ
* T=100000000&feeNQT=100000000&deadline=60" http://localhost:7876/apl
*/
public String getPeers(String url) {
Map<String, String> params = new HashMap<>();
params.put("requestType", "getPeers");
params.put("active", "true");
params.put("state", "CONNECTED");
params.put("includePeerInfo", "true");
URI uri = createURI(url);
return getJson(uri, params);
}
public List<Peer> getPeersList(String url) throws IOException {
String json = getPeers(url);
JsonNode root = MAPPER.readTree(json);
JsonNode peersArray = root.get("peers");
return MAPPER.readValue(peersArray.toString(), new TypeReference<List<Peer>>() {});
}
public String getBlocks(String url, int from, int to, boolean includeTransactions, Long timestamp) {
Map<String, String> params = new HashMap<>();
params.put("requestType", "getBlocks");
params.put("firstIndex", String.valueOf(from));
params.put("lastIndex", String.valueOf(to));
if (timestamp != null) {
params.put("timestamp", timestamp.toString());
}
params.put("includeTransactions", String.valueOf(includeTransactions));
URI uri = createURI(url);
return getJson(uri, params);
}
public String getBlocks(String url) {
return getBlocks(url, 0, 4, false, null); //last 5 blocks
}
public Long getBlockchainHeight(String url) throws Exception {
String blocks = getBlocks(url);
return (Long) ((JSONObject) ((JSONArray) ((JSONObject) PARSER.parse(blocks)).get("blocks")).get(0)).get("height");
}
public String getAccountTransactions(String url, String rsAddress) {
Map<String, String> params = new HashMap<>();
URI uri = createURI(url);
params.put("requestType", "getBlockchainTransactions");
params.put("account", rsAddress);
params.put("includeTransactions", "true");
params.put("includeExecutedPhased", "false");
return getJson(uri, params);
}
public List<Transaction> getAccountTransactions(String url, String rsAddress, int from, int to, int type, int subtype) throws IOException {
Map<String, String> params = new HashMap<>();
URI uri = createURI(url);
params.put("requestType", "getBlockchainTransactions");
params.put("account", rsAddress);
params.put("includeTransactions", "true");
params.put("firstIndex", String.valueOf(from));
params.put("lastIndex", String.valueOf(to));
if (type >= 0) {
params.put("type", String.valueOf(type));
}
if (subtype >= 0) {
params.put("subtype", String.valueOf(subtype));
}
String json = getJson(uri, params);
JsonNode root = MAPPER.readTree(json);
JsonNode transactionsArray = root.get("transactions");
try {
return MAPPER.readValue(transactionsArray.toString(), new TypeReference<List<Transaction>>() {});
}
catch (Throwable e) {
return Collections.emptyList();
}
}
public List<Transaction> getAccountTransactionsList(String url, String rsAddress) throws ParseException, IOException {
String accountTransactions = getAccountTransactions(url, rsAddress);
JsonNode root = MAPPER.readTree(accountTransactions);
JsonNode transactionsArray = root.get("transactions");
List<Transaction> transactionsList = MAPPER.readValue(transactionsArray.toString(), new TypeReference<List<Transaction>>() {});
return transactionsList;
}
public List<String> getPeersIPs(String url) throws IOException {
Map<String, String> params = new HashMap<>();
params.put("requestType", "getPeers");
params.put("state", "CONNECTED");
URI uri = createURI(url);
String json = getJson(uri, params);
JsonNode root = MAPPER.readTree(json);
JsonNode peersArray = root.get("peers");
return MAPPER.readValue(peersArray.toString(), new TypeReference<List<String>>() {});
}
public int getPeersCount(String url) throws ParseException {
String peers = getPeers(url);
return ((JSONArray) ((JSONObject) PARSER.parse(peers)).get("peers")).size();
}
public String getBlockTransactions(String url, Long height) throws ParseException {
Map<String, String> params = new HashMap<>();
params.put("requestType", "getBlock");
params.put("height", height.toString());
params.put("includeTransactions", "true");
params.put("includeExecutedPhased", "true");
URI uri = createURI(url);
String json = getJson(uri, params);
JSONArray transactions = (JSONArray) ((JSONObject) PARSER.parse(json)).get("transactions");
return transactions.toJSONString();
}
public List<Transaction> getBlockTransactionsList(String url, Long height) throws ParseException, IOException {
String blockTransactions = getBlockTransactions(url, height);
List<Transaction> transactionsList = MAPPER.readValue(blockTransactions, new TypeReference<List<Transaction>>() {});
return transactionsList;
}
public String sendMoney(String url, String secretPhrase, String recipient, Long amountNQT, Long feeNQT, Long deadline) {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "sendMoney");
parameters.put("secretPhrase", secretPhrase);
parameters.put("recipient", recipient);
parameters.put("amountNQT", amountNQT.toString());
parameters.put("feeNQT", feeNQT.toString());
parameters.put("deadline", deadline.toString());
return postJson(createURI(url), parameters, "");
}
public Transaction sendMoneyTransaction(String url, String secretPhrase, String recipient, Long amountNQT, Long feeNQT, Long deadline) throws IOException, ParseException {
String json = sendMoney(url, secretPhrase, recipient, amountNQT, feeNQT, deadline);
String transactionJSON = ((JSONObject) ((JSONObject) PARSER.parse(json)).get("transactionJSON")).toJSONString();
return MAPPER.readValue(transactionJSON, Transaction.class);
}
private String sendMoneyPrivate(String url, String secretPhrase, String recipient, Long amountNQT, Long feeNQT, Long deadline) {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "sendMoneyPrivate");
parameters.put("secretPhrase", secretPhrase);
parameters.put("recipient", recipient);
parameters.put("amountNQT", amountNQT.toString());
parameters.put("feeNQT", feeNQT.toString());
parameters.put("deadline", deadline.toString());
return postJson(createURI(url), parameters, "");
}
public Transaction sendMoneyPrivateTransaction(String url, String secretPhrase, String recipient, Long amountNQT, Long feeNQT, Long deadline) throws IOException, ParseException {
String json = sendMoneyPrivate(url, secretPhrase, recipient, amountNQT, feeNQT, deadline);
String transactionJSON = ((JSONObject) ((JSONObject) PARSER.parse(json)).get("transactionJSON")).toJSONString();
return MAPPER.readValue(transactionJSON, Transaction.class);
}
public Transaction sendMoneyTransaction(String url, String secretPhrase, String recipient, Long amountNQT, Long feeNQT) throws IOException, ParseException {
return sendMoneyTransaction(url, secretPhrase, recipient, amountNQT, feeNQT, DEFAULT_DEADLINE);
}
public Transaction sendMoneyTransaction(String url, String secretPhrase, String recipient, Long amountNQT) throws IOException, ParseException {
return sendMoneyTransaction(url, secretPhrase, recipient, amountNQT, DEFAULT_FEE);
}
public List<Block> getBlocksList(String url, boolean includeTransactions, Long timestamp) throws IOException {
String json = getBlocks(url, 0, 4, includeTransactions, timestamp);
JsonNode root = MAPPER.readTree(json);
JsonNode blocksArray = root.get("blocks");
return MAPPER.readValue(blocksArray.toString(), new TypeReference<List<Block>>() {});
}
public String sendMoney(String url, String secretPhrase, String recipient, Long amountNQT) {
return sendMoney(url, secretPhrase, recipient, amountNQT, DEFAULT_FEE);
}
public String sendMoney(String url, String secretPhrase, String recipient, Long amountNQT, Long feeNQT) {
return sendMoney(url, secretPhrase, recipient, amountNQT, feeNQT, DEFAULT_DEADLINE);
}
public String sendMoney(String url, String secretPhrase, String recipient) {
return sendMoney(url, secretPhrase, recipient, DEFAULT_AMOUNT);
}
public Transaction getTransaction(String url, String fullHash) throws IOException {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "getTransaction");
parameters.put("fullHash", fullHash);
String json = getJson(createURI(url), parameters);
return MAPPER.readValue(json, Transaction.class);
}
public List<Transaction> getPrivateBlockchainTransactionsList(String url, String secretPhrase, Long height, Long firstIndex, Long lastIndex) throws Exception {
String json = getPrivateBlockchainTransactionsJson(url, secretPhrase, height, firstIndex, lastIndex);
JsonNode root = MAPPER.readTree(json);
JsonNode transactionsArray = root.get("transactions");
return MAPPER.readValue(transactionsArray.toString(), new TypeReference<List<Transaction>>() {});
}
public String getPrivateBlockchainTransactionsJson(String url, String secretPhrase, Long height, Long firstIndex, Long lastIndex) {
Map<String, String> params = new HashMap<>();
URI uri = createURI(url);
params.put("requestType", "getPrivateBlockchainTransactions");
params.put("secretPhrase", secretPhrase);
if (height != null) {
params.put("height", height.toString());
}
if (firstIndex != null) {
params.put("firstIndex", firstIndex.toString());
}
if (lastIndex != null) {
params.put("lastIndex", lastIndex.toString());
}
return getJson(uri, params);
}
public List<LedgerEntry> getAccountLedger(String url, String rsAddress, boolean includeTransactions) throws Exception {
return getAccountLedger(url, rsAddress, includeTransactions, 0, -1);
}
public List<LedgerEntry> getAccountLedger(String url, String rsAddress, boolean includeTransactions, int from, int to) throws Exception {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "getAccountLedger");
parameters.put("account", rsAddress);
parameters.put("includeTransactions", String.valueOf(includeTransactions));
parameters.put("firstIndex", String.valueOf(from));
parameters.put("lastIndex", String.valueOf(to));
String json = getJson(createURI(url), parameters);
JsonNode root = MAPPER.readTree(json);
JsonNode entriesArray = root.get("entries");
return MAPPER.readValue(entriesArray.toString(), new TypeReference<List<LedgerEntry>>() {});
}
public List<Transaction> getUnconfirmedTransactions(String url, String rsAddress, int from, int to) throws
Exception {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "getUnconfirmedTransactions");
parameters.put("account", rsAddress);
parameters.put("firstIndex", String.valueOf(from));
parameters.put("lastIndex", String.valueOf(to));
String json = getJson(createURI(url), parameters);
JsonNode root = MAPPER.readTree(json);
JsonNode entriesArray = root.get("unconfirmedTransactions");
return MAPPER.readValue(entriesArray.toString(), new TypeReference<List<Transaction>>() {});
}
public List<Transaction> getPrivateUnconfirmedTransactions(String url, String secretPhrase, int from, int to) throws
Exception {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "getPrivateUnconfirmedTransactions");
parameters.put("secretPhrase", secretPhrase);
parameters.put("firstIndex", String.valueOf(from));
parameters.put("lastIndex", String.valueOf(to));
String json = getJson(createURI(url), parameters);
JsonNode root = MAPPER.readTree(json);
JsonNode entriesArray = root.get("unconfirmedTransactions");
return MAPPER.readValue(entriesArray.toString(), new TypeReference<List<Transaction>>() {});
}
public List<LedgerEntry> getPrivateAccountLedger(String url, String secretPhrase, boolean includeTransactions) throws Exception {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "getPrivateAccountLedger");
parameters.put("secretPhrase", secretPhrase);
parameters.put("includeTransactions", String.valueOf(includeTransactions));
String json = getJson(createURI(url), parameters);
JsonNode root = MAPPER.readTree(json);
JsonNode entriesArray = root.get("entries");
return MAPPER.readValue(entriesArray.toString(), new TypeReference<List<LedgerEntry>>() {});
}
public Transaction getPrivateTransaction(String url, String secretPhrase, String fullHash, String transactionId) throws IOException {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "getPrivateTransaction");
parameters.put("secretPhrase", secretPhrase);
if (transactionId != null && !transactionId.isEmpty()) {
parameters.put("transaction", String.valueOf(transactionId));
} else if (fullHash != null && !fullHash.isEmpty()) {
parameters.put("fullHash", fullHash);
} else {
throw new RuntimeException("Both fullHash and transactionId are not provided");
}
String json = getJson(createURI(url), parameters);
return MAPPER.readValue(json, Transaction.class);
}
public Block getBlock(String url, Long height) throws IOException {
Map<String, String> params = new HashMap<>();
params.put("requestType", "getBlock");
params.put("height", height.toString());
params.put("includeTransactions", "true");
URI uri = createURI(url);
String json = getJson(uri, params);
return MAPPER.readValue(json, Block.class);
}
public LedgerEntry getAccountLedgerEntry(String url, Long ledgerId, Boolean includeTransaction) throws IOException {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "getAccountLedgerEntry");
parameters.put("ledgerId", ledgerId.toString());
parameters.put("includeTransaction", includeTransaction.toString());
String json = getJson(createURI(url), parameters);
return MAPPER.readValue(json, LedgerEntry.class);
}
public LedgerEntry getPrivateAccountLedgerEntry(String url, String secretPhrase, Long ledgerId, Boolean includeTransaction) throws IOException {
Map<String, String> parameters = new HashMap<>();
parameters.put("requestType", "getPrivateAccountLedgerEntry");
parameters.put("ledgerId", ledgerId.toString());
parameters.put("secretPhrase", secretPhrase);
parameters.put("includeTransaction", includeTransaction.toString());
String json = getJson(createURI(url), parameters);
return MAPPER.readValue(json, LedgerEntry.class);
}
}