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
{
50
+ testPool.setSanityCheck (true );
51
+ auto * utxoHasher = new MockUtxoHasher ();
52
+ testPool.utxoHasher .reset (utxoHasher);
53
+ testPool.clear ();
54
+
55
+ CMutableTransaction base;
56
+ base.vout .emplace_back (99000LL , CScript () << OP_11 << OP_EQUAL);
57
+ view.ModifyCoins (base.GetHash ())->FromTx (base, 0 );
58
+
59
+ tip.pprev = nullptr ;
60
+ tip.nHeight = 0 ;
61
+ mapBlockIndex[tip.GetBlockHeader ().GetHash ()] = &tip;
62
+ view.SetBestBlock (tip.GetBlockHeader ().GetHash ());
63
+ chainActive.SetTip (&tip);
64
+
33
65
txParent.vin .resize (1 );
34
66
txParent.vin [0 ].scriptSig = CScript () << OP_11;
67
+ txParent.vin [0 ].prevout .hash = base.GetHash ();
68
+ txParent.vin [0 ].prevout .n = 0 ;
35
69
txParent.vout .resize (3 );
36
70
for (int i = 0 ; i < 3 ; i++)
37
71
{
@@ -48,20 +82,37 @@ class MempoolTestFixture
48
82
txChild[i].vout .resize (1 );
49
83
txChild[i].vout [0 ].scriptPubKey = CScript () << OP_11 << OP_EQUAL;
50
84
txChild[i].vout [0 ].nValue = 11000LL ;
85
+ utxoHasher->UseBareTxid (txChild[i]);
51
86
}
52
87
53
88
for (int i = 0 ; i < 3 ; i++)
54
89
{
55
90
txGrandChild[i].vin .resize (1 );
56
91
txGrandChild[i].vin [0 ].scriptSig = CScript () << OP_11;
57
- txGrandChild[i].vin [0 ].prevout .hash = txChild[i].GetHash ();
92
+ txGrandChild[i].vin [0 ].prevout .hash = txChild[i].GetBareTxid ();
58
93
txGrandChild[i].vin [0 ].prevout .n = 0 ;
59
94
txGrandChild[i].vout .resize (1 );
60
95
txGrandChild[i].vout [0 ].scriptPubKey = CScript () << OP_11 << OP_EQUAL;
61
96
txGrandChild[i].vout [0 ].nValue = 11000LL ;
62
97
}
63
98
}
64
99
100
+ ~MempoolTestFixture ()
101
+ {
102
+ mapBlockIndex.clear ();
103
+ }
104
+
105
+ /* * Adds the parent, childs and grandchilds to the mempool. */
106
+ void AddAll ()
107
+ {
108
+ testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ));
109
+ for (int i = 0 ; i < 3 ; i++)
110
+ {
111
+ testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ));
112
+ testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ));
113
+ }
114
+ }
115
+
65
116
};
66
117
67
118
BOOST_FIXTURE_TEST_SUITE (mempool_tests, MempoolTestFixture)
@@ -83,12 +134,10 @@ BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
83
134
removed.clear ();
84
135
85
136
// Parent, children, grandchildren:
86
- testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ));
87
- for (int i = 0 ; i < 3 ; i++)
88
- {
89
- testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ));
90
- testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ));
91
- }
137
+ AddAll ();
138
+
139
+ testPool.check (&view);
140
+
92
141
// Remove Child[0], GrandChild[0] should be removed:
93
142
testPool.remove (txChild[0 ], removed, true );
94
143
BOOST_CHECK_EQUAL (removed.size (), 2 );
@@ -123,12 +172,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexByBareTxid)
123
172
CTransaction tx;
124
173
std::list<CTransaction> removed;
125
174
126
- testPool.addUnchecked (txParent.GetHash (), CTxMemPoolEntry (txParent, 0 , 0 , 0.0 , 1 ));
127
- for (int i = 0 ; i < 3 ; ++i)
128
- {
129
- testPool.addUnchecked (txChild[i].GetHash (), CTxMemPoolEntry (txChild[i], 0 , 0 , 0.0 , 1 ));
130
- testPool.addUnchecked (txGrandChild[i].GetHash (), CTxMemPoolEntry (txGrandChild[i], 0 , 0 , 0.0 , 1 ));
131
- }
175
+ AddAll ();
132
176
133
177
BOOST_CHECK (testPool.lookupBareTxid (txParent.GetBareTxid (), tx));
134
178
BOOST_CHECK (tx.GetHash () == txParent.GetHash ());
@@ -140,4 +184,28 @@ BOOST_AUTO_TEST_CASE(MempoolIndexByBareTxid)
140
184
BOOST_CHECK (!testPool.lookupBareTxid (txGrandChild[0 ].GetBareTxid (), tx));
141
185
}
142
186
187
+ BOOST_AUTO_TEST_CASE (MempoolOutpointLookup)
188
+ {
189
+ CTransaction tx;
190
+ CCoins coins;
191
+
192
+ AddAll ();
193
+ CCoinsViewMemPool viewPool (&view, testPool);
194
+
195
+ BOOST_CHECK (testPool.lookupOutpoint (txParent.GetHash (), tx));
196
+ BOOST_CHECK (!testPool.lookupOutpoint (txParent.GetBareTxid (), tx));
197
+ BOOST_CHECK (!testPool.lookupOutpoint (txChild[0 ].GetHash (), tx));
198
+ BOOST_CHECK (testPool.lookupOutpoint (txChild[0 ].GetBareTxid (), tx));
199
+
200
+ BOOST_CHECK (viewPool.HaveCoins (txParent.GetHash ()));
201
+ BOOST_CHECK (viewPool.GetCoins (txParent.GetHash (), coins));
202
+ BOOST_CHECK (!viewPool.HaveCoins (txParent.GetBareTxid ()));
203
+ BOOST_CHECK (!viewPool.GetCoins (txParent.GetBareTxid (), coins));
204
+
205
+ BOOST_CHECK (!viewPool.HaveCoins (txChild[0 ].GetHash ()));
206
+ BOOST_CHECK (!viewPool.GetCoins (txChild[0 ].GetHash (), coins));
207
+ BOOST_CHECK (viewPool.HaveCoins (txChild[0 ].GetBareTxid ()));
208
+ BOOST_CHECK (viewPool.GetCoins (txChild[0 ].GetBareTxid (), coins));
209
+ }
210
+
143
211
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments