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
19
+ private:
20
+
21
+ /* * Empty coins view used to back the cached view we actually use. */
22
+ CCoinsView emptyView;
23
+
24
+ /* * Tip of our fake chain. */
25
+ CBlockIndex tip;
26
+
14
27
protected:
15
28
16
29
/* * A parent transaction. */
17
30
CMutableTransaction txParent;
18
31
19
- /* * Three children of the parent. */
32
+ /* * Three children of the parent. They use the bare txid for their UTXOs
33
+ * in our UTXO hasher. */
20
34
CMutableTransaction txChild[3 ];
21
35
22
36
/* * Three grand children. */
23
37
CMutableTransaction txGrandChild[3 ];
24
38
39
+ /* * Coins view with the parent inputs. */
40
+ CCoinsViewCache view;
41
+
25
42
/* * The test mempool instance. */
26
43
CTxMemPool testPool;
27
44
28
45
public:
29
46
30
47
MempoolTestFixture ()
31
- : testPool(CFeeRate(0 ))
48
+ : view(&emptyView), testPool(CFeeRate(0 ))
32
49
{
33
- txParent.vin .resize (2 );
50
+ std::unique_ptr<MockUtxoHasher> utxoHasher (new MockUtxoHasher ());
51
+
52
+ CMutableTransaction base;
53
+ base.vout .emplace_back (99000LL , CScript () << OP_11 << OP_EQUAL);
54
+ view.ModifyCoins (base.GetHash ())->FromTx (base, 0 );
55
+
56
+ tip.pprev = nullptr ;
57
+ tip.nHeight = 0 ;
58
+ mapBlockIndex[tip.GetBlockHeader ().GetHash ()] = &tip;
59
+ view.SetBestBlock (tip.GetBlockHeader ().GetHash ());
60
+ chainActive.SetTip (&tip);
61
+
62
+ txParent.vin .resize (1 );
34
63
txParent.vin [0 ].scriptSig = CScript () << OP_11;
35
- /* Add a second input to make sure the transaction does not qualify as
36
- coinbase and thus has a bare txid unequal to its normal hash. */
37
- txParent.vin [1 ].scriptSig = CScript () << OP_12;
64
+ txParent.vin [0 ].prevout .hash = base.GetHash ();
65
+ txParent.vin [0 ].prevout .n = 0 ;
38
66
txParent.vout .resize (3 );
39
67
for (int i = 0 ; i < 3 ; i++)
40
68
{
@@ -52,18 +80,39 @@ class MempoolTestFixture
52
80
txChild[i].vout .resize (1 );
53
81
txChild[i].vout [0 ].scriptPubKey = CScript () << OP_11 << OP_EQUAL;
54
82
txChild[i].vout [0 ].nValue = 11000LL ;
83
+ utxoHasher->UseBareTxid (txChild[i]);
55
84
}
56
85
57
86
for (int i = 0 ; i < 3 ; i++)
58
87
{
59
88
txGrandChild[i].vin .resize (1 );
60
89
txGrandChild[i].vin [0 ].scriptSig = CScript () << OP_11;
61
- txGrandChild[i].vin [0 ].prevout .hash = txChild[i].GetHash ();
90
+ txGrandChild[i].vin [0 ].prevout .hash = txChild[i].GetBareTxid ();
62
91
txGrandChild[i].vin [0 ].prevout .n = 0 ;
63
92
txGrandChild[i].vout .resize (1 );
64
93
txGrandChild[i].vout [0 ].scriptPubKey = CScript () << OP_11 << OP_EQUAL;
65
94
txGrandChild[i].vout [0 ].nValue = 11000LL ;
66
95
}
96
+
97
+ testPool.setSanityCheck (true );
98
+ testPool.SetUtxoHasherForTesting (std::move (utxoHasher));
99
+ testPool.clear ();
100
+ }
101
+
102
+ ~MempoolTestFixture ()
103
+ {
104
+ mapBlockIndex.clear ();
105
+ }
106
+
107
+ /* * Adds the parent, childs and grandchilds to the mempool. */
108
+ void AddAll ()
109
+ {
110
+ testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ));
111
+ for (int i = 0 ; i < 3 ; i++)
112
+ {
113
+ testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ));
114
+ testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ));
115
+ }
67
116
}
68
117
69
118
};
@@ -87,12 +136,10 @@ BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
87
136
removed.clear ();
88
137
89
138
// Parent, children, grandchildren:
90
- testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ));
91
- for (int i = 0 ; i < 3 ; i++)
92
- {
93
- testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ));
94
- testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ));
95
- }
139
+ AddAll ();
140
+
141
+ testPool.check (&view);
142
+
96
143
// Remove Child[0], GrandChild[0] should be removed:
97
144
testPool.remove (txChild[0 ], removed, true );
98
145
BOOST_CHECK_EQUAL (removed.size (), 2 );
@@ -127,12 +174,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexByBareTxid)
127
174
CTransaction tx;
128
175
std::list<CTransaction> removed;
129
176
130
- testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ));
131
- for (int i = 0 ; i < 3 ; ++i)
132
- {
133
- testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ));
134
- testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ));
135
- }
177
+ AddAll ();
136
178
137
179
BOOST_CHECK (testPool.lookupBareTxid (txParent.GetBareTxid (), tx));
138
180
BOOST_CHECK (tx.GetHash () == txParent.GetHash ());
@@ -144,4 +186,28 @@ BOOST_AUTO_TEST_CASE(MempoolIndexByBareTxid)
144
186
BOOST_CHECK (!testPool.lookupBareTxid (txGrandChild[0 ].GetBareTxid (), tx));
145
187
}
146
188
189
+ BOOST_AUTO_TEST_CASE (MempoolOutpointLookup)
190
+ {
191
+ CTransaction tx;
192
+ CCoins coins;
193
+
194
+ AddAll ();
195
+ CCoinsViewMemPool viewPool (&view, testPool);
196
+
197
+ BOOST_CHECK (testPool.lookupOutpoint (txParent.GetHash (), tx));
198
+ BOOST_CHECK (!testPool.lookupOutpoint (txParent.GetBareTxid (), tx));
199
+ BOOST_CHECK (!testPool.lookupOutpoint (txChild[0 ].GetHash (), tx));
200
+ BOOST_CHECK (testPool.lookupOutpoint (txChild[0 ].GetBareTxid (), tx));
201
+
202
+ BOOST_CHECK (viewPool.HaveCoins (txParent.GetHash ()));
203
+ BOOST_CHECK (viewPool.GetCoins (txParent.GetHash (), coins));
204
+ BOOST_CHECK (!viewPool.HaveCoins (txParent.GetBareTxid ()));
205
+ BOOST_CHECK (!viewPool.GetCoins (txParent.GetBareTxid (), coins));
206
+
207
+ BOOST_CHECK (!viewPool.HaveCoins (txChild[0 ].GetHash ()));
208
+ BOOST_CHECK (!viewPool.GetCoins (txChild[0 ].GetHash (), coins));
209
+ BOOST_CHECK (viewPool.HaveCoins (txChild[0 ].GetBareTxid ()));
210
+ BOOST_CHECK (viewPool.GetCoins (txChild[0 ].GetBareTxid (), coins));
211
+ }
212
+
147
213
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments