MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / BuildSingleTest

Method BuildSingleTest

src/test/txrequest_tests.cpp:265–318  ·  view source on GitHub ↗

Add to scenario a test with a single tx announced by a single peer. * * config is an integer in [0, 32), which controls which variant of the test is used. */

Source from the content-addressed store, hash-verified

263 * config is an integer in [0, 32), which controls which variant of the test is used.
264 */
265void TxRequestTest::BuildSingleTest(Scenario& scenario, int config)
266{
267 auto peer = scenario.NewPeer();
268 auto gtxid = scenario.NewGTxid();
269 bool immediate = config & 1;
270 bool preferred = config & 2;
271 auto delay = immediate ? NO_TIME : RandomTime8s();
272
273 scenario.SetTestName(strprintf("Single(config=%i)", config));
274
275 // Receive an announcement, either immediately requestable or delayed.
276 scenario.ReceivedInv(peer, gtxid, preferred, immediate ? MIN_TIME : scenario.Now() + delay);
277 if (immediate) {
278 scenario.Check(peer, {gtxid}, 1, 0, 0, "s1");
279 } else {
280 scenario.Check(peer, {}, 1, 0, 0, "s2");
281 scenario.AdvanceTime(delay - MICROSECOND);
282 scenario.Check(peer, {}, 1, 0, 0, "s3");
283 scenario.AdvanceTime(MICROSECOND);
284 scenario.Check(peer, {gtxid}, 1, 0, 0, "s4");
285 }
286
287 if (config >> 3) { // We'll request the transaction
288 scenario.AdvanceTime(RandomTime8s());
289 auto expiry = RandomTime8s();
290 scenario.Check(peer, {gtxid}, 1, 0, 0, "s5");
291 scenario.RequestedTx(peer, gtxid.ToUint256(), scenario.Now() + expiry);
292 scenario.Check(peer, {}, 0, 1, 0, "s6");
293
294 if ((config >> 3) == 1) { // The request will time out
295 scenario.AdvanceTime(expiry - MICROSECOND);
296 scenario.Check(peer, {}, 0, 1, 0, "s7");
297 scenario.AdvanceTime(MICROSECOND);
298 scenario.Check(peer, {}, 0, 0, 0, "s8");
299 scenario.CheckExpired(peer, gtxid);
300 return;
301 } else {
302 scenario.AdvanceTime(std::chrono::microseconds{m_rng.randrange(expiry.count())});
303 scenario.Check(peer, {}, 0, 1, 0, "s9");
304 if ((config >> 3) == 3) { // A response will arrive for the transaction
305 scenario.ReceivedResponse(peer, gtxid.ToUint256());
306 scenario.Check(peer, {}, 0, 0, 0, "s10");
307 return;
308 }
309 }
310 }
311
312 if (config & 4) { // The peer will go offline
313 scenario.DisconnectedPeer(peer);
314 } else { // The transaction is no longer needed
315 scenario.ForgetTxHash(gtxid.ToUint256());
316 }
317 scenario.Check(peer, {}, 0, 0, 0, "s11");
318}
319
320/** Add to scenario a test with a single tx announced by two peers, to verify the
321 * right peer is selected for requests.

Callers

nothing calls this directly

Calls 14

NewPeerMethod · 0.80
NewGTxidMethod · 0.80
SetTestNameMethod · 0.80
CheckExpiredMethod · 0.80
randrangeMethod · 0.80
countMethod · 0.80
ReceivedInvMethod · 0.45
NowMethod · 0.45
CheckMethod · 0.45
AdvanceTimeMethod · 0.45
RequestedTxMethod · 0.45
ReceivedResponseMethod · 0.45

Tested by

no test coverage detected