forked from woodser/monero-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestMoneroDaemonRpc.java
2059 lines (1792 loc) · 71.2 KB
/
TestMoneroDaemonRpc.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import common.utils.JsonUtils;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import monero.common.MoneroError;
import monero.common.MoneroRpcConnection;
import monero.common.MoneroRpcError;
import monero.daemon.MoneroDaemon;
import monero.daemon.MoneroDaemonRpc;
import monero.daemon.model.MoneroAltChain;
import monero.daemon.model.MoneroBan;
import monero.daemon.model.MoneroBlock;
import monero.daemon.model.MoneroBlockHeader;
import monero.daemon.model.MoneroBlockTemplate;
import monero.daemon.model.MoneroConnectionSpan;
import monero.daemon.model.MoneroDaemonInfo;
import monero.daemon.model.MoneroDaemonListener;
import monero.daemon.model.MoneroDaemonSyncInfo;
import monero.daemon.model.MoneroDaemonUpdateCheckResult;
import monero.daemon.model.MoneroDaemonUpdateDownloadResult;
import monero.daemon.model.MoneroFeeEstimate;
import monero.daemon.model.MoneroHardForkInfo;
import monero.daemon.model.MoneroKeyImage;
import monero.daemon.model.MoneroKeyImageSpentStatus;
import monero.daemon.model.MoneroMinerTxSum;
import monero.daemon.model.MoneroMiningStatus;
import monero.daemon.model.MoneroOutput;
import monero.daemon.model.MoneroOutputDistributionEntry;
import monero.daemon.model.MoneroOutputHistogramEntry;
import monero.daemon.model.MoneroPeer;
import monero.daemon.model.MoneroPruneResult;
import monero.daemon.model.MoneroSubmitTxResult;
import monero.daemon.model.MoneroTx;
import monero.daemon.model.MoneroTxPoolStats;
import monero.daemon.model.MoneroVersion;
import monero.wallet.MoneroWallet;
import monero.wallet.model.MoneroTxConfig;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import utils.TestUtils;
/**
* Tests a Monero daemon.
*/
public class TestMoneroDaemonRpc {
// classes to test
private static MoneroDaemonRpc daemon; // could test base interface if multiple daemon implementations come along
private static MoneroWallet wallet;
// test configuration
private static final boolean LITE_MODE = false;
private static boolean TEST_NON_RELAYS = true;
private static boolean TEST_RELAYS = true; // creates and relays outgoing txs
private static boolean TEST_NOTIFICATIONS = true;
// config for testing binary blocks
// TODO: binary blocks have inconsistent client-side pruning
// TODO: get_blocks_by_height.bin does not return output indices (#5127)
private static TestContext BINARY_BLOCK_CTX = new TestContext();
{
BINARY_BLOCK_CTX.hasHex = false;
BINARY_BLOCK_CTX.headerIsFull = false;
BINARY_BLOCK_CTX.hasTxs = true;
BINARY_BLOCK_CTX.txContext = new TestContext();
BINARY_BLOCK_CTX.txContext.isPruned = false;
BINARY_BLOCK_CTX.txContext.isConfirmed = true;
BINARY_BLOCK_CTX.txContext.fromGetTxPool = false;
BINARY_BLOCK_CTX.txContext.hasOutputIndices = false;
BINARY_BLOCK_CTX.txContext.fromBinaryBlock = true;
}
// logger
//private static final Logger LOGGER = Logger.getLogger(TestMoneroDaemonRpc.class);
@BeforeAll
public static void beforeAll() throws Exception {
daemon = TestUtils.getDaemonRpc();
wallet = TestUtils.getWalletRpc();
TestUtils.WALLET_TX_TRACKER.reset(); // all wallets need to wait for txs to confirm to reliably sync
}
@BeforeEach
public void beforeEach() {
}
// -------------------------------- NON RELAYS ------------------------------
// Can start and stop a daemon process
@Test
public void testStartDaemon() {
// create command to start monerod process
List<String> cmd = new ArrayList<String>(Arrays.asList(
TestUtils.DAEMON_LOCAL_PATH,
"--" + TestUtils.NETWORK_TYPE.toString().toLowerCase(),
"--no-igd",
"--hide-my-port",
"--data-dir", TestUtils.MONERO_BINS_DIR + "/node1",
"--p2p-bind-port", "58080",
"--rpc-bind-port", "58081",
"--rpc-login", "superuser:abctesting123",
"--zmq-rpc-bind-port", "58082"));
// start monerod process from command
MoneroDaemonRpc daemon;
try {
daemon = new MoneroDaemonRpc(cmd);
} catch (IOException e) {
throw new RuntimeException(e);
}
// query daemon
MoneroRpcConnection connection = daemon.getRpcConnection();
assertEquals("http://127.0.0.1:58081", connection.getUri());
assertEquals("superuser", connection.getUsername());
assertEquals("abctesting123", connection.getPassword());
assertTrue(daemon.getHeight() > 0);
MoneroDaemonInfo info = daemon.getInfo();
testInfo(info);
// add listeners
daemon.addListener(new MoneroDaemonListener());
daemon.addListener(new MoneroDaemonListener());
// stop daemon
daemon.stopProcess();
}
// Can get the daemon's version
@Test
public void testGetVersion() {
assumeTrue(TEST_NON_RELAYS);
MoneroVersion version = daemon.getVersion();
assertNotNull(version.getNumber());
assertTrue(version.getNumber() > 0);
assertNotNull(version.getIsRelease());
}
// Can indicate if it's trusted
@Test
public void testIsTrusted() {
assumeTrue(TEST_NON_RELAYS);
daemon.isTrusted();
}
// Can get the blockchain height
@Test
public void testGetHeight() {
assumeTrue(TEST_NON_RELAYS);
long height = daemon.getHeight();
assertTrue(height > 0, "Height must be greater than 0");
}
// Can get a block hash by height
@Test
public void testGetBlockIdByHeight() {
assumeTrue(TEST_NON_RELAYS);
MoneroBlockHeader lastHeader = daemon.getLastBlockHeader();
String hash = daemon.getBlockHash(lastHeader.getHeight());
assertNotNull(hash);
assertEquals(64, hash.length());
}
// Can get a block template
@Test
public void testGetBlockTemplate() {
assumeTrue(TEST_NON_RELAYS);
MoneroBlockTemplate template = daemon.getBlockTemplate(TestUtils.ADDRESS, 2);
testBlockTemplate(template);
}
// Can get the last block's header
@Test
public void testGetLastBlockHeader() {
assumeTrue(TEST_NON_RELAYS);
MoneroBlockHeader lastHeader = daemon.getLastBlockHeader();
testBlockHeader(lastHeader, true);
}
// Can get a block header by hash
@Test
public void testGetBlockHeaderByHash() {
assumeTrue(TEST_NON_RELAYS);
// retrieve by hash of last block
MoneroBlockHeader lastHeader = daemon.getLastBlockHeader();
String hash = daemon.getBlockHash(lastHeader.getHeight());
MoneroBlockHeader header = daemon.getBlockHeaderByHash(hash);
testBlockHeader(header, true);
assertEquals(lastHeader, header);
// retrieve by hash of previous to last block
hash = daemon.getBlockHash(lastHeader.getHeight() - 1);
header = daemon.getBlockHeaderByHash(hash);
testBlockHeader(header, true);
assertEquals(lastHeader.getHeight() - 1, (long) header.getHeight());
}
// Can get a block header by height
@Test
public void testGetBlockHeaderByHeight() {
assumeTrue(TEST_NON_RELAYS);
// retrieve by height of last block
MoneroBlockHeader lastHeader = daemon.getLastBlockHeader();
MoneroBlockHeader header = daemon.getBlockHeaderByHeight(lastHeader.getHeight());
testBlockHeader(header, true);
assertEquals(lastHeader, header);
// retrieve by height of previous to last block
header = daemon.getBlockHeaderByHeight(lastHeader.getHeight() - 1);
testBlockHeader(header, true);
assertEquals(lastHeader.getHeight() - 1, (long) header.getHeight());
}
// Can get block headers by range
// TODO: test start with no end, vice versa, inclusivity
@Test
public void testGetBlockHeadersByRange() {
assumeTrue(TEST_NON_RELAYS);
// determine start and end height based on number of blocks and how many blocks ago
long numBlocks = 100;
long numBlocksAgo = 100;
long currentHeight = daemon.getHeight();
long startHeight = currentHeight - numBlocksAgo;
long endHeight = currentHeight - (numBlocksAgo - numBlocks) - 1;
// fetch headers
List<MoneroBlockHeader> headers = daemon.getBlockHeadersByRange(startHeight, endHeight);
// test headers
assertEquals(numBlocks, headers.size());
for (int i = 0; i < numBlocks; i++) {
MoneroBlockHeader header = headers.get(i);
assertEquals(startHeight + i, (long) header.getHeight());
testBlockHeader(header, true);
}
}
// Can get a block by hash
@Test
public void testGetBlockByHash() {
assumeTrue(TEST_NON_RELAYS);
// test config
TestContext ctx = new TestContext();
ctx.hasHex = true;
ctx.hasTxs = false;
ctx.headerIsFull = true;
// retrieve by hash of last block
MoneroBlockHeader lastHeader = daemon.getLastBlockHeader();
String hash = daemon.getBlockHash(lastHeader.getHeight());
MoneroBlock block = daemon.getBlockByHash(hash);
testBlock(block, ctx);
assertEquals(daemon.getBlockByHeight(block.getHeight()), block);
assertEquals(null, block.getTxs());
// retrieve by hash of previous to last block
hash = daemon.getBlockHash(lastHeader.getHeight() - 1);
block = daemon.getBlockByHash(hash);
testBlock(block, ctx);
assertEquals(daemon.getBlockByHeight(lastHeader.getHeight() - 1), block);
assertEquals(null, block.getTxs());
}
// Can get blocks by hash which includes transactions (binary)
@Test
public void testGetBlocksByHashBinary() {
assumeTrue(TEST_NON_RELAYS);
throw new RuntimeException("Not implemented");
}
// Can get a block by height
@Test
public void testGetBlockByHeight() {
assumeTrue(TEST_NON_RELAYS);
// config for testing blocks
TestContext ctx = new TestContext();
ctx.hasHex = true;
ctx.headerIsFull = true;
ctx.hasTxs = false;
// retrieve by height of last block
MoneroBlockHeader lastHeader = daemon.getLastBlockHeader();
MoneroBlock block = daemon.getBlockByHeight(lastHeader.getHeight());
testBlock(block, ctx);
assertEquals(daemon.getBlockByHeight(block.getHeight()), block);
// retrieve by height of previous to last block
block = daemon.getBlockByHeight(lastHeader.getHeight() - 1);
testBlock(block, ctx);
assertEquals(lastHeader.getHeight() - 1, (long) block.getHeight());
}
// Can get blocks by height which includes transactions (binary)
@Test
public void testGetBlocksByHeightBinary() {
assumeTrue(TEST_NON_RELAYS);
// set number of blocks to test
int numBlocks = 100;
// select random heights // TODO: this is horribly inefficient way of computing last 100 blocks if not shuffling
long currentHeight = daemon.getHeight();
List<Long> allHeights = new ArrayList<Long>();
for (long i = 0; i < currentHeight - 1; i++) allHeights.add(i);
//GenUtils.shuffle(allHeights);
List<Long> heights = new ArrayList<Long>();
for (int i = allHeights.size() - numBlocks; i < allHeights.size(); i++) heights.add(allHeights.get(i));
// fetch blocks
List<MoneroBlock> blocks = daemon.getBlocksByHeight(heights);
// test blocks
boolean txFound = false;
assertEquals(numBlocks, blocks.size());
for (int i = 0; i < heights.size(); i++) {
MoneroBlock block = blocks.get(i);
if (!block.getTxs().isEmpty()) txFound = true;
testBlock(block, BINARY_BLOCK_CTX);
assertEquals(block.getHeight(), heights.get(i));
}
assertTrue(txFound, "No transactions found to test");
}
// Can get blocks by range in a single request
@Test
public void testGetBlocksByRange() {
assumeTrue(TEST_NON_RELAYS);
// get height range
long numBlocks = 100;
long numBlocksAgo = 190;
assertTrue(numBlocks > 0);
assertTrue(numBlocksAgo >= numBlocks);
long height = daemon.getHeight();
assertTrue(height - numBlocksAgo + numBlocks - 1 < height);
long startHeight = height - numBlocksAgo;
long endHeight = height - numBlocksAgo + numBlocks - 1;
// test known start and end heights
testGetBlocksRange(startHeight, endHeight, height, false);
// test unspecified start
testGetBlocksRange(null, numBlocks - 1, height, false);
// test unspecified end
testGetBlocksRange(height - numBlocks - 1, null, height, false);
};
// Can get blocks by range using chunked requests
@Test
public void testGetBlocksByRangeChunked() {
assumeTrue(TEST_NON_RELAYS && !LITE_MODE);
// get long height range
long numBlocks = Math.min(daemon.getHeight() - 2, 1440); // test up to ~2 days of blocks
assertTrue(numBlocks > 0);
long height = daemon.getHeight();
assertTrue(height - numBlocks - 1 < height);
long startHeight = height - numBlocks;
long endHeight = height - 1;
// test known start and end heights
testGetBlocksRange(startHeight, endHeight, height, true);
// test unspecified start
testGetBlocksRange(null, numBlocks - 1, height, true);
// test unspecified end
testGetBlocksRange(endHeight - numBlocks - 1, null, height, true);
};
// Can get block hashes (binary)
@Test
public void testGetBlockIdsBinary() {
assumeTrue(TEST_NON_RELAYS);
//get_hashes.bin
throw new RuntimeException("Not implemented");
}
// Can get a transaction by hash with and without pruning
@Test
public void testGetTxByHash() {
assumeTrue(TEST_NON_RELAYS);
// fetch transaction hashes to test
List<String> txHashes = getConfirmedTxHashes(daemon);
// context for testing txs
TestContext ctx = new TestContext();
ctx.isPruned = false;
ctx.isConfirmed = true;
ctx.fromGetTxPool = false;
// fetch each tx by hash without pruning
for (String txHash : txHashes) {
MoneroTx tx = daemon.getTx(txHash);
testTx(tx, ctx);
}
// fetch each tx by hash with pruning
for (String txHash : txHashes) {
MoneroTx tx = daemon.getTx(txHash, true);
ctx.isPruned = true;
testTx(tx, ctx);
}
// fetch invalid hash
try {
daemon.getTx("invalid tx hash");
throw new RuntimeException("fail");
} catch (MoneroError e) {
assertEquals("Invalid transaction hash", e.getMessage());
}
}
// Can get transactions by hashes with and without pruning
@Test
public void testGetTxsByHashes() {
assumeTrue(TEST_NON_RELAYS);
// fetch transaction hashes to test
List<String> txHashes = getConfirmedTxHashes(daemon);
assertTrue(txHashes.size() > 0);
// context for testing txs
TestContext ctx = new TestContext();
ctx.isPruned = false;
ctx.isConfirmed = true;
ctx.fromGetTxPool = false;
// fetch txs by hash without pruning
List<MoneroTx> txs = daemon.getTxs(txHashes);
assertEquals(txHashes.size(), txs.size());
for (MoneroTx tx : txs) {
testTx(tx, ctx);
}
// fetch txs by hash with pruning
txs = daemon.getTxs(txHashes, true);
ctx.isPruned = true;
assertEquals(txHashes.size(), txs.size());
for (MoneroTx tx : txs) {
testTx(tx, ctx);
}
// fetch missing hash
MoneroTx tx = wallet.createTx(new MoneroTxConfig().setAccountIndex(0).addDestination(wallet.getPrimaryAddress(), TestUtils.MAX_FEE));
assertNull(daemon.getTx(tx.getHash()));
txHashes.add(tx.getHash());
int numTxs = txs.size();
txs = daemon.getTxs(txHashes);
assertEquals(numTxs, txs.size());
// fetch invalid hash
txHashes.add("invalid tx hash");
try {
daemon.getTxs(txHashes);
throw new RuntimeException("fail");
} catch (MoneroError e) {
assertEquals("Invalid transaction hash", e.getMessage());
}
}
// Can get transactions by hashes that are in the transaction pool
@Test
public void testGetTxsByHashesInPool() {
assumeTrue(TEST_NON_RELAYS);
TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(wallet); // wait for wallet's txs in the pool to clear to ensure reliable sync
// submit txs to the pool but don't relay
List<String> txHashes = new ArrayList<String>();
for (int i = 1; i < 3; i++) {
MoneroTx tx = getUnrelayedTx(wallet, i);
MoneroSubmitTxResult result = daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
assertFalse(result.isRelayed());
txHashes.add(tx.getHash());
}
// fetch txs by hash
System.out.print("Fetching txs...");
List<MoneroTx> txs = daemon.getTxs(txHashes);
System.out.println("done");
// context for testing tx
TestContext ctx = new TestContext();
ctx.isPruned = false;
ctx.isConfirmed = false;
ctx.fromGetTxPool = false;
// test fetched txs
assertEquals(txHashes.size(), txs.size());
for (MoneroTx tx : txs) {
testTx(tx, ctx);
}
// clear txs from pool
daemon.flushTxPool(txHashes);
wallet.sync();
}
// Can get a transaction hex by hash with and without pruning
@Test
public void getTxHexByHash() {
assumeTrue(TEST_NON_RELAYS);
// fetch transaction hashes to test
List<String> txHashes = getConfirmedTxHashes(daemon);
// fetch each tx hex by hash with and without pruning
List<String> hexes = new ArrayList<String>();
List<String> hexesPruned = new ArrayList<String>();
for (String txHash : txHashes) {
hexes.add(daemon.getTxHex(txHash));
hexesPruned.add(daemon.getTxHex(txHash, true));
}
// test results
assertEquals(hexes.size(), txHashes.size());
assertEquals(hexesPruned.size(), txHashes.size());
for (int i = 0; i < hexes.size(); i++) {
assertNotNull(hexes.get(i));
assertNotNull(hexesPruned.get(i));
assertFalse(hexesPruned.isEmpty());
assertTrue(hexes.get(i).length() > hexesPruned.get(i).length()); // pruned hex is shorter
}
// fetch invalid hash
try {
daemon.getTxHex("invalid tx hash");
throw new RuntimeException("fail");
} catch (MoneroError e) {
assertEquals("Invalid transaction hash", e.getMessage());
}
}
// Can get transaction hexes by hashes with and without pruning
@Test
public void testGetTxHexesByHashes() {
assumeTrue(TEST_NON_RELAYS);
// fetch transaction hashes to test
List<String> txHashes = getConfirmedTxHashes(daemon);
// fetch tx hexes by hash with and without pruning
List<String> hexes = daemon.getTxHexes(txHashes);
List<String> hexesPruned = daemon.getTxHexes(txHashes, true);
// test results
assertEquals(hexes.size(), txHashes.size());
assertEquals(hexesPruned.size(), txHashes.size());
for (int i = 0; i < hexes.size(); i++) {
assertNotNull(hexes.get(i));
assertNotNull(hexesPruned.get(i));
assertFalse(hexesPruned.isEmpty());
assertTrue(hexes.get(i).length() > hexesPruned.get(i).length()); // pruned hex is shorter
}
// fetch invalid hash
txHashes.add("invalid tx hash");
try {
daemon.getTxHexes(txHashes);
throw new RuntimeException("fail");
} catch (MoneroError e) {
assertEquals("Invalid transaction hash", e.getMessage());
}
}
// Can get the miner transaction sum
@Test
public void testGetMinerTxSum() {
assumeTrue(TEST_NON_RELAYS);
MoneroMinerTxSum sum = daemon.getMinerTxSum(0l, Math.min(50000l, daemon.getHeight()));
testMinerTxSum(sum);
}
// Can get a fee estimate
@Test
public void testGetFeeEstimate() {
assumeTrue(TEST_NON_RELAYS);
MoneroFeeEstimate feeEstimate = daemon.getFeeEstimate();
TestUtils.testUnsignedBigInteger(feeEstimate.getFee(), true);
assertTrue(feeEstimate.getFees().size() == 4); // slow, normal, fast, fastest
for (int i = 0; i < 4; i++) TestUtils.testUnsignedBigInteger(feeEstimate.getFees().get(i), true);
TestUtils.testUnsignedBigInteger(feeEstimate.getQuantizationMask(), true);
}
// Can get all transactions in the transaction pool
@Test
public void testGetTxsInPool() {
assumeTrue(TEST_NON_RELAYS);
TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(wallet);
// submit tx to pool but don't relay
MoneroTx tx = getUnrelayedTx(wallet, 1);
MoneroSubmitTxResult result = daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
assertFalse(result.isRelayed());
// fetch txs in pool
List<MoneroTx> txs = daemon.getTxPool();
// context for testing tx
TestContext ctx = new TestContext();
ctx.isPruned = false;
ctx.isConfirmed = false;
ctx.fromGetTxPool = true;
// test txs
assertFalse(txs.isEmpty(), "Test requires an unconfirmed tx in the tx pool");
for (MoneroTx aTx : txs) {
testTx(aTx, ctx);
}
// flush the tx from the pool, gg
daemon.flushTxPool(tx.getHash());
wallet.sync();
}
// Can get hashes of transactions in the transaction pool (binary)
@Test
public void testGetIdsOfTxsInPoolBin() {
assumeTrue(TEST_NON_RELAYS);
// TODO: get_transaction_pool_hashes.bin
throw new RuntimeException("Not implemented");
}
// Can get the transaction pool backlog (binary)
@Test
public void testGetTxPoolBacklogBin() {
assumeTrue(TEST_NON_RELAYS);
// TODO: get_txpool_backlog
throw new RuntimeException("Not implemented");
}
// Can get transaction pool statistics
@Test
public void testGetTxPoolStatistics() {
assumeTrue(TEST_NON_RELAYS);
TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(wallet);
Throwable err = null;
Collection<String> txIds = new HashSet<String>();
try {
// submit txs to the pool but don't relay
for (int i = 1; i < 3; i++) {
// submit tx hex
MoneroTx tx = getUnrelayedTx(wallet, i);
MoneroSubmitTxResult result = daemon.submitTxHex(tx.getFullHex(), true);
assertTrue(result.isGood(), JsonUtils.serialize(result));
txIds.add(tx.getHash());
// get tx pool stats
MoneroTxPoolStats stats = daemon.getTxPoolStats();
assertTrue(stats.getNumTxs() > i - 1);
testTxPoolStats(stats);
}
} catch (Throwable e) {
err = e;
}
// flush txs
daemon.flushTxPool(txIds);
if (err != null) throw new RuntimeException(err);
}
// Can flush all transactions from the pool
@Test
public void testFlushTxsFromPool() {
assumeTrue(TEST_NON_RELAYS);
TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(wallet);
// preserve original transactions in the pool
List<MoneroTx> txPoolBefore = daemon.getTxPool();
// submit txs to the pool but don't relay
for (int i = 1; i < 3; i++) {
MoneroTx tx = getUnrelayedTx(wallet, i);
MoneroSubmitTxResult result = daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
}
assertEquals(txPoolBefore.size() + 2, daemon.getTxPool().size());
// flush tx pool
daemon.flushTxPool();
assertEquals(0, daemon.getTxPool().size());
// re-submit original transactions
for (MoneroTx tx : txPoolBefore) {
MoneroSubmitTxResult result = daemon.submitTxHex(tx.getFullHex(), tx.isRelayed());
testSubmitTxResultGood(result);
}
// pool is back to original state
assertEquals(txPoolBefore.size(), daemon.getTxPool().size());
// sync wallet for next test
wallet.sync();
}
// Can flush a transaction from the pool by hash
@Test
public void testFlushTxFromPoolByHash() {
assumeTrue(TEST_NON_RELAYS);
TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(wallet);
// preserve original transactions in the pool
List<MoneroTx> txPoolBefore = daemon.getTxPool();
// submit txs to the pool but don't relay
List<MoneroTx> txs = new ArrayList<MoneroTx>();
for (int i = 1; i < 3; i++) {
MoneroTx tx = getUnrelayedTx(wallet, i);
MoneroSubmitTxResult result = daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
txs.add(tx);
}
// remove each tx from the pool by hash and test
for (int i = 0; i < txs.size(); i++) {
// flush tx from pool
daemon.flushTxPool(txs.get(i).getHash());
// test tx pool
List<MoneroTx> poolTxs = daemon.getTxPool();
assertEquals(txs.size() - i - 1, poolTxs.size());
}
// pool is back to original state
assertEquals(txPoolBefore.size(), daemon.getTxPool().size());
// sync wallet for next test
wallet.sync();
}
// Can flush transactions from the pool by hashes
@Test
public void testFlushTxsFromPoolByHashes() {
assumeTrue(TEST_NON_RELAYS);
TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(wallet);
// preserve original transactions in the pool
List<MoneroTx> txPoolBefore = daemon.getTxPool();
// submit txs to the pool but don't relay
List<String> txHashes = new ArrayList<String>();
for (int i = 1; i < 3; i++) {
MoneroTx tx = getUnrelayedTx(wallet, i);
MoneroSubmitTxResult result = daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
txHashes.add(tx.getHash());
}
assertEquals(txPoolBefore.size() + txHashes.size(), daemon.getTxPool().size());
// remove all txs by hashes
daemon.flushTxPool(txHashes);
// pool is back to original state
assertEquals(txPoolBefore.size(), daemon.getTxPool().size());
wallet.sync();
}
// Can get the spent status of key images
@Test
public void testGetSpentStatusOfKeyImages() {
assumeTrue(TEST_NON_RELAYS);
TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(wallet);
// submit txs to the pool to collect key images then flush them
List<MoneroTx> txs = new ArrayList<MoneroTx>();
for (int i = 1; i < 3; i++) {
MoneroTx tx = getUnrelayedTx(wallet, i);
daemon.submitTxHex(tx.getFullHex(), true);
txs.add(tx);
}
List<String> keyImages = new ArrayList<String>();
List<String> txHashes = new ArrayList<String>();
for (MoneroTx tx : txs) txHashes.add(tx.getHash());
for (MoneroTx tx : daemon.getTxs(txHashes)) {
for (MoneroOutput input : tx.getInputs()) keyImages.add(input.getKeyImage().getHex());
}
daemon.flushTxPool(txHashes);
// key images are not spent
testSpentStatuses(keyImages, MoneroKeyImageSpentStatus.NOT_SPENT);
// submit txs to the pool but don't relay
for (MoneroTx tx : txs) daemon.submitTxHex(tx.getFullHex(), true);
// key images are in the tx pool
testSpentStatuses(keyImages, MoneroKeyImageSpentStatus.TX_POOL);
// collect key images of confirmed txs
keyImages = new ArrayList<String>();
txs = getConfirmedTxs(daemon, 10);
for (MoneroTx tx : txs) {
for (MoneroOutput input : tx.getInputs()) keyImages.add(input.getKeyImage().getHex());
}
// key images are all spent
testSpentStatuses(keyImages, MoneroKeyImageSpentStatus.CONFIRMED);
// flush this test's txs from pool
daemon.flushTxPool(txHashes);
}
// Can get output indices given a list of transaction hashes (binary)
@Test
public void testGetOutputIndicesFromTxIdsBinary() {
assumeTrue(TEST_NON_RELAYS);
throw new RuntimeException("Not implemented"); // get_o_indexes.bin
}
// Can get outputs given a list of output amounts and indices (binary)
@Test
public void testGetOutputsFromAmountsAndIndicesBinary() {
assumeTrue(TEST_NON_RELAYS);
throw new RuntimeException("Not implemented"); // get_outs.bin
}
// Can get an output histogram (binary)
@Test
public void testGetOutputHistogramBinary() {
assumeTrue(TEST_NON_RELAYS);
List<MoneroOutputHistogramEntry> entries = daemon.getOutputHistogram(null, null, null, null, null);
assertFalse(entries.isEmpty());
for (MoneroOutputHistogramEntry entry : entries) {
testOutputHistogramEntry(entry);
}
}
// Can get an output distribution (binary)
@Test
public void testGetOutputDistributionBinary() {
assumeTrue(TEST_NON_RELAYS);
List<BigInteger> amounts = new ArrayList<BigInteger>();
amounts.add(BigInteger.valueOf(0));
amounts.add(BigInteger.valueOf(1));
amounts.add(BigInteger.valueOf(10));
amounts.add(BigInteger.valueOf(100));
amounts.add(BigInteger.valueOf(1000));
amounts.add(BigInteger.valueOf(10000));
amounts.add(BigInteger.valueOf(100000));
amounts.add(BigInteger.valueOf(1000000));
List<MoneroOutputDistributionEntry> entries = daemon.getOutputDistribution(amounts);
for (MoneroOutputDistributionEntry entry : entries) {
testOutputDistributionEntry(entry);
}
}
// Can get general information
@Test
public void testGetGeneralInformation() {
assumeTrue(TEST_NON_RELAYS);
MoneroDaemonInfo info = daemon.getInfo();
testInfo(info);
}
// Can get sync information
@Test
public void testGetSyncInformation() {
assumeTrue(TEST_NON_RELAYS);
MoneroDaemonSyncInfo syncInfo = daemon.getSyncInfo();
testSyncInfo(syncInfo);
}
// Can get hard fork information
@Test
public void testGetHardForkInformation() {
assumeTrue(TEST_NON_RELAYS);
MoneroHardForkInfo hardForkInfo = daemon.getHardForkInfo();
testHardForkInfo(hardForkInfo);
}
// Can get alternative chains
@Test
public void testGetAlternativeChains() {
assumeTrue(TEST_NON_RELAYS);
List<MoneroAltChain> altChains = daemon.getAltChains();
for (MoneroAltChain altChain : altChains) {
testAltChain(altChain);
}
}
// Can get alternative block hashes
@Test
public void testGetAlternativeBlockIds() {
assumeTrue(TEST_NON_RELAYS);
List<String> altBlockIds = daemon.getAltBlockHashes();
for (String altBlockId : altBlockIds) {
assertNotNull(altBlockId);
assertEquals(64, altBlockId.length()); // TODO: common validation
}
}
// Can get, set, and reset a download bandwidth limit
@Test
public void testSetDownloadBandwidth() {
assumeTrue(TEST_NON_RELAYS);
int initVal = daemon.getDownloadLimit();
assertTrue(initVal > 0);
int setVal = initVal * 2;
daemon.setDownloadLimit(setVal);
assertEquals(setVal, daemon.getDownloadLimit());
int resetVal = daemon.resetDownloadLimit();
assertEquals(initVal, resetVal);
// test invalid limits
try {
daemon.setDownloadLimit(0);
fail("Should have thrown error on invalid input");
} catch (MoneroError e) {
assertEquals("Download limit must be an integer greater than 0", e.getMessage());
}
assertEquals(daemon.getDownloadLimit(), initVal);
}
// Can get, set, and reset an upload bandwidth limit
@Test
public void testSetUploadBandwidth() {
assumeTrue(TEST_NON_RELAYS);
int initVal = daemon.getUploadLimit();
assertTrue(initVal > 0);
int setVal = initVal * 2;
daemon.setUploadLimit(setVal);
assertEquals(setVal, daemon.getUploadLimit());
int resetVal = daemon.resetUploadLimit();
assertEquals(initVal, resetVal);
// test invalid limits
try {
daemon.setUploadLimit(0);
fail("Should have thrown error on invalid input");
} catch (MoneroError e) {
assertEquals("Upload limit must be an integer greater than 0", e.getMessage());
}
assertEquals(initVal, daemon.getUploadLimit());
}
// Can get peers with active incoming or outgoing connections
@Test
public void testGetPeers() {
assumeTrue(TEST_NON_RELAYS);
List<MoneroPeer> peers = daemon.getPeers();
assertFalse(peers.isEmpty(), "Daemon has no incoming or outgoing peers to test");
for (MoneroPeer peer : peers) {
testPeer(peer);
}
}
// Can get all known peers which may be online or offline
@Test
public void testGetKnownPeers() {
assumeTrue(TEST_NON_RELAYS);
List<MoneroPeer> peers = daemon.getKnownPeers();
assertFalse(peers.isEmpty(), "Daemon has no known peers to test");
for (MoneroPeer peer : peers) {
testKnownPeer(peer, false);
}
}
// Can limit the number of outgoing peers
@Test
public void testSetOutgoingPeerLimit() {
assumeTrue(TEST_NON_RELAYS);
daemon.setOutgoingPeerLimit(0);
daemon.setOutgoingPeerLimit(8);
daemon.setOutgoingPeerLimit(10);
}
// Can limit the number of incoming peers