forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdigidollar_change_tests.cpp
More file actions
467 lines (379 loc) · 16.5 KB
/
Copy pathdigidollar_change_tests.cpp
File metadata and controls
467 lines (379 loc) · 16.5 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
// 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 <test/util/setup_common.h>
#include <test/util/random.h>
#include <digidollar/txbuilder.h>
#include <digidollar/validation.h>
#include <digidollar/digidollar.h>
#include <consensus/digidollar.h>
#include <primitives/transaction.h>
#include <script/script.h>
#include <key.h>
#include <util/strencodings.h>
#include <validation.h>
#include <boost/test/unit_test.hpp>
#include <map>
using namespace DigiDollar;
// External mock UTXO stores (shared with transfer tests)
extern std::map<COutPoint, CAmount> g_mockDDUTXOs;
extern std::map<COutPoint, CAmount> g_mockDGBUTXOs;
/**
* Test-specific TransferTxBuilder that uses mock UTXO stores
*/
class MockTransferTxBuilder : public TransferTxBuilder {
public:
using TransferTxBuilder::TransferTxBuilder;
protected:
CAmount GetDDFromUTXO(const COutPoint& outpoint) const override {
auto it = g_mockDDUTXOs.find(outpoint);
if (it != g_mockDDUTXOs.end()) {
return it->second;
}
return 0;
}
CAmount GetDGBFromUTXO(const COutPoint& outpoint) const override {
auto it = g_mockDGBUTXOs.find(outpoint);
if (it != g_mockDGBUTXOs.end()) {
return it->second;
}
return 100 * COIN; // Default for testing
}
};
BOOST_FIXTURE_TEST_SUITE(digidollar_change_tests, TestingSetup)
/**
* Test fixture for DigiDollar change output tests
*/
struct DDChangeTestFixture : public TestingSetup {
const CChainParams& chainParams;
CKey senderKey;
CKey recipientKey;
int currentHeight;
CAmount oraclePrice;
DDChangeTestFixture() : chainParams(m_node.chainman->GetParams()) {
senderKey.MakeNewKey(true);
recipientKey.MakeNewKey(true);
currentHeight = 100000;
oraclePrice = 2500; // $25.00 per DGB
// Clear mock UTXO stores
g_mockDDUTXOs.clear();
g_mockDGBUTXOs.clear();
}
COutPoint CreateMockDDUTXO(CAmount ddAmount) {
COutPoint outpoint(InsecureRand256(), 0);
g_mockDDUTXOs[outpoint] = ddAmount;
return outpoint;
}
COutPoint CreateMockDGBUTXO(CAmount dgbAmount) {
COutPoint outpoint(InsecureRand256(), 1);
g_mockDGBUTXOs[outpoint] = dgbAmount;
return outpoint;
}
std::string CreateDDAddress(const CPubKey& pubkey) {
XOnlyPubKey xonly(pubkey);
WitnessV1Taproot dest(xonly);
return EncodeDigiDollarAddress(dest, chainParams);
}
/**
* Extract DD amounts from OP_RETURN metadata output
* Transfer format: OP_RETURN <"DD"> <txType=2> <amount1> <amount2> ... <amountN>
*/
bool ExtractDDAmountsFromOpReturn(const CScript& script, std::vector<CAmount>& amounts) {
amounts.clear();
auto pc = script.begin();
opcodetype opcode;
std::vector<unsigned char> data;
// Check for OP_RETURN
if (!script.GetOp(pc, opcode, data) || opcode != OP_RETURN) {
return false;
}
// Get "DD" marker
if (!script.GetOp(pc, opcode, data) || data.size() != 2 || data[0] != 'D' || data[1] != 'D') {
return false;
}
// Get transaction type (should be 2 for TRANSFER)
if (!script.GetOp(pc, opcode, data)) {
return false;
}
// Extract all remaining amounts
while (script.GetOp(pc, opcode, data)) {
try {
CScriptNum scriptNum(data, false);
CAmount amount = scriptNum.GetInt64();
if (amount > 0) {
amounts.push_back(amount);
}
} catch (const scriptnum_error&) {
// Skip invalid data
continue;
}
}
return !amounts.empty();
}
};
// =============================================================================
// Phase 2.5 - Change Output Creation Tests
// =============================================================================
BOOST_FIXTURE_TEST_CASE(test_dd_change_output, DDChangeTestFixture)
{
// Arrange: Select 1000 DD, send 600 DD, expect 400 DD change
std::string recipientAddr = CreateDDAddress(recipientKey.GetPubKey());
TxBuilderTransferParams params;
params.recipients = {{recipientAddr, 60000}}; // Send 600 DD ($600.00)
params.feeRate = 100000; // 100,000 sat/kB
params.spenderKey = senderKey;
params.ddUtxos.push_back(CreateMockDDUTXO(100000)); // 1000 DD ($1000.00)
params.feeUtxos.push_back(CreateMockDGBUTXO(20000000)); // 0.2 DGB for 0.1 DGB min fee + change
MockTransferTxBuilder builder(chainParams, currentHeight, oraclePrice);
// Act
TxBuilderResult result = builder.BuildTransferTransaction(params);
// Assert
BOOST_REQUIRE_EQUAL(result.success, true);
BOOST_CHECK_MESSAGE(result.error.empty(), "Unexpected error: " << result.error);
// Find OP_RETURN output and extract DD amounts
std::vector<CAmount> ddAmounts;
bool found_opreturn = false;
for (const auto& output : result.tx.vout) {
if (output.scriptPubKey.size() > 0 && output.scriptPubKey[0] == OP_RETURN) {
found_opreturn = ExtractDDAmountsFromOpReturn(output.scriptPubKey, ddAmounts);
break;
}
}
BOOST_REQUIRE_MESSAGE(found_opreturn, "No OP_RETURN found in transaction");
// Debug: Count P2TR DD outputs
int dd_p2tr_count = 0;
for (const auto& output : result.tx.vout) {
if (output.nValue == 0 && output.scriptPubKey.size() > 0 && output.scriptPubKey[0] == 0x51) {
dd_p2tr_count++;
}
}
if (ddAmounts.size() != dd_p2tr_count) {
BOOST_TEST_MESSAGE("WARNING: OP_RETURN amounts (" << ddAmounts.size() << ") != P2TR outputs (" << dd_p2tr_count << ")");
}
// Should have 2 DD amounts in OP_RETURN: recipient + change
BOOST_REQUIRE_EQUAL(ddAmounts.size(), 2);
// Verify total DD is conserved (1000 DD)
CAmount totalDD = 0;
for (CAmount amount : ddAmounts) {
totalDD += amount;
}
BOOST_CHECK_EQUAL(totalDD, 100000); // 1000 DD
// Verify we have the recipient amount and change amount
std::sort(ddAmounts.begin(), ddAmounts.end());
BOOST_CHECK_EQUAL(ddAmounts[0], 40000); // 400 DD change
BOOST_CHECK_EQUAL(ddAmounts[1], 60000); // 600 DD recipient
}
BOOST_FIXTURE_TEST_CASE(test_dgb_change_output, DDChangeTestFixture)
{
// Arrange: Use large DGB UTXO for fees, expect DGB change
std::string recipientAddr = CreateDDAddress(recipientKey.GetPubKey());
TxBuilderTransferParams params;
params.recipients = {{recipientAddr, 10000}}; // Send 100 DD ($100.00)
params.feeRate = 100000; // 100,000 sat/kB
params.spenderKey = senderKey;
params.ddUtxos.push_back(CreateMockDDUTXO(10000)); // Exact DD amount
params.feeUtxos.push_back(CreateMockDGBUTXO(20000000)); // 0.2 DGB (enough for 0.1 min fee + change)
MockTransferTxBuilder builder(chainParams, currentHeight, oraclePrice);
// Act
TxBuilderResult result = builder.BuildTransferTransaction(params);
// Assert
BOOST_CHECK_EQUAL(result.success, true);
// Count DGB outputs (those with nValue > 0)
std::vector<CAmount> dgbAmounts;
for (const auto& output : result.tx.vout) {
if (output.nValue > 0) {
dgbAmounts.push_back(output.nValue);
}
}
// Should have at least 1 DGB change output
BOOST_CHECK_GE(dgbAmounts.size(), 1);
// The DGB change should be significantly less than the input (most went to fees or dust)
CAmount totalDGBOut = 0;
for (CAmount amount : dgbAmounts) {
totalDGBOut += amount;
}
// Change should be less than input minus minimum fee
BOOST_CHECK_LT(totalDGBOut, 20000000); // Less than the 0.2 DGB input
BOOST_CHECK_GT(totalDGBOut, 0); // But greater than zero (we should have change)
// Should be approximately input - min_fee (0.2 - 0.1 = 0.1 DGB)
BOOST_CHECK_GT(totalDGBOut, 9000000); // At least 0.09 DGB change
BOOST_CHECK_LT(totalDGBOut, 11000000); // At most 0.11 DGB change
}
BOOST_FIXTURE_TEST_CASE(test_no_dd_change_exact_amount, DDChangeTestFixture)
{
// Arrange: Exact DD amount match, no change needed
std::string recipientAddr = CreateDDAddress(recipientKey.GetPubKey());
TxBuilderTransferParams params;
params.recipients = {{recipientAddr, 50000}}; // Send 500 DD ($500.00)
params.feeRate = 100000; // 100,000 sat/kB
params.spenderKey = senderKey;
params.ddUtxos.push_back(CreateMockDDUTXO(50000)); // Exact amount
params.feeUtxos.push_back(CreateMockDGBUTXO(20000000));
MockTransferTxBuilder builder(chainParams, currentHeight, oraclePrice);
// Act
TxBuilderResult result = builder.BuildTransferTransaction(params);
// Assert
BOOST_REQUIRE_EQUAL(result.success, true);
BOOST_CHECK_MESSAGE(result.error.empty(), "Unexpected error: " << result.error);
// Find OP_RETURN and extract DD amounts
std::vector<CAmount> ddAmounts;
bool found_opreturn = false;
for (const auto& output : result.tx.vout) {
if (output.scriptPubKey.size() > 0 && output.scriptPubKey[0] == OP_RETURN) {
found_opreturn = ExtractDDAmountsFromOpReturn(output.scriptPubKey, ddAmounts);
break;
}
}
BOOST_REQUIRE_MESSAGE(found_opreturn, "No OP_RETURN found in transaction");
// Should have exactly 1 DD amount in OP_RETURN (recipient only, no change)
BOOST_REQUIRE_EQUAL(ddAmounts.size(), 1);
BOOST_CHECK_EQUAL(ddAmounts[0], 50000);
}
BOOST_FIXTURE_TEST_CASE(test_dd_change_below_dust, DDChangeTestFixture)
{
// Arrange: DD change would be below the $1 minimum send threshold.
// Unlike DGB dust, DD has real dollar value — sub-$1 change MUST be
// preserved as a change output, not silently dropped. (Bug #27 fix)
std::string recipientAddr = CreateDDAddress(recipientKey.GetPubKey());
const auto& ddParams = chainParams.GetDigiDollarParams();
CAmount minOutput = DigiDollar::GetMinimumDDOutput(ddParams); // 100 cents = $1
// Send 100 DD minus 99 cents → leaves 99 cents change (sub-$1)
CAmount totalDD = 10000; // 100 DD ($100.00)
CAmount sendAmount = totalDD - (minOutput - 1); // 10000 - 99 = 9901 cents ($99.01)
CAmount expectedChange = minOutput - 1; // 99 cents ($0.99)
TxBuilderTransferParams params;
params.recipients = {{recipientAddr, sendAmount}};
params.feeRate = 100000;
params.spenderKey = senderKey;
params.ddUtxos.push_back(CreateMockDDUTXO(totalDD));
params.feeUtxos.push_back(CreateMockDGBUTXO(20000000));
MockTransferTxBuilder builder(chainParams, currentHeight, oraclePrice);
// Act
TxBuilderResult result = builder.BuildTransferTransaction(params);
// Assert: Transaction succeeds
BOOST_REQUIRE_EQUAL(result.success, true);
BOOST_CHECK_MESSAGE(result.error.empty(), "Unexpected error: " << result.error);
// Extract DD amounts from OP_RETURN
std::vector<CAmount> ddAmounts;
bool found_opreturn = false;
for (const auto& output : result.tx.vout) {
if (output.scriptPubKey.size() > 0 && output.scriptPubKey[0] == OP_RETURN) {
found_opreturn = ExtractDDAmountsFromOpReturn(output.scriptPubKey, ddAmounts);
break;
}
}
BOOST_REQUIRE_MESSAGE(found_opreturn, "No OP_RETURN found in transaction");
// Must have 2 DD amounts: recipient + sub-$1 change (NOT dropped as dust)
BOOST_REQUIRE_EQUAL(ddAmounts.size(), 2);
// Total DD must be perfectly conserved
CAmount totalDDOut = 0;
for (CAmount amount : ddAmounts) {
totalDDOut += amount;
}
BOOST_CHECK_EQUAL(totalDDOut, totalDD);
// Verify exact amounts
std::sort(ddAmounts.begin(), ddAmounts.end());
BOOST_CHECK_EQUAL(ddAmounts[0], expectedChange); // 99 cents change
BOOST_CHECK_EQUAL(ddAmounts[1], sendAmount); // 9901 cents recipient
}
/**
* Bug #27 regression test: Many small DD-UTXOs where multi-input aggregation
* produces sub-$1 change. This is the exact scenario reported by shenger.
*
* Previously, the builder silently dropped sub-$1 DD change and excluded it
* from the OP_RETURN. The validator then saw inputDD != outputDD and rejected
* the transaction with "transfer-dd-conservation-violation, DD not conserved."
*/
BOOST_FIXTURE_TEST_CASE(test_bug27_multi_input_dd_conservation, DDChangeTestFixture)
{
std::string recipientAddr = CreateDDAddress(recipientKey.GetPubKey());
// Simulate shenger's wallet: 20 small DD-UTXOs of $5 each = $100 total
const int numUtxos = 20;
const CAmount perUtxo = 500; // 500 cents = $5.00 each
CAmount totalDD = numUtxos * perUtxo; // 10000 cents = $100.00
TxBuilderTransferParams params;
// Send $99.50 — leaves $0.50 change (below old $1 dust threshold)
params.recipients = {{recipientAddr, 9950}}; // $99.50
params.feeRate = 100000;
params.spenderKey = senderKey;
for (int i = 0; i < numUtxos; i++) {
params.ddUtxos.push_back(CreateMockDDUTXO(perUtxo));
}
params.feeUtxos.push_back(CreateMockDGBUTXO(20000000));
MockTransferTxBuilder builder(chainParams, currentHeight, oraclePrice);
// Act
TxBuilderResult result = builder.BuildTransferTransaction(params);
// Assert: Must succeed — this was the exact failure scenario
BOOST_REQUIRE_EQUAL(result.success, true);
BOOST_CHECK_MESSAGE(result.error.empty(), "Bug #27 regression: " << result.error);
// Extract DD amounts from OP_RETURN
std::vector<CAmount> ddAmounts;
bool found_opreturn = false;
for (const auto& output : result.tx.vout) {
if (output.scriptPubKey.size() > 0 && output.scriptPubKey[0] == OP_RETURN) {
found_opreturn = ExtractDDAmountsFromOpReturn(output.scriptPubKey, ddAmounts);
break;
}
}
BOOST_REQUIRE_MESSAGE(found_opreturn, "No OP_RETURN found in transaction");
// Must have 2 amounts: $99.50 recipient + $0.50 change
BOOST_REQUIRE_EQUAL(ddAmounts.size(), 2);
// CRITICAL: Total DD must be perfectly conserved — not one cent lost
CAmount totalDDOut = 0;
for (CAmount amount : ddAmounts) {
totalDDOut += amount;
}
BOOST_CHECK_EQUAL(totalDDOut, totalDD); // 10000 == 10000
// Verify exact amounts
std::sort(ddAmounts.begin(), ddAmounts.end());
BOOST_CHECK_EQUAL(ddAmounts[0], 50); // 50 cents ($0.50) change — was silently dropped before
BOOST_CHECK_EQUAL(ddAmounts[1], 9950); // 9950 cents ($99.50) recipient
// Count P2TR DD outputs (nValue==0, starts with OP_1)
int dd_outputs = 0;
for (const auto& output : result.tx.vout) {
if (output.nValue == 0 && output.scriptPubKey.size() > 1 && output.scriptPubKey[0] == 0x51) {
dd_outputs++;
}
}
// Must have 2 DD P2TR outputs: recipient + change (change was missing before)
BOOST_CHECK_EQUAL(dd_outputs, 2);
}
/**
* Verify that single-cent DD change is preserved.
* Edge case: send $99.99 from $100.00 — $0.01 change must survive.
*/
BOOST_FIXTURE_TEST_CASE(test_single_cent_dd_change_preserved, DDChangeTestFixture)
{
std::string recipientAddr = CreateDDAddress(recipientKey.GetPubKey());
TxBuilderTransferParams params;
params.recipients = {{recipientAddr, 9999}}; // $99.99
params.feeRate = 100000;
params.spenderKey = senderKey;
params.ddUtxos.push_back(CreateMockDDUTXO(10000)); // $100.00
params.feeUtxos.push_back(CreateMockDGBUTXO(20000000));
MockTransferTxBuilder builder(chainParams, currentHeight, oraclePrice);
TxBuilderResult result = builder.BuildTransferTransaction(params);
BOOST_REQUIRE_EQUAL(result.success, true);
BOOST_CHECK_MESSAGE(result.error.empty(), "Unexpected error: " << result.error);
// Extract DD amounts from OP_RETURN
std::vector<CAmount> ddAmounts;
bool found_opreturn = false;
for (const auto& output : result.tx.vout) {
if (output.scriptPubKey.size() > 0 && output.scriptPubKey[0] == OP_RETURN) {
found_opreturn = ExtractDDAmountsFromOpReturn(output.scriptPubKey, ddAmounts);
break;
}
}
BOOST_REQUIRE_MESSAGE(found_opreturn, "No OP_RETURN found in transaction");
// Must have 2 amounts: $99.99 + $0.01 change
BOOST_REQUIRE_EQUAL(ddAmounts.size(), 2);
CAmount totalDDOut = 0;
for (CAmount amount : ddAmounts) {
totalDDOut += amount;
}
BOOST_CHECK_EQUAL(totalDDOut, 10000); // Perfect conservation
std::sort(ddAmounts.begin(), ddAmounts.end());
BOOST_CHECK_EQUAL(ddAmounts[0], 1); // 1 cent change — the absolute minimum
BOOST_CHECK_EQUAL(ddAmounts[1], 9999); // $99.99 recipient
}
BOOST_AUTO_TEST_SUITE_END()