2
2
// Distributed under the MIT software license, see the accompanying
3
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
4
5
- #include " main.h"
6
5
#include " txmempool.h"
7
6
7
+ #include " blockmap.h"
8
+ #include " MockUtxoHasher.h"
9
+
8
10
#include < boost/test/unit_test.hpp>
9
11
#include < list>
10
12
13
+ extern CChain chainActive;
14
+ extern BlockMap mapBlockIndex;
15
+
11
16
class MempoolTestFixture
12
17
{
13
18
14
19
private:
15
20
16
21
CCoinsView coinsDummy;
17
22
23
+ /* * Tip of our fake chain. */
24
+ CBlockIndex tip;
25
+
18
26
protected:
19
27
20
28
/* The test mempool will use these flags instead of the global ones. */
@@ -24,7 +32,8 @@ class MempoolTestFixture
24
32
/* * A parent transaction. */
25
33
CMutableTransaction txParent;
26
34
27
- /* * Three children of the parent. */
35
+ /* * Three children of the parent. They use the bare txid for their UTXOs
36
+ * in our UTXO hasher. */
28
37
CMutableTransaction txChild[3 ];
29
38
30
39
/* * Three grand children. */
@@ -45,11 +54,19 @@ class MempoolTestFixture
45
54
: testPool(CFeeRate(0 ), addressIndex, spentIndex),
46
55
coinsMemPool (&coinsDummy, testPool), coins(&coinsMemPool)
47
56
{
57
+ std::unique_ptr<MockUtxoHasher> utxoHasher (new MockUtxoHasher ());
58
+
48
59
CMutableTransaction mtx;
49
- mtx.vout .emplace_back (COIN, CScript () << OP_TRUE);
60
+ mtx.vout .emplace_back (2 * COIN, CScript () << OP_TRUE);
50
61
mtx.vout .emplace_back (COIN, CScript () << OP_TRUE);
51
62
coins.ModifyCoins (mtx.GetHash ())->FromTx (mtx, 0 );
52
63
64
+ tip.pprev = nullptr ;
65
+ tip.nHeight = 0 ;
66
+ mapBlockIndex[tip.GetBlockHeader ().GetHash ()] = &tip;
67
+ coins.SetBestBlock (tip.GetBlockHeader ().GetHash ());
68
+ chainActive.SetTip (&tip);
69
+
53
70
txParent.vin .resize (2 );
54
71
txParent.vin [0 ].prevout = COutPoint (mtx.GetHash (), 0 );
55
72
txParent.vin [0 ].scriptSig = CScript () << OP_11;
@@ -61,7 +78,7 @@ class MempoolTestFixture
61
78
for (int i = 0 ; i < 3 ; i++)
62
79
{
63
80
txParent.vout [i].scriptPubKey = CScript () << OP_11 << OP_EQUAL;
64
- txParent.vout [i].nValue = 33000LL ;
81
+ txParent.vout [i].nValue = COIN ;
65
82
}
66
83
assert (txParent.GetHash () != txParent.GetBareTxid ());
67
84
@@ -73,19 +90,40 @@ class MempoolTestFixture
73
90
txChild[i].vin [0 ].prevout .n = i;
74
91
txChild[i].vout .resize (1 );
75
92
txChild[i].vout [0 ].scriptPubKey = CScript () << OP_11 << OP_EQUAL;
76
- txChild[i].vout [0 ].nValue = 11000LL ;
93
+ txChild[i].vout [0 ].nValue = COIN;
94
+ utxoHasher->UseBareTxid (txChild[i]);
77
95
}
78
96
79
97
for (int i = 0 ; i < 3 ; i++)
80
98
{
81
99
txGrandChild[i].vin .resize (1 );
82
100
txGrandChild[i].vin [0 ].scriptSig = CScript () << OP_11;
83
- txGrandChild[i].vin [0 ].prevout .hash = txChild[i].GetHash ();
101
+ txGrandChild[i].vin [0 ].prevout .hash = txChild[i].GetBareTxid ();
84
102
txGrandChild[i].vin [0 ].prevout .n = 0 ;
85
103
txGrandChild[i].vout .resize (1 );
86
104
txGrandChild[i].vout [0 ].scriptPubKey = CScript () << OP_11 << OP_EQUAL;
87
- txGrandChild[i].vout [0 ].nValue = 11000LL ;
105
+ txGrandChild[i].vout [0 ].nValue = COIN ;
88
106
}
107
+
108
+ testPool.setSanityCheck (true );
109
+ testPool.SetUtxoHasherForTesting (std::move (utxoHasher));
110
+ testPool.clear ();
111
+ }
112
+
113
+ ~MempoolTestFixture ()
114
+ {
115
+ mapBlockIndex.clear ();
116
+ }
117
+
118
+ /* * Adds the parent, childs and grandchilds to the mempool. */
119
+ void AddAll ()
120
+ {
121
+ testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ), coins);
122
+ for (int i = 0 ; i < 3 ; i++)
123
+ {
124
+ testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ), coins);
125
+ testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ), coins);
126
+ }
89
127
}
90
128
91
129
};
@@ -109,12 +147,10 @@ BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
109
147
removed.clear ();
110
148
111
149
// Parent, children, grandchildren:
112
- testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ), coins);
113
- for (int i = 0 ; i < 3 ; i++)
114
- {
115
- testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ), coins);
116
- testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ), coins);
117
- }
150
+ AddAll ();
151
+
152
+ testPool.check (&coins);
153
+
118
154
// Remove Child[0], GrandChild[0] should be removed:
119
155
testPool.remove (txChild[0 ], removed, true );
120
156
BOOST_CHECK_EQUAL (removed.size (), 2 );
@@ -149,12 +185,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexByBareTxid)
149
185
CTransaction tx;
150
186
std::list<CTransaction> removed;
151
187
152
- testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ), coins);
153
- for (int i = 0 ; i < 3 ; ++i)
154
- {
155
- testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ), coins);
156
- testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ), coins);
157
- }
188
+ AddAll ();
158
189
159
190
BOOST_CHECK (testPool.lookupBareTxid (txParent.GetBareTxid (), tx));
160
191
BOOST_CHECK (tx.GetHash () == txParent.GetHash ());
@@ -189,4 +220,28 @@ BOOST_AUTO_TEST_CASE(MempoolSpentIndex)
189
220
BOOST_CHECK (!testPool.getSpentIndex (keyChild, value));
190
221
}
191
222
223
+ BOOST_AUTO_TEST_CASE (MempoolOutpointLookup)
224
+ {
225
+ CTransaction tx;
226
+ CCoins c;
227
+
228
+ AddAll ();
229
+ CCoinsViewMemPool viewPool (&coins, testPool);
230
+
231
+ BOOST_CHECK (testPool.lookupOutpoint (txParent.GetHash (), tx));
232
+ BOOST_CHECK (!testPool.lookupOutpoint (txParent.GetBareTxid (), tx));
233
+ BOOST_CHECK (!testPool.lookupOutpoint (txChild[0 ].GetHash (), tx));
234
+ BOOST_CHECK (testPool.lookupOutpoint (txChild[0 ].GetBareTxid (), tx));
235
+
236
+ BOOST_CHECK (viewPool.HaveCoins (txParent.GetHash ()));
237
+ BOOST_CHECK (viewPool.GetCoins (txParent.GetHash (), c));
238
+ BOOST_CHECK (!viewPool.HaveCoins (txParent.GetBareTxid ()));
239
+ BOOST_CHECK (!viewPool.GetCoins (txParent.GetBareTxid (), c));
240
+
241
+ BOOST_CHECK (!viewPool.HaveCoins (txChild[0 ].GetHash ()));
242
+ BOOST_CHECK (!viewPool.GetCoins (txChild[0 ].GetHash (), c));
243
+ BOOST_CHECK (viewPool.HaveCoins (txChild[0 ].GetBareTxid ()));
244
+ BOOST_CHECK (viewPool.GetCoins (txChild[0 ].GetBareTxid (), c));
245
+ }
246
+
192
247
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments