forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevo_assetlocks_tests.cpp
More file actions
558 lines (443 loc) · 22 KB
/
Copy pathevo_assetlocks_tests.cpp
File metadata and controls
558 lines (443 loc) · 22 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
// Copyright (c) 2023-2025 The Dash 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 <consensus/amount.h>
#include <consensus/tx_check.h>
#include <consensus/validation.h>
#include <evo/assetlocktx.h>
#include <evo/creditpool.h>
#include <evo/specialtx.h>
#include <llmq/context.h>
#include <policy/policy.h>
#include <script/script.h>
#include <script/signingprovider.h>
#include <script/standard.h>
#include <util/ranges_set.h>
#include <validation.h>
#include <boost/test/unit_test.hpp>
// Helpers:
static bool IsStandardTx(const CTransaction& tx, std::string& reason)
{
return IsStandardTx(tx, MAX_OP_RETURN_RELAY, DEFAULT_PERMIT_BAREMULTISIG, CFeeRate{DUST_RELAY_TX_FEE}, reason);
}
// Create two dummy transactions, each with two outputs. The first has 11 and 50 CENT outputs
// paid to a TX_PUBKEY, the second 21 and 22 CENT outputs paid to a TX_PUBKEYHASH.
static std::vector<CMutableTransaction>
SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet)
{
std::vector<CMutableTransaction> dummyTransactions;
dummyTransactions.resize(2);
// Add some keys to the keystore:
std::array<CKey, 4> key;
{
bool flip = true;
for (auto& k : key) {
k.MakeNewKey(flip);
keystoreRet.AddKey(k);
flip = !flip;
}
}
// Create some dummy input transactions
dummyTransactions[0].vout.resize(2);
dummyTransactions[0].vout[0].nValue = 11*CENT;
dummyTransactions[0].vout[0].scriptPubKey = GetScriptForRawPubKey(key[0].GetPubKey());
dummyTransactions[0].vout[1].nValue = 50*CENT;
dummyTransactions[0].vout[1].scriptPubKey = GetScriptForRawPubKey(key[1].GetPubKey());
AddCoins(coinsRet, CTransaction(dummyTransactions[0]), 0);
dummyTransactions[1].vout.resize(2);
dummyTransactions[1].vout[0].nValue = 21*CENT;
dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[2].GetPubKey()));
dummyTransactions[1].vout[1].nValue = 22*CENT;
dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(PKHash(key[3].GetPubKey()));
AddCoins(coinsRet, CTransaction(dummyTransactions[1]), 0);
return dummyTransactions;
}
static CMutableTransaction CreateAssetLockTx(FillableSigningProvider& keystore, CCoinsViewCache& coins, CKey& key)
{
std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
std::vector<CTxOut> creditOutputs(2);
creditOutputs[0].nValue = 17 * CENT;
creditOutputs[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
creditOutputs[1].nValue = 13 * CENT;
creditOutputs[1].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
CAssetLockPayload assetLockTx(creditOutputs, CAssetLockPayload::INITIAL_VERSION);
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_ASSET_LOCK;
SetTxPayload(tx, assetLockTx);
tx.vin.resize(1);
tx.vin[0].prevout.hash = dummyTransactions[0].GetHash();
tx.vin[0].prevout.n = 1;
tx.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
tx.vout.resize(2);
tx.vout[0].nValue = 30 * CENT;
tx.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("");
tx.vout[1].nValue = 20 * CENT;
tx.vout[1].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
return tx;
}
static CMutableTransaction CreateAssetUnlockTx(FillableSigningProvider& keystore, CKey& key)
{
int nVersion = 1;
// just a big number bigger than uint32_t
uint64_t index = 0x001122334455667788L;
// big enough to overflow int32_t
uint32_t fee = 2000'000'000u;
// just big enough to overflow uint16_t
uint32_t requestedHeight = 1000'000;
uint256 quorumHash;
CBLSSignature quorumSig;
CAssetUnlockPayload assetUnlockTx(nVersion, index, fee, requestedHeight, quorumHash, quorumSig);
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_ASSET_UNLOCK;
SetTxPayload(tx, assetUnlockTx);
tx.vin.resize(0);
tx.vout.resize(2);
tx.vout[0].nValue = 10 * CENT;
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
tx.vout[1].nValue = 20 * CENT;
tx.vout[1].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
return tx;
}
static CTransactionRef CreateCreditPoolUnlockTx(uint64_t index, CAmount amount)
{
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_ASSET_UNLOCK;
tx.vout.emplace_back(amount, CScript{});
SetTxPayload(tx, CAssetUnlockPayload{1, index, 0, 0, {}, {}});
return MakeTransactionRef(std::move(tx));
}
BOOST_FIXTURE_TEST_SUITE(evo_assetlocks_tests, TestChain100Setup)
static void CheckAssetLockCommon(uint8_t version, bool is_v24_active)
{
LOCK(cs_main);
FillableSigningProvider keystore;
CCoinsView coinsDummy;
CCoinsViewCache coins(&coinsDummy);
CKey key;
key.MakeNewKey(true);
CMutableTransaction baseTx = CreateAssetLockTx(keystore, coins, key);
const auto basePayload = GetTxPayload<CAssetLockPayload>(CTransaction(baseTx));
const std::vector<CTxOut> creditOutputs = basePayload->getCreditOutputs();
SetTxPayload(baseTx, CAssetLockPayload(creditOutputs, version));
const CTransaction tx{baseTx};
std::string reason;
BOOST_CHECK(IsStandardTx(CTransaction(tx), reason));
TxValidationState tx_state;
std::string strTest;
BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), tx_state), strTest);
BOOST_CHECK(tx_state.IsValid());
BOOST_CHECK(CheckAssetLockTx(CTransaction(tx), tx_state, is_v24_active));
BOOST_CHECK(AreInputsStandard(CTransaction(tx), coins));
// Check version
{
BOOST_CHECK(tx.IsSpecialTxVersion());
const auto opt_payload = GetTxPayload<CAssetLockPayload>(tx);
BOOST_CHECK(opt_payload.has_value());
BOOST_CHECK(opt_payload->getVersion() == version);
}
{
// Wrong type "Asset Unlock TX" instead "Asset Lock TX"
CMutableTransaction txWrongType(tx);
txWrongType.nType = TRANSACTION_ASSET_UNLOCK;
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txWrongType), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-type");
}
{
CAmount inSum = 0;
for (const auto& vin : tx.vin) {
inSum += coins.AccessCoin(vin.prevout).out.nValue;
}
auto outSum = CTransaction(tx).GetValueOut();
BOOST_CHECK(inSum == outSum);
// Outputs should not be bigger than inputs
CMutableTransaction txBigOutput(baseTx);
txBigOutput.vout[0].nValue += 1;
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txBigOutput), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-creditamount");
// Smaller outputs are allown
CMutableTransaction txSmallOutput(baseTx);
txSmallOutput.vout[1].nValue -= 1;
BOOST_CHECK(CheckAssetLockTx(CTransaction(txSmallOutput), tx_state, is_v24_active));
}
{
// Sum of credit output greater than OP_RETURN
std::vector<CTxOut> wrongOutput = creditOutputs;
wrongOutput[0].nValue += CENT;
CMutableTransaction txGreaterCredits(baseTx);
SetTxPayload(txGreaterCredits, CAssetLockPayload(wrongOutput, version));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txGreaterCredits), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-creditamount");
// Sum of credit output less than OP_RETURN
wrongOutput[1].nValue -= 2 * CENT;
CMutableTransaction txLessCredits(baseTx);
SetTxPayload(txLessCredits, CAssetLockPayload(wrongOutput, version));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txLessCredits), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-creditamount");
}
{
// Credit output is out-of-range
std::vector<CTxOut> creditOutputsOutOfRange = creditOutputs;
creditOutputsOutOfRange[0].nValue = 0;
CMutableTransaction txInvalidOutputs(baseTx);
SetTxPayload(txInvalidOutputs, CAssetLockPayload(creditOutputsOutOfRange, version));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txInvalidOutputs), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-credit-outofrange");
// one of output is out of range
creditOutputsOutOfRange[0].nValue = MAX_MONEY + 1;
SetTxPayload(txInvalidOutputs, CAssetLockPayload(creditOutputsOutOfRange, version));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txInvalidOutputs), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-credit-outofrange");
// sum of some of output is out of range
creditOutputsOutOfRange[0].nValue = MAX_MONEY + 1 - creditOutputsOutOfRange[1].nValue;
SetTxPayload(txInvalidOutputs, CAssetLockPayload(creditOutputsOutOfRange, version));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txInvalidOutputs), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-credit-outofrange");
}
{
// One credit output keys is not pub key
std::vector<CTxOut> creditOutputsNotPubkey = creditOutputs;
creditOutputsNotPubkey[0].scriptPubKey = CScript() << OP_1;
CMutableTransaction txNotPubkey(baseTx);
SetTxPayload(txNotPubkey, CAssetLockPayload(creditOutputsNotPubkey, version));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txNotPubkey), tx_state, is_v24_active));
if (version >= 2) {
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-script-pubkey");
} else {
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-pubKeyHash");
}
}
{
// OP_RETURN must be only one, not more
CMutableTransaction txMultipleReturn(baseTx);
txMultipleReturn.vout[1].scriptPubKey = CScript() << OP_RETURN << ParseHex("");
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txMultipleReturn), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-multiple-return");
}
{
// zero/negative OP_RETURN
CMutableTransaction txReturnOutOfRange(baseTx);
txReturnOutOfRange.vout[0].nValue = 0;
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txReturnOutOfRange), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-opreturn-outofrange");
txReturnOutOfRange.vout[0].nValue = MAX_MONEY + 1;
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txReturnOutOfRange), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-opreturn-outofrange");
}
{
// OP_RETURN is missing
CMutableTransaction txNoReturn(baseTx);
txNoReturn.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txNoReturn), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-no-return");
}
{
// OP_RETURN should not have any data
CMutableTransaction txReturnData(baseTx);
txReturnData.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("abcd");
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txReturnData), tx_state, is_v24_active));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-non-empty-return");
}
}
BOOST_FIXTURE_TEST_CASE(evo_assetlock, TestChain100Setup)
{
CheckAssetLockCommon(CAssetLockPayload::INITIAL_VERSION, /*is_v24_active=*/false);
CheckAssetLockCommon(CAssetLockPayload::INITIAL_VERSION, /*is_v24_active=*/true);
CheckAssetLockCommon(CAssetLockPayload::CURRENT_VERSION, /*is_v24_active=*/true);
}
BOOST_FIXTURE_TEST_CASE(evo_assetlock_v2, TestChain100Setup)
{
LOCK(cs_main);
FillableSigningProvider keystore;
CCoinsView coinsDummy;
CCoinsViewCache coins(&coinsDummy);
CKey key;
key.MakeNewKey(true);
CMutableTransaction tx = CreateAssetLockTx(keystore, coins, key);
TxValidationState tx_state;
const auto v1Payload = GetTxPayload<CAssetLockPayload>(CTransaction(tx));
const std::vector<CTxOut> creditOutputs = v1Payload->getCreditOutputs();
// Build v2 payload with same P2PKH credit outputs
CAssetLockPayload v2Payload(creditOutputs);
BOOST_CHECK(v2Payload.getVersion() == CAssetLockPayload::CURRENT_VERSION);
SetTxPayload(tx, v2Payload);
{
// v2 P2PKH: accepted with is_v24_active=true
BOOST_CHECK(CheckAssetLockTx(CTransaction(tx), tx_state, /*is_v24_active=*/true));
}
{
// v2 P2PKH: rejected pre-v24
BOOST_CHECK(!CheckAssetLockTx(CTransaction(tx), tx_state, /*is_v24_active=*/false));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-version-2");
}
{
// v2 with P2SH credit output: accepted with is_v24_active=true
CScript p2sh_script = GetScriptForDestination(ScriptHash(CScript() << OP_1));
std::vector<CTxOut> p2shOutputs(1);
p2shOutputs[0].nValue = 30 * CENT;
p2shOutputs[0].scriptPubKey = p2sh_script;
CMutableTransaction txP2SH(tx);
SetTxPayload(txP2SH, CAssetLockPayload(p2shOutputs));
BOOST_CHECK(CheckAssetLockTx(CTransaction(txP2SH), tx_state, /*is_v24_active=*/true));
}
{
// v1 with P2SH credit output: rejected even post-v24
CScript p2sh_script = GetScriptForDestination(ScriptHash(CScript() << OP_1));
std::vector<CTxOut> p2shOutputs(1);
p2shOutputs[0].nValue = 30 * CENT;
p2shOutputs[0].scriptPubKey = p2sh_script;
CMutableTransaction txV1P2SH(tx);
SetTxPayload(txV1P2SH, CAssetLockPayload(p2shOutputs, CAssetLockPayload::INITIAL_VERSION));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txV1P2SH), tx_state, /*is_v24_active=*/true));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-pubKeyHash");
}
{
// v2 with non-P2PKH/non-P2SH credit output: rejected
std::vector<CTxOut> badOutputs(1);
badOutputs[0].nValue = 30 * CENT;
badOutputs[0].scriptPubKey = CScript() << OP_1;
CMutableTransaction txBad(tx);
SetTxPayload(txBad, CAssetLockPayload(badOutputs));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txBad), tx_state, /*is_v24_active=*/true));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-script-pubkey");
}
{
// v1 still works post-v24
CMutableTransaction txV1(tx);
SetTxPayload(txV1, CAssetLockPayload(creditOutputs, CAssetLockPayload::INITIAL_VERSION));
BOOST_CHECK(CheckAssetLockTx(CTransaction(txV1), tx_state, /*is_v24_active=*/true));
}
{
// version 3 (future): rejected even post-v24
CMutableTransaction txV3(tx);
SetTxPayload(txV3, CAssetLockPayload(creditOutputs, /*nVersion=*/3));
BOOST_CHECK(!CheckAssetLockTx(CTransaction(txV3), tx_state, /*is_v24_active=*/true));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetlocktx-version");
}
}
BOOST_FIXTURE_TEST_CASE(evo_assetunlock, TestChain100Setup)
{
LOCK(cs_main);
FillableSigningProvider keystore;
CKey key;
key.MakeNewKey(true);
const CTransaction tx{CreateAssetUnlockTx(keystore, key)};
std::string reason;
BOOST_CHECK(IsStandardTx(CTransaction(tx), reason));
TxValidationState tx_state;
std::string strTest;
BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), tx_state), strTest);
BOOST_CHECK(tx_state.IsValid());
auto& blockman = Assert(m_node.chainman)->m_blockman;
auto& qman = *Assert(m_node.llmq_ctx)->qman;
const CBlockIndex *block_index = m_node.chainman->ActiveChain().Tip();
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(tx), block_index, std::nullopt, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlock-quorum-hash");
{
// Any input should be a reason to fail CheckAssetUnlockTx()
CCoinsView coinsDummy;
CCoinsViewCache coins(&coinsDummy);
std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
CMutableTransaction txNonemptyInput(tx);
txNonemptyInput.vin.resize(1);
txNonemptyInput.vin[0].prevout.hash = dummyTransactions[0].GetHash();
txNonemptyInput.vin[0].prevout.n = 1;
txNonemptyInput.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
std::string reason;
BOOST_CHECK(IsStandardTx(CTransaction(tx), reason));
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(txNonemptyInput), block_index, std::nullopt, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlocktx-have-input");
}
{
const auto unlockPayload = GetTxPayload<CAssetUnlockPayload>(tx);
BOOST_CHECK(unlockPayload.has_value());
BOOST_CHECK(unlockPayload->getVersion() == 1);
BOOST_CHECK(unlockPayload->getRequestedHeight() == 1000'000);
BOOST_CHECK(unlockPayload->getFee() == 2000'000'000u);
BOOST_CHECK(unlockPayload->getIndex() == 0x001122334455667788L);
// Wrong type "Asset Lock TX" instead "Asset Unlock TX"
CMutableTransaction txWrongType(tx);
txWrongType.nType = TRANSACTION_ASSET_LOCK;
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(txWrongType), block_index, std::nullopt, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlocktx-type");
// Check version of tx and payload
BOOST_CHECK(tx.IsSpecialTxVersion());
for (uint8_t payload_version : {0, 1, 2, 255}) {
CAssetUnlockPayload unlockPayload_tmp{payload_version,
unlockPayload->getIndex(),
unlockPayload->getFee(),
unlockPayload->getRequestedHeight(),
unlockPayload->getQuorumHash(),
unlockPayload->getQuorumSig()};
CMutableTransaction txWrongVersion(tx);
SetTxPayload(txWrongVersion, unlockPayload_tmp);
if (payload_version != 1) {
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(txWrongVersion), block_index, std::nullopt, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlocktx-version");
} else {
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(txWrongVersion), block_index, std::nullopt, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlock-quorum-hash");
}
}
}
{
// Exactly 32 withdrawal is fine
CMutableTransaction txManyOutputs(tx);
int outputsLimit = 32;
txManyOutputs.vout.resize(outputsLimit);
for (auto& out : txManyOutputs.vout) {
out.nValue = CENT;
out.scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
}
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(txManyOutputs), block_index, std::nullopt, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlock-quorum-hash");
// Basic checks for CRangesSet
CRangesSet indexes;
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(txManyOutputs), block_index, indexes, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlock-quorum-hash");
BOOST_CHECK(indexes.Add(0x001122334455667788L));
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(txManyOutputs), block_index, indexes, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlock-duplicated-index");
// Should not be more than 32 withdrawal in one transaction
txManyOutputs.vout.resize(outputsLimit + 1);
txManyOutputs.vout.back().nValue = CENT;
txManyOutputs.vout.back().scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
BOOST_CHECK(!CheckAssetUnlockTx(blockman, qman, CTransaction(txManyOutputs), block_index, std::nullopt, tx_state));
BOOST_CHECK(tx_state.GetRejectReason() == "bad-assetunlocktx-too-many-outs");
}
}
BOOST_FIXTURE_TEST_CASE(credit_pool_package_atomicity, TestChain100Setup)
{
LOCK(cs_main);
const auto unlock_seven = CreateCreditPoolUnlockTx(1, 7 * COIN);
const auto unlock_four = CreateCreditPoolUnlockTx(2, 4 * COIN);
const auto* tip = m_node.chainman->ActiveChain().Tip();
CCreditPoolDiff diff{CCreditPool{100 * COIN, 10 * COIN}, tip, m_node.chainman->GetConsensus(), 0};
TxValidationState package_state;
BOOST_CHECK(!diff.ProcessLockUnlockTransactions({unlock_seven, unlock_four}, package_state));
BOOST_CHECK_EQUAL(package_state.GetRejectReason(), "failed-creditpool-unlock-too-much");
BOOST_CHECK_EQUAL(diff.GetTotalLocked(), 100 * COIN);
TxValidationState retry_state;
BOOST_CHECK(diff.ProcessLockUnlockTransaction(*unlock_seven, retry_state));
BOOST_CHECK_EQUAL(diff.GetTotalLocked(), 93 * COIN);
// A later package's rollback must erase only its own indexes: index 1 was
// committed above and must survive the failed package below.
const auto unlock_two = CreateCreditPoolUnlockTx(3, 2 * COIN);
const auto unlock_dup = CreateCreditPoolUnlockTx(1, COIN);
TxValidationState dup_state;
BOOST_CHECK(!diff.ProcessLockUnlockTransactions({unlock_two, unlock_dup}, dup_state));
BOOST_CHECK_EQUAL(dup_state.GetRejectReason(), "failed-creditpool-unlock-duplicated-index");
BOOST_CHECK_EQUAL(diff.GetTotalLocked(), 93 * COIN);
// index 1 must still be known as used...
TxValidationState still_dup_state;
BOOST_CHECK(!diff.ProcessLockUnlockTransactions({unlock_dup}, still_dup_state));
BOOST_CHECK_EQUAL(still_dup_state.GetRejectReason(), "failed-creditpool-unlock-duplicated-index");
// ...while index 3 was rolled back and is usable again
TxValidationState reuse_state;
BOOST_CHECK(diff.ProcessLockUnlockTransactions({unlock_two}, reuse_state));
BOOST_CHECK_EQUAL(diff.GetTotalLocked(), 91 * COIN);
}
BOOST_AUTO_TEST_SUITE_END()