-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathTestMoneroDaemonRpc.ts
1864 lines (1606 loc) · 71.4 KB
/
TestMoneroDaemonRpc.ts
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 assert from "assert";
import TestUtils from "./utils/TestUtils";
import {connectToDaemonRpc,
GenUtils,
MoneroDaemonInfo,
MoneroNetworkType,
MoneroTxWallet,
MoneroTx,
MoneroKeyImageSpentStatus,
MoneroWallet,
MoneroDaemon,
MoneroPeer,
MoneroDaemonUpdateCheckResult,
MoneroBan,
MoneroDaemonListener,
MoneroDaemonSyncInfo,
MoneroTxConfig,
MoneroKeyImage,
MoneroOutput,
MoneroAltChain,
MoneroSubmitTxResult,
MoneroTxPoolStats} from "../../index";
// context for testing binary blocks
// TODO: binary blocks have inconsistent client-side pruning
// TODO: get_blocks_by_height.bin does not return output indices (#5127)
const BINARY_BLOCK_CTX = { hasHex: false, headerIsFull: false, hasTxs: true, ctx: { isPruned: false, isConfirmed: true, fromGetTxPool: false, hasOutputIndices: false, fromBinaryBlock: true } };
/**
* Tests a Monero daemon.
*/
export default class TestMoneroDaemonRpc {
// static variables
static readonly MAX_REQ_SIZE = "3000000";
static readonly DEFAULT_ID = "0000000000000000000000000000000000000000000000000000000000000000"; // uninitialized tx or block hash from daemon rpc
static readonly NUM_HEADERS_PER_REQ = 750; // number of headers to fetch and cache per request
// state variables
testConfig: any;
wallet: MoneroWallet;
daemon: MoneroDaemon;
constructor(testConfig) {
this.testConfig = testConfig;
TestUtils.WALLET_TX_TRACKER.reset(); // all wallets need to wait for txs to confirm to reliably sync
}
/**
* Run all tests.
*/
runTests() {
let that = this;
let testConfig = this.testConfig;
describe("TEST MONERO DAEMON RPC", function() {
// initialize wallet before all tests
before(async function() {
try {
that.wallet = await TestUtils.getWalletRpc();
that.daemon = await TestUtils.getDaemonRpc();
TestUtils.WALLET_TX_TRACKER.reset(); // all wallets need to wait for txs to confirm to reliably sync
} catch (e) {
console.error("Error before tests: ");
console.error(e);
throw e;
}
});
// -------------------------- TEST NON RELAYS ---------------------------
if (testConfig.testNonRelays && !GenUtils.isBrowser())
it("Can start and stop a daemon process", async function() {
// create command to start monerod process
let cmd = [
TestUtils.DAEMON_LOCAL_PATH,
"--" + GenUtils.getEnumKeyByValue(MoneroNetworkType, TestUtils.NETWORK_TYPE)!.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
let daemon = await connectToDaemonRpc(cmd);
// query daemon
let connection = await daemon.getRpcConnection();
assert.equal("http://127.0.0.1:58081", connection.getUri());
assert.equal("superuser", connection.getUsername());
assert.equal("abctesting123", connection.getPassword());
assert(await daemon.getHeight() > 0);
let info = await daemon.getInfo();
testInfo(info);
// stop daemon
await daemon.stopProcess();
});
if (testConfig.testNonRelays)
it("Can get the daemon's version", async function() {
let version = await that.daemon.getVersion();
assert(version.getNumber() > 0);
assert.equal(typeof version.getIsRelease(), "boolean");
});
if (testConfig.testNonRelays)
it("Can indicate if it's trusted", async function() {
let isTrusted = await that.daemon.isTrusted();
assert.equal(typeof isTrusted, "boolean");
});
if (testConfig.testNonRelays)
it("Can get the blockchain height", async function() {
let height = await that.daemon.getHeight();
assert(height, "Height must be initialized");
assert(height > 0, "Height must be greater than 0");
});
if (testConfig.testNonRelays)
it("Can get a block hash by height", async function() {
let lastHeader = await that.daemon.getLastBlockHeader();
let hash = await that.daemon.getBlockHash(lastHeader.getHeight());
assert(hash);
assert.equal(hash.length, 64);
});
if (testConfig.testNonRelays)
it("Can get a block template", async function() {
let template = await that.daemon.getBlockTemplate(TestUtils.ADDRESS, 2);
testBlockTemplate(template);
});
if (testConfig.testNonRelays)
it("Can get the last block's header", async function() {
let lastHeader = await that.daemon.getLastBlockHeader();
testBlockHeader(lastHeader, true);
});
if (testConfig.testNonRelays)
it("Can get a block header by hash", async function() {
// retrieve by hash of last block
let lastHeader = await that.daemon.getLastBlockHeader();
let hash = await that.daemon.getBlockHash(lastHeader.getHeight());
let header = await that.daemon.getBlockHeaderByHash(hash);
testBlockHeader(header, true);
assert.deepEqual(header, lastHeader);
// retrieve by hash of previous to last block
hash = await that.daemon.getBlockHash(lastHeader.getHeight() - 1);
header = await that.daemon.getBlockHeaderByHash(hash);
testBlockHeader(header, true);
assert.equal(header.getHeight(), lastHeader.getHeight() - 1);
});
if (testConfig.testNonRelays)
it("Can get a block header by height", async function() {
// retrieve by height of last block
let lastHeader = await that.daemon.getLastBlockHeader();
let header = await that.daemon.getBlockHeaderByHeight(lastHeader.getHeight());
testBlockHeader(header, true);
assert.deepEqual(header, lastHeader);
// retrieve by height of previous to last block
header = await that.daemon.getBlockHeaderByHeight(lastHeader.getHeight() - 1);
testBlockHeader(header, true);
assert.equal(header.getHeight(), lastHeader.getHeight() - 1);
});
// TODO: test start with no end, vice versa, inclusivity
if (testConfig.testNonRelays)
it("Can get block headers by range", async function() {
// determine start and end height based on number of blocks and how many blocks ago
let numBlocks = 100;
let numBlocksAgo = 100;
let currentHeight = await that.daemon.getHeight();
let startHeight = currentHeight - numBlocksAgo;
let endHeight = currentHeight - (numBlocksAgo - numBlocks) - 1;
// fetch headers
let headers = await that.daemon.getBlockHeadersByRange(startHeight, endHeight);
// test headers
assert.equal(headers.length, numBlocks);
for (let i = 0; i < numBlocks; i++) {
let header = headers[i];
assert.equal(header.getHeight(), startHeight + i);
testBlockHeader(header, true);
}
});
if (testConfig.testNonRelays)
it("Can get a block by hash", async function() {
// context for testing blocks
let testBlockCtx = { hasHex: true, headerIsFull: true, hasTxs: false };
// retrieve by hash of last block
let lastHeader = await that.daemon.getLastBlockHeader();
let hash = await that.daemon.getBlockHash(lastHeader.getHeight());
let block = await that.daemon.getBlockByHash(hash);
testBlock(block, testBlockCtx);
assert.deepEqual(block, await that.daemon.getBlockByHeight(block.getHeight()));
assert(block.getTxs() === undefined);
// retrieve by hash of previous to last block
hash = await that.daemon.getBlockHash(lastHeader.getHeight() - 1);
block = await that.daemon.getBlockByHash(hash);
testBlock(block, testBlockCtx);
assert.deepEqual(block, await that.daemon.getBlockByHeight(lastHeader.getHeight() - 1));
assert(block.getTxs() === undefined);
});
if (testConfig.testNonRelays)
it("Can get blocks by hash which includes transactions (binary)", async function() {
throw new Error("Not implemented");
})
if (testConfig.testNonRelays)
it("Can get a block by height", async function() {
// context for testing blocks
let testBlockCtx = { hasHex: true, headerIsFull: true, hasTxs: false };
// retrieve by height of last block
let lastHeader = await that.daemon.getLastBlockHeader();
let block = await that.daemon.getBlockByHeight(lastHeader.getHeight());
testBlock(block, testBlockCtx);
assert.deepEqual(block, await that.daemon.getBlockByHeight(block.getHeight()));
// retrieve by height of previous to last block
block = await that.daemon.getBlockByHeight(lastHeader.getHeight() - 1);
testBlock(block, testBlockCtx);
assert.deepEqual(block.getHeight(), lastHeader.getHeight() - 1);
});
if (testConfig.testNonRelays)
it("Can get blocks by height which includes transactions (binary)", async function() {
// set number of blocks to test
const numBlocks = 200;
// select random heights // TODO: this is horribly inefficient way of computing last 100 blocks if not shuffling
let currentHeight = await that.daemon.getHeight();
let allHeights: number[] = [];
for (let i = 0; i < currentHeight - 1; i++) allHeights.push(i);
//GenUtils.shuffle(allHeights);
let heights: number[] = [];
for (let i = allHeights.length - numBlocks; i < allHeights.length; i++) heights.push(allHeights[i]);
//heights.push(allHeights[i]);
// fetch blocks
let blocks = await that.daemon.getBlocksByHeight(heights);
// test blocks
let txFound = false;
assert.equal(blocks.length, numBlocks);
for (let i = 0; i < heights.length; i++) {
let block = blocks[i];
if (block.getTxs().length) txFound = true;
testBlock(block, BINARY_BLOCK_CTX);
assert.equal(block.getHeight(), heights[i]);
}
assert(txFound, "No transactions found to test");
});
if (testConfig.testNonRelays)
it("Can get blocks by range in a single request", async function() {
// get height range
let numBlocks = 100;
let numBlocksAgo = 190;
assert(numBlocks > 0);
assert(numBlocksAgo >= numBlocks);
let height = await that.daemon.getHeight();
assert(height - numBlocksAgo + numBlocks - 1 < height);
let startHeight = height - numBlocksAgo;
let endHeight = height - numBlocksAgo + numBlocks - 1;
// test known start and end heights
await testGetBlocksRange(startHeight, endHeight, height, false);
// test unspecified start
await testGetBlocksRange(undefined, numBlocks - 1, height, false);
// test unspecified end
await testGetBlocksRange(height - numBlocks - 1, undefined, height, false);
});
// Can get blocks by range using chunked requests
if (testConfig.testNonRelays)
it("Can get blocks by range using chunked requests", async function() {
// get long height range
let numBlocks = Math.min(await that.daemon.getHeight() - 2, 1440); // test up to ~2 days of blocks
assert(numBlocks > 0);
let height = await that.daemon.getHeight();
assert(height - numBlocks - 1 < height);
let startHeight = height - numBlocks;
let endHeight = height - 1;
// test known start and end heights
await testGetBlocksRange(startHeight, endHeight, height, true);
// test unspecified start
await testGetBlocksRange(undefined, numBlocks - 1, height, true);
// test unspecified end
await testGetBlocksRange(endHeight - numBlocks - 1, undefined, height, true);
});
async function testGetBlocksRange(startHeight, endHeight, chainHeight, chunked) {
// fetch blocks by range
let realStartHeight = startHeight === undefined ? 0 : startHeight;
let realEndHeight = endHeight === undefined ? chainHeight - 1 : endHeight;
let blocks = chunked ? await that.daemon.getBlocksByRangeChunked(startHeight, endHeight) : await that.daemon.getBlocksByRange(startHeight, endHeight);
assert.equal(blocks.length, realEndHeight - realStartHeight + 1);
// test each block
for (let i = 0; i < blocks.length; i++) {
assert.equal(blocks[i].getHeight(), realStartHeight + i);
testBlock(blocks[i], BINARY_BLOCK_CTX);
}
}
if (testConfig.testNonRelays)
it("Can get block hashes (binary)", async function() {
//get_hashes.bin
throw new Error("Not implemented");
});
if (testConfig.testNonRelays)
it("Can get a transaction by hash with and without pruning", async function() {
// fetch transaction hashes to test
let txHashes = await getConfirmedTxHashes(that.daemon);
// fetch each tx by hash without pruning
for (let txHash of txHashes) {
let tx = await that.daemon.getTx(txHash);
testTx(tx, {isPruned: false, isConfirmed: true, fromGetTxPool: false});
}
// fetch each tx by hash with pruning
for (let txHash of txHashes) {
let tx = await that.daemon.getTx(txHash, true);
testTx(tx, {isPruned: true, isConfirmed: true, fromGetTxPool: false});
}
// fetch invalid hash
try {
await that.daemon.getTx("invalid tx hash");
throw new Error("fail");
} catch (e: any) {
assert.equal("Invalid transaction hash", e.message);
}
});
if (testConfig.testNonRelays)
it ("Can get transactions by hashes with and without pruning", async function() {
// fetch transaction hashes to test
let txHashes = await getConfirmedTxHashes(that.daemon);
assert(txHashes.length > 0);
// fetch txs by hash without pruning
let txs = await that.daemon.getTxs(txHashes);
assert.equal(txs.length, txHashes.length);
for (let tx of txs) {
testTx(tx, {isPruned: false, isConfirmed: true, fromGetTxPool: false});
}
// fetch txs by hash with pruning
txs = await that.daemon.getTxs(txHashes, true);
assert.equal(txs.length, txHashes.length);
for (let tx of txs) {
testTx(tx, {isPruned: true, isConfirmed: true, fromGetTxPool: false});
}
// fetch missing hash
let tx = await that.wallet.createTx({accountIndex: 0, address: await that.wallet.getPrimaryAddress(), amount: TestUtils.MAX_FEE});
assert.equal(undefined, await that.daemon.getTx(tx.getHash()));
txHashes.push(tx.getHash());
let numTxs = txs.length;
txs = await that.daemon.getTxs(txHashes);
assert.equal(numTxs, txs.length);
// fetch invalid hash
txHashes.push("invalid tx hash");
try {
await that.daemon.getTxs(txHashes);
throw new Error("fail");
} catch (e: any) {
assert.equal("Invalid transaction hash", e.message);
}
});
if (testConfig.testNonRelays)
it("Can get transactions by hashes that are in the transaction pool", async function() {
await TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(that.wallet); // wait for wallet's txs in the pool to clear to ensure reliable sync
// submit txs to the pool but don't relay
let txHashes: string[] = [];
for (let i = 1; i < 3; i++) {
let tx = await getUnrelayedTx(that.wallet, i);
let result = await that.daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
assert.equal(result.getIsRelayed(), false);
txHashes.push(tx.getHash());
}
// fetch txs by hash
let txs = await that.daemon.getTxs(txHashes);
// test fetched txs
assert.equal(txs.length, txHashes.length);
for (let tx of txs) {
testTx(tx, {isConfirmed: false, fromGetTxPool: false, isPruned: false});
}
// clear txs from pool
await that.daemon.flushTxPool(txHashes);
await that.wallet.sync();
});
if (testConfig.testNonRelays)
it("Can get a transaction hex by hash with and without pruning", async function() {
// fetch transaction hashes to test
let txHashes = await getConfirmedTxHashes(that.daemon);
// fetch each tx hex by hash with and without pruning
let hexes: string[] = []
let hexesPruned: string[] = [];
for (let txHash of txHashes) {
hexes.push(await that.daemon.getTxHex(txHash));
hexesPruned.push(await that.daemon.getTxHex(txHash, true));
}
// test results
assert.equal(hexes.length, txHashes.length);
assert.equal(hexesPruned.length, txHashes.length);
for (let i = 0; i < hexes.length; i++) {
assert.equal(typeof hexes[i], "string");
assert.equal(typeof hexesPruned[i], "string");
assert(hexesPruned[i].length > 0);
assert(hexes[i].length > hexesPruned[i].length); // pruned hex is shorter
}
// fetch invalid hash
try {
await that.daemon.getTxHex("invalid tx hash");
throw new Error("fail");
} catch (e: any) {
assert.equal("Invalid transaction hash", e.message);
}
});
if (testConfig.testNonRelays)
it("Can get transaction hexes by hashes with and without pruning", async function() {
// fetch transaction hashes to test
let txHashes = await getConfirmedTxHashes(that.daemon);
// fetch tx hexes by hash with and without pruning
let hexes = await that.daemon.getTxHexes(txHashes);
let hexesPruned = await that.daemon.getTxHexes(txHashes, true);
// test results
assert.equal(hexes.length, txHashes.length);
assert.equal(hexesPruned.length, txHashes.length);
for (let i = 0; i < hexes.length; i++) {
assert.equal(typeof hexes[i], "string");
assert.equal(typeof hexesPruned[i], "string");
assert(hexesPruned[i].length > 0);
assert(hexes[i].length > hexesPruned[i].length); // pruned hex is shorter
}
// fetch invalid hash
txHashes.push("invalid tx hash");
try {
await that.daemon.getTxHexes(txHashes);
throw new Error("fail");
} catch (e: any) {
assert.equal("Invalid transaction hash", e.message);
}
});
if (testConfig.testNonRelays)
it("Can get the miner transaction sum", async function() {
let sum = await that.daemon.getMinerTxSum(0, Math.min(50000, await that.daemon.getHeight()));
testMinerTxSum(sum);
});
if (testConfig.testNonRelays)
it("Can get a fee estimate", async function() {
let feeEstimate = await that.daemon.getFeeEstimate();
TestUtils.testUnsignedBigInt(feeEstimate.getFee(), true);
assert(feeEstimate.getFees().length === 4); // slow, normal, fast, fastest
for (let i = 0; i < 4; i++) TestUtils.testUnsignedBigInt(feeEstimate.getFees()[i], true);
TestUtils.testUnsignedBigInt(feeEstimate.getQuantizationMask(), true);
});
if (testConfig.testNonRelays)
it("Can get all transactions in the transaction pool", async function() {
await TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(that.wallet);
// submit tx to pool but don't relay
let tx = await getUnrelayedTx(that.wallet, 0);
let result = await that.daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
assert.equal(result.getIsRelayed(), false);
// fetch txs in pool
let txs = await that.daemon.getTxPool();
// test txs
assert(Array.isArray(txs));
assert(txs.length > 0, "Test requires an unconfirmed tx in the tx pool");
for (let tx of txs) {
testTx(tx, { isPruned: false, isConfirmed: false, fromGetTxPool: true });
}
// flush the tx from the pool, gg
await that.daemon.flushTxPool(tx.getHash());
await that.wallet.sync();
});
if (testConfig.testNonRelays)
it("Can get hashes of transactions in the transaction pool (binary)", async function() {
// TODO: get_transaction_pool_hashes.bin
throw new Error("Not implemented");
});
if (testConfig.testNonRelays)
it("Can get the transaction pool backlog (binary)", async function() {
// TODO: get_txpool_backlog
throw new Error("Not implemented");
});
if (testConfig.testNonRelays)
it("Can get transaction pool statistics", async function() {
await TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(that.wallet);
let err;
let txIds = [];
try {
// submit txs to the pool but don't relay
for (let i = 1; i < 3; i++) {
// submit tx hex
let tx = await getUnrelayedTx(that.wallet, i);
let result = await that.daemon.submitTxHex(tx.getFullHex(), true);
assert.equal(result.getIsGood(), true, "Bad tx submit result: " + result.toJson());
// get tx pool stats
let stats = await that.daemon.getTxPoolStats();
assert(stats.getNumTxs() > i - 1);
testTxPoolStats(stats);
}
} catch (e) {
err = e;
}
// flush txs
await that.daemon.flushTxPool(txIds);
if (err) throw err;
});
if (testConfig.testNonRelays)
it("Can flush all transactions from the pool", async function() {
await TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(that.wallet);
// preserve original transactions in the pool
let txPoolBefore = await that.daemon.getTxPool();
// submit txs to the pool but don't relay
for (let i = 0; i < 2; i++) {
let tx = await getUnrelayedTx(that.wallet, i);
let result = await that.daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
}
assert.equal((await that.daemon.getTxPool()).length, txPoolBefore.length + 2);
// flush tx pool
await that.daemon.flushTxPool();
assert.equal((await that.daemon.getTxPool()).length, 0);
// re-submit original transactions
for (let tx of txPoolBefore) {
let result = await that.daemon.submitTxHex(tx.getFullHex(), tx.getIsRelayed());
testSubmitTxResultGood(result);
}
// pool is back to original state
assert.equal((await that.daemon.getTxPool()).length, txPoolBefore.length);
// sync wallet for next test
await that.wallet.sync();
});
if (testConfig.testNonRelays)
it("Can flush a transaction from the pool by hash", async function() {
await TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(that.wallet);
// preserve original transactions in the pool
let txPoolBefore = await that.daemon.getTxPool();
// submit txs to the pool but don't relay
let txs: MoneroTx[] = [];
for (let i = 1; i < 3; i++) {
let tx = await getUnrelayedTx(that.wallet, i);
let result = await that.daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
txs.push(tx);
}
// remove each tx from the pool by hash and test
for (let i = 0; i < txs.length; i++) {
// flush tx from pool
await that.daemon.flushTxPool(txs[i].getHash());
// test tx pool
let poolTxs = await that.daemon.getTxPool();
assert.equal(poolTxs.length, txs.length - i - 1);
}
// pool is back to original state
assert.equal((await that.daemon.getTxPool()).length, txPoolBefore.length);
// sync wallet for next test
await that.wallet.sync();
});
if (testConfig.testNonRelays)
it("Can flush transactions from the pool by hashes", async function() {
await TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(that.wallet);
// preserve original transactions in the pool
let txPoolBefore = await that.daemon.getTxPool();
// submit txs to the pool but don't relay
let txHashes: string[] = [];
for (let i = 1; i < 3; i++) {
let tx = await getUnrelayedTx(that.wallet, i);
let result = await that.daemon.submitTxHex(tx.getFullHex(), true);
testSubmitTxResultGood(result);
txHashes.push(tx.getHash());
}
assert.equal((await that.daemon.getTxPool()).length, txPoolBefore.length + txHashes.length);
// remove all txs by hashes
await that.daemon.flushTxPool(txHashes);
// pool is back to original state
assert.equal((await that.daemon.getTxPool()).length, txPoolBefore.length, "Tx pool size is different from start");
await that.wallet.sync();
});
if (testConfig.testNonRelays)
it("Can get the spent status of key images", async function() {
await TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(that.wallet);
// submit txs to the pool to collect key images then flush them
let txs: MoneroTx[] = [];
for (let i = 1; i < 3; i++) {
let tx = await getUnrelayedTx(that.wallet, i);
await that.daemon.submitTxHex(tx.getFullHex(), true);
txs.push(tx);
}
let keyImages: string[] = [];
let txHashes = txs.map(tx => tx.getHash());
for (let tx of await that.daemon.getTxs(txHashes)) {
for (let input of tx.getInputs()) keyImages.push(input.getKeyImage().getHex());
}
await that.daemon.flushTxPool(txHashes);
// key images are not spent
await testSpentStatuses(keyImages, MoneroKeyImageSpentStatus.NOT_SPENT);
// submit txs to the pool but don't relay
for (let tx of txs) await that.daemon.submitTxHex(tx.getFullHex(), true);
// key images are in the tx pool
await testSpentStatuses(keyImages, MoneroKeyImageSpentStatus.TX_POOL);
// collect key images of confirmed txs
keyImages = [];
txs = await getConfirmedTxs(that.daemon, 10);
for (let tx of txs) {
for (let input of tx.getInputs()) keyImages.push(input.getKeyImage().getHex());
}
// key images are all spent
await testSpentStatuses(keyImages, MoneroKeyImageSpentStatus.CONFIRMED);
// flush this test's txs from pool
await that.daemon.flushTxPool(txHashes);
// helper function to check the spent status of a key image or array of key images
async function testSpentStatuses(keyImages, expectedStatus) {
// test image
for (let keyImage of keyImages) {
assert.equal(await that.daemon.getKeyImageSpentStatus(keyImage), expectedStatus);
}
// test array of images
let statuses = keyImages.length == 0 ? [] : await that.daemon.getKeyImageSpentStatuses(keyImages);
assert(Array.isArray(statuses));
assert.equal(statuses.length, keyImages.length);
for (let status of statuses) assert.equal(status, expectedStatus);
}
});
if (testConfig.testNonRelays)
it("Can get output indices given a list of transaction hashes (binary)", async function() {
throw new Error("Not implemented"); // get_o_indexes.bin
});
if (testConfig.testNonRelays)
it("Can get outputs given a list of output amounts and indices (binary)", async function() {
throw new Error("Not implemented"); // get_outs.bin
});
if (testConfig.testNonRelays)
it("Can get an output histogram (binary)", async function() {
let entries = await that.daemon.getOutputHistogram();
assert(Array.isArray(entries));
assert(entries.length > 0);
for (let entry of entries) {
testOutputHistogramEntry(entry);
}
});
// if (testConfig.testNonRelays)
// it("Can get an output distribution (binary)", async function() {
// let amounts: bigint[] = [];
// amounts.push(BigInt(0));
// amounts.push(BigInt(1));
// amounts.push(BigInt(10));
// amounts.push(BigInt(100));
// amounts.push(BigInt(1000));
// amounts.push(BigInt(10000));
// amounts.push(BigInt(100000));
// amounts.push(BigInt(1000000));
// let entries = await that.daemon.getOutputDistribution(amounts);
// for (let entry of entries) {
// testOutputDistributionEntry(entry);
// }
// });
if (testConfig.testNonRelays)
it("Can get general information", async function() {
let info = await that.daemon.getInfo();
testInfo(info);
});
if (testConfig.testNonRelays)
it("Can get sync information", async function() {
let syncInfo = await that.daemon.getSyncInfo();
testSyncInfo(syncInfo);
});
if (testConfig.testNonRelays)
it("Can get hard fork information", async function() {
let hardForkInfo = await that.daemon.getHardForkInfo();
testHardForkInfo(hardForkInfo);
});
if (testConfig.testNonRelays)
it("Can get alternative chains", async function() {
let altChains = await that.daemon.getAltChains();
assert(Array.isArray(altChains) && altChains.length >= 0);
for (let altChain of altChains) {
testAltChain(altChain);
}
});
if (testConfig.testNonRelays)
it("Can get alternative block hashes", async function() {
let altBlockHashes = await that.daemon.getAltBlockHashes();
assert(Array.isArray(altBlockHashes) && altBlockHashes.length >= 0);
for (let altBlockHash of altBlockHashes) {
assert.equal(typeof altBlockHash, "string");
assert.equal(altBlockHash.length, 64); // TODO: common validation
}
});
if (testConfig.testNonRelays)
it("Can get, set, and reset a download bandwidth limit", async function() {
let initVal = await that.daemon.getDownloadLimit();
assert(initVal > 0);
let setVal = initVal * 2;
await that.daemon.setDownloadLimit(setVal);
assert.equal(await that.daemon.getDownloadLimit(), setVal);
let resetVal = await that.daemon.resetDownloadLimit();
assert.equal(resetVal, initVal);
// test invalid limits
try {
await that.daemon.setDownloadLimit(0);
throw new Error("Should have thrown error on invalid input");
} catch (e: any) {
assert.equal("Download limit must be an integer greater than 0", e.message);
}
try {
await that.daemon.setDownloadLimit(1.2);
throw new Error("Should have thrown error on invalid input");
} catch (e: any) {
assert.equal("Download limit must be an integer greater than 0", e.message);
}
assert.equal(await that.daemon.getDownloadLimit(), initVal);
});
if (testConfig.testNonRelays)
it("Can get, set, and reset an upload bandwidth limit", async function() {
let initVal = await that.daemon.getUploadLimit();
assert(initVal > 0);
let setVal = initVal * 2;
await that.daemon.setUploadLimit(setVal);
assert.equal(await that.daemon.getUploadLimit(), setVal);
let resetVal = await that.daemon.resetUploadLimit();
assert.equal(resetVal, initVal);
// test invalid limits
try {
await that.daemon.setUploadLimit(0);
throw new Error("Should have thrown error on invalid input");
} catch (e: any) {
assert.equal("Upload limit must be an integer greater than 0", e.message);
}
try {
await that.daemon.setUploadLimit(1.2);
throw new Error("Should have thrown error on invalid input");
} catch (e: any) {
assert.equal("Upload limit must be an integer greater than 0", e.message);
}
assert.equal(await that.daemon.getUploadLimit(), initVal);
});
if (testConfig.testNonRelays)
it("Can get peers with active incoming or outgoing peers", async function() {
let peers = await that.daemon.getPeers();
assert(Array.isArray(peers));
assert(peers.length > 0, "Daemon has no incoming or outgoing peers to test");
for (let peer of peers) {
testPeer(peer);
}
});
if (testConfig.testNonRelays)
it("Can get known peers which may be online or offline", async function() {
let peers = await that.daemon.getKnownPeers();
assert(peers.length > 0, "Daemon has no known peers to test");
for (let peer of peers) {
testKnownPeer(peer);
}
});
if (testConfig.testNonRelays)
it("Can limit the number of outgoing peers", async function() {
await that.daemon.setOutgoingPeerLimit(0);
await that.daemon.setOutgoingPeerLimit(8);
await that.daemon.setOutgoingPeerLimit(10);
});
if (testConfig.testNonRelays)
it("Can limit the number of incoming peers", async function() {
await that.daemon.setIncomingPeerLimit(0);
await that.daemon.setIncomingPeerLimit(8);
await that.daemon.setIncomingPeerLimit(10);
});
if (testConfig.testNonRelays)
it("Can ban a peer", async function() {
// set ban
let ban = new MoneroBan({
host: "192.168.1.56",
isBanned: true,
seconds: 60
});
await that.daemon.setPeerBan(ban);
// test ban
let bans = await that.daemon.getPeerBans();
let found = false;
for (let aBan of bans) {
testMoneroBan(aBan);
if (aBan.getHost() === "192.168.1.56") found = true;
}
assert(found);
});
if (testConfig.testNonRelays)
it("Can ban peers", async function() {
// set bans
let ban1 = new MoneroBan();
ban1.setHost("192.168.1.52");
ban1.setIsBanned(true);
ban1.setSeconds(60);
let ban2 = new MoneroBan();
ban2.setHost("192.168.1.53");
ban2.setIsBanned(true);
ban2.setSeconds(60);
let bans: MoneroBan[] = [];
bans.push(ban1);
bans.push(ban2);
await that.daemon.setPeerBans(bans);
// test bans
bans = await that.daemon.getPeerBans();
let found1 = false;
let found2 = false;
for (let aBan of bans) {
testMoneroBan(aBan);
if (aBan.getHost() === "192.168.1.52") found1 = true;
if (aBan.getHost() === "192.168.1.53") found2 = true;
}
assert(found1);
assert(found2);
});
if (testConfig.testNonRelays)
it("Can start and stop mining", async function() {
// stop mining at beginning of test
try { await that.daemon.stopMining(); }
catch(e) { }
// generate address to mine to
let address = await that.wallet.getPrimaryAddress();
// start mining
await that.daemon.startMining(address, 2, false, true);
// stop mining
await that.daemon.stopMining();
});
if (testConfig.testNonRelays)
it("Can get mining status", async function() {
try {
// stop mining at beginning of test
try { await that.daemon.stopMining(); }
catch(e) { }
// test status without mining
let status = await that.daemon.getMiningStatus();
assert.equal(status.getIsActive(), false);
assert.equal(status.getAddress(), undefined);
assert.equal(status.getSpeed(), 0);
assert.equal(status.getNumThreads(), 0);
assert.equal(status.getIsBackground(), undefined);
// test status with mining
let address = await that.wallet.getPrimaryAddress();
let threadCount = 3;
let isBackground = false;
await that.daemon.startMining(address, threadCount, isBackground, true);
status = await that.daemon.getMiningStatus();
assert.equal(status.getIsActive(), true);
assert.equal(status.getAddress(), address);
assert(status.getSpeed() >= 0);
assert.equal(status.getNumThreads(), threadCount);
assert.equal(status.getIsBackground(), isBackground);
} catch(e) {
throw e;
} finally {
// stop mining at end of test
try { await that.daemon.stopMining(); }
catch(e) { }
}
});
if (testConfig.testNonRelays)
it("Can submit a mined block to the network", async function() {
// get template to mine on
let template = await that.daemon.getBlockTemplate(TestUtils.ADDRESS);
// TODO test mining and submitting block
// try to submit block hashing blob without nonce
try {