forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdigidollar_mint_tests.cpp
More file actions
1226 lines (976 loc) · 44.8 KB
/
Copy pathdigidollar_mint_tests.cpp
File metadata and controls
1226 lines (976 loc) · 44.8 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
// Copyright (c) 2024 The DigiByte Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <boost/test/unit_test.hpp>
#include <digidollar/txbuilder.h>
#include <digidollar/scripts.h>
#include <digidollar/validation.h>
#include <consensus/digidollar.h>
#include <kernel/chainparams.h>
#include <chainparams.h>
#include <key.h>
#include <random.h>
#include <script/standard.h>
#include <test/util/setup_common.h>
#include <base58.h>
using namespace DigiDollar;
BOOST_FIXTURE_TEST_SUITE(digidollar_mint_tests, RegTestingSetup)
static constexpr CAmount MIN_DD_MINT_FEE = 10000000; // 0.1 DGB
// Helper function to create a test key
CKey CreateTestKey() {
CKey key;
key.MakeNewKey(true);
return key;
}
// Helper function to create test UTXOs with specific values
std::vector<COutPoint> CreateTestUTXOsWithValues(const std::vector<CAmount>& values) {
std::vector<COutPoint> utxos;
for (size_t i = 0; i < values.size(); ++i) {
uint256 hash;
hash.SetHex("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcd" + std::to_string(i).substr(0,2));
utxos.emplace_back(hash, i);
}
return utxos;
}
// Helper function to create test oracle price (DGB/USD in micro-USD)
// Format: 1,000,000 micro-USD = $1.00
CAmount CreateTestOraclePrice() {
return 50000; // $0.05 per DGB = 50,000 micro-USD
}
void SetCanonicalLock(TxBuilderMintParams& params, int lockDays)
{
const int tierIndex = GetLockTierIndex(LockDaysToBlocks(lockDays), Params().GetDigiDollarParams());
BOOST_REQUIRE_MESSAGE(tierIndex >= 0, "test requested non-canonical lock days: " + std::to_string(lockDays));
params.lockDays = lockDays;
params.lockTier = static_cast<uint32_t>(tierIndex);
}
// Helper function to create mock mint transaction builder
class MockMintTxBuilder : public MintTxBuilder {
private:
std::map<COutPoint, CAmount> m_utxo_values;
public:
MockMintTxBuilder(const CChainParams& params, int height, CAmount price)
: MintTxBuilder(params, height, price) {}
// Override UTXO value lookup for testing
void SetUTXOValue(const COutPoint& outpoint, CAmount value) {
m_utxo_values[outpoint] = value;
}
// Override GetUTXOValue for testing
CAmount GetUTXOValue(const COutPoint& outpoint) const {
auto it = m_utxo_values.find(outpoint);
return (it != m_utxo_values.end()) ? it->second : 100 * COIN; // Default for testing
}
// Override GetUTXOValueVirtual to make SelectCoins work with mocked values
CAmount GetUTXOValueVirtual(const COutPoint& outpoint) const override {
return GetUTXOValue(outpoint);
}
// Expose public methods for testing
using MintTxBuilder::CalculateRequiredCollateral;
};
// ============================================================================
// Basic Mint Creation Tests
// ============================================================================
BOOST_AUTO_TEST_CASE(mint_minimum_amount)
{
// Test minting minimum allowed amount ($100.00 = 10000 cents on mainnet/testnet, $1.00 = 100 cents on regtest)
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// Set up UTXOs with sufficient value
auto utxos = CreateTestUTXOsWithValues({100 * COIN, 200 * COIN});
for (size_t i = 0; i < utxos.size(); ++i) {
builder.SetUTXOValue(utxos[i], (i + 1) * 100 * COIN);
}
TxBuilderMintParams mintParams;
// Use consensus minimum - regtest uses $100.00 minimum (10000 cents)
mintParams.ddAmount = params.GetDigiDollarParams().minMintAmount; // $100.00 in cents
SetCanonicalLock(mintParams, 365); // 1 year
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
if (!result.success) {
std::cout << "ERROR: BuildMintTransaction FAILED: " << result.error << std::endl;
}
BOOST_CHECK(result.success);
BOOST_CHECK(result.error.empty());
BOOST_CHECK(result.tx.vin.size() > 0);
BOOST_CHECK(result.tx.vout.size() >= 3); // Collateral + DD + OP_RETURN outputs
BOOST_CHECK(result.collateralRequired > 0);
BOOST_CHECK(result.totalFees > 0);
// Check transaction version format
BOOST_CHECK(result.tx.IsDigiDollar());
BOOST_CHECK(::GetDigiDollarTxType(CTransaction(result.tx)) == ::DD_TX_MINT);
}
BOOST_AUTO_TEST_CASE(mint_standard_amounts)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// Test standard amounts (regtest max is 100,000 cents = $1,000)
// Test: $1, $10, $100 in cents
std::vector<CAmount> amounts = {100, 1000, 10000}; // $1, $10, $100 in cents
for (CAmount amount : amounts) {
// Provide enough for largest amount ($100 needs ~6000 DGB at 300% ratio)
auto utxos = CreateTestUTXOsWithValues({3000 * COIN, 3000 * COIN, 3000 * COIN});
for (size_t i = 0; i < utxos.size(); ++i) {
builder.SetUTXOValue(utxos[i], 3000 * COIN);
}
TxBuilderMintParams mintParams;
mintParams.ddAmount = amount;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK_MESSAGE(result.success, "Failed for amount: " + std::to_string(amount));
BOOST_CHECK(result.collateralRequired > 0);
// Collateral should scale roughly with DD amount
CAmount expectedCollateral = builder.CalculateRequiredCollateral(amount, 365);
BOOST_CHECK_MESSAGE(result.collateralRequired == expectedCollateral,
"Collateral mismatch for amount: " + std::to_string(amount));
}
}
BOOST_AUTO_TEST_CASE(mint_maximum_amount)
{
// Test minting maximum allowed amount per transaction (regtest: 100,000 cents = $1,000)
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// Create large UTXOs for maximum mint
auto utxos = CreateTestUTXOsWithValues({
10000 * COIN, 20000 * COIN, 30000 * COIN, 40000 * COIN
});
for (size_t i = 0; i < utxos.size(); ++i) {
builder.SetUTXOValue(utxos[i], (i + 1) * 10000 * COIN);
}
TxBuilderMintParams mintParams;
mintParams.ddAmount = 100000; // 100,000 cents = $1,000 (regtest max)
SetCanonicalLock(mintParams, 3650); // 10 years (lowest collateral ratio = 200%)
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
if (!result.success) {
std::cout << "ERROR: BuildMintTransaction FAILED: " << result.error << std::endl;
}
BOOST_CHECK(result.success);
BOOST_CHECK(result.collateralRequired > 0);
// With 10-year lock and $1k mint at 200% ratio and $0.05 DGB (50000 micro-USD):
// Formula: (DD_cents * COIN * ratio * 100) / oracle_micro_usd
// = (100000 * COIN * 200 * 100) / 50000 = 40000 DGB
// Note: May be higher due to DCA multiplier (system health < 200%)
CAmount expectedBase = (static_cast<int64_t>(100000) * COIN * 200 * 100) / price;
// Allow for DCA multiplier up to 2.0x
BOOST_CHECK(result.collateralRequired >= expectedBase);
BOOST_CHECK(result.collateralRequired <= expectedBase * 2); // Max 2x with DCA
}
BOOST_AUTO_TEST_CASE(mint_invalid_amounts)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
auto utxos = CreateTestUTXOsWithValues({100 * COIN});
builder.SetUTXOValue(utxos[0], 100 * COIN);
// Test zero amount
{
TxBuilderMintParams mintParams;
mintParams.ddAmount = 0;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(!result.success);
BOOST_CHECK(!result.error.empty());
}
// Test negative amount
{
TxBuilderMintParams mintParams;
mintParams.ddAmount = -1000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(!result.success);
BOOST_CHECK(!result.error.empty());
}
// Test amount below minimum (consensus minMintAmount is $100.00 = 10000 cents)
{
TxBuilderMintParams mintParams;
mintParams.ddAmount = params.GetDigiDollarParams().minMintAmount - 1; // Just below minimum
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(!result.success);
BOOST_CHECK(result.error.find("Invalid mint parameters") != std::string::npos);
}
// Test amount above maximum (regtest: 100,000 cents = $1,000)
{
TxBuilderMintParams mintParams;
mintParams.ddAmount = 150000; // 150,000 cents = $1,500 (above regtest max)
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(!result.success);
BOOST_CHECK(!result.error.empty());
}
}
// ============================================================================
// Collateral Calculation Tests
// ============================================================================
BOOST_AUTO_TEST_CASE(collateral_all_lock_tiers)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice(); // $0.05 per DGB
MockMintTxBuilder builder(params, height, price);
CAmount ddAmount = 10000; // $100 in cents
// Test all 8 lock tiers with expected collateral ratios
struct LockTier {
int days;
int expectedRatio; // percentage
};
std::vector<LockTier> tiers = {
{0, 1000}, // 1 hour (0 days = 240 blocks): 1000% (testing/onboarding)
{30, 500}, // 30 days: 500%
{90, 400}, // 3 months: 400%
{180, 350}, // 6 months: 350%
{365, 300}, // 1 year: 300%
{1095, 250}, // 3 years: 250%
{1825, 225}, // 5 years: 225%
{2555, 212}, // 7 years: 212%
{3650, 200} // 10 years: 200%
};
for (const auto& tier : tiers) {
CAmount collateral = builder.CalculateRequiredCollateral(ddAmount, tier.days);
// Just verify the collateral amount is positive and reasonable
// The actual calculation is tested against the production code's output
// which is validated by real-world testnet transactions
BOOST_CHECK_MESSAGE(collateral > 0,
"Tier " + std::to_string(tier.days) + " days: collateral must be positive");
// For higher ratios, more collateral should be required
// Tier 0 (1000%) should require more collateral than Tier 4 (300%)
// This verifies the ratio is being applied correctly
if (tier.days > 0) {
CAmount tier0Collateral = builder.CalculateRequiredCollateral(ddAmount, 0);
BOOST_CHECK_MESSAGE(collateral < tier0Collateral,
"Tier " + std::to_string(tier.days) + " days should require less collateral than tier 0");
}
}
}
BOOST_AUTO_TEST_CASE(collateral_calculation_consistency)
{
// Test that collateral calculation is consistent across multiple calls
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
CAmount ddAmount = 10000; // $100
// Multiple calls should return the same result
CAmount collateral1 = builder.CalculateRequiredCollateral(ddAmount, 365);
CAmount collateral2 = builder.CalculateRequiredCollateral(ddAmount, 365);
CAmount collateral3 = builder.CalculateRequiredCollateral(ddAmount, 365);
BOOST_CHECK_EQUAL(collateral1, collateral2);
BOOST_CHECK_EQUAL(collateral2, collateral3);
BOOST_CHECK(collateral1 > 0);
}
BOOST_AUTO_TEST_CASE(collateral_price_dependency)
{
// Test that collateral requirements change with different oracle prices
const CChainParams& params = Params();
int height = 1000;
CAmount ddAmount = 10000; // $100
// Test with different prices (micro-USD format: 1,000,000 = $1.00)
CAmount lowPrice = 25000; // $0.025 per DGB (25,000 micro-USD)
CAmount highPrice = 100000; // $0.10 per DGB (100,000 micro-USD)
MockMintTxBuilder builderLow(params, height, lowPrice);
MockMintTxBuilder builderHigh(params, height, highPrice);
CAmount collateralLow = builderLow.CalculateRequiredCollateral(ddAmount, 365);
CAmount collateralHigh = builderHigh.CalculateRequiredCollateral(ddAmount, 365);
// Higher DGB price should require less DGB collateral for same USD amount
BOOST_CHECK(collateralLow > collateralHigh);
BOOST_CHECK(collateralLow > 0);
BOOST_CHECK(collateralHigh > 0);
}
BOOST_AUTO_TEST_CASE(collateral_insufficient_rejection)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice(); // $0.05 per DGB
MockMintTxBuilder builder(params, height, price);
// Create UTXOs with insufficient value
auto utxos = CreateTestUTXOsWithValues({50 * COIN}); // Only 50 DGB
builder.SetUTXOValue(utxos[0], 50 * COIN);
TxBuilderMintParams mintParams;
// Mint $1000 (max for regtest) - needs 200% * $1000 / $0.05 = 40,000 DGB at minimum (10 year lock)
// With 1 year lock (300%), needs 60,000 DGB
mintParams.ddAmount = 100000; // $1000 in cents (regtest max)
SetCanonicalLock(mintParams, 365); // 1 year = 300% ratio
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(!result.success);
BOOST_CHECK(result.error.find("Insufficient funds") != std::string::npos);
}
// ============================================================================
// P2TR Output Creation Tests
// ============================================================================
BOOST_AUTO_TEST_CASE(p2tr_script_creation_through_transaction)
{
// Test P2TR script creation through successful transaction building
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// $100 at 300% ratio needs ~6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
BOOST_CHECK(result.tx.vout.size() >= 3); // Collateral + DD + OP_RETURN
// First output should be collateral P2TR script (OP_1 + 32 bytes)
BOOST_CHECK(result.tx.vout[0].scriptPubKey.size() == 34);
BOOST_CHECK(result.tx.vout[0].scriptPubKey[0] == OP_1);
BOOST_CHECK(result.tx.vout[0].scriptPubKey[1] == 32);
// Second output should be DigiDollar P2TR script (OP_1 + 32 bytes)
BOOST_CHECK(result.tx.vout[1].scriptPubKey.size() == 34);
BOOST_CHECK(result.tx.vout[1].scriptPubKey[0] == OP_1);
BOOST_CHECK(result.tx.vout[1].scriptPubKey[1] == 32);
}
BOOST_AUTO_TEST_CASE(p2tr_redemption_paths_verification)
{
// Test that mint transactions create proper scripts through the full transaction
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// $100 at 300% ratio needs ~6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
// Verify that scripts are created (specific MAST verification would be in scripts_tests.cpp)
BOOST_CHECK(!result.tx.vout[0].scriptPubKey.empty());
BOOST_CHECK(!result.tx.vout[1].scriptPubKey.empty());
}
// ============================================================================
// Transaction Structure Tests
// ============================================================================
BOOST_AUTO_TEST_CASE(transaction_version_field)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// $100 at 300% ratio needs ~6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
// Check DigiDollar version format
// Version structure: [Type:8][Flags:8][Marker:16]
// Marker is 0x0770 for DigiDollar transactions
BOOST_CHECK(result.tx.IsDigiDollar());
BOOST_CHECK((result.tx.nVersion & 0x0000FFFF) == 0x0770); // Check DigiDollar marker
// Check mint transaction type (type is in bits 24-31)
BOOST_CHECK(::GetDigiDollarTxType(CTransaction(result.tx)) == ::DD_TX_MINT);
}
BOOST_AUTO_TEST_CASE(transaction_input_consumption)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// Provide multiple UTXOs
// $100 at 300% ratio and $0.05/DGB needs ~6000 DGB + fees
auto utxos = CreateTestUTXOsWithValues({3000 * COIN, 3000 * COIN, 1000 * COIN});
for (size_t i = 0; i < utxos.size(); ++i) {
if (i == 0 || i == 1) builder.SetUTXOValue(utxos[i], 3000 * COIN);
else builder.SetUTXOValue(utxos[i], 1000 * COIN);
}
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000; // $100
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
if (!result.success) {
std::cout << "ERROR: BuildMintTransaction FAILED: " << result.error << std::endl;
}
BOOST_CHECK(result.success);
BOOST_CHECK(result.tx.vin.size() > 0);
BOOST_CHECK(result.tx.vin.size() <= utxos.size());
// Verify inputs reference provided UTXOs
for (const auto& input : result.tx.vin) {
bool found = false;
for (const auto& utxo : utxos) {
if (input.prevout == utxo) {
found = true;
break;
}
}
BOOST_CHECK(found);
}
}
BOOST_AUTO_TEST_CASE(transaction_output_creation)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// $100 at 300% ratio needs ~6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
BOOST_CHECK(result.tx.vout.size() >= 3); // Collateral + DD + OP_RETURN + possible change
// First output should be collateral (positive DGB value)
BOOST_CHECK(result.tx.vout[0].nValue > 0);
BOOST_CHECK(result.tx.vout[0].nValue == result.collateralRequired);
const auto& collateralScript = result.tx.vout[0].scriptPubKey;
BOOST_CHECK(collateralScript.size() == 34 && collateralScript[0] == OP_1 && collateralScript[1] == 32);
// Second output should be DigiDollar (zero DGB value)
BOOST_CHECK(result.tx.vout[1].nValue == 0);
const auto& ddScript = result.tx.vout[1].scriptPubKey;
BOOST_CHECK(ddScript.size() == 34 && ddScript[0] == OP_1 && ddScript[1] == 32);
// Third output should be OP_RETURN (zero DGB value)
BOOST_CHECK(result.tx.vout[2].nValue == 0);
BOOST_CHECK(result.tx.vout[2].scriptPubKey[0] == OP_RETURN);
// If change exists, it should be in a fourth output
if (result.tx.vout.size() > 3) {
BOOST_CHECK(result.tx.vout[3].nValue > 0); // Change has positive value
}
}
BOOST_AUTO_TEST_CASE(transaction_fee_calculation)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// $100 at 300% needs ~6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
// Low fee-rate callers should be floored to the DD minimum; a high
// enough rate can still pay above the floor.
std::vector<CAmount> feeRates = {100000, 500000, 50000000}; // sat/kB
CAmount prevFee = 0;
for (size_t i = 0; i < feeRates.size(); ++i) {
CAmount feeRate = feeRates[i];
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = feeRate;
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
BOOST_CHECK_GE(result.totalFees, MIN_DD_MINT_FEE);
if (i < 2) {
BOOST_CHECK_EQUAL(result.totalFees, MIN_DD_MINT_FEE);
} else {
BOOST_CHECK_GT(result.totalFees, MIN_DD_MINT_FEE);
}
BOOST_CHECK_GE(result.totalFees, prevFee);
prevFee = result.totalFees;
}
}
BOOST_AUTO_TEST_CASE(mint_fee_floor_applies_to_low_fee_rate_callers)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 500000; // Qt mint path rate, below the DD fee floor.
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_REQUIRE_MESSAGE(result.success, result.error);
BOOST_CHECK_GE(result.totalFees, MIN_DD_MINT_FEE);
}
BOOST_AUTO_TEST_CASE(transaction_signing_preparation)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// $100 at 300% ratio needs ~6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
// Transaction should be ready for signing
BOOST_CHECK(!result.tx.vin.empty());
BOOST_CHECK(!result.tx.vout.empty());
// All inputs should have empty signatures (unsigned)
for (const auto& input : result.tx.vin) {
BOOST_CHECK(input.scriptSig.empty());
BOOST_CHECK(input.scriptWitness.IsNull());
}
}
// ============================================================================
// Edge Cases and Error Handling Tests
// ============================================================================
BOOST_AUTO_TEST_CASE(edge_case_exact_collateral_no_change)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// Calculate exact collateral needed
CAmount ddAmount = 10000;
int lockDays = 365;
CAmount requiredCollateral = builder.CalculateRequiredCollateral(ddAmount, lockDays);
// Fund the exact collateral plus the DD mint fee floor. Low fee-rate
// callers are still charged at least this amount.
CAmount estimatedFees = MIN_DD_MINT_FEE;
// Provide exact amount needed (collateral + fees)
auto utxos = CreateTestUTXOsWithValues({requiredCollateral + estimatedFees});
builder.SetUTXOValue(utxos[0], requiredCollateral + estimatedFees);
TxBuilderMintParams mintParams;
mintParams.ddAmount = ddAmount;
SetCanonicalLock(mintParams, lockDays);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
// Should have 3-4 outputs (collateral + DD + OP_RETURN + optional change)
// Getting exact fee estimate is difficult, so we allow small change
BOOST_CHECK(result.tx.vout.size() >= 3 && result.tx.vout.size() <= 4);
}
BOOST_AUTO_TEST_CASE(edge_case_multiple_inputs)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// Provide many small UTXOs that need to be combined
// With $100 DD at 300% ratio and $0.05/DGB (50000 micro-USD):
// Formula: (10000 * COIN * 300 * 100) / 50000 = 6000 DGB
// Use 1000 DGB UTXOs so multiple inputs are required
std::vector<CAmount> values;
for (int i = 0; i < 10; ++i) {
values.push_back(1000 * COIN); // 1000 DGB each = 10000 DGB total
}
auto utxos = CreateTestUTXOsWithValues(values);
for (size_t i = 0; i < utxos.size(); ++i) {
builder.SetUTXOValue(utxos[i], values[i]);
}
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000; // $100 (needs 6000 DGB at 300% ratio with $0.05/DGB)
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
BOOST_CHECK(result.tx.vin.size() > 1); // Multiple inputs used
BOOST_CHECK(result.tx.vin.size() <= 10); // Not more than provided
}
BOOST_AUTO_TEST_CASE(edge_case_invalid_lock_times)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
auto utxos = CreateTestUTXOsWithValues({1000 * COIN});
builder.SetUTXOValue(utxos[0], 1000 * COIN);
// Test various invalid lock times
// Valid: 0 (1 hour testing), 30-3650 days
// Invalid: 1-29, >3650
std::vector<int> invalidLockTimes = {1, 10, 29, 3651, 5000}; // Too short or too long
for (int lockDays : invalidLockTimes) {
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
mintParams.lockDays = lockDays;
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK_MESSAGE(!result.success, "Lock time " + std::to_string(lockDays) + " should be invalid");
BOOST_CHECK(!result.error.empty());
}
}
BOOST_AUTO_TEST_CASE(edge_case_oracle_price_unavailable)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = 0; // No oracle price available
MockMintTxBuilder builder(params, height, price);
// $100 at 300% ratio needs ~6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
// Should fail gracefully when oracle price is zero/unavailable
BOOST_CHECK(!result.success);
BOOST_CHECK(!result.error.empty());
}
BOOST_AUTO_TEST_CASE(edge_case_extreme_fee_rates)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
auto utxos = CreateTestUTXOsWithValues({10000 * COIN});
builder.SetUTXOValue(utxos[0], 10000 * COIN);
// Test extremely high fee rate (max is 5M sat/kB)
{
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 200000000; // 200M sat/kB (above max of 100M sat/kB)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(!result.success);
BOOST_CHECK(!result.error.empty());
}
// Test zero fee rate
{
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 0;
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(!result.success);
BOOST_CHECK(!result.error.empty());
}
}
BOOST_AUTO_TEST_CASE(edge_case_invalid_keys)
{
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// $100 at 300% ratio needs ~6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000;
SetCanonicalLock(mintParams, 365);
// mintParams.ownerKey not set (invalid)
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(!result.success);
BOOST_CHECK(!result.error.empty());
}
// ============================================================================
// Integration Tests
// ============================================================================
BOOST_AUTO_TEST_CASE(integration_complete_mint_flow)
{
// Test complete mint transaction flow with realistic parameters
const CChainParams& params = Params();
int height = 1000;
// Oracle price in micro-USD format: 50,000 = $0.05 per DGB
CAmount price = 50000; // 50,000 micro-USD = $0.05 per DGB
MockMintTxBuilder builder(params, height, price);
// Set up realistic UTXOs
// $500 at 300% ratio needs 30,000 DGB at $0.05/DGB price
auto utxos = CreateTestUTXOsWithValues({
15000 * COIN, // 15000 DGB
15000 * COIN, // 15000 DGB
5000 * COIN // 5000 DGB (for fees)
});
for (size_t i = 0; i < utxos.size(); ++i) {
builder.SetUTXOValue(utxos[i], (i == 0 ? 15000 : (i == 1 ? 15000 : 5000)) * COIN);
}
// Mint $500 worth of DigiDollars with 1-year lock (within regtest max of $1000)
TxBuilderMintParams mintParams;
mintParams.ddAmount = 50000; // $500 in cents
SetCanonicalLock(mintParams, 365); // 1 year = 300% collateral ratio
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 200000; // 200,000 sat/kB (= 200 sat/vB)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
BOOST_CHECK(result.error.empty());
// Verify transaction structure
BOOST_CHECK(result.tx.IsDigiDollar());
BOOST_CHECK(::GetDigiDollarTxType(CTransaction(result.tx)) == ::DD_TX_MINT);
BOOST_CHECK(result.tx.vin.size() > 0);
BOOST_CHECK(result.tx.vout.size() >= 3); // Collateral + DD + OP_RETURN
// Verify collateral calculation
// $500 at 300% collateral = $1500 worth of DGB
// At $0.05/DGB = 30,000 DGB = 30,000 * COIN sats
CAmount expectedCollateral = 30000 * COIN;
// Account for 1% safety margin added in Bug #16 fix
CAmount expectedWithMargin = (expectedCollateral * 101) / 100;
BOOST_CHECK(std::abs(result.collateralRequired - expectedWithMargin) < COIN);
// Verify outputs
BOOST_CHECK(result.tx.vout[0].nValue == result.collateralRequired); // Collateral
BOOST_CHECK(result.tx.vout[1].nValue == 0); // DigiDollar output
BOOST_CHECK(result.tx.vout[2].nValue == 0); // OP_RETURN
const auto& script0 = result.tx.vout[0].scriptPubKey;
const auto& script1 = result.tx.vout[1].scriptPubKey;
BOOST_CHECK(script0.size() == 34 && script0[0] == OP_1 && script0[1] == 32);
BOOST_CHECK(script1.size() == 34 && script1[0] == OP_1 && script1[1] == 32);
BOOST_CHECK(result.tx.vout[2].scriptPubKey[0] == OP_RETURN);
// Verify the DD mint fee floor is enforced.
BOOST_CHECK_GE(result.totalFees, MIN_DD_MINT_FEE);
}
// ============================================================================
// DCA Integration Tests
// ============================================================================
BOOST_AUTO_TEST_CASE(mint_with_dca_healthy_system)
{
// Test minting with healthy system (no DCA adjustment)
const CChainParams& params = Params();
int height = 1000;
CAmount price = CreateTestOraclePrice();
MockMintTxBuilder builder(params, height, price);
// Create mock system state for healthy system (200% collateralization)
CAmount totalCollateral = 100000000 * COIN; // 100M DGB
CAmount totalDD = 10000000; // 10M DD ($100k)
// Health = (100M * $0.05) / $100k * 100 = $5M / $100k * 100 = 500%
// $100 DD at 300% ratio with $0.05/DGB (50000 micro-USD) needs ~6000 DGB
// Formula: (10000 * COIN * 300 * 100) / 50000 = 6000 DGB
auto utxos = CreateTestUTXOsWithValues({7000 * COIN});
builder.SetUTXOValue(utxos[0], 7000 * COIN);
TxBuilderMintParams mintParams;
mintParams.ddAmount = 10000; // $100
SetCanonicalLock(mintParams, 365); // 1 year (300% base ratio)
mintParams.ownerKey = CreateTestKey();
mintParams.feeRate = 100000; // 100,000 sat/kB (minimum for DigiByte)
mintParams.utxos = utxos;
TxBuilderResult result = builder.BuildMintTransaction(mintParams);
BOOST_CHECK(result.success);
// With healthy system, DCA multiplier should be 1.0x (no adjustment)
// Formula: (DD_cents * COIN * ratio * 100) / oracle_micro_usd
// Expected: (10000 * COIN * 300 * 100) / 50000 = 6000 DGB
CAmount expectedBaseCollateral = (static_cast<int64_t>(10000) * COIN * 300 * 100) / price;
// Account for 1% safety margin added in Bug #16 fix
CAmount expectedWithMargin = (expectedBaseCollateral * 101) / 100;
BOOST_CHECK(std::abs(result.collateralRequired - expectedWithMargin) < COIN);
}