@@ -114,6 +114,10 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
114
114
FillableSigningProvider keystore;
115
115
BOOST_CHECK (keystore.AddKey (key));
116
116
117
+ // Freeze time for length of test
118
+ auto now{GetTime<std::chrono::seconds>()};
119
+ SetMockTime (now);
120
+
117
121
// 50 orphan transactions:
118
122
for (int i = 0 ; i < 50 ; i++)
119
123
{
@@ -172,22 +176,52 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
172
176
BOOST_CHECK (!orphanage.AddTx (MakeTransactionRef (tx), i));
173
177
}
174
178
175
- // Test EraseOrphansFor:
179
+ size_t expected_num_orphans = orphanage.CountOrphans ();
180
+
181
+ // Non-existent peer; nothing should be deleted
182
+ orphanage.EraseForPeer (/* peer=*/ -1 );
183
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), expected_num_orphans);
184
+
185
+ // Each of first three peers stored
186
+ // two transactions each.
176
187
for (NodeId i = 0 ; i < 3 ; i++)
177
188
{
178
- size_t sizeBefore = orphanage.CountOrphans ();
179
189
orphanage.EraseForPeer (i);
180
- BOOST_CHECK (orphanage.CountOrphans () < sizeBefore);
190
+ expected_num_orphans -= 2 ;
191
+ BOOST_CHECK (orphanage.CountOrphans () == expected_num_orphans);
181
192
}
182
193
183
- // Test LimitOrphanTxSize() function:
194
+ // Test LimitOrphanTxSize() function, nothing should timeout :
184
195
FastRandomContext rng{/* fDeterministic=*/ true };
196
+ orphanage.LimitOrphans (/* max_orphans=*/ expected_num_orphans, rng);
197
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), expected_num_orphans);
198
+ expected_num_orphans -= 1 ;
199
+ orphanage.LimitOrphans (/* max_orphans=*/ expected_num_orphans, rng);
200
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), expected_num_orphans);
201
+ assert (expected_num_orphans > 40 );
185
202
orphanage.LimitOrphans (40 , rng);
186
- BOOST_CHECK (orphanage.CountOrphans () <= 40 );
203
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 40 );
187
204
orphanage.LimitOrphans (10 , rng);
188
- BOOST_CHECK (orphanage.CountOrphans () <= 10 );
205
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 10 );
189
206
orphanage.LimitOrphans (0 , rng);
190
- BOOST_CHECK (orphanage.CountOrphans () == 0 );
207
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 0 );
208
+
209
+ // Add one more orphan, check timeout logic
210
+ auto timeout_tx = MakeTransactionSpending (/* outpoints=*/ {}, rng);
211
+ orphanage.AddTx (timeout_tx, 0 );
212
+ orphanage.LimitOrphans (1 , rng);
213
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 1 );
214
+
215
+ // One second shy of expiration
216
+ SetMockTime (now + ORPHAN_TX_EXPIRE_TIME - 1s);
217
+ orphanage.LimitOrphans (1 , rng);
218
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 1 );
219
+
220
+ // Jump one more second, orphan should be timed out on limiting
221
+ SetMockTime (now + ORPHAN_TX_EXPIRE_TIME);
222
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 1 );
223
+ orphanage.LimitOrphans (1 , rng);
224
+ BOOST_CHECK_EQUAL (orphanage.CountOrphans (), 0 );
191
225
}
192
226
193
227
BOOST_AUTO_TEST_CASE (same_txid_diff_witness)
0 commit comments