use cid::Version as CidVersion;
mod handle;
mod metrics;
mod service;
pub use cid::Cid;
pub(crate) use handle::BitswapCommand;
pub use handle::{BitswapError, BitswapHandle, FetchItem};
pub use service::start;
pub(crate) const LOG_TARGET: &str = "sub-libp2p::bitswap";
pub const MAX_WANTED_BLOCKS: usize = 16;
pub const RAW_CODEC: u64 = 0x55;
pub const BLAKE2B_256_MULTIHASH_CODE: u64 = 0xb220;
pub const SHA2_256_MULTIHASH_CODE: u64 = 0x12;
pub const KECCAK_256_MULTIHASH_CODE: u64 = 0x1b;
pub fn is_cid_supported(cid: &Cid) -> bool {
cid.version() != CidVersion::V0 &&
cid.hash().size() == 32 &&
matches!(
cid.hash().code(),
BLAKE2B_256_MULTIHASH_CODE | SHA2_256_MULTIHASH_CODE | KECCAK_256_MULTIHASH_CODE
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_cid_supported_accepts_all_three_supported_hashings() {
use cid::multihash::Multihash;
for multihash_code in
[BLAKE2B_256_MULTIHASH_CODE, SHA2_256_MULTIHASH_CODE, KECCAK_256_MULTIHASH_CODE]
{
let digest = [9u8; 32];
let mh = Multihash::<64>::wrap(multihash_code, &digest).unwrap();
let cid = Cid::new_v1(RAW_CODEC, mh);
assert!(is_cid_supported(&cid), "{multihash_code} CID should be supported");
}
}
#[test]
fn is_cid_supported_rejects_unknown_multihash_code() {
use cid::multihash::Multihash;
let digest = [9u8; 32];
let mh = Multihash::<64>::wrap(0x99, &digest).unwrap();
let cid = Cid::new_v1(RAW_CODEC, mh);
assert!(!is_cid_supported(&cid));
}
}