Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f2e9415
added CID filtering to polkadot-sdk's Bitswap server
x3c41a Nov 25, 2025
599b739
refactor
x3c41a Nov 25, 2025
69e39ca
reworked filtering
x3c41a Nov 26, 2025
4f9cc3d
removed newline
x3c41a Nov 26, 2025
c41bfdd
Merge branch 'master' into cid_filtering
x3c41a Nov 26, 2025
af1e599
updated comment
x3c41a Nov 26, 2025
7208cad
fmt
x3c41a Nov 26, 2025
8ad5a9c
added prodc
x3c41a Nov 26, 2025
003dd56
added prodc
x3c41a Nov 26, 2025
d7a8725
updated PRdoc
x3c41a Nov 26, 2025
4d69697
updated PRdoc
x3c41a Nov 26, 2025
4eaa0b2
Merge branch 'master' into cid_filtering
x3c41a Nov 26, 2025
78f3b0b
removed cid dependency, simplified filtering
x3c41a Nov 27, 2025
baa2427
reverted bitswap.rs
x3c41a Nov 28, 2025
cc6b60a
updated litep2p dependency
x3c41a Nov 28, 2025
cc54be4
use new litep2p types
x3c41a Nov 28, 2025
243e036
removed cid dependency
x3c41a Nov 28, 2025
17eef01
removed dep
x3c41a Nov 28, 2025
8ea0e2d
reverted substrate/client/network/src/bitswap/mod.rs
x3c41a Nov 28, 2025
76c68f0
added is_valid_cid
x3c41a Nov 28, 2025
9cc549c
added cid as dev dependency and fixed tests
x3c41a Nov 28, 2025
68523e6
changed log level
x3c41a Nov 28, 2025
4907631
added newline in the end
x3c41a Nov 28, 2025
00a4f8e
Merge branch 'master' into cid_filtering
x3c41a Nov 28, 2025
2dae245
added cargo.lock
x3c41a Nov 28, 2025
7a463b2
improved version filtering
x3c41a Dec 1, 2025
5773d20
added is_cid_supported check to bitswap server
x3c41a Dec 1, 2025
58c9a70
Merge branch 'master' into cid_filtering
x3c41a Dec 1, 2025
d807753
Update from github-actions[bot] running command 'fmt'
github-actions[bot] Dec 2, 2025
f043a94
Merge branch 'master' into cid_filtering
x3c41a Dec 2, 2025
672a81a
Update substrate/client/network/Cargo.toml
bkontur Dec 2, 2025
9e859e0
NIt
bkontur Dec 2, 2025
beff085
Apply suggestions from code review
bkontur Dec 2, 2025
a9c4b4a
nit
bkontur Dec 2, 2025
7cd70fd
Update prdoc/pr_10416.prdoc
bkontur Dec 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 6 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ cfg-if = { version = "1.0" }
chain-spec-builder = { path = "substrate/bin/utils/chain-spec-builder", default-features = false, package = "staging-chain-spec-builder" }
chain-spec-guide-runtime = { path = "docs/sdk/src/reference_docs/chain_spec_runtime" }
chrono = { version = "0.4.31" }
cid = { version = "0.9.0" }
cid = { version = "0.11.1" }
clap = { version = "4.5.13" }
clap_complete = { version = "4.5.13" }
cmd_lib = { version = "1.9.5" }
Expand Down Expand Up @@ -906,7 +906,7 @@ linked-hash-map = { version = "0.5.4" }
linked_hash_set = { version = "0.1.4" }
linregress = { version = "0.5.1" }
lite-json = { version = "0.2.0", default-features = false }
litep2p = { version = "0.12.0", features = ["rsa", "websocket"] }
litep2p = { version = "0.12.2", features = ["rsa", "websocket"] }
log = { version = "0.4.22", default-features = false }
macro_magic = { version = "0.5.1" }
maplit = { version = "1.0.2" }
Expand Down
10 changes: 10 additions & 0 deletions prdoc/pr_10416.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: 'Added CID filtering to Bitswap server'
doc:
- audience: Node Dev
description: |-
This PR adds CID (Content Identifier) validation and filtering to the Bitswap server
implementation. The server now validates incoming CID requests before processing them.

crates:
- name: sc-network
bump: minor
2 changes: 1 addition & 1 deletion substrate/client/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ async-channel = { workspace = true }
async-trait = { workspace = true }
asynchronous-codec = { workspace = true }
bytes = { workspace = true, default-features = true }
cid = { workspace = true }
codec = { features = ["derive"], workspace = true, default-features = true }
either = { workspace = true, default-features = true }
fnv = { workspace = true }
Expand Down Expand Up @@ -70,6 +69,7 @@ zeroize = { workspace = true, default-features = true }

[dev-dependencies]
assert_matches = { workspace = true }
cid = { workspace = true }
multistream-select = { workspace = true }
sc-block-builder = { workspace = true, default-features = true }
sp-consensus = { workspace = true, default-features = true }
Expand Down
25 changes: 14 additions & 11 deletions substrate/client/network/src/bitswap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::{
MAX_RESPONSE_SIZE,
};

use cid::{self, Version};
use futures::StreamExt;
use litep2p::types::cid::{Cid, Error as CidError, Version as CidVersion};
use log::{debug, error, trace};
use prost::Message;
use sc_client_api::BlockBackend;
Expand Down Expand Up @@ -60,11 +60,16 @@ const MAX_WANTED_BLOCKS: usize = 16;
/// Bitswap protocol name
const PROTOCOL_NAME: &'static str = "/ipfs/bitswap/1.2.0";

/// Check if a CID is supported by the bitswap protocol.
pub fn is_cid_supported(cid: &Cid) -> bool {
cid.version() != CidVersion::V0 && cid.hash().size() == 32
}

/// Prefix represents all metadata of a CID, without the actual content.
#[derive(PartialEq, Eq, Clone, Debug)]
struct Prefix {
/// The version of CID.
pub version: Version,
pub version: CidVersion,
/// The codec of CID.
pub codec: u64,
/// The multihash type of CID.
Expand Down Expand Up @@ -189,19 +194,16 @@ impl<B: BlockT> BitswapRequestHandler<B> {
}

for entry in wantlist.entries {
let cid = match cid::Cid::read_bytes(entry.block.as_slice()) {
let cid = match Cid::read_bytes(entry.block.as_slice()) {
Ok(cid) => cid,
Err(e) => {
trace!(target: LOG_TARGET, "Bad CID {:?}: {:?}", entry.block, e);
continue
},
};

if cid.version() != cid::Version::V1 ||
cid.hash().code() != u64::from(cid::multihash::Code::Blake2b256) ||
cid.hash().size() != 32
{
debug!(target: LOG_TARGET, "Ignoring unsupported CID {}: {}", peer, cid);
if !is_cid_supported(&cid) {
trace!(target: LOG_TARGET, "Ignoring unsupported CID {}: {}", peer, cid);
continue
}

Expand Down Expand Up @@ -270,7 +272,7 @@ pub enum BitswapError {

/// Error parsing CID
#[error(transparent)]
BadCid(#[from] cid::Error),
BadCid(#[from] CidError),

/// Packet read error.
#[error(transparent)]
Expand All @@ -293,6 +295,7 @@ pub enum BitswapError {
mod tests {
use super::*;
use futures::channel::oneshot;
use litep2p::types::multihash::Code;
use sc_block_builder::BlockBuilderBuilder;
use schema::bitswap::{
message::{wantlist::Entry, Wantlist},
Expand Down Expand Up @@ -441,7 +444,7 @@ mod tests {
block: cid::Cid::new_v1(
0x70,
cid::multihash::Multihash::wrap(
u64::from(cid::multihash::Code::Blake2b256),
u64::from(Code::Blake2b256),
&[0u8; 32],
)
.unwrap(),
Expand Down Expand Up @@ -502,7 +505,7 @@ mod tests {
block: cid::Cid::new_v1(
0x70,
cid::multihash::Multihash::wrap(
u64::from(cid::multihash::Code::Blake2b256),
u64::from(Code::Blake2b256),
&sp_crypto_hashing::blake2_256(&ext.encode()[pattern_index..]),
)
.unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions substrate/client/network/src/litep2p/shim/bitswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

//! Shim for litep2p's Bitswap implementation to make it work with `sc-network`.

use crate::bitswap::is_cid_supported;
use futures::StreamExt;
use litep2p::protocol::libp2p::bitswap::{
BitswapEvent, BitswapHandle, BlockPresenceType, Config, ResponseType, WantType,
Expand Down Expand Up @@ -60,6 +61,7 @@ impl<Block: BlockT> BitswapServer<Block> {

let response: Vec<ResponseType> = cids
.into_iter()
.filter(|(cid, _)| is_cid_supported(&cid))
.map(|(cid, want_type)| {
let mut hash = Block::Hash::default();
hash.as_mut().copy_from_slice(&cid.hash().digest()[0..32]);
Expand Down
Loading