reth_downloaders/test_utils/
bodies_client.rs1use alloy_primitives::{map::B256Map, B256};
2use reth_ethereum_primitives::BlockBody;
3use reth_network_p2p::{
4 bodies::client::{BodiesClient, BodiesFut},
5 download::DownloadClient,
6 priority::Priority,
7};
8use reth_network_peers::PeerId;
9use std::{
10 fmt::Debug,
11 ops::RangeInclusive,
12 sync::{
13 atomic::{AtomicU64, Ordering},
14 Arc,
15 },
16 time::Duration,
17};
18use tokio::sync::Mutex;
19
20#[derive(Debug, Default)]
22pub struct TestBodiesClient {
23 bodies: Arc<Mutex<B256Map<BlockBody>>>,
24 should_delay: bool,
25 max_batch_size: Option<usize>,
26 times_requested: AtomicU64,
27 empty_response_mod: Option<u64>,
28 bad_messages: AtomicU64,
29}
30
31impl TestBodiesClient {
32 pub(crate) fn with_bodies(mut self, bodies: B256Map<BlockBody>) -> Self {
33 self.bodies = Arc::new(Mutex::new(bodies));
34 self
35 }
36
37 pub(crate) const fn with_should_delay(mut self, should_delay: bool) -> Self {
38 self.should_delay = should_delay;
39 self
40 }
41
42 pub(crate) const fn with_empty_responses(mut self, empty_mod: u64) -> Self {
45 self.empty_response_mod = Some(empty_mod);
46 self
47 }
48
49 pub(crate) const fn with_max_batch_size(mut self, max_batch_size: usize) -> Self {
50 self.max_batch_size = Some(max_batch_size);
51 self
52 }
53
54 pub(crate) fn times_requested(&self) -> u64 {
55 self.times_requested.load(Ordering::Relaxed)
56 }
57
58 pub(crate) fn bad_messages(&self) -> u64 {
60 self.bad_messages.load(Ordering::Relaxed)
61 }
62
63 pub(crate) fn should_respond_empty(&self) -> bool {
68 self.empty_response_mod.is_some_and(|empty_response_mod| {
69 self.times_requested.load(Ordering::Relaxed).is_multiple_of(empty_response_mod)
70 })
71 }
72}
73
74impl DownloadClient for TestBodiesClient {
75 fn report_bad_message(&self, _peer_id: PeerId) {
76 self.bad_messages.fetch_add(1, Ordering::Relaxed);
77 }
78
79 fn num_connected_peers(&self) -> usize {
80 0
81 }
82}
83
84impl BodiesClient for TestBodiesClient {
85 type Body = BlockBody;
86 type Output = BodiesFut;
87
88 fn get_block_bodies_with_priority_and_range_hint(
89 &self,
90 hashes: Vec<B256>,
91 _priority: Priority,
92 _range_hint: Option<RangeInclusive<u64>>,
93 ) -> Self::Output {
94 let should_delay = self.should_delay;
95 let bodies = self.bodies.clone();
96 let max_batch_size = self.max_batch_size;
97
98 self.times_requested.fetch_add(1, Ordering::Relaxed);
99 let should_respond_empty = self.should_respond_empty();
100
101 Box::pin(async move {
102 if should_respond_empty {
103 return Ok((PeerId::default(), vec![]).into())
104 }
105
106 if should_delay {
107 tokio::time::sleep(Duration::from_millis((hashes[0][0] % 100) as u64)).await;
108 }
109
110 let bodies = &mut *bodies.lock().await;
111 Ok((
112 PeerId::default(),
113 hashes
114 .into_iter()
115 .take(max_batch_size.unwrap_or(usize::MAX))
116 .map(|hash| {
117 bodies
118 .remove(&hash)
119 .expect("Downloader asked for a block it should not ask for")
120 })
121 .collect(),
122 )
123 .into())
124 })
125 }
126}