forked from btcpay-monero/monero-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneroDaemonRpcIntegrationTest.cs
More file actions
1508 lines (1346 loc) · 46.9 KB
/
Copy pathMoneroDaemonRpcIntegrationTest.cs
File metadata and controls
1508 lines (1346 loc) · 46.9 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
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
using Monero.Common;
using Monero.Daemon;
using Monero.Daemon.Common;
using Monero.Daemon.Rpc;
using Monero.IntegrationTests.Utils;
using Xunit;
namespace Monero.IntegrationTests;
public class MoneroDaemonRpcIntegrationTest
{
private readonly MoneroDaemonRpc _daemon = TestUtils.GetDaemonRpc(); // daemon instance to test
public MoneroDaemonRpcIntegrationTest()
{
ulong daemonHeight = _daemon.GetHeight().GetAwaiter().GetResult();
if (daemonHeight == 1)
{
_daemon.WaitForNextBlockHeader().GetAwaiter().GetResult();
}
}
#region Notification Tests
// Can notify listeners when a new block is added to the chain
[Fact]
public async Task TestBlockListener()
{
try
{
// register a listener
MoneroDaemonListener listener = new();
_daemon.AddListener(listener);
// wait for the next block notification
MoneroBlockHeader header = await _daemon.WaitForNextBlockHeader();
_daemon.RemoveListener(listener); // unregister listener so daemon does not keep polling
TestBlockHeader(header, true);
// test that listener was called with the equivalent header
Assert.True(header.Equals(listener.GetLastBlockHeader()));
}
finally
{
// stop mining
try { await _daemon.StopMining(); }
catch (MoneroError)
{
// ignore
}
}
}
#endregion
#region Non Relays Tests
[Fact]
public async Task TestGetVersion()
{
MoneroVersion version = await _daemon.GetVersion();
Assert.NotNull(version.Number);
Assert.True(version.Number > 0);
Assert.NotNull(version.IsRelease);
}
// Can get the blockchain height
[Fact]
public async Task TestGetHeight()
{
ulong height = await _daemon.GetHeight();
Assert.True(height > 0, "Height must be greater than 0");
}
// Can get a block hash by height
[Fact]
public async Task TestGetBlockIdByHeight()
{
MoneroBlockHeader lastHeader = await _daemon.GetLastBlockHeader();
string hash = await _daemon.GetBlockHash((ulong)lastHeader.GetHeight()!);
Assert.NotNull(hash);
Assert.Equal(64, hash.Length);
}
// Can get a block template
[Fact]
public async Task TestGetBlockTemplate()
{
MoneroBlockTemplate template = await _daemon.GetBlockTemplate(TestUtils.Address, 2);
TestBlockTemplate(template);
}
// Can get the last block's header
[Fact]
public async Task TestGetLastBlockHeader()
{
MoneroBlockHeader lastHeader = await _daemon.GetLastBlockHeader();
TestBlockHeader(lastHeader, true);
}
// Can get a block header by hash
[Fact]
public async Task TestGetBlockHeaderByHash()
{
// retrieve by hash of the last block
MoneroBlockHeader lastHeader = await _daemon.GetLastBlockHeader();
string hash = await _daemon.GetBlockHash((ulong)lastHeader.GetHeight()!);
MoneroBlockHeader header = await _daemon.GetBlockHeaderByHash(hash);
TestBlockHeader(header, true);
Assert.True(lastHeader.Equals(header));
// retrieve by hash of previous to last block
hash = await _daemon.GetBlockHash((ulong)lastHeader.GetHeight()! - 1);
header = await _daemon.GetBlockHeaderByHash(hash);
TestBlockHeader(header, true);
Assert.True(lastHeader.GetHeight() - 1 == (ulong)header.GetHeight()!);
}
// Can get a block header by height
[Fact]
public async Task TestGetBlockHeaderByHeight()
{
// retrieve by height of the last block
MoneroBlockHeader lastHeader = await _daemon.GetLastBlockHeader();
MoneroBlockHeader header = await _daemon.GetBlockHeaderByHeight((ulong)lastHeader.GetHeight()!);
TestBlockHeader(header, true);
Assert.True(lastHeader.Equals(header));
// retrieve by height of previous to last block
header = await _daemon.GetBlockHeaderByHeight((ulong)lastHeader.GetHeight()! - 1);
TestBlockHeader(header, true);
Assert.True(lastHeader.GetHeight() - 1 == (ulong)header.GetHeight()!);
}
// Can get block headers by range
// TODO: test start with no end, vice versa, inclusivity
[Fact]
public async Task TestGetBlockHeadersByRange()
{
// determine start and end height based on the number of blocks and how many blocks ago
ulong numBlocks = 10;
ulong numBlocksAgo = 10;
ulong currentHeight = await _daemon.GetHeight();
ulong startHeight = currentHeight - numBlocksAgo;
ulong endHeight = currentHeight - 1;
// fetch headers
List<MoneroBlockHeader> headers = await
_daemon.GetBlockHeadersByRange(startHeight, endHeight);
// test headers
Assert.True(numBlocks == (ulong)headers.Count);
int j = 0;
for (ulong i = 0; i < numBlocks; i++)
{
MoneroBlockHeader header = headers[j];
Assert.True(startHeight + i == (ulong)header.GetHeight()!);
TestBlockHeader(header, true);
j++;
}
}
// Can get a block by hash
[Fact]
public async Task TestGetBlockByHash()
{
// test config
TestContext ctx = new() { HasHex = true, HasTxs = false, HeaderIsFull = true };
// retrieve by hash of the last block
MoneroBlockHeader lastHeader = await _daemon.GetLastBlockHeader();
string hash = await _daemon.GetBlockHash((ulong)lastHeader.GetHeight()!);
MoneroBlock block = await _daemon.GetBlockByHash(hash);
TestBlock(block, ctx);
Assert.True((await _daemon.GetBlockByHeight((ulong)block.GetHeight()!)).Equals(block));
Assert.Null(block.Txs);
// retrieve by hash of previous to last block
hash = await _daemon.GetBlockHash((ulong)lastHeader.GetHeight()! - 1);
block = await _daemon.GetBlockByHash(hash);
TestBlock(block, ctx);
Assert.True((await _daemon.GetBlockByHeight((ulong)lastHeader.GetHeight()! - 1)).Equals(block));
Assert.Null(block.Txs);
}
// Can get blocks by hash which includes transactions (binary)
[Fact(Skip = "Binary request not implemented")]
public Task TestGetBlocksByHashBinary()
{
throw new MoneroError("Not implemented");
}
// Can get a block by height
[Fact]
public async Task TestGetBlockByHeight()
{
// config for testing blocks
TestContext ctx = new();
ctx.HasHex = true;
ctx.HeaderIsFull = true;
ctx.HasTxs = false;
// retrieve by height of the last block
MoneroBlockHeader lastHeader = await _daemon.GetLastBlockHeader();
MoneroBlock block = await _daemon.GetBlockByHeight((ulong)lastHeader.GetHeight()!);
TestBlock(block, ctx);
Assert.True((await _daemon.GetBlockByHeight((ulong)block.GetHeight()!)).Equals(block));
// retrieve by height of previous to last block
block = await _daemon.GetBlockByHeight((ulong)lastHeader.GetHeight()! - 1);
TestBlock(block, ctx);
Assert.True(lastHeader.GetHeight() - 1 == (ulong)block.GetHeight()!);
}
// Can get a transaction by hash with and without pruning
[Fact]
public async Task TestGetTxByHash()
{
// fetch transaction hashes to test
List<string> txHashes = await GetConfirmedTxHashes(_daemon);
// context for testing txs
TestContext ctx = new() { IsPruned = false, IsConfirmed = true, FromGetTxPool = false };
// fetch each tx by hash without pruning
if (txHashes.Count > 0)
{
List<MoneroTx> txs = await _daemon.GetTxs(txHashes, false);
foreach (MoneroTx tx in txs)
{
TestTx(tx, ctx);
}
}
// fetch each tx by hash with pruning
if (txHashes.Count > 0)
{
List<MoneroTx> prunedTxs = await _daemon.GetTxs(txHashes, true);
foreach (MoneroTx tx in prunedTxs)
{
ctx.IsPruned = true;
TestTx(tx, ctx);
}
}
// fetch invalid hash
try
{
await _daemon.GetTxs(["invalid tx hash"], false);
throw new MoneroError("fail");
}
catch (MoneroError e)
{
Assert.Equal("Invalid transaction hash", e.Message);
}
}
// Can get a transaction hex by hash with and without pruning
[Fact]
public async Task TestGetTxHexByHash()
{
// fetch transaction hashes to test
List<string> txHashes = await GetConfirmedTxHashes(_daemon);
// fetch each tx hex by hash with and without pruning
List<string> hexes = [];
List<string> hexesPruned = [];
if (txHashes.Count > 0)
{
hexes.AddRange(await _daemon.GetTxHexes(txHashes, false));
hexesPruned.AddRange(await _daemon.GetTxHexes(txHashes, true));
}
// test results
TestTxHexes(hexes, hexesPruned, txHashes);
// fetch invalid hash
try
{
await _daemon.GetTxHexes(["invalid tx hash"], false);
throw new MoneroError("fail");
}
catch (MoneroError e)
{
Assert.Equal("Invalid transaction hash", e.Message);
}
}
// Can get transaction hexes by hashes with and without pruning
[Fact(Skip = "Needs monero-wallet-rpc")]
public async Task TestGetTxHexesByHashes()
{
// fetch transaction hashes to test
List<string> txHashes = await GetConfirmedTxHashes(_daemon);
// fetch tx hexes by hash with and without pruning
List<string> hexes = await _daemon.GetTxHexes(txHashes, false);
List<string> hexesPruned = await _daemon.GetTxHexes(txHashes, true);
// test results
TestTxHexes(hexes, hexesPruned, txHashes);
// fetch invalid hash
txHashes.Add("invalid tx hash");
try
{
await _daemon.GetTxHexes(txHashes, false);
throw new MoneroError("fail");
}
catch (MoneroError e)
{
Assert.Equal("Invalid transaction hash", e.Message);
}
}
// Can get the miner transaction sum
[Fact(Skip = "Not supported by regtest daemon")]
public async Task TestGetMinerTxSum()
{
MoneroMinerTxSum sum = await _daemon.GetMinerTxSum(0, Math.Min(50000, await _daemon.GetHeight()));
TestMinerTxSum(sum);
}
// Can get a fee estimate
[Fact(Skip = "Not supported by testnet daemon")]
public async Task TestGetFeeEstimate()
{
GetFeeEstimateResponse feeEstimateResponse = await _daemon.GetFeeEstimate(null);
TestUtils.TestUnsignedBigInteger(feeEstimateResponse.Fee, true);
Assert.Equal(4, feeEstimateResponse.Fees?.Count); // slow, normal, fast, fastest
for (int i = 0; i < 4; i++)
{
TestUtils.TestUnsignedBigInteger(feeEstimateResponse?.Fees?[i], true);
}
TestUtils.TestUnsignedBigInteger(feeEstimateResponse?.QuantizationMask, true);
}
// Can get hashes of transactions in the transaction pool (binary)
[Fact(Skip = "Binary request not implemented")]
public Task TestGetIdsOfTxsInPoolBin()
{
// TODO: get_transaction_pool_hashes.bin
throw new MoneroError("Not implemented");
}
// Can get the transaction pool backlog (binary)
[Fact(Skip = "Binary request not implemented")]
public Task TestGetTxPoolBacklogBin()
{
// TODO: get_txpool_backlog
throw new MoneroError("Not implemented");
}
// Can get output indices given a list of transaction hashes (binary)
[Fact(Skip = "Binary request not implemented")]
public Task TestGetOutputIndicesFromTxIdsBinary()
{
throw new Exception("Not implemented"); // get_o_indexes.bin
}
// Can get outputs given a list of output amounts and indices (binary)
[Fact(Skip = "Binary request not implemented")]
public Task TestGetOutputsFromAmountsAndIndicesBinary()
{
throw new Exception("Not implemented"); // get_outs.bin
}
// Can get an output histogram (binary)
[Fact(Skip = "Binary request not implemented")]
public async Task TestGetOutputHistogramBinary()
{
List<MoneroOutputHistogramEntry> entries = await
_daemon.GetOutputHistogram([], null, null, null, null);
Assert.True(entries.Count > 0);
foreach (MoneroOutputHistogramEntry entry in entries)
{
TestOutputHistogramEntry(entry);
}
}
// Can get an output distribution (binary)
[Fact(Skip = "Binary request not implemented")]
public async Task TestGetOutputDistributionBinary()
{
List<ulong> amounts = [];
amounts.Add(0);
amounts.Add(1);
amounts.Add(10);
amounts.Add(100);
amounts.Add(1000);
amounts.Add(10000);
amounts.Add(100000);
amounts.Add(1000000);
List<MoneroOutputDistributionEntry> entries = await _daemon.GetOutputDistribution(amounts, false, null, null);
foreach (MoneroOutputDistributionEntry entry in entries)
{
TestOutputDistributionEntry(entry);
}
}
// Can get general information
[Fact]
public async Task TestGetGeneralInformation()
{
MoneroDaemonInfo info = await _daemon.GetInfo();
TestInfo(info);
}
// Can get sync information
[Fact]
public async Task TestGetSyncInformation()
{
MoneroDaemonSyncInfo syncInfo = await _daemon.GetSyncInfo();
TestSyncInfo(syncInfo);
}
// Can get hard fork information
[Fact]
public async Task TestGetHardForkInformation()
{
MoneroHardForkInfo hardForkInfo = await _daemon.GetHardForkInfo();
TestHardForkInfo(hardForkInfo);
}
// Can get alternative chains
[Fact]
public async Task TestGetAlternativeChains()
{
List<MoneroAltChain> altChains = await _daemon.GetAltChains();
foreach (MoneroAltChain altChain in altChains)
{
TestAltChain(altChain);
}
}
// Can get alternative block hashes
[Fact]
public async Task TestGetAlternativeBlockIds()
{
List<string> altBlockIds = await _daemon.GetAltBlockHashes();
foreach (string altBlockId in altBlockIds)
{
Assert.NotNull(altBlockId);
Assert.Equal(64, altBlockId.Length); // TODO: common validation
}
}
// Can get, set, and reset a download bandwidth limit
[Fact]
public async Task TestSetDownloadBandwidth()
{
int initVal = await _daemon.GetDownloadLimit();
Assert.True(initVal > 0);
int setVal = initVal * 2;
await _daemon.SetDownloadLimit(setVal);
Assert.True(setVal == await _daemon.GetDownloadLimit());
int resetVal = await _daemon.ResetDownloadLimit();
Assert.True(initVal == resetVal);
// test invalid limits
try
{
await _daemon.SetDownloadLimit(0);
throw new MoneroError("Should have thrown error on invalid input");
}
catch (MoneroError e)
{
Assert.Equal("Download limit must be an integer greater than 0", e.Message);
}
Assert.True(await _daemon.GetDownloadLimit() == initVal);
}
// Can get, set, and reset an upload bandwidth limit
[Fact]
public async Task TestSetUploadBandwidth()
{
int initVal = await _daemon.GetUploadLimit();
Assert.True(initVal > 0);
int setVal = initVal * 2;
await _daemon.SetUploadLimit(setVal);
Assert.True(setVal == await _daemon.GetUploadLimit());
int resetVal = await _daemon.ResetUploadLimit();
Assert.True(initVal == resetVal);
// test invalid limits
try
{
await _daemon.SetUploadLimit(0);
throw new Exception("Should have thrown error on invalid input");
}
catch (MoneroError e)
{
Assert.Equal("Upload limit must be an integer greater than 0", e.Message);
}
Assert.True(initVal == await _daemon.GetUploadLimit());
}
// Can get peers with active incoming or outgoing connections
[Fact]
public async Task TestGetPeers()
{
List<MoneroPeer> peers = await _daemon.GetPeers();
Assert.True(peers.Count > 0, "Daemon has no incoming or outgoing peers to test");
foreach (MoneroPeer peer in peers)
{
TestPeer(peer);
}
}
// Can get all known peers that may be online or offline
[Fact(Skip = "Daemon has no known peers to test")]
public async Task TestGetKnownPeers()
{
List<MoneroPeer> peers = await _daemon.GetKnownPeers();
Assert.True(peers.Count > 0, "Daemon has no known peers to test");
foreach (MoneroPeer peer in peers)
{
TestKnownPeer(peer, false);
}
}
// Can limit the number of outgoing peers
[Fact]
public async Task TestSetOutgoingPeerLimit()
{
await _daemon.SetOutgoingPeerLimit(0);
await _daemon.SetOutgoingPeerLimit(8);
await _daemon.SetOutgoingPeerLimit(10);
}
// Can limit the number of incoming peers
[Fact]
public async Task TestSetIncomingPeerLimit()
{
await _daemon.SetIncomingPeerLimit(0);
await _daemon.SetIncomingPeerLimit(8);
await _daemon.SetIncomingPeerLimit(10);
}
// Can ban a peer
[Fact]
public async Task TestBanPeer()
{
// set ban
MoneroBan ban = new()
{
Host = "192.168.1.51",
IsBanned = true,
Seconds = 60
};
await _daemon.SetPeerBans([ban]);
// test ban
List<MoneroBan> bans = await _daemon.GetPeerBans();
bool found = false;
foreach (MoneroBan aBan in bans)
{
TestMoneroBan(aBan);
if ("192.168.1.51".Equals(aBan.Host))
{
found = true;
}
}
Assert.True(found);
}
// Can ban peers
[Fact]
public async Task TestBanPeers()
{
// set bans
MoneroBan ban1 = new();
ban1.Host = "192.168.1.52";
ban1.IsBanned = true;
ban1.Seconds = 60;
MoneroBan ban2 = new();
ban2.Host = "192.168.1.53";
ban2.IsBanned = true;
ban2.Seconds = 60;
List<MoneroBan> bans = [];
bans.Add(ban1);
bans.Add(ban2);
await _daemon.SetPeerBans(bans);
// test bans
bans = await _daemon.GetPeerBans();
bool found1 = false;
bool found2 = false;
foreach (MoneroBan aBan in bans)
{
TestMoneroBan(aBan);
if ("192.168.1.52".Equals(aBan.Host))
{
found1 = true;
}
if ("192.168.1.53".Equals(aBan.Host))
{
found2 = true;
}
}
Assert.True(found1);
Assert.True(found2);
}
// Can start and stop mining
[Fact(Skip = "Fails on github CI")]
public async Task TestMining()
{
// stop mining at the beginning of the test
try { await _daemon.StopMining(); }
catch (MoneroError)
{
// ignore
}
// generate address to mine to
// TODO use wallet rpc
string address = TestUtils.Address;
// start mining
await _daemon.StartMining(address, 1, false, true);
GenUtils.WaitFor(30);
// stop mining
await _daemon.StopMining();
}
// Can get mining status
// TODO why this test fails on github runner?
[Fact(Skip = "Fails on github CI")]
public async Task TestGetMiningStatus()
{
try
{
// stop mining at the beginning of the test
try { await _daemon.StopMining(); }
catch (MoneroError)
{
// ignore
}
// test status without mining
MoneroMiningStatus status = await _daemon.GetMiningStatus();
Assert.False(status.IsActive);
Assert.Null(status.Address);
Assert.Equal(0, (long)status.Speed!);
Assert.Equal(0, (int)status.NumThreads!);
Assert.Null(status.IsBackground);
// test status with mining
// TODO use wallet rpc address
string address = TestUtils.Address;
ulong threadCount = 1;
bool isBackground = false;
await _daemon.StartMining(address, threadCount, isBackground, true);
status = await _daemon.GetMiningStatus();
Assert.True(status.IsActive);
Assert.True(address == status.Address);
Assert.True(status.Speed >= 0);
Assert.True(threadCount == status.NumThreads);
Assert.True(isBackground == status.IsBackground);
}
finally
{
// stop mining at the end of the test
try { await _daemon.StopMining(); }
catch (MoneroError)
{
// ignore
}
}
}
// Can submit a mined block to the network
[Fact(Skip = "Not supported by regtest daemon")]
public async Task TestSubmitMinedBlock()
{
// get template to mine on
MoneroBlockTemplate template = await _daemon.GetBlockTemplate(TestUtils.Address, 0);
// TODO monero rpc: way to get mining nonce when found in order to submit?
// try to submit a block hashing blob without nonce
try
{
await _daemon.SubmitBlocks([template.BlockTemplateBlob!]);
throw new Exception("Should have thrown error");
}
catch (MoneroRpcError e)
{
Assert.True(-7 == e.GetCode());
Assert.Equal("Block not accepted", e.Message);
}
}
// Can prune the blockchain
[Fact(Skip = "Not supported by regtest daemon")]
public async Task TestPruneBlockchain()
{
MoneroPruneResponse response = await _daemon.PruneBlockchain(true);
if (response.IsPruned == true)
{
Assert.True(response.PruningSeed > 0);
}
else
{
Assert.True(0 == response.PruningSeed);
}
}
// Can check for an update
[Fact(Skip = "Unstable update call")]
public async Task TestCheckForUpdate()
{
MoneroDaemonUpdateCheckResponse response = await _daemon.CheckForUpdate();
TestUpdateCheckResult(response);
}
// Can download an update
[Fact(Skip = "Non supported by regtest daemon")]
public async Task TestDownloadUpdate()
{
// download to a default path
MoneroDaemonUpdateDownloadResponse response = await _daemon.DownloadUpdate("");
TestUpdateDownloadResult(response, null);
// download to a defined path
string path = "test_download_" + DateTime.Now + ".tar.bz2";
response = await _daemon.DownloadUpdate(path);
TestUpdateDownloadResult(response, path);
// test invalid path
if (response.IsUpdateAvailable == true)
{
try
{
await _daemon.DownloadUpdate("./ohhai/there");
throw new Exception("Should have thrown error");
}
catch (MoneroRpcError e)
{
Assert.NotEqual("Should have thrown error", e.Message);
Assert.Equal(500, e.GetCode()); // TODO monerod: this causes a 500 in daemon rpc
}
}
}
// Can be stopped
[Fact(Skip = "Disabled")]
public async Task TestStop()
{
// stop the daemon
await _daemon.Stop();
// give the daemon time to shut down
GenUtils.WaitFor(TestUtils.SyncPeriodInMs);
// try to interact with the daemon
try
{
await _daemon.GetHeight();
throw new Exception("Should have thrown error");
}
catch (MoneroError e)
{
Assert.NotEqual("Should have thrown error", e.Message);
}
}
#endregion
#region Test Helpers
private static void TestBlockHeader(MoneroBlockHeader? header, bool isFull)
{
Assert.NotNull(header);
Assert.True(header.GetHeight() >= 0);
Assert.True(header.GetMajorVersion() > 0);
Assert.True(header.GetMinorVersion() >= 0);
if (header.GetHeight() == 0)
{
Assert.True(header.GetTimestamp() == 0);
}
else
{
Assert.True(header.GetTimestamp() > 0);
}
Assert.NotNull(header.GetPrevHash());
Assert.NotNull(header.GetNonce());
if (header.GetNonce() == 0)
{
MoneroUtils.Log(0,
"WARNING: header nonce is 0 at height " +
header.GetHeight()); // TODO (monero-project): why is header nonce 0?
}
else
{
Assert.True(header.GetNonce() > 0);
}
Assert.NotNull(header.GetPowHash()); // never seen defined
if (isFull)
{
Assert.True(header.GetSize() > 0);
Assert.True(header.GetDepth() >= 0);
Assert.True(header.GetDifficulty() > 0);
Assert.True(header.GetCumulativeDifficulty() > 0);
Assert.Equal(64, header.GetHash()!.Length);
Assert.Equal(64, header.GetMinerTxHash()!.Length);
Assert.True(header.GetNumTxs() >= 0);
Assert.NotNull(header.GetOrphanStatus());
Assert.NotNull(header.GetReward());
Assert.NotNull(header.GetWeight());
Assert.True(header.GetWeight() > 0);
}
else
{
Assert.Null(header.GetSize());
Assert.Null(header.GetDepth());
Assert.Null(header.GetDifficulty());
Assert.Null(header.GetCumulativeDifficulty());
Assert.Null(header.GetHash());
Assert.Null(header.GetMinerTxHash());
Assert.Null(header.GetNumTxs());
Assert.Null(header.GetOrphanStatus());
Assert.Null(header.GetReward());
Assert.Null(header.GetWeight());
}
}
private static async Task<List<string>> GetConfirmedTxHashes(IMoneroDaemon daemon)
{
const int numTxs = 5;
List<string> txHashes = [];
ulong height = await daemon.GetHeight();
while (txHashes.Count < numTxs && height > 0)
{
MoneroBlock block = await daemon.GetBlockByHeight(--height);
txHashes.AddRange(block.TxHashes);
}
return txHashes;
}
private static void TestBlockTemplate(MoneroBlockTemplate template)
{
Assert.NotNull(template);
Assert.NotNull(template.BlockTemplateBlob);
Assert.NotNull(template.BlockHashingBlob);
Assert.NotNull(template.Difficulty);
Assert.NotNull(template.ExpectedReward);
Assert.NotNull(template.Height);
Assert.NotNull(template.PrevHash);
Assert.NotNull(template.ReservedOffset);
Assert.NotNull(template.SeedHeight);
// regtest daemon has seed height equal to zero
Assert.NotNull(template.SeedHash);
Assert.Equal(0, template.SeedHash.Length);
// next seed hash can be null or initialized // TODO: test circumstances for each
}
// TODO: test block deep copy
private static void TestBlock(MoneroBlock block, TestContext ctx)
{
// test required fields
Assert.NotNull(block);
TestBlockHeader(block, ctx.HeaderIsFull == true);
if (ctx.HasHex == true)
{
Assert.NotNull(block.Hex);
Assert.True(block.Hex!.Length > 1);
}
else
{
Assert.NotNull(block.Hex);
}
if (ctx.HasTxs == true)
{
Assert.NotNull(ctx.TxContext);
foreach (MoneroTx tx in block.Txs!)
{
Assert.True(block.Equals(tx.GetBlock()));
TestTx(tx, ctx.TxContext);
}
}
else
{
Assert.Null(ctx.TxContext);
Assert.Null(block.Txs);
}
}
private static void TestTx(MoneroTx? tx, TestContext? ctx)
{
// check inputs
Assert.NotNull(tx);
Assert.NotNull(ctx);
Assert.NotNull(ctx.IsPruned);
Assert.NotNull(ctx.IsConfirmed);
Assert.NotNull(ctx.FromGetTxPool);
// standard across all txs
Assert.Equal(64, tx.GetHash()!.Length);
if (tx.IsRelayed() == null)
{
Assert.True(tx.InTxPool()); // TODO monerod: add relayed to get_transactions
}
else
{
Assert.NotNull(tx.IsRelayed());
}
Assert.NotNull(tx.IsConfirmed());
Assert.NotNull(tx.InTxPool());
Assert.NotNull(tx.IsMinerTx());
Assert.NotNull(tx.IsDoubleSpendSeen());
Assert.True(tx.GetVersion() >= 0);
Assert.True(tx.GetUnlockTime() >= 0);
Assert.NotNull(tx.GetInputs());
Assert.NotNull(tx.GetOutputs());
Assert.True(tx.GetExtra()!.Length > 0);
TestUtils.TestUnsignedBigInteger(tx.GetFee(), true);
// test presence of output indices
// TODO: change this over to outputs only
if (tx.IsMinerTx() == true)
{
Assert.Null(tx.GetOutputIndices()); // TODO: how to get output indices for miner transactions?
}
if (tx.InTxPool() == true || ctx.FromGetTxPool == true || ctx.HasOutputIndices == false)
{
Assert.Null(tx.GetOutputIndices());
}
else
{
Assert.NotNull(tx.GetOutputIndices());
}
if (tx.GetOutputIndices() != null)
{
Assert.True(tx.GetOutputIndices()!.Count > 0);
}
// test confirmed ctx
if (ctx.IsConfirmed == true)
{
Assert.True(tx.IsConfirmed());
}
if (ctx.IsConfirmed == false)
{
Assert.False(tx.IsConfirmed());
}
// test confirmed
if (tx.IsConfirmed() == true)
{
Assert.NotNull(tx.GetBlock());
Assert.Contains(tx, tx.GetBlock()!.Txs!);
Assert.True(tx.GetBlock()!.GetHeight() > 0);
Assert.Contains(tx, tx.GetBlock()!.Txs!);
Assert.True(tx.GetBlock()!.GetHeight() > 0);
Assert.True(tx.GetBlock()!.GetTimestamp() > 0);
Assert.True(tx.GetRelay());
Assert.True(tx.IsRelayed());
Assert.False(tx.IsFailed());
Assert.False(tx.InTxPool());
Assert.False(tx.IsDoubleSpendSeen());
if (ctx.FromBinaryBlock == true)
{
Assert.Null(tx.GetNumConfirmations());
}
else
{
Assert.True(tx.GetNumConfirmations() > 0);
}
}
else
{
Assert.Null(tx.GetBlock());
Assert.Equal(0, (long)tx.GetNumConfirmations()!);
}
// test in tx pool
if (tx.InTxPool() == true)
{
Assert.False(tx.IsConfirmed());
Assert.False(tx.IsDoubleSpendSeen());
Assert.Null(tx.GetLastFailedHeight());
Assert.Null(tx.GetLastFailedHash());