forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdigidollar_restore_tests.cpp
More file actions
423 lines (336 loc) · 15.4 KB
/
Copy pathdigidollar_restore_tests.cpp
File metadata and controls
423 lines (336 loc) · 15.4 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
// 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 <wallet/digidollarwallet.h>
#include <test/util/setup_common.h>
#include <primitives/transaction.h>
#include <script/script.h>
#include <script/interpreter.h>
#include <consensus/digidollar.h>
#include <key.h>
#include <pubkey.h>
#include <random.h>
using namespace DigiDollar;
BOOST_FIXTURE_TEST_SUITE(digidollar_restore_tests, TestingSetup)
// Helper function to create a test key
static CKey CreateTestKey() {
CKey key;
key.MakeNewKey(true);
return key;
}
// Helper function to create a mock mint transaction with OP_RETURN (v2 format with tier)
static CMutableTransaction CreateMockMintTx(CAmount dd_amount, int64_t unlock_height, uint32_t lock_tier) {
CMutableTransaction mtx;
mtx.SetDigiDollarType(::DD_TX_MINT);
// Add a dummy input
mtx.vin.resize(1);
mtx.vin[0].prevout = COutPoint(uint256S("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"), 0);
// vout[0]: P2TR Collateral Lock (nValue = dgb_collateral in satoshis)
// Using a simple P2TR output for testing
CKey ownerKey = CreateTestKey();
XOnlyPubKey xonly(ownerKey.GetPubKey());
WitnessV1Taproot taproot_dest(xonly);
CScript collateralScript = GetScriptForDestination(taproot_dest);
mtx.vout.push_back(CTxOut(100 * COIN, collateralScript));
// vout[1]: P2TR DD Token (nValue = 0, DD amount in OP_RETURN)
CScript ddScript = GetScriptForDestination(taproot_dest);
mtx.vout.push_back(CTxOut(0, ddScript));
// vout[2]: OP_RETURN Metadata (v2 format with explicit tier)
// Format: OP_RETURN <"DD"> <txType> <ddAmount> <lockHeight> <lockTier>
CScript metadataScript = CScript() << OP_RETURN
<< std::vector<unsigned char>{'D', 'D'}
<< CScriptNum(1) // 1 = MINT transaction
<< CScriptNum(dd_amount) // DD amount in cents
<< CScriptNum(unlock_height) // Lock height in blocks
<< CScriptNum(lock_tier); // Lock tier (0-8)
mtx.vout.push_back(CTxOut(0, metadataScript));
return mtx;
}
// ============================================================================
// Test 1: Extract DD amount from OP_RETURN metadata
// ============================================================================
BOOST_AUTO_TEST_CASE(extract_dd_amount_from_opreturn)
{
// Test extracting DD amount from a mint transaction OP_RETURN
CAmount expected_amount = 10000; // $100.00 in cents
int64_t unlock_height = 100000;
uint32_t lock_tier = 1; // 30 days
CMutableTransaction mtx = CreateMockMintTx(expected_amount, unlock_height, lock_tier);
CTransaction tx(mtx);
// Test extraction
CAmount extracted_amount = 0;
bool result = DigiDollarWallet::ExtractDDAmountFromOpReturn(tx, extracted_amount);
BOOST_CHECK(result);
BOOST_CHECK_EQUAL(extracted_amount, expected_amount);
}
BOOST_AUTO_TEST_CASE(extract_dd_amount_invalid_tx)
{
// Test with a transaction that has no OP_RETURN
CMutableTransaction mtx;
mtx.nVersion = 2;
mtx.vin.resize(1);
mtx.vin[0].prevout = COutPoint(uint256S("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"), 0);
// Just add a normal output, no OP_RETURN
CScript normalScript = CScript() << OP_TRUE;
mtx.vout.push_back(CTxOut(100 * COIN, normalScript));
CTransaction tx(mtx);
CAmount extracted_amount = 0;
bool result = DigiDollarWallet::ExtractDDAmountFromOpReturn(tx, extracted_amount);
BOOST_CHECK(!result);
BOOST_CHECK_EQUAL(extracted_amount, 0);
}
BOOST_AUTO_TEST_CASE(extract_dd_amount_wrong_marker)
{
// Test with wrong marker (not "DD")
CMutableTransaction mtx;
mtx.nVersion = 2;
mtx.vin.resize(1);
mtx.vin[0].prevout = COutPoint(uint256S("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"), 0);
CScript badOpReturn = CScript() << OP_RETURN
<< std::vector<unsigned char>{'B', 'T'}
<< CScriptNum(1)
<< CScriptNum(10000);
mtx.vout.push_back(CTxOut(0, badOpReturn));
CTransaction tx(mtx);
CAmount extracted_amount = 0;
bool result = DigiDollarWallet::ExtractDDAmountFromOpReturn(tx, extracted_amount);
BOOST_CHECK(!result);
}
// ============================================================================
// Test 2: Extract unlock height from OP_RETURN metadata
// ============================================================================
BOOST_AUTO_TEST_CASE(extract_unlock_height_from_opreturn)
{
// Test extracting unlock height from a mint transaction OP_RETURN
CAmount dd_amount = 10000; // $100.00 in cents
int64_t expected_unlock_height = 518640; // 90 days from height 240 (518640 - 240 = 518400 blocks)
uint32_t lock_tier = 2; // 90 days
CMutableTransaction mtx = CreateMockMintTx(dd_amount, expected_unlock_height, lock_tier);
CTransaction tx(mtx);
// Test extraction
int64_t extracted_height = 0;
bool result = DigiDollarWallet::ExtractUnlockHeightFromOpReturn(tx, extracted_height);
BOOST_CHECK(result);
BOOST_CHECK_EQUAL(extracted_height, expected_unlock_height);
}
BOOST_AUTO_TEST_CASE(extract_unlock_height_invalid_tx)
{
// Test with a transaction that has no OP_RETURN
CMutableTransaction mtx;
mtx.nVersion = 2;
mtx.vin.resize(1);
mtx.vin[0].prevout = COutPoint(uint256S("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"), 0);
CScript normalScript = CScript() << OP_TRUE;
mtx.vout.push_back(CTxOut(100 * COIN, normalScript));
CTransaction tx(mtx);
int64_t extracted_height = 0;
bool result = DigiDollarWallet::ExtractUnlockHeightFromOpReturn(tx, extracted_height);
BOOST_CHECK(!result);
BOOST_CHECK_EQUAL(extracted_height, 0);
}
// ============================================================================
// Test 2.5: Extract lock tier from OP_RETURN metadata (new v2 format)
// ============================================================================
BOOST_AUTO_TEST_CASE(extract_tier_from_opreturn)
{
// Test extracting lock tier from a v2 mint transaction OP_RETURN
CAmount dd_amount = 10000;
int64_t unlock_height = 173800;
uint32_t expected_tier = 1; // 30 days
CMutableTransaction mtx = CreateMockMintTx(dd_amount, unlock_height, expected_tier);
CTransaction tx(mtx);
uint32_t extracted_tier = 0;
bool result = DigiDollarWallet::ExtractTierFromOpReturn(tx, extracted_tier);
BOOST_CHECK(result);
BOOST_CHECK_EQUAL(extracted_tier, expected_tier);
}
BOOST_AUTO_TEST_CASE(extract_tier_from_opreturn_all_tiers)
{
// Test tier extraction for all valid tiers (0-8)
for (uint32_t tier = 0; tier <= 8; ++tier) {
CMutableTransaction mtx = CreateMockMintTx(10000, 100000, tier);
CTransaction tx(mtx);
uint32_t extracted_tier = 99; // Invalid value to verify extraction works
bool result = DigiDollarWallet::ExtractTierFromOpReturn(tx, extracted_tier);
BOOST_CHECK_MESSAGE(result, "Failed to extract tier " + std::to_string(tier));
BOOST_CHECK_EQUAL(extracted_tier, tier);
}
}
BOOST_AUTO_TEST_CASE(extract_tier_invalid_tx)
{
// Test with a transaction that has no tier in OP_RETURN (old format)
CMutableTransaction mtx;
mtx.nVersion = 2;
mtx.vin.resize(1);
mtx.vin[0].prevout = COutPoint(uint256S("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"), 0);
// Create old format OP_RETURN (without tier)
CScript metadataScript = CScript() << OP_RETURN
<< std::vector<unsigned char>{'D', 'D'}
<< CScriptNum(1)
<< CScriptNum(10000)
<< CScriptNum(100000); // No tier field
mtx.vout.push_back(CTxOut(0, metadataScript));
CTransaction tx(mtx);
uint32_t extracted_tier = 0;
bool result = DigiDollarWallet::ExtractTierFromOpReturn(tx, extracted_tier);
// Should fail for old format - tier is REQUIRED
BOOST_CHECK(!result);
}
// ============================================================================
// Test 3: Derive lock tier from mint height and unlock height (DEPRECATED)
// ============================================================================
BOOST_AUTO_TEST_CASE(derive_lock_tier_from_heights)
{
// Test tier derivation for all standard lock tiers
// Tier 0: 1 hour (240 blocks)
uint32_t tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 1240);
BOOST_CHECK_EQUAL(tier, 0);
// Tier 1: 30 days (172,800 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 173800);
BOOST_CHECK_EQUAL(tier, 1);
// Tier 2: 90 days (518,400 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 519400);
BOOST_CHECK_EQUAL(tier, 2);
// Tier 3: 180 days (1,036,800 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 1037800);
BOOST_CHECK_EQUAL(tier, 3);
// Tier 4: 365 days (2,102,400 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 2103400);
BOOST_CHECK_EQUAL(tier, 4);
// Tier 5: 3 years = 1095 days (6,307,200 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 6308200);
BOOST_CHECK_EQUAL(tier, 5);
// Tier 6: 5 years = 1825 days (10,512,000 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 10513000);
BOOST_CHECK_EQUAL(tier, 6);
// Tier 7: 7 years = 2555 days (14,716,800 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 14717800);
BOOST_CHECK_EQUAL(tier, 7);
// Tier 8: 10 years = 3650 days (21,024,000 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 21025000);
BOOST_CHECK_EQUAL(tier, 8);
}
BOOST_AUTO_TEST_CASE(derive_lock_tier_edge_cases)
{
// Test edge case: exact tier boundaries
//
// NOTE: DeriveLockTierFromHeight is DEPRECATED for position reconstruction.
// New MINT transactions store tier explicitly in OP_RETURN.
// This function uses exact thresholds for diagnostic/validation purposes.
//
// Lock tier block thresholds:
// Tier 0: < 172,800 blocks (< 30 days)
// Tier 1: >= 172,800 blocks (30 days)
// Tier 2: >= 518,400 blocks (90 days)
// etc.
// Well under 30 days (172,799 blocks) - tier 0
uint32_t tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 173799);
BOOST_CHECK_EQUAL(tier, 0);
// Exactly 30 days (172,800 blocks) - tier 1
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 173800);
BOOST_CHECK_EQUAL(tier, 1);
// Just over 30 days (172,801 blocks) - tier 1
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 173801);
BOOST_CHECK_EQUAL(tier, 1);
// Between tiers - values in this range are mapped to the lower tier
// (300,000 blocks is between 172,800 and 518,400 so tier 1)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 300000); // Between 30d and 90d
BOOST_CHECK_EQUAL(tier, 1);
// Exactly 90 days (518,400 blocks) - tier 2
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 519400);
BOOST_CHECK_EQUAL(tier, 2);
// Test tier 0 boundary (1 hour = 240 blocks)
tier = DigiDollarWallet::DeriveLockTierFromHeight(1000, 1240);
BOOST_CHECK_EQUAL(tier, 0);
}
// ============================================================================
// Test 4: Build complete WalletCollateralPosition from mint transaction
// ============================================================================
BOOST_AUTO_TEST_CASE(build_position_from_mint_tx)
{
// Create a test wallet
DigiDollarWallet wallet;
// Test parameters
CAmount dd_amount = 50000; // $500.00 in cents
int64_t mint_height = 1000;
int64_t unlock_height = mint_height + LockDaysToBlocks(90); // 90 day lock
uint32_t expected_tier = 2; // 90 days = tier 2
// Create mock mint transaction (v2 format with explicit tier)
CMutableTransaction mtx = CreateMockMintTx(dd_amount, unlock_height, expected_tier);
CTransaction tx(mtx);
// Extract position
WalletCollateralPosition position;
bool result = wallet.ExtractPositionFromMintTx(tx, mint_height, position);
BOOST_CHECK(result);
BOOST_CHECK_EQUAL(position.dd_timelock_id, tx.GetHash());
BOOST_CHECK_EQUAL(position.dd_minted, dd_amount);
BOOST_CHECK_EQUAL(position.dgb_collateral, 100 * COIN); // From vout[0]
BOOST_CHECK_EQUAL(position.lock_tier, expected_tier); // Tier from OP_RETURN
BOOST_CHECK_EQUAL(position.unlock_height, unlock_height);
BOOST_CHECK(position.is_active);
}
BOOST_AUTO_TEST_CASE(build_position_invalid_tx)
{
// Create a test wallet
DigiDollarWallet wallet;
// Create invalid transaction (no OP_RETURN)
CMutableTransaction mtx;
mtx.nVersion = 2;
mtx.vin.resize(1);
mtx.vin[0].prevout = COutPoint(uint256S("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"), 0);
CScript normalScript = CScript() << OP_TRUE;
mtx.vout.push_back(CTxOut(100 * COIN, normalScript));
CTransaction tx(mtx);
// Try to extract position
WalletCollateralPosition position;
bool result = wallet.ExtractPositionFromMintTx(tx, 1000, position);
BOOST_CHECK(!result);
}
BOOST_AUTO_TEST_CASE(build_position_different_tiers)
{
// Test position extraction for different lock tiers
// Tier is now stored explicitly in OP_RETURN (not derived from block heights)
DigiDollarWallet wallet;
int64_t mint_height = 5000;
// Test tier 1 (30 days)
{
CAmount dd_amount = 10000;
uint32_t expected_tier = 1;
int64_t unlock_height = mint_height + LockDaysToBlocks(30);
CMutableTransaction mtx = CreateMockMintTx(dd_amount, unlock_height, expected_tier);
CTransaction tx(mtx);
WalletCollateralPosition position;
bool result = wallet.ExtractPositionFromMintTx(tx, mint_height, position);
BOOST_CHECK(result);
BOOST_CHECK_EQUAL(position.lock_tier, expected_tier);
BOOST_CHECK_EQUAL(position.dd_minted, dd_amount);
}
// Test tier 4 (365 days)
{
CAmount dd_amount = 100000;
uint32_t expected_tier = 4;
int64_t unlock_height = mint_height + LockDaysToBlocks(365);
CMutableTransaction mtx = CreateMockMintTx(dd_amount, unlock_height, expected_tier);
CTransaction tx(mtx);
WalletCollateralPosition position;
bool result = wallet.ExtractPositionFromMintTx(tx, mint_height, position);
BOOST_CHECK(result);
BOOST_CHECK_EQUAL(position.lock_tier, expected_tier);
BOOST_CHECK_EQUAL(position.dd_minted, dd_amount);
}
// Test tier 8 (10 years)
{
CAmount dd_amount = 500000;
uint32_t expected_tier = 8;
int64_t unlock_height = mint_height + LockDaysToBlocks(3650);
CMutableTransaction mtx = CreateMockMintTx(dd_amount, unlock_height, expected_tier);
CTransaction tx(mtx);
WalletCollateralPosition position;
bool result = wallet.ExtractPositionFromMintTx(tx, mint_height, position);
BOOST_CHECK(result);
BOOST_CHECK_EQUAL(position.lock_tier, expected_tier);
BOOST_CHECK_EQUAL(position.dd_minted, dd_amount);
}
}
BOOST_AUTO_TEST_SUITE_END()