zebra-network 15.0.0

Networking code for Zebra
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Regression checkpoints for inventory failure eligibility, attribution, and wakeups.

use std::{future::Future, sync::Arc};

use futures::{channel::mpsc, SinkExt};
use zebra_chain::serialization::SerializationError;
use zebra_test::mock_service::{MockService, PanicAssertion};

use crate::{
    constants::REQUEST_TIMEOUT,
    peer::{Connection, ErrorSlot},
    peer_set::ActiveConnectionCounter,
    protocol::{external::Message, internal::InventoryResponse},
};

use super::*;

/// A real block and matching download request, so successful routing also completes the download.
fn block_download() -> (Request, InventoryHash, Response) {
    let block: block::Block = zebra_test::vectors::BLOCK_MAINNET_10_BYTES
        .zcash_deserialize_into()
        .expect("the historical block vector is valid");
    let hash = block::Hash::from(&block);
    (
        Request::BlocksByHash(iter::once(hash).collect()),
        InventoryHash::Block(hash),
        Response::Blocks(vec![InventoryResponse::Available((Arc::new(block), None))]),
    )
}

/// Complete an already dispatched download through the real client response channel.
async fn finish_download(
    handle: &mut ClientTestHarness,
    request: Request,
    response: Response,
    response_fut: impl Future<Output = Result<Response, BoxError>>,
) {
    let client_request = handle
        .try_to_receive_outbound_client_request()
        .request()
        .expect("the eligible peer must receive the download before its queue deadline");
    assert_eq!(client_request.request, request);
    client_request
        .tx
        .send(Ok(response.clone()))
        .expect("the download is still waiting for its response");
    assert_eq!(
        timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT, response_fut)
            .await
            .expect("a dispatched download must not hang")
            .expect("the peer returned the requested block"),
        response,
    );
}

/// Carried provenance cannot override explicit missing inventory or a completed peer failure.
#[tokio::test(start_paused = true)]
async fn carried_advertiser_respects_missing_and_failed_candidates() {
    let _init_guard = zebra_test::init();
    for failed in [false, true] {
        let (mut peer_set, mut guard, addrs, mut handles, _best_tip) =
            peer_set_with_services(&SERVING_AND_NON_SERVING);
        let (_, inventory, response) = block_download();
        let InventoryHash::Block(hash) = inventory else {
            unreachable!("the helper constructs block inventory");
        };
        let request = Request::BlockDownload {
            hash,
            advertiser: Some(addrs[1]),
        };

        if failed {
            assert!(respond_to_request(
                &mut peer_set,
                &mut handles[1],
                request.clone(),
                Err(PeerError::ConnectionReceiveTimeout.into()),
            )
            .await
            .is_err());
        } else {
            send_inventory(
                &mut guard,
                InventoryStatus::new_missing(inventory, addrs[1]),
            );
        }

        let download = peer_set
            .ready()
            .await
            .expect("the healthy serving connection remains ready")
            .call(request.clone());
        assert_eq!(
            received_request(&mut handles[1]),
            None,
            "known provenance must not restore an ineligible source",
        );
        finish_download(&mut handles[0], request, response, download).await;
    }
}

/// N7: a failed connected advertisement must not redirect historic work to unrelated peers.
#[tokio::test(start_paused = true)]
async fn failed_advertiser_does_not_enable_nonserving_fallback() {
    let _init_guard = zebra_test::init();
    let (mut peer_set, mut guard, addrs, mut handles, _best_tip) = peer_set_with_services(&[
        PeerServices::NODE_NETWORK,
        PeerServices::empty(),
        PeerServices::empty(),
    ]);
    let (request, hash, response) = block_download();
    send_inventory(&mut guard, InventoryStatus::new_available(hash, addrs[1]));
    assert!(respond_to_request(
        &mut peer_set,
        &mut handles[1],
        request.clone(),
        Err(PeerError::ConnectionReceiveTimeout.into()),
    )
    .await
    .is_err());

    let _busy_futs = make_serving_peer_busy(&mut peer_set, addrs[0]).await;
    assert!(peer_set.has_peer_with_addr(addrs[1]));
    assert!(peer_set
        .inventory_registry
        .failed_peers(hash)
        .any(|p| *p == addrs[1]));
    let mut queued = peer_set
        .ready()
        .await
        .expect("fallback peers are ready")
        .call(request.clone());
    assert!(timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT / 4, &mut queued)
        .await
        .is_err());
    assert_eq!(received_request(&mut handles[1]), None);
    assert_eq!(
        received_request(&mut handles[2]),
        None,
        "the failed advertisement must not enable a non-serving fallback",
    );

    assert_eq!(received_request(&mut handles[0]), Some(block_request(1)));
    assert_eq!(received_request(&mut handles[0]), Some(block_request(9)));
    timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT / 4, peer_set.ready())
        .await
        .expect("released client capacity must become ready")
        .expect("the peer set remains healthy");
    finish_download(&mut handles[0], request, response, queued).await;
}

#[derive(Clone, Copy)]
enum CandidateChange {
    Failure,
    Removal,
    Advertisement,
}

/// Keep both serving-channel slots full across the event: no readiness transition can help.
async fn queued_block_reroutes_on_candidate_change(change: CandidateChange) {
    let _init_guard = zebra_test::init();
    let (initial_peers, addrs, mut handles) = mock_peers_with_services(&SERVING_AND_NON_SERVING);
    let (mut discovery_tx, discovery_rx) = mpsc::channel(2);
    let (minimum_peer_version, _best_tip) =
        MinimumPeerVersion::with_mock_chain_tip(&Network::Mainnet);
    let (mut peer_set, mut guard) = PeerSetBuilder::new()
        .with_discover(stream::select(initial_peers, discovery_rx))
        .with_minimum_peer_version(minimum_peer_version)
        .max_conns_per_ip(max(SERVING_AND_NON_SERVING.len(), DEFAULT_MAX_CONNS_PER_IP))
        .build();
    let (request, hash, response) = block_download();

    let first_response = peer_set
        .ready()
        .await
        .expect("serving peer is ready")
        .call(request.clone());
    let first_request = handles[0]
        .try_to_receive_outbound_client_request()
        .request()
        .expect("the serving peer receives the original download");
    assert_eq!(first_request.request, request);
    let _busy_futs = make_serving_peer_busy(&mut peer_set, addrs[0]).await;
    let (mut peer_set, poll_task) = peer_set.into_buffer(10);
    let mut queued = peer_set
        .ready()
        .await
        .expect("the buffer has room")
        .call(request.clone());
    assert!(timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT / 4, &mut queued)
        .await
        .is_err());
    assert_eq!(received_request(&mut handles[1]), None);

    match change {
        CandidateChange::Failure => {
            first_request
                .tx
                .send(Err(PeerError::ConnectionReceiveTimeout.into()))
                .expect("the original download is still waiting");
            timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT / 4, first_response)
                .await
                .expect("the real client must deliver its error")
                .expect_err("the original download failed");
        }
        CandidateChange::Removal => {
            discovery_tx
                .try_send(Ok(Change::Remove(addrs[0])))
                .expect("the discovery channel has room");
        }
        CandidateChange::Advertisement => {
            send_inventory(&mut guard, InventoryStatus::new_available(hash, addrs[1]));
        }
    }

    // No request or readiness poll is sent to Buffer after the candidate change. Only the
    // production notification task can wake it and dispatch the already queued download.
    tokio::time::sleep(INVENTORY_BUSY_PEER_WAIT_TIMEOUT / 4).await;
    finish_download(&mut handles[1], request, response, queued).await;
    poll_task.abort();
}

/// N9: an actual response error must wake and reroute queued work behind Buffer.
#[tokio::test(start_paused = true)]
async fn queued_block_reroutes_on_failure_behind_buffer() {
    queued_block_reroutes_on_candidate_change(CandidateChange::Failure).await;
}

/// N9: removing the only busy serving peer must immediately enable the ready fallback.
#[tokio::test(start_paused = true)]
async fn queued_block_reroutes_on_removal_behind_buffer() {
    queued_block_reroutes_on_candidate_change(CandidateChange::Removal).await;
}

/// N9: a useful advertisement must wake the idle Buffer without any peer becoming ready.
#[tokio::test(start_paused = true)]
async fn queued_block_reroutes_on_advertisement_behind_buffer() {
    queued_block_reroutes_on_candidate_change(CandidateChange::Advertisement).await;
}

/// N13: dropping a pending caller is not evidence that its connected peer failed.
#[tokio::test(start_paused = true)]
async fn caller_drop_does_not_record_block_failure() {
    let _init_guard = zebra_test::init();
    let (mut peer_set, _guard, addrs, mut handles, _best_tip) =
        peer_set_with_services(&SERVING_AND_NON_SERVING);
    let (request, hash, response) = block_download();
    let mut pending = peer_set
        .ready()
        .await
        .expect("serving peer is ready")
        .call(request.clone());
    let in_flight = handles[0]
        .try_to_receive_outbound_client_request()
        .request()
        .expect("the serving peer received the download");
    assert!(pending.as_mut().now_or_never().is_none());
    drop(pending);
    assert!(in_flight.tx.is_canceled());

    peer_set
        .ready()
        .await
        .expect("caller cancellation leaves the peer usable");
    assert_eq!(peer_set.inventory_registry.failed_peers(hash).count(), 0);
    assert_eq!(peer_set.inventory_registry.missing_peers(hash).count(), 0);
    assert!(peer_set.has_peer_with_addr(addrs[0]));
    let retry = peer_set.call(request.clone());
    assert_eq!(received_request(&mut handles[1]), None);
    finish_download(&mut handles[0], request, response, retry).await;
}

/// Feed a real peer response (or silence) through Connection, Client, and PeerSet accounting.
async fn connection_block_error_attribution(not_found: bool) {
    let _init_guard = zebra_test::init();
    let (mut peer_set, mut guard, addrs, mut handles, _best_tip) =
        peer_set_with_services(&SERVING_AND_NON_SERVING);
    let (request, hash, response) = block_download();
    peer_set.ready().await.expect("serving peer is ready");
    let connection_info = peer_set.ready_services[&addrs[0]].connection_info().clone();
    let pending = peer_set.call(request.clone());
    let mut client_request = handles[0]
        .try_to_receive_outbound_client_request()
        .request()
        .expect("the serving client received the download");

    // The channel harness normally has an isolated collector. Connect the production response
    // sender to this PeerSet's collector instead of fabricating a missing-inventory event.
    client_request.inv_collector = guard.inventory_sender().as_ref().cloned();
    client_request.transient_addr = Some(addrs[0]);
    let (mut client_tx, client_rx) = mpsc::channel(1);
    let (_advert_tx, advert_rx) = tokio::sync::mpsc::channel(1);
    let (mut remote_tx, remote_rx) = mpsc::channel(1);
    let (outbound_tx, mut outbound_rx) = mpsc::channel(1);
    let inbound: MockService<Request, Response, PanicAssertion> = MockService::build().finish();
    let connection = Connection::new(
        inbound,
        (
            client_rx,
            advert_rx,
            peer_set.minimum_peer_version.clone(),
            peer_set.bans_receiver.clone(),
        ),
        ErrorSlot::default(),
        outbound_tx.sink_map_err(|_| {
            SerializationError::from(std::io::Error::new(
                std::io::ErrorKind::BrokenPipe,
                "test peer message receiver closed",
            ))
        }),
        ActiveConnectionCounter::new_counter().track_connection(),
        connection_info,
        Vec::new(),
    );
    let connection_task = tokio::spawn(connection.run(remote_rx));
    client_tx
        .try_send(client_request)
        .expect("the client channel has room");
    assert_eq!(
        timeout(REQUEST_TIMEOUT / 4, outbound_rx.next())
            .await
            .expect("the connection must send getdata before its response timeout"),
        Some(Message::GetData(vec![hash])),
    );
    if not_found {
        remote_tx
            .try_send(Ok(Message::NotFound(vec![hash])))
            .expect("the peer response channel has room");
    }
    let error = timeout(REQUEST_TIMEOUT + Duration::from_secs(1), pending)
        .await
        .expect("the production response deadline must complete the request")
        .expect_err("the peer either refused the block or timed out");
    let error = error
        .downcast_ref::<SharedPeerError>()
        .expect("client errors retain their type");
    if not_found {
        assert!(error.inner_debug().contains("NotFoundResponse"));
    } else {
        assert_eq!(error.inner_debug(), "ConnectionReceiveTimeout");
    }

    peer_set
        .ready()
        .await
        .expect("the fallback peer remains ready");
    assert_eq!(
        peer_set
            .inventory_registry
            .missing_peers(hash)
            .copied()
            .collect::<Vec<_>>(),
        if not_found { vec![addrs[0]] } else { vec![] },
    );
    assert_eq!(
        peer_set
            .inventory_registry
            .failed_peers(hash)
            .copied()
            .collect::<Vec<_>>(),
        if not_found { vec![] } else { vec![addrs[0]] },
    );
    let retry = peer_set.call(request.clone());
    assert_eq!(received_request(&mut handles[0]), None);
    finish_download(&mut handles[1], request, response, retry).await;
    connection_task.abort();
}

/// Preservation: an explicit wire notfound records missing inventory, never a peer failure.
#[tokio::test(start_paused = true)]
async fn explicit_notfound_records_missing_without_block_failure() {
    connection_block_error_attribution(true).await;
}

/// Preservation/N13 control: real response-deadline expiry still records a genuine failure.
#[tokio::test(start_paused = true)]
async fn connection_timeout_records_block_failure_without_missing() {
    connection_block_error_attribution(false).await;
}

/// N14: busy non-serving failed peers have the same last-resort eligibility as ready ones.
#[tokio::test(start_paused = true)]
async fn busy_failed_nonserving_peer_remains_last_resort() {
    let _init_guard = zebra_test::init();
    let (mut peer_set, mut guard, addrs, mut handles, _best_tip) =
        peer_set_with_services(&[PeerServices::empty(), PeerServices::empty()]);
    let (request, hash, response) = block_download();
    send_inventory(&mut guard, InventoryStatus::new_missing(hash, addrs[1]));
    assert!(respond_to_request(
        &mut peer_set,
        &mut handles[0],
        request.clone(),
        Err(PeerError::ConnectionReceiveTimeout.into()),
    )
    .await
    .is_err());

    // Advertise only the unrelated requests used to fill this client's channel. The target
    // block has no advertisement and its only other ready peer explicitly lacks it.
    let mut busy = Vec::new();
    for byte in [1, 9] {
        send_inventory(
            &mut guard,
            InventoryStatus::new_available(InventoryHash::Block(block::Hash([byte; 32])), addrs[0]),
        );
        busy.push(
            peer_set
                .ready()
                .await
                .expect("a peer is ready")
                .call(block_request(byte)),
        );
    }
    peer_set
        .ready()
        .await
        .expect("the missing peer is still ready");
    assert!(peer_set.cancel_handles.contains_key(&addrs[0]));
    let mut queued = peer_set.call(request.clone());
    assert!(
        timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT / 4, &mut queued)
            .await
            .is_err(),
        "the only non-missing peer must be awaited rather than immediately refused",
    );
    assert_eq!(received_request(&mut handles[1]), None);
    assert_eq!(received_request(&mut handles[0]), Some(block_request(1)));
    assert_eq!(received_request(&mut handles[0]), Some(block_request(9)));
    timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT / 4, peer_set.ready())
        .await
        .expect("released client capacity must become ready")
        .expect("the peer set remains healthy");
    finish_download(&mut handles[0], request, response, queued).await;
}

#[derive(Clone, Copy)]
enum ReplacementFailure {
    Current,
    Previous,
    Delayed,
}

async fn replacement_connection_ignores_old_failure(failure: ReplacementFailure) {
    let _init_guard = zebra_test::init();
    let (initial_peers, addrs, mut handles) = mock_peers_with_services(&SERVING_AND_NON_SERVING);
    let (mut discovery_tx, discovery_rx) = mpsc::channel(2);
    let (minimum_peer_version, _best_tip) =
        MinimumPeerVersion::with_mock_chain_tip(&Network::Mainnet);
    let (mut peer_set, _guard) = PeerSetBuilder::new()
        .with_discover(stream::select(initial_peers, discovery_rx))
        .with_minimum_peer_version(minimum_peer_version)
        .max_conns_per_ip(max(SERVING_AND_NON_SERVING.len(), DEFAULT_MAX_CONNS_PER_IP))
        .build();
    let (request, hash, response) = block_download();
    let old_response = peer_set
        .ready()
        .await
        .expect("old connection is ready")
        .call(request.clone());
    let old_request = handles[0]
        .try_to_receive_outbound_client_request()
        .request()
        .expect("the old connection receives the download");
    let delayed = if matches!(failure, ReplacementFailure::Delayed) {
        Some((old_response, old_request))
    } else {
        old_request
            .tx
            .send(Err(PeerError::ConnectionReceiveTimeout.into()))
            .expect("the old request is waiting for its response");
        timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT, old_response)
            .await
            .expect("the old response must not hang")
            .expect_err("the old connection failed");
        peer_set.ready().await.expect("fallback remains ready");
        assert_eq!(
            peer_set
                .inventory_registry
                .failed_peers(hash)
                .copied()
                .collect::<Vec<_>>(),
            vec![addrs[0]]
        );
        if matches!(failure, ReplacementFailure::Previous) {
            tokio::time::advance(crate::constants::INVENTORY_ROTATION_INTERVAL).await;
            peer_set
                .ready()
                .await
                .expect("rotation leaves connected peers usable");
        }
        None
    };

    let (replacement, mut replacement_handle) = ClientTestHarness::build()
        .with_version(CURRENT_NETWORK_PROTOCOL_VERSION)
        .with_services(PeerServices::NODE_NETWORK)
        .finish();
    discovery_tx
        .try_send(Ok(Change::Remove(addrs[0])))
        .expect("the discovery channel has room");
    discovery_tx
        .try_send(Ok(Change::Insert(addrs[0], replacement.into())))
        .expect("the discovery channel has room for the replacement");
    timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT, peer_set.ready())
        .await
        .expect("the replacement must become ready")
        .expect("the peer set remains healthy");
    assert!(!handles[0].wants_connection_heartbeats());

    if let Some((old_response, old_request)) = delayed {
        old_request
            .tx
            .send(Err(PeerError::ConnectionReceiveTimeout.into()))
            .expect("the old request still owns its response receiver");
        timeout(INVENTORY_BUSY_PEER_WAIT_TIMEOUT, old_response)
            .await
            .expect("the delayed response must not hang")
            .expect_err("the old request failed");
        peer_set
            .ready()
            .await
            .expect("the replacement remains ready");
    }
    let retry = peer_set.call(request.clone());
    assert_eq!(
        received_request(&mut handles[1]),
        None,
        "the replacement must not lose serving preference to its predecessor's failure",
    );
    finish_download(&mut replacement_handle, request, response, retry).await;
    assert_eq!(peer_set.inventory_registry.failed_peers(hash).count(), 0);
}

/// N15: neither failure-cache generation may carry an old connection's failures forward.
#[tokio::test(start_paused = true)]
async fn replacement_connection_does_not_inherit_cached_block_failure() {
    for failure in [ReplacementFailure::Current, ReplacementFailure::Previous] {
        replacement_connection_ignores_old_failure(failure).await;
    }
}

/// N15: clearing the cache on replacement is insufficient if delayed events lack identity.
#[tokio::test(start_paused = true)]
async fn replacement_connection_ignores_delayed_old_block_failure() {
    replacement_connection_ignores_old_failure(ReplacementFailure::Delayed).await;
}

/// Missed rotations must not immediately age out a newly registered block failure.
#[tokio::test(start_paused = true)]
async fn overdue_inventory_rotations_preserve_new_block_failure() {
    let _init_guard = zebra_test::init();
    let (mut peer_set, _guard, addrs, mut handles, _best_tip) =
        peer_set_with_services(&SERVING_AND_NON_SERVING);
    let (request, hash, _) = block_download();
    respond_to_request(
        &mut peer_set,
        &mut handles[0],
        request,
        Err(PeerError::ConnectionReceiveTimeout.into()),
    )
    .await
    .expect_err("the serving peer failed the block request");

    // Leave the completed failure queued while three rotation ticks become overdue.
    tokio::time::advance(3 * crate::constants::INVENTORY_ROTATION_INTERVAL).await;
    for _ in 0..3 {
        peer_set.ready().await.expect("fallback remains ready");
    }
    assert!(
        peer_set
            .inventory_registry
            .failed_peers(hash)
            .any(|peer| *peer == addrs[0]),
        "missed ticks must not expire a failure registered on the first poll",
    );
}