forked from namecoin/namecoin-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathname_mempool_tests.cpp
444 lines (354 loc) · 13.7 KB
/
name_mempool_tests.cpp
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
// Copyright (c) 2014-2019 Daniel Kraft
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <base58.h>
#include <coins.h>
#include <key_io.h>
#include <names/encoding.h>
#include <names/mempool.h>
#include <primitives/transaction.h>
#include <script/names.h>
#include <sync.h>
#include <txmempool.h>
#include <validation.h>
#include <validationinterface.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
/* No space between BOOST_FIXTURE_TEST_SUITE and '(', so that extraction of
the test-suite name works with grep as done in the Makefile. */
BOOST_AUTO_TEST_SUITE(name_mempool_tests)
namespace
{
class NameMempoolTestSetup : public TestingSetup
{
public:
CScript ADDR;
CScript OTHER_ADDR;
const LockPoints lp;
NameMempoolTestSetup ()
{
ENTER_CRITICAL_SECTION (cs_main);
ENTER_CRITICAL_SECTION (mempool.cs);
mempool.clear ();
ADDR = CScript () << OP_TRUE;
OTHER_ADDR = CScript () << OP_TRUE << OP_RETURN;
}
~NameMempoolTestSetup ()
{
LEAVE_CRITICAL_SECTION (mempool.cs);
LEAVE_CRITICAL_SECTION (cs_main);
}
/**
* Returns a valtype name based on the given string.
*/
static valtype
Name (const std::string& str)
{
return DecodeName (str, NameEncoding::ASCII);
}
/**
* Returns the hash bytes for a name_new.
*/
static valtype
NewHash (const std::string& nm, const char rand)
{
const valtype nameVal = Name (nm);
const valtype randVal(20, rand);
valtype toHash(randVal);
toHash.insert (toHash.end (), nameVal.begin (), nameVal.end ());
const uint160 hash = Hash160 (toHash);
return valtype (hash.begin (), hash.end ());
}
/**
* Builds a name new script for the given test name and rand based
* on a character (just to allow different rand's).
*/
static CScript
NewScript (const CScript& addr, const std::string& nm, const char rand)
{
const valtype randVal(20, rand);
return CNameScript::buildNameNew (addr, Name (nm), randVal);
}
/**
* Builds a name_firstupdate script for the given name and rand value.
* The value we update to is just a fixed one.
*/
static CScript
FirstScript (const CScript& addr, const std::string& nm, const char rand)
{
const valtype randVal(20, rand);
const valtype value = DecodeName ("firstupdate value", NameEncoding::ASCII);
return CNameScript::buildNameFirstupdate (addr, Name (nm), value, randVal);
}
/**
* Builds a name_update script based on the given name and value.
*/
static CScript
UpdateScript (const CScript& addr, const std::string& nm,
const std::string& val)
{
const valtype value = DecodeName (val, NameEncoding::ASCII);
return CNameScript::buildNameUpdate (addr, Name (nm), value);
}
/**
* Builds a transaction spending to a name-output script. The transaction
* is not valid, but it is "valid enough" for testing the name mempool
* rules with it.
*/
static CTransaction
Tx (const CScript& out)
{
CMutableTransaction mtx;
mtx.SetNamecoin ();
mtx.vout.push_back (CTxOut (COIN, out));
return CTransaction (mtx);
}
/**
* Builds a mempool entry for the given transaction.
*/
CTxMemPoolEntry
Entry (const CTransaction& tx)
{
return CTxMemPoolEntry (MakeTransactionRef (tx), 0, 0, 100, false, 1, lp);
}
};
} // anonymous namespace
/* ************************************************************************** */
BOOST_FIXTURE_TEST_CASE (invalid_tx, NameMempoolTestSetup)
{
/* Invalid transactions should not crash / assert fail the mempool check. */
CMutableTransaction mtx;
mtx.SetNamecoin ();
mempool.checkNameOps (CTransaction (mtx));
mtx.vout.push_back (CTxOut (COIN, NewScript (ADDR, "foo", 'a')));
mtx.vout.push_back (CTxOut (COIN, NewScript (ADDR, "bar", 'b')));
mtx.vout.push_back (CTxOut (COIN, FirstScript (ADDR, "foo", 'a')));
mtx.vout.push_back (CTxOut (COIN, FirstScript (ADDR, "bar", 'b')));
mtx.vout.push_back (CTxOut (COIN, UpdateScript (ADDR, "foo", "x")));
mtx.vout.push_back (CTxOut (COIN, UpdateScript (ADDR, "bar", "y")));
mempool.checkNameOps (CTransaction (mtx));
}
BOOST_FIXTURE_TEST_CASE (empty_mempool, NameMempoolTestSetup)
{
/* While the mempool is empty (we do not add any transactions in this test),
all should be fine without respect to conflicts among the transactions. */
BOOST_CHECK (!mempool.registersName (Name ("foo")));
BOOST_CHECK (!mempool.updatesName (Name ("foo")));
BOOST_CHECK (mempool.checkNameOps (Tx (NewScript (ADDR, "foo", 'a'))));
BOOST_CHECK (mempool.checkNameOps (Tx (NewScript (ADDR, "foo", 'b'))));
BOOST_CHECK (mempool.checkNameOps (Tx (NewScript (OTHER_ADDR, "foo", 'a'))));
BOOST_CHECK (mempool.checkNameOps (Tx (FirstScript (ADDR, "foo", 'a'))));
BOOST_CHECK (mempool.checkNameOps (Tx (FirstScript (ADDR, "foo", 'b'))));
BOOST_CHECK (mempool.checkNameOps (Tx (UpdateScript (ADDR, "foo", "x"))));
BOOST_CHECK (mempool.checkNameOps (Tx (UpdateScript (ADDR, "foo", "y"))));
}
BOOST_FIXTURE_TEST_CASE (pendingChainLength_lastNameOutput,
NameMempoolTestSetup)
{
const auto txNew = Tx (NewScript (ADDR, "new", 'a'));
const auto txReg = Tx (FirstScript (ADDR, "reg", 'b'));
const auto txUpd = Tx (UpdateScript (ADDR, "upd", "x"));
mempool.addUnchecked (Entry (txNew));
mempool.addUnchecked (Entry (txReg));
mempool.addUnchecked (Entry (txUpd));
/* For testing chained name updates, we have to build a "real" chain of
transactions with matching inputs and outputs. */
CMutableTransaction mtx;
mtx.SetNamecoin ();
mtx.vout.push_back (CTxOut (COIN, FirstScript (ADDR, "chain", 'a')));
mtx.vout.push_back (CTxOut (COIN, ADDR));
mtx.vout.push_back (CTxOut (COIN, OTHER_ADDR));
const CTransaction chain1(mtx);
mempool.addUnchecked (Entry (chain1));
mtx.vout.clear ();
mtx.vout.push_back (CTxOut (COIN, ADDR));
mtx.vout.push_back (CTxOut (COIN, UpdateScript (ADDR, "chain", "x")));
mtx.vin.push_back (CTxIn (COutPoint (chain1.GetHash (), 0)));
const CTransaction chain2(mtx);
mempool.addUnchecked (Entry (chain2));
mtx.vout.clear ();
mtx.vout.push_back (CTxOut (COIN, OTHER_ADDR));
mtx.vout.push_back (CTxOut (COIN, UpdateScript (ADDR, "chain", "y")));
mtx.vin.push_back (CTxIn (COutPoint (chain2.GetHash (), 0)));
mtx.vin.push_back (CTxIn (COutPoint (chain1.GetHash (), 1)));
const CTransaction chain3(mtx);
mempool.addUnchecked (Entry (chain3));
CMutableTransaction mtxCurrency;
mtxCurrency.vin.push_back (CTxIn (COutPoint (chain1.GetHash (), 2)));
mtxCurrency.vin.push_back (CTxIn (COutPoint (chain3.GetHash (), 0)));
mempool.addUnchecked (Entry (CTransaction (mtxCurrency)));
BOOST_CHECK (mempool.lastNameOutput (Name ("new")).IsNull ());
BOOST_CHECK (mempool.lastNameOutput (Name ("reg"))
== COutPoint (txReg.GetHash (), 0));
BOOST_CHECK (mempool.lastNameOutput (Name ("upd"))
== COutPoint (txUpd.GetHash (), 0));
BOOST_CHECK (mempool.lastNameOutput (Name ("chain"))
== COutPoint (chain3.GetHash (), 1));
BOOST_CHECK_EQUAL (mempool.pendingNameChainLength (Name ("new")), 0);
BOOST_CHECK_EQUAL (mempool.pendingNameChainLength (Name ("reg")), 1);
BOOST_CHECK_EQUAL (mempool.pendingNameChainLength (Name ("upd")), 1);
BOOST_CHECK_EQUAL (mempool.pendingNameChainLength (Name ("chain")), 3);
}
BOOST_FIXTURE_TEST_CASE (name_new, NameMempoolTestSetup)
{
const auto tx1 = Tx (NewScript (ADDR, "foo", 'a'));
const auto tx1p = Tx (NewScript (OTHER_ADDR, "foo", 'a'));
const auto tx2 = Tx (NewScript (ADDR, "foo", 'b'));
const auto e1 = Entry (tx1);
const auto e2 = Entry (tx2);
BOOST_CHECK (e1.isNameNew () && e2.isNameNew ());
BOOST_CHECK (e1.getNameNewHash () == NewHash ("foo", 'a'));
BOOST_CHECK (e2.getNameNewHash () == NewHash ("foo", 'b'));
mempool.addUnchecked (e1);
mempool.addUnchecked (e2);
BOOST_CHECK (mempool.checkNameOps (tx1));
BOOST_CHECK (mempool.checkNameOps (tx2));
BOOST_CHECK (!mempool.checkNameOps (tx1p));
mempool.removeRecursive (tx1, MemPoolRemovalReason::EXPIRY);
mempool.removeRecursive (tx2, MemPoolRemovalReason::EXPIRY);
BOOST_CHECK (mempool.checkNameOps (tx1));
BOOST_CHECK (mempool.checkNameOps (tx2));
BOOST_CHECK (!mempool.checkNameOps (tx1p));
}
BOOST_FIXTURE_TEST_CASE (name_firstupdate, NameMempoolTestSetup)
{
const auto tx1 = Tx (FirstScript (ADDR, "foo", 'a'));
const auto tx2 = Tx (FirstScript (ADDR, "foo", 'b'));
const auto e = Entry (tx1);
BOOST_CHECK (e.isNameRegistration () && !e.isNameUpdate ());
BOOST_CHECK (e.getName () == Name ("foo"));
mempool.addUnchecked (e);
BOOST_CHECK (mempool.registersName (Name ("foo")));
BOOST_CHECK (!mempool.updatesName (Name ("foo")));
BOOST_CHECK (!mempool.checkNameOps (tx2));
mempool.removeRecursive (tx1, MemPoolRemovalReason::EXPIRY);
BOOST_CHECK (!mempool.registersName (Name ("foo")));
BOOST_CHECK (mempool.checkNameOps (tx1));
BOOST_CHECK (mempool.checkNameOps (tx2));
}
BOOST_FIXTURE_TEST_CASE (name_update, NameMempoolTestSetup)
{
const auto tx1 = Tx (UpdateScript (ADDR, "foo", "x"));
const auto tx2 = Tx (UpdateScript (ADDR, "foo", "y"));
const auto tx3 = Tx (UpdateScript (ADDR, "bar", "z"));
const auto e1 = Entry (tx1);
const auto e2 = Entry (tx2);
const auto e3 = Entry (tx3);
BOOST_CHECK (!e1.isNameRegistration () && e1.isNameUpdate ());
BOOST_CHECK (e1.getName () == Name ("foo"));
mempool.addUnchecked (e1);
mempool.addUnchecked (e2);
mempool.addUnchecked (e3);
BOOST_CHECK (!mempool.registersName (Name ("foo")));
BOOST_CHECK (mempool.updatesName (Name ("foo")));
BOOST_CHECK (mempool.updatesName (Name ("bar")));
mempool.removeRecursive (tx2, MemPoolRemovalReason::EXPIRY);
BOOST_CHECK (mempool.updatesName (Name ("foo")));
BOOST_CHECK (mempool.updatesName (Name ("bar")));
mempool.removeRecursive (tx1, MemPoolRemovalReason::EXPIRY);
BOOST_CHECK (!mempool.updatesName (Name ("foo")));
BOOST_CHECK (mempool.updatesName (Name ("bar")));
mempool.removeRecursive (tx3, MemPoolRemovalReason::EXPIRY);
BOOST_CHECK (!mempool.updatesName (Name ("foo")));
BOOST_CHECK (!mempool.updatesName (Name ("bar")));
}
BOOST_FIXTURE_TEST_CASE (mempool_sanity_check, NameMempoolTestSetup)
{
mempool.addUnchecked (Entry (Tx (NewScript (ADDR, "new", 'a'))));
mempool.addUnchecked (Entry (Tx (NewScript (ADDR, "new", 'b'))));
mempool.addUnchecked (Entry (Tx (FirstScript (ADDR, "reg", 'a'))));
mempool.addUnchecked (Entry (Tx (UpdateScript (ADDR, "reg", "n"))));
mempool.addUnchecked (Entry (Tx (UpdateScript (ADDR, "upd", "x"))));
mempool.addUnchecked (Entry (Tx (UpdateScript (ADDR, "upd", "y"))));
ChainstateManager& chainman = g_chainman;
CCoinsViewCache view(&chainman.ActiveChainstate ().CoinsTip ());
const CNameScript nameOp(UpdateScript (ADDR, "upd", "o"));
CNameData data;
data.fromScript (100, COutPoint (uint256 (), 0), nameOp);
view.SetName (Name ("upd"), data, false);
mempool.checkNames (chainman, &view);
}
namespace
{
/**
* Helper class that listens to TransactionRemovedFromMempool and records
* the txid's of all transactions removed.
*/
class NameConflictTracker : public CValidationInterface
{
private:
/** The txids that have been removed according to our callback. */
std::vector<uint256> txids;
public:
NameConflictTracker ()
{
RegisterValidationInterface (this);
}
~NameConflictTracker ()
{
UnregisterValidationInterface (this);
}
void
TransactionRemovedFromMempool (const CTransactionRef& ptxn,
const MemPoolRemovalReason reason) override
{
txids.push_back (ptxn->GetHash ());
}
/**
* Expects the given list of txids to be removed. Waits for all outstanding
* callbacks to be processed as needed.
*/
void
ExpectTxids (const std::vector<uint256>& expected) const
{
while (GetMainSignals ().CallbacksPending () > 0)
UninterruptibleSleep (std::chrono::milliseconds (10));
BOOST_CHECK (txids == expected);
}
};
} // anonymous namespace
BOOST_FIXTURE_TEST_CASE (registration_conflicts, NameMempoolTestSetup)
{
const auto tx1 = Tx (FirstScript (ADDR, "foo", 'a'));
const auto tx2 = Tx (FirstScript (ADDR, "foo", 'b'));
const auto e = Entry (tx1);
mempool.addUnchecked (e);
BOOST_CHECK (mempool.registersName (Name ("foo")));
BOOST_CHECK (!mempool.checkNameOps (tx2));
NameConflictTracker tracker;
mempool.removeConflicts (tx2);
tracker.ExpectTxids ({tx1.GetHash ()});
BOOST_CHECK (!mempool.registersName (Name ("foo")));
BOOST_CHECK (mempool.checkNameOps (tx1));
BOOST_CHECK (mempool.checkNameOps (tx2));
BOOST_CHECK (mempool.mapTx.empty ());
}
BOOST_FIXTURE_TEST_CASE (expire_conflicts, NameMempoolTestSetup)
{
const auto tx1 = Tx (UpdateScript (ADDR, "foo", "x"));
const auto tx2 = Tx (UpdateScript (ADDR, "foo", "y"));
const auto e1 = Entry (tx1);
const auto e2 = Entry (tx2);
mempool.addUnchecked (e1);
mempool.addUnchecked (e2);
BOOST_CHECK (mempool.updatesName (Name ("foo")));
NameConflictTracker tracker;
mempool.removeExpireConflicts ({Name ("foo")});
tracker.ExpectTxids ({tx1.GetHash (), tx2.GetHash ()});
BOOST_CHECK (!mempool.updatesName (Name ("foo")));
BOOST_CHECK (mempool.mapTx.empty ());
}
BOOST_FIXTURE_TEST_CASE (unexpire_conflicts, NameMempoolTestSetup)
{
const auto tx = Tx (FirstScript (ADDR, "foo", 'a'));
const auto e = Entry (tx);
mempool.addUnchecked (e);
BOOST_CHECK (mempool.registersName (Name ("foo")));
NameConflictTracker tracker;
mempool.removeUnexpireConflicts ({Name ("foo")});
tracker.ExpectTxids ({tx.GetHash ()});
BOOST_CHECK (!mempool.registersName (Name ("foo")));
BOOST_CHECK (mempool.mapTx.empty ());
}
/* ************************************************************************** */
BOOST_AUTO_TEST_SUITE_END ()