MCPcopy Create free account
hub / github.com/ElementsProject/elements / BuildSingleTest

Function BuildSingleTest

src/test/txrequest_tests.cpp:247–300  ·  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

245 * config is an integer in [0, 32), which controls which variant of the test is used.
246 */
247void BuildSingleTest(Scenario& scenario, int config)
248{
249 auto peer = scenario.NewPeer();
250 auto gtxid = scenario.NewGTxid();
251 bool immediate = config & 1;
252 bool preferred = config & 2;
253 auto delay = immediate ? NO_TIME : RandomTime8s();
254
255 scenario.SetTestName(strprintf("Single(config=%i)", config));
256
257 // Receive an announcement, either immediately requestable or delayed.
258 scenario.ReceivedInv(peer, gtxid, preferred, immediate ? MIN_TIME : scenario.Now() + delay);
259 if (immediate) {
260 scenario.Check(peer, {gtxid}, 1, 0, 0, "s1");
261 } else {
262 scenario.Check(peer, {}, 1, 0, 0, "s2");
263 scenario.AdvanceTime(delay - MICROSECOND);
264 scenario.Check(peer, {}, 1, 0, 0, "s3");
265 scenario.AdvanceTime(MICROSECOND);
266 scenario.Check(peer, {gtxid}, 1, 0, 0, "s4");
267 }
268
269 if (config >> 3) { // We'll request the transaction
270 scenario.AdvanceTime(RandomTime8s());
271 auto expiry = RandomTime8s();
272 scenario.Check(peer, {gtxid}, 1, 0, 0, "s5");
273 scenario.RequestedTx(peer, gtxid.GetHash(), scenario.Now() + expiry);
274 scenario.Check(peer, {}, 0, 1, 0, "s6");
275
276 if ((config >> 3) == 1) { // The request will time out
277 scenario.AdvanceTime(expiry - MICROSECOND);
278 scenario.Check(peer, {}, 0, 1, 0, "s7");
279 scenario.AdvanceTime(MICROSECOND);
280 scenario.Check(peer, {}, 0, 0, 0, "s8");
281 scenario.CheckExpired(peer, gtxid);
282 return;
283 } else {
284 scenario.AdvanceTime(std::chrono::microseconds{InsecureRandRange(expiry.count())});
285 scenario.Check(peer, {}, 0, 1, 0, "s9");
286 if ((config >> 3) == 3) { // A response will arrive for the transaction
287 scenario.ReceivedResponse(peer, gtxid.GetHash());
288 scenario.Check(peer, {}, 0, 0, 0, "s10");
289 return;
290 }
291 }
292 }
293
294 if (config & 4) { // The peer will go offline
295 scenario.DisconnectedPeer(peer);
296 } else { // The transaction is no longer needed
297 scenario.ForgetTxHash(gtxid.GetHash());
298 }
299 scenario.Check(peer, {}, 0, 0, 0, "s11");
300}
301
302/** Add to scenario a test with a single tx announced by two peers, to verify the
303 * right peer is selected for requests.

Callers 1

TestInterleavedScenariosFunction · 0.85

Calls 15

RandomTime8sFunction · 0.85
InsecureRandRangeFunction · 0.85
NewPeerMethod · 0.80
NewGTxidMethod · 0.80
SetTestNameMethod · 0.80
CheckExpiredMethod · 0.80
countMethod · 0.80
ReceivedInvMethod · 0.45
NowMethod · 0.45
CheckMethod · 0.45
AdvanceTimeMethod · 0.45
RequestedTxMethod · 0.45

Tested by

no test coverage detected