sc-network 0.58.0

Substrate network protocol
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <https://www.gnu.org/licenses/>.

use crate::{IfDisconnected, NetworkRequest, ProtocolName};

use cid::{multihash::Multihash as CidMultihash, Cid, Version as CidVersion};
use log::{debug, trace, warn};
use prost::Message;
use sc_network_types::PeerId;
use std::collections::{HashMap, HashSet};

use super::{
	is_cid_supported,
	schema::bitswap::{
		message::{
			wantlist::{Entry, WantType as ProtoWantType},
			BlockPresence, BlockPresenceType, Wantlist,
		},
		Message as BitswapMessage,
	},
	Prefix, LOG_TARGET, MAX_WANTED_BLOCKS, PROTOCOL_NAME,
};

/// Const from <https://github.com/multiformats/multicodec/blame/master/table.csv>
/// Multihash code for BLAKE2b-256.
pub const BLAKE2B_256_MULTIHASH_CODE: u64 = 0xb220;
/// Multihash code for SHA2-256.
pub const SHA2_256_MULTIHASH_CODE: u64 = 0x12;
/// Multihash code for Keccak-256.
pub const KECCAK_256_MULTIHASH_CODE: u64 = 0x1b;

/// Per-CID outcome from a Bitswap block request.
///
/// The public contract is intentionally narrow: either the peer delivered the bytes for the CID
/// or it did not. A peer signalling `DONT_HAVE` and a peer staying silent for a CID are both
/// surfaced as [`FetchOutcome::Missing`]; callers needing a different policy must implement it
/// over [`FetchOutcome`].
#[derive(Debug)]
pub enum FetchOutcome {
	/// Peer returned bytes for the requested CID.
	Block(Vec<u8>),
	/// Peer did not deliver bytes for this CID.
	///
	/// Covers the peer explicitly answering `DONT_HAVE`, the peer answering `HAVE` without bytes,
	/// and the peer not acknowledging the CID at all. From the caller's perspective these are
	/// equivalent: no block was delivered.
	Missing,
}

/// Multihash type with a 64-byte digest capacity.
type Multihash = CidMultihash<64>;

/// Validate the wantlist length is within bounds.
fn validate_wantlist_size(len: usize) -> Result<(), BitswapError> {
	if len == 0 {
		return Err(BitswapError::DecodeError("empty wantlist".into()));
	}
	if len > MAX_WANTED_BLOCKS {
		return Err(BitswapError::DecodeError(format!(
			"wantlist too large: {len} > {MAX_WANTED_BLOCKS}",
		)));
	}
	Ok(())
}

/// Validate CIDs: enforce length, CID support, and CID uniqueness.
fn validate_cids(cids: &[Cid]) -> Result<(), BitswapError> {
	validate_wantlist_size(cids.len())?;

	let mut seen: HashSet<Cid> = HashSet::with_capacity(cids.len());
	for cid in cids {
		if !is_cid_supported(cid) {
			return Err(BitswapError::UnsupportedHashing { multihash_code: cid.hash().code() });
		}
		if !seen.insert(*cid) {
			return Err(BitswapError::DecodeError(format!("duplicate CID in wantlist: {cid}")));
		}
	}

	Ok(())
}

/// Send one `WANT-BLOCK` request for `cids` to `peer` and classify the response.
///
/// Returned blocks are verified by recomputing the CID from the response prefix and bytes.
/// Blocks whose recomputed CID was not requested are ignored.
///
/// Errors if `cids` is empty, larger than [`MAX_WANTED_BLOCKS`], contains an unsupported CID,
/// or contains a duplicate CID.
///
/// Note: This is a temporary API that shall be superseeded by a better abstraction such as
///  <https://github.com/paritytech/polkadot-sdk/issues/12052>
pub async fn request_bitswap_blocks<N>(
	network: &N,
	peer: PeerId,
	cids: &[Cid],
) -> Result<HashMap<Cid, FetchOutcome>, BitswapError>
where
	N: NetworkRequest + ?Sized,
{
	validate_cids(cids)?;

	let wanted: HashSet<Cid> = cids.iter().copied().collect();
	let response = send_request(network, peer, cids).await?;
	Ok(classify_response(response, &wanted, peer))
}

/// Like [`request_bitswap_blocks`], but does not recompute or verify the hash of received bytes.
///
/// Use this when the requester must fetch by CID-shaped identifiers before it can verify the
/// returned bytes through an external authority. The response is matched by request order and
/// CID prefix only; integrity verification is delegated to the caller.
///
/// Note: This is a temporary API that shall be superseeded by a better abstraction such as
///  <https://github.com/paritytech/polkadot-sdk/issues/12052>
pub async fn request_bitswap_blocks_unverified<N>(
	network: &N,
	peer: PeerId,
	cids: &[Cid],
) -> Result<HashMap<Cid, FetchOutcome>, BitswapError>
where
	N: NetworkRequest + ?Sized,
{
	validate_cids(cids)?;

	let response = send_request(network, peer, cids).await?;
	Ok(classify_response_unverified(response, cids, peer))
}

/// Dispatch a bitswap WANT request to `peer` and decode the response.
async fn send_request<N>(
	network: &N,
	peer: PeerId,
	cids: &[Cid],
) -> Result<BitswapMessage, BitswapError>
where
	N: NetworkRequest + ?Sized,
{
	let entries: Vec<Entry> = cids
		.iter()
		.copied()
		.map(|cid| Entry {
			block: cid.to_bytes(),
			want_type: ProtoWantType::Block as i32,
			send_dont_have: true,
			..Default::default()
		})
		.collect();
	let request =
		BitswapMessage { wantlist: Some(Wantlist { entries, full: false }), ..Default::default() };

	trace!(
		target: LOG_TARGET,
		"client: sending Bitswap wantlist for {} CIDs to {peer}, protocol {PROTOCOL_NAME}",
		cids.len(),
	);

	let payload = match network
		.request(
			peer,
			ProtocolName::from(PROTOCOL_NAME),
			request.encode_to_vec(),
			None,
			IfDisconnected::TryConnect,
		)
		.await
	{
		Ok((payload, _)) => payload,
		Err(err) => {
			debug!(target: LOG_TARGET, "client: batch request to {peer} rejected by network: {err:?}");
			return Err(BitswapError::RequestFailed(err.to_string()));
		},
	};

	BitswapMessage::decode(&payload[..]).map_err(|err| {
		debug!(target: LOG_TARGET, "client: failed to decode batch response from {peer}: {err}");
		BitswapError::DecodeError(err.to_string())
	})
}

/// Classify the response by verifying each block's CID against the wanted set.
///
/// Every wanted CID is recorded exactly once: as [`FetchOutcome::Block`] if the peer delivered
/// bytes whose recomputed CID is in `wanted`, otherwise as [`FetchOutcome::Missing`]. Presence
/// frames (`HAVE` / `DONT_HAVE`) are logged for diagnostics but do not change the outcome.
fn classify_response(
	response: BitswapMessage,
	wanted: &HashSet<Cid>,
	peer: PeerId,
) -> HashMap<Cid, FetchOutcome> {
	let mut result: HashMap<Cid, FetchOutcome> = HashMap::with_capacity(wanted.len());

	for block in response.payload {
		let Ok(cid) = cid_from_block_prefix(&block.prefix, &block.data).inspect_err(|err| {
			debug!(target: LOG_TARGET, "client: malformed block prefix from {peer}: {err:?}");
		}) else {
			continue;
		};
		if !wanted.contains(&cid) {
			debug!(target: LOG_TARGET, "client: {peer} returned unsolicited block for CID {cid}");
			continue;
		}
		debug!(target: LOG_TARGET, "client: {peer} returned {} bytes for CID {cid}", block.data.len());
		result.insert(cid, FetchOutcome::Block(block.data));
	}

	log_presences(response.block_presences, wanted, peer);

	for cid in wanted {
		result.entry(*cid).or_insert(FetchOutcome::Missing);
	}

	result
}

/// Classify an unverified response via order-based correlation.
///
/// Every wanted CID is recorded exactly once: as [`FetchOutcome::Block`] if the peer delivered
/// bytes whose declared prefix matches a requested CID at the corresponding position in the
/// wantlist, otherwise as [`FetchOutcome::Missing`].
fn classify_response_unverified(
	response: BitswapMessage,
	cids: &[Cid],
	peer: PeerId,
) -> HashMap<Cid, FetchOutcome> {
	let mut result: HashMap<Cid, FetchOutcome> = HashMap::with_capacity(cids.len());
	let wanted_set: HashSet<Cid> = cids.iter().copied().collect();
	let mut dont_have_cids: HashSet<Cid> = HashSet::with_capacity(cids.len());

	for presence in response.block_presences {
		let Ok(cid) = Cid::read_bytes(presence.cid.as_slice()).inspect_err(|err| {
			debug!(target: LOG_TARGET, "client: malformed presence CID from {peer}: {err}");
		}) else {
			continue;
		};
		if !wanted_set.contains(&cid) {
			debug!(target: LOG_TARGET, "client: {peer} returned unsolicited presence for CID {cid}");
			continue;
		}
		if presence.r#type == BlockPresenceType::DontHave as i32 {
			debug!(target: LOG_TARGET, "client: {peer} DONT_HAVE for CID {cid}");
			dont_have_cids.insert(cid);
		} else if presence.r#type == BlockPresenceType::Have as i32 {
			debug!(target: LOG_TARGET, "client: {peer} HAVE for CID {cid}");
		} else {
			warn!(
				target: LOG_TARGET,
				"client: {peer} unexpected presence type {} for CID {cid}",
				presence.r#type,
			);
		}
	}

	// Unverified payloads cannot be matched by recomputing their CID from bytes, so attribute
	// each block to the next requested CID (skipping any the peer already said it doesn't have)
	// whose CID metadata matches the payload prefix.
	let mut expected_payload_order =
		cids.iter().copied().filter(|cid| !dont_have_cids.contains(cid));

	for block in response.payload {
		let Some(expected_cid) = expected_payload_order.next() else {
			debug!(target: LOG_TARGET, "client: {peer} returned more payload blocks than expected; dropping extras");
			break;
		};
		let Ok(prefix) = decode_prefix(&block.prefix).inspect_err(|err| {
			debug!(target: LOG_TARGET, "client: malformed block prefix from {peer}: {err:?}");
		}) else {
			break;
		};
		if !prefix_matches_cid(&prefix, &expected_cid) {
			debug!(
				target: LOG_TARGET,
				"client: {peer} returned block with prefix {:?} but expected CID {expected_cid}; \
				 stopping payload attribution",
				prefix,
			);
			break;
		}
		debug!(
			target: LOG_TARGET,
			"client: {peer} returned {} unverified bytes for CID {expected_cid}",
			block.data.len(),
		);
		result.insert(expected_cid, FetchOutcome::Block(block.data.clone()));
	}

	for cid in cids {
		result.entry(*cid).or_insert(FetchOutcome::Missing);
	}

	result
}

/// Log per-CID presence frames for diagnostics. Presence does not influence the public outcome.
fn log_presences(presences: Vec<BlockPresence>, wanted: &HashSet<Cid>, peer: PeerId) {
	for presence in presences {
		let Ok(cid) = Cid::read_bytes(presence.cid.as_slice()).inspect_err(|err| {
			debug!(target: LOG_TARGET, "client: malformed presence CID from {peer}: {err}");
		}) else {
			continue;
		};
		if !wanted.contains(&cid) {
			debug!(target: LOG_TARGET, "client: {peer} returned unsolicited presence for CID {cid}");
			continue;
		}
		if presence.r#type == BlockPresenceType::DontHave as i32 {
			debug!(target: LOG_TARGET, "client: {peer} DONT_HAVE for CID {cid}");
		} else if presence.r#type == BlockPresenceType::Have as i32 {
			debug!(target: LOG_TARGET, "client: {peer} HAVE for CID {cid}");
		} else {
			debug!(
				target: LOG_TARGET,
				"client: {peer} unexpected presence type {} for CID {cid}",
				presence.r#type,
			);
		}
	}
}

/// Check that a decoded prefix matches a CID's version, codec, and multihash metadata.
fn prefix_matches_cid(prefix: &Prefix, cid: &Cid) -> bool {
	prefix.version == cid.version() &&
		prefix.codec == cid.codec() &&
		prefix.mh_type == cid.hash().code() &&
		prefix.mh_len == cid.hash().size()
}

/// Reconstruct a CID from a block's prefix bytes and payload data.
fn cid_from_block_prefix(prefix: &[u8], data: &[u8]) -> Result<Cid, BitswapError> {
	let prefix = decode_prefix(prefix)?;
	if prefix.version != CidVersion::V1 {
		return Err(BitswapError::UnsupportedCidVersion { version: prefix.version.into() });
	}

	let hash = hash_for_multihash_code(prefix.mh_type, data)
		.ok_or(BitswapError::UnsupportedHashing { multihash_code: prefix.mh_type })?;
	let multihash = Multihash::wrap(prefix.mh_type, &hash)
		.map_err(|err| BitswapError::DecodeError(err.to_string()))?;
	Ok(Cid::new_v1(prefix.codec, multihash))
}

/// Compute a 32-byte hash for the given multihash code.
fn hash_for_multihash_code(multihash_code: u64, data: &[u8]) -> Option<[u8; 32]> {
	match multihash_code {
		BLAKE2B_256_MULTIHASH_CODE => Some(sp_crypto_hashing::blake2_256(data)),
		SHA2_256_MULTIHASH_CODE => Some(sp_crypto_hashing::sha2_256(data)),
		KECCAK_256_MULTIHASH_CODE => Some(sp_crypto_hashing::keccak_256(data)),
		_ => None,
	}
}

/// Decode varint-encoded CID prefix bytes.
fn decode_prefix(mut bytes: &[u8]) -> Result<Prefix, BitswapError> {
	let mut read_varint = || -> Result<u64, BitswapError> {
		let (v, rest) = unsigned_varint::decode::u64(bytes)
			.map_err(|err| BitswapError::DecodeError(err.to_string()))?;
		bytes = rest;
		Ok(v)
	};

	let version = read_varint()?;
	let codec = read_varint()?;
	let mh_type = read_varint()?;
	let mh_len = read_varint()?;

	if !bytes.is_empty() {
		return Err(BitswapError::DecodeError("bitswap block prefix had trailing bytes".into()));
	}

	let version = CidVersion::try_from(version)
		.map_err(|_| BitswapError::UnsupportedCidVersion { version })?;
	let mh_len = u8::try_from(mh_len).map_err(|_| {
		BitswapError::DecodeError(format!("multihash length {mh_len} does not fit into u8"))
	})?;

	Ok(Prefix { version, codec, mh_type, mh_len })
}

/// Bitswap client errors.
#[derive(Debug)]
pub enum BitswapError {
	/// Failed to decode or validate a bitswap payload.
	DecodeError(String),
	/// Request/response exchange failed.
	RequestFailed(String),
	/// Block prefix declared an unsupported multihash code.
	UnsupportedHashing {
		/// The unrecognised IPFS multihash code.
		multihash_code: u64,
	},
	/// CID version is unsupported for this bitswap client.
	UnsupportedCidVersion {
		/// The unsupported CID version number.
		version: u64,
	},
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{OutboundFailure, RequestFailure};
	use futures::channel::oneshot;
	use sc_network_types::PeerId;
	use std::{collections::VecDeque, sync::Mutex};

	use super::super::{
		is_supported_multihash_code,
		schema::bitswap::message::{Block as MessageBlock, BlockPresence, BlockPresenceType},
		RAW_CODEC,
	};

	/// Build a raw-codec CID from a 32-byte digest and supported multihash code.
	fn raw_cid_from_digest(multihash_code: u64, digest: [u8; 32]) -> Result<Cid, BitswapError> {
		if !is_supported_multihash_code(multihash_code) {
			return Err(BitswapError::UnsupportedHashing { multihash_code });
		}
		let multihash = CidMultihash::wrap(multihash_code, &digest)
			.map_err(|e| BitswapError::DecodeError(e.to_string()))?;
		Ok(Cid::new_v1(RAW_CODEC, multihash))
	}

	struct StubSender {
		responses: Mutex<VecDeque<Result<Vec<u8>, RequestFailure>>>,
		requests: Mutex<Vec<Vec<u8>>>,
	}

	impl StubSender {
		fn new(responses: impl IntoIterator<Item = Result<Vec<u8>, RequestFailure>>) -> Self {
			Self {
				responses: Mutex::new(responses.into_iter().collect()),
				requests: Mutex::new(Vec::new()),
			}
		}

		fn pop_request(&self) -> BitswapMessage {
			let bytes = self.requests.lock().unwrap().pop().expect("request should be recorded");
			BitswapMessage::decode(bytes.as_slice()).expect("request should decode")
		}
	}

	#[async_trait::async_trait]
	impl NetworkRequest for StubSender {
		async fn request(
			&self,
			_target: PeerId,
			_protocol: ProtocolName,
			request: Vec<u8>,
			_fallback_request: Option<(Vec<u8>, ProtocolName)>,
			_connect: IfDisconnected,
		) -> Result<(Vec<u8>, ProtocolName), RequestFailure> {
			self.requests.lock().unwrap().push(request);
			self.responses
				.lock()
				.unwrap()
				.pop_front()
				.expect("StubSender: no canned response queued")
				.map(|bytes| (bytes, ProtocolName::from(PROTOCOL_NAME)))
		}

		fn start_request(
			&self,
			_peer: PeerId,
			_protocol: ProtocolName,
			payload: Vec<u8>,
			_fallback_request: Option<(Vec<u8>, ProtocolName)>,
			tx: oneshot::Sender<Result<(Vec<u8>, ProtocolName), RequestFailure>>,
			_connect: IfDisconnected,
		) {
			self.requests.lock().unwrap().push(payload);
			let resp = self
				.responses
				.lock()
				.unwrap()
				.pop_front()
				.expect("StubSender: no canned response queued");
			let _ = tx.send(resp.map(|bytes| (bytes, ProtocolName::from(PROTOCOL_NAME))));
		}
	}

	fn prefix_for(multihash_code: u64) -> Vec<u8> {
		Prefix { version: CidVersion::V1, codec: RAW_CODEC, mh_type: multihash_code, mh_len: 32 }
			.to_bytes()
	}

	fn cid_for_data(multihash_code: u64, data: &[u8]) -> Cid {
		raw_cid_from_digest(multihash_code, hash_for_multihash_code(multihash_code, data).unwrap())
			.unwrap()
	}

	fn cid_for_digest(multihash_code: u64, digest: [u8; 32]) -> Cid {
		raw_cid_from_digest(multihash_code, digest).unwrap()
	}

	fn encode_response(blocks: &[(u64, Vec<u8>)], presences: &[(Cid, i32)]) -> Vec<u8> {
		let payload = blocks
			.iter()
			.map(|(multihash_code, data)| MessageBlock {
				prefix: prefix_for(*multihash_code),
				data: data.clone(),
			})
			.collect();
		let block_presences = presences
			.iter()
			.map(|(cid, ptype)| BlockPresence { cid: cid.to_bytes(), r#type: *ptype })
			.collect();
		BitswapMessage { payload, block_presences, ..Default::default() }.encode_to_vec()
	}

	#[tokio::test]
	async fn request_bitswap_blocks_returns_blocks_for_all_wanted() {
		let data_a = b"hash-a-payload".to_vec();
		let data_b = b"hash-b-payload".to_vec();
		let data_c = b"hash-c-payload".to_vec();
		let cid_a = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data_a);
		let cid_b = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data_b);
		let cid_c = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data_c);

		let response = encode_response(
			&[
				(BLAKE2B_256_MULTIHASH_CODE, data_a.clone()),
				(BLAKE2B_256_MULTIHASH_CODE, data_b.clone()),
				(BLAKE2B_256_MULTIHASH_CODE, data_c.clone()),
			],
			&[],
		);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &[cid_a, cid_b, cid_c])
			.await
			.expect("request_bitswap_blocks should succeed");

		assert_eq!(result.len(), 3);
		assert!(matches!(result.get(&cid_a), Some(FetchOutcome::Block(d)) if *d == data_a));
		assert!(matches!(result.get(&cid_b), Some(FetchOutcome::Block(d)) if *d == data_b));
		assert!(matches!(result.get(&cid_c), Some(FetchOutcome::Block(d)) if *d == data_c));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_dont_have_is_surfaced_as_missing() {
		let data_a = b"a".to_vec();
		let data_b = b"b".to_vec();
		let cid_a = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data_a);
		let cid_b = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data_b);
		let cid_c = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, b"c-not-served");

		let response = encode_response(
			&[
				(BLAKE2B_256_MULTIHASH_CODE, data_a.clone()),
				(BLAKE2B_256_MULTIHASH_CODE, data_b.clone()),
			],
			&[(cid_c, BlockPresenceType::DontHave as i32)],
		);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &[cid_a, cid_b, cid_c])
			.await
			.unwrap();

		assert_eq!(result.len(), 3);
		assert!(matches!(result.get(&cid_a), Some(FetchOutcome::Block(_))));
		assert!(matches!(result.get(&cid_b), Some(FetchOutcome::Block(_))));
		assert!(matches!(result.get(&cid_c), Some(FetchOutcome::Missing)));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_corrupted_data_dropped_as_unsolicited() {
		let real_data = b"real-payload".to_vec();
		let wanted_cid = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &real_data);
		let corrupted_data = b"i-am-not-the-real-payload".to_vec();
		let response = encode_response(&[(BLAKE2B_256_MULTIHASH_CODE, corrupted_data)], &[]);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &[wanted_cid]).await.unwrap();

		assert_eq!(result.len(), 1);
		assert!(matches!(result.get(&wanted_cid), Some(FetchOutcome::Missing)));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_encodes_only_want_block_entries() {
		let cid_a = cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, [1u8; 32]);
		let cid_b = cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, [2u8; 32]);
		let stub = StubSender::new([Ok(BitswapMessage::default().encode_to_vec())]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &[cid_a, cid_b])
			.await
			.expect("block-only request must encode");

		assert!(matches!(result.get(&cid_a), Some(FetchOutcome::Missing)));
		assert!(matches!(result.get(&cid_b), Some(FetchOutcome::Missing)));

		let request = stub.pop_request();
		let entries = request.wantlist.expect("wantlist should be present").entries;
		assert_eq!(entries.len(), 2);
		assert_eq!(entries[0].want_type, ProtoWantType::Block as i32);
		assert_eq!(entries[1].want_type, ProtoWantType::Block as i32);
	}

	#[tokio::test]
	async fn request_bitswap_blocks_have_presence_alone_is_missing() {
		let cid = cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, [3u8; 32]);
		let response = encode_response(&[], &[(cid, BlockPresenceType::Have as i32)]);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &[cid])
			.await
			.expect("HAVE-only response should classify successfully");

		assert_eq!(result.len(), 1);
		assert!(matches!(result.get(&cid), Some(FetchOutcome::Missing)));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_unverified_accepts_bytes_without_hash_recompute() {
		let data = b"sha2-digest-but-blake2b-request-prefix".to_vec();
		let cid = cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, sp_crypto_hashing::sha2_256(&data));
		let response = encode_response(&[(BLAKE2B_256_MULTIHASH_CODE, data.clone())], &[]);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks_unverified(&stub, PeerId::random(), &[cid])
			.await
			.expect("unverified fetch should not recompute hashes");

		assert_eq!(result.len(), 1);
		assert!(matches!(result.get(&cid), Some(FetchOutcome::Block(d)) if *d == data));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_unverified_dont_have_returned_as_missing() {
		let cid = cid_for_digest(
			BLAKE2B_256_MULTIHASH_CODE,
			sp_crypto_hashing::sha2_256(b"pruned-unverified-payload"),
		);
		let response = encode_response(&[], &[(cid, BlockPresenceType::DontHave as i32)]);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks_unverified(&stub, PeerId::random(), &[cid])
			.await
			.expect("unverified DONT_HAVE should classify successfully");

		assert_eq!(result.len(), 1);
		assert!(matches!(result.get(&cid), Some(FetchOutcome::Missing)));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_unverified_empty_wants_errors() {
		let stub = StubSender::new(std::iter::empty());

		let err = request_bitswap_blocks_unverified(&stub, PeerId::random(), &[])
			.await
			.expect_err("empty wantlist must error");
		assert!(matches!(err, BitswapError::DecodeError(msg) if msg == "empty wantlist"));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_duplicate_cids_error() {
		let cid = cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, [9u8; 32]);
		let stub = StubSender::new(std::iter::empty());

		let err = request_bitswap_blocks(&stub, PeerId::random(), &[cid, cid])
			.await
			.expect_err("two wants for the same CID are ambiguous");
		assert!(matches!(err, BitswapError::DecodeError(msg) if msg.starts_with("duplicate CID")));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_unverified_multi_want_all_served_in_request_order() {
		let data_a = b"first-unverified-payload".to_vec();
		let data_b = b"second-unverified-payload".to_vec();
		let data_c = b"third-unverified-payload".to_vec();
		let cid_a =
			cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, sp_crypto_hashing::sha2_256(&data_a));
		let cid_b =
			cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, sp_crypto_hashing::keccak_256(&data_b));
		let cid_c = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data_c);

		let response = encode_response(
			&[
				(BLAKE2B_256_MULTIHASH_CODE, data_a.clone()),
				(BLAKE2B_256_MULTIHASH_CODE, data_b.clone()),
				(BLAKE2B_256_MULTIHASH_CODE, data_c.clone()),
			],
			&[],
		);
		let stub = StubSender::new([Ok(response)]);

		let result =
			request_bitswap_blocks_unverified(&stub, PeerId::random(), &[cid_a, cid_b, cid_c])
				.await
				.expect("multi-want unverified must succeed via positional correlation");

		assert_eq!(result.len(), 3);
		assert!(matches!(result.get(&cid_a), Some(FetchOutcome::Block(d)) if *d == data_a));
		assert!(matches!(result.get(&cid_b), Some(FetchOutcome::Block(d)) if *d == data_b));
		assert!(matches!(result.get(&cid_c), Some(FetchOutcome::Block(d)) if *d == data_c));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_unverified_dont_have_skips_position_in_payload_order() {
		let data = b"second-payload-after-dont-have".to_vec();
		let dont_have_cid = cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, [4u8; 32]);
		let block_cid =
			cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, sp_crypto_hashing::sha2_256(&data));
		let response = encode_response(
			&[(BLAKE2B_256_MULTIHASH_CODE, data.clone())],
			&[(dont_have_cid, BlockPresenceType::DontHave as i32)],
		);
		let stub = StubSender::new([Ok(response)]);

		let result =
			request_bitswap_blocks_unverified(&stub, PeerId::random(), &[dont_have_cid, block_cid])
				.await
				.expect("unverified mixed presence/payload should classify successfully");

		assert_eq!(result.len(), 2);
		assert!(matches!(result.get(&dont_have_cid), Some(FetchOutcome::Missing)));
		assert!(matches!(result.get(&block_cid), Some(FetchOutcome::Block(d)) if *d == data));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_dispatches_per_entry_multihash() {
		let data_b2 = b"blake2b-payload".to_vec();
		let data_sha = b"sha2-256-payload".to_vec();
		let data_kec = b"keccak-256-payload".to_vec();
		let cid_b2 = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data_b2);
		let cid_sha = cid_for_data(SHA2_256_MULTIHASH_CODE, &data_sha);
		let cid_kec = cid_for_data(KECCAK_256_MULTIHASH_CODE, &data_kec);

		let response = encode_response(
			&[
				(BLAKE2B_256_MULTIHASH_CODE, data_b2.clone()),
				(SHA2_256_MULTIHASH_CODE, data_sha.clone()),
				(KECCAK_256_MULTIHASH_CODE, data_kec.clone()),
			],
			&[],
		);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &[cid_b2, cid_sha, cid_kec])
			.await
			.unwrap();

		assert_eq!(result.len(), 3);
		assert!(matches!(result.get(&cid_b2), Some(FetchOutcome::Block(d)) if *d == data_b2));
		assert!(matches!(result.get(&cid_sha), Some(FetchOutcome::Block(d)) if *d == data_sha));
		assert!(matches!(result.get(&cid_kec), Some(FetchOutcome::Block(d)) if *d == data_kec));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_over_cap_errors() {
		let wants: Vec<_> = (0..(MAX_WANTED_BLOCKS + 1) as u8)
			.map(|i| {
				let mut h = [0u8; 32];
				h[0] = i;
				cid_for_digest(BLAKE2B_256_MULTIHASH_CODE, h)
			})
			.collect();
		let stub = StubSender::new(std::iter::empty());

		let err = request_bitswap_blocks(&stub, PeerId::random(), &wants)
			.await
			.expect_err("over-cap wantlist must error");
		assert!(matches!(err, BitswapError::DecodeError(_)));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_at_exactly_max_wanted_blocks_succeeds() {
		let mut wants = Vec::with_capacity(MAX_WANTED_BLOCKS);
		let mut blocks = Vec::with_capacity(MAX_WANTED_BLOCKS);
		for i in 0..MAX_WANTED_BLOCKS {
			let data = format!("payload-{i}").into_bytes();
			wants.push(cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data));
			blocks.push((BLAKE2B_256_MULTIHASH_CODE, data));
		}

		let response = encode_response(&blocks, &[]);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &wants)
			.await
			.expect("exactly MAX_WANTED_BLOCKS must succeed");

		assert_eq!(result.len(), MAX_WANTED_BLOCKS);
		for cid in &wants {
			assert!(matches!(result.get(cid), Some(FetchOutcome::Block(_))));
		}
	}

	#[tokio::test]
	async fn request_bitswap_blocks_block_beats_presence_for_same_cid() {
		let data = b"both-block-and-presence".to_vec();
		let cid = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &data);

		let response = encode_response(
			&[(BLAKE2B_256_MULTIHASH_CODE, data.clone())],
			&[(cid, BlockPresenceType::DontHave as i32)],
		);
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &[cid]).await.unwrap();

		assert_eq!(result.len(), 1);
		assert!(matches!(result.get(&cid), Some(FetchOutcome::Block(d)) if *d == data));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_response_decode_failure() {
		let stub = StubSender::new([Ok(vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])]);
		let cid = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, b"any");

		let err = request_bitswap_blocks(&stub, PeerId::random(), &[cid])
			.await
			.expect_err("malformed response bytes must surface as DecodeError");
		assert!(matches!(err, BitswapError::DecodeError(_)));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_request_failure_propagates() {
		struct FailingSender;
		#[async_trait::async_trait]
		impl NetworkRequest for FailingSender {
			async fn request(
				&self,
				_target: PeerId,
				_protocol: ProtocolName,
				_request: Vec<u8>,
				_fallback_request: Option<(Vec<u8>, ProtocolName)>,
				_connect: IfDisconnected,
			) -> Result<(Vec<u8>, ProtocolName), RequestFailure> {
				Err(RequestFailure::Network(OutboundFailure::ConnectionClosed))
			}

			fn start_request(
				&self,
				_peer: PeerId,
				_protocol: ProtocolName,
				_payload: Vec<u8>,
				_fallback_request: Option<(Vec<u8>, ProtocolName)>,
				tx: oneshot::Sender<Result<(Vec<u8>, ProtocolName), RequestFailure>>,
				_connect: IfDisconnected,
			) {
				drop(tx);
			}
		}

		let cid = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, b"any");
		let err = request_bitswap_blocks(&FailingSender, PeerId::random(), &[cid])
			.await
			.expect_err("request failure must surface as RequestFailed");
		assert!(matches!(err, BitswapError::RequestFailed(_)));
	}

	#[tokio::test]
	async fn request_bitswap_blocks_unsupported_multihash_in_block_dropped() {
		let wanted_data = b"wanted".to_vec();
		let wanted_cid = cid_for_data(BLAKE2B_256_MULTIHASH_CODE, &wanted_data);
		const UNSUPPORTED_MH_CODE: u64 = 0x99;
		let bad_prefix = Prefix {
			version: CidVersion::V1,
			codec: RAW_CODEC,
			mh_type: UNSUPPORTED_MH_CODE,
			mh_len: 32,
		}
		.to_bytes();

		let mut payload_msg = BitswapMessage::default();
		payload_msg.payload =
			vec![MessageBlock { prefix: bad_prefix, data: b"some-bytes".to_vec() }];
		let response = payload_msg.encode_to_vec();
		let stub = StubSender::new([Ok(response)]);

		let result = request_bitswap_blocks(&stub, PeerId::random(), &[wanted_cid]).await.unwrap();

		assert_eq!(result.len(), 1);
		assert!(matches!(result.get(&wanted_cid), Some(FetchOutcome::Missing)));
	}

	#[test]
	fn cid_from_block_prefix_rejects_cid_v0_as_unsupported() {
		let prefix = Prefix {
			version: CidVersion::V0,
			codec: RAW_CODEC,
			mh_type: BLAKE2B_256_MULTIHASH_CODE,
			mh_len: 32,
		}
		.to_bytes();

		let err =
			cid_from_block_prefix(&prefix, b"payload").expect_err("CIDv0 must be unsupported");
		assert!(matches!(err, BitswapError::UnsupportedCidVersion { version: 0 }));
	}
}