@@ -26,20 +26,6 @@ class TxOrphanageTest : public node::TxOrphanage
26
26
public:
27
27
TxOrphanageTest (FastRandomContext& rng) : m_rng{rng} {}
28
28
29
- inline size_t CountOrphans () const
30
- {
31
- return m_orphans.size ();
32
- }
33
-
34
- CTransactionRef RandomOrphan ()
35
- {
36
- std::map<Wtxid, OrphanTx>::iterator it;
37
- it = m_orphans.lower_bound (Wtxid::FromUint256 (m_rng.rand256 ()));
38
- if (it == m_orphans.end ())
39
- it = m_orphans.begin ();
40
- return it->second .tx ;
41
- }
42
-
43
29
FastRandomContext& m_rng;
44
30
};
45
31
@@ -114,6 +100,8 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
114
100
auto now{GetTime<std::chrono::seconds>()};
115
101
SetMockTime (now);
116
102
103
+ std::vector<CTransactionRef> orphans_added;
104
+
117
105
// 50 orphan transactions:
118
106
for (int i = 0 ; i < 50 ; i++)
119
107
{
@@ -126,13 +114,15 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
126
114
tx.vout [0 ].nValue = i*CENT;
127
115
tx.vout [0 ].scriptPubKey = GetScriptForDestination (PKHash (key.GetPubKey ()));
128
116
129
- orphanage.AddTx (MakeTransactionRef (tx), i);
117
+ auto ptx = MakeTransactionRef (tx);
118
+ orphanage.AddTx (ptx, i);
119
+ orphans_added.emplace_back (ptx);
130
120
}
131
121
132
122
// ... and 50 that depend on other orphans:
133
123
for (int i = 0 ; i < 50 ; i++)
134
124
{
135
- CTransactionRef txPrev = orphanage. RandomOrphan () ;
125
+ const auto & txPrev = orphans_added[m_rng. randrange (orphans_added. size ())] ;
136
126
137
127
CMutableTransaction tx;
138
128
tx.vin .resize (1 );
@@ -144,13 +134,15 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
144
134
SignatureData empty;
145
135
BOOST_CHECK (SignSignature (keystore, *txPrev, tx, 0 , SIGHASH_ALL, empty));
146
136
147
- orphanage.AddTx (MakeTransactionRef (tx), i);
137
+ auto ptx = MakeTransactionRef (tx);
138
+ orphanage.AddTx (ptx, i);
139
+ orphans_added.emplace_back (ptx);
148
140
}
149
141
150
142
// This really-big orphan should be ignored:
151
143
for (int i = 0 ; i < 10 ; i++)
152
144
{
153
- CTransactionRef txPrev = orphanage. RandomOrphan () ;
145
+ const auto & txPrev = orphans_added[m_rng. randrange (orphans_added. size ())] ;
154
146
155
147
CMutableTransaction tx;
156
148
tx.vout .resize (1 );
@@ -172,52 +164,52 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
172
164
BOOST_CHECK (!orphanage.AddTx (MakeTransactionRef (tx), i));
173
165
}
174
166
175
- size_t expected_num_orphans = orphanage.CountOrphans ();
167
+ size_t expected_num_orphans = orphanage.Size ();
176
168
177
169
// Non-existent peer; nothing should be deleted
178
170
orphanage.EraseForPeer (/* peer=*/ -1 );
179
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), expected_num_orphans);
171
+ BOOST_CHECK_EQUAL (orphanage.Size (), expected_num_orphans);
180
172
181
173
// Each of first three peers stored
182
174
// two transactions each.
183
175
for (NodeId i = 0 ; i < 3 ; i++)
184
176
{
185
177
orphanage.EraseForPeer (i);
186
178
expected_num_orphans -= 2 ;
187
- BOOST_CHECK (orphanage.CountOrphans () == expected_num_orphans);
179
+ BOOST_CHECK (orphanage.Size () == expected_num_orphans);
188
180
}
189
181
190
182
// Test LimitOrphanTxSize() function, nothing should timeout:
191
183
FastRandomContext rng{/* fDeterministic=*/ true };
192
184
orphanage.LimitOrphans (/* max_orphans=*/ expected_num_orphans, rng);
193
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), expected_num_orphans);
185
+ BOOST_CHECK_EQUAL (orphanage.Size (), expected_num_orphans);
194
186
expected_num_orphans -= 1 ;
195
187
orphanage.LimitOrphans (/* max_orphans=*/ expected_num_orphans, rng);
196
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), expected_num_orphans);
188
+ BOOST_CHECK_EQUAL (orphanage.Size (), expected_num_orphans);
197
189
assert (expected_num_orphans > 40 );
198
190
orphanage.LimitOrphans (40 , rng);
199
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 40 );
191
+ BOOST_CHECK_EQUAL (orphanage.Size (), 40 );
200
192
orphanage.LimitOrphans (10 , rng);
201
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 10 );
193
+ BOOST_CHECK_EQUAL (orphanage.Size (), 10 );
202
194
orphanage.LimitOrphans (0 , rng);
203
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 0 );
195
+ BOOST_CHECK_EQUAL (orphanage.Size (), 0 );
204
196
205
197
// Add one more orphan, check timeout logic
206
198
auto timeout_tx = MakeTransactionSpending (/* outpoints=*/ {}, rng);
207
199
orphanage.AddTx (timeout_tx, 0 );
208
200
orphanage.LimitOrphans (1 , rng);
209
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 1 );
201
+ BOOST_CHECK_EQUAL (orphanage.Size (), 1 );
210
202
211
203
// One second shy of expiration
212
204
SetMockTime (now + node::ORPHAN_TX_EXPIRE_TIME - 1s);
213
205
orphanage.LimitOrphans (1 , rng);
214
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 1 );
206
+ BOOST_CHECK_EQUAL (orphanage.Size (), 1 );
215
207
216
208
// Jump one more second, orphan should be timed out on limiting
217
209
SetMockTime (now + node::ORPHAN_TX_EXPIRE_TIME);
218
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 1 );
210
+ BOOST_CHECK_EQUAL (orphanage.Size (), 1 );
219
211
orphanage.LimitOrphans (1 , rng);
220
- BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 0 );
212
+ BOOST_CHECK_EQUAL (orphanage.Size (), 0 );
221
213
}
222
214
223
215
BOOST_AUTO_TEST_CASE (same_txid_diff_witness)
0 commit comments