Struct iroh_bitswap::Block
source · Expand description
A wrapper around bytes with their Cid.
Fields§
§cid: Cid§data: BytesImplementations§
source§impl Block
impl Block
sourcepub fn new(data: Bytes, cid: Cid) -> Self
pub fn new(data: Bytes, cid: Cid) -> Self
Examples found in repository?
src/block.rs (line 61)
57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn create_block_v1<B: Into<Bytes>>(bytes: B) -> Block {
let bytes = bytes.into();
let digest = Code::Sha2_256.digest(&bytes);
let cid = Cid::new_v1(RAW, digest);
Block::new(bytes, cid)
}
pub fn create_block_v0<B: Into<Bytes>>(bytes: B) -> Block {
let bytes = bytes.into();
let digest = Code::Sha2_256.digest(&bytes);
let cid = Cid::new_v0(digest).unwrap();
Block::new(bytes, cid)
}More examples
src/message.rs (line 460)
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
fn try_from(pbm: pb::Message) -> Result<Self, Self::Error> {
let full = pbm.wantlist.as_ref().map(|w| w.full).unwrap_or_default();
let mut message = BitswapMessage::new(full);
if let Some(wantlist) = pbm.wantlist {
for entry in wantlist.entries {
let cid = Cid::try_from(entry.block)?;
message.add_full_entry(
cid,
entry.priority,
entry.cancel,
entry.want_type.try_into()?,
entry.send_dont_have,
);
}
}
// deprecated
for data in pbm.blocks {
// CID v0, SHA26
let block = Block::from_v0_data(data)?;
message.add_block(block);
}
for block in pbm.payload {
let prefix = Prefix::new(&block.prefix)?;
let cid = prefix.to_cid(&block.data)?;
let block = Block::new(block.data, cid);
message.add_block(block);
}
for block_presence in pbm.block_presences {
let cid = Cid::try_from(block_presence.cid)?;
message.add_block_presence(cid, block_presence.r#type.try_into()?);
}
message.pending_bytes = pbm.pending_bytes;
Ok(message)
}sourcepub fn from_v0_data(data: Bytes) -> Result<Self>
pub fn from_v0_data(data: Bytes) -> Result<Self>
Examples found in repository?
src/message.rs (line 453)
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
fn try_from(pbm: pb::Message) -> Result<Self, Self::Error> {
let full = pbm.wantlist.as_ref().map(|w| w.full).unwrap_or_default();
let mut message = BitswapMessage::new(full);
if let Some(wantlist) = pbm.wantlist {
for entry in wantlist.entries {
let cid = Cid::try_from(entry.block)?;
message.add_full_entry(
cid,
entry.priority,
entry.cancel,
entry.want_type.try_into()?,
entry.send_dont_have,
);
}
}
// deprecated
for data in pbm.blocks {
// CID v0, SHA26
let block = Block::from_v0_data(data)?;
message.add_block(block);
}
for block in pbm.payload {
let prefix = Prefix::new(&block.prefix)?;
let cid = prefix.to_cid(&block.data)?;
let block = Block::new(block.data, cid);
message.add_block(block);
}
for block_presence in pbm.block_presences {
let cid = Cid::try_from(block_presence.cid)?;
message.add_block_presence(cid, block_presence.r#type.try_into()?);
}
message.pending_bytes = pbm.pending_bytes;
Ok(message)
}sourcepub fn cid(&self) -> &Cid
pub fn cid(&self) -> &Cid
Examples found in repository?
src/message.rs (line 350)
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
pub fn add_block(&mut self, block: Block) {
self.block_presences.remove(block.cid());
self.blocks.insert(*block.cid(), block);
}
pub fn add_block_presence(&mut self, cid: Cid, typ: BlockPresenceType) {
if self.blocks.contains_key(&cid) {
return;
}
self.block_presences.insert(cid, typ);
}
pub fn add_have(&mut self, cid: Cid) {
self.add_block_presence(cid, BlockPresenceType::Have);
}
pub fn add_dont_have(&mut self, cid: Cid) {
self.add_block_presence(cid, BlockPresenceType::DontHave);
}
pub fn encoded_len(&self) -> usize {
let block_size: usize = self.blocks.values().map(|b| b.data.len()).sum();
let block_presence_size: usize = self.block_presences().map(|bp| bp.encoded_len()).sum();
let wantlist_size: usize = self.wantlist.values().map(|e| e.encoded_len()).sum();
block_size + block_presence_size + wantlist_size
}
pub fn encode_as_proto_v0(&self) -> pb::Message {
let mut message = pb::Message::default();
// wantlist
let mut wantlist = pb::message::Wantlist::default();
for entry in self.wantlist.values() {
wantlist.entries.push(entry.into());
}
wantlist.full = self.full;
message.wantlist = Some(wantlist);
// blocks
for block in self.blocks.values() {
message.blocks.push(block.data().clone());
}
message
}
pub fn encode_as_proto_v1(&self) -> pb::Message {
let mut message = pb::Message::default();
// wantlist
let mut wantlist = pb::message::Wantlist::default();
for entry in self.wantlist.values() {
wantlist.entries.push(entry.into());
}
wantlist.full = self.full;
message.wantlist = Some(wantlist);
// blocks
for block in self.blocks.values() {
message.payload.push(pb::message::Block {
prefix: Prefix::from(block.cid()).to_bytes(),
data: block.data().clone(),
});
}
// block presences
for (cid, typ) in &self.block_presences {
message.block_presences.push(pb::message::BlockPresence {
cid: cid.to_bytes(),
r#type: (*typ).into(),
});
}
message.pending_bytes = self.pending_bytes();
message
}More examples
src/server.rs (line 286)
281 282 283 284 285 286 287 288 289 290 291 292 293
pub async fn notify_new_blocks(&self, blocks: &[Block]) -> Result<()> {
// send wanted blocks to the decision engine
self.engine.notify_new_blocks(blocks).await;
if self.inner.provide_enabled {
for block in blocks {
if let Err(err) = self.inner.new_blocks.send(*block.cid()).await {
warn!("failed to send new blocks: {:?}", err);
}
}
}
Ok(())
}src/server/decision.rs (line 467)
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
pub async fn message_sent(&self, peer: &PeerId, message: &BitswapMessage) {
let l = self.find_or_create(peer).await;
let mut ledger = l.lock().await;
// remove sent blocks from the want list for the peer
for block in message.blocks() {
self.score_ledger
.add_to_sent_bytes(ledger.partner(), block.data().len())
.await;
ledger
.wantlist_mut()
.remove_type(block.cid(), WantType::Block);
}
// remove sent block presences from the wantlist for the peer
for bp in message.block_presences() {
// don't record sent data, we reserve that for data blocks
if bp.typ == BlockPresenceType::Have {
ledger.wantlist_mut().remove_type(&bp.cid, WantType::Have);
}
}
}
fn split_wants<'a>(
&self,
peer: &PeerId,
entries: impl Iterator<Item = &'a Entry>,
) -> (Vec<&'a Entry>, Vec<&'a Entry>, Vec<&'a Entry>) {
let mut wants = Vec::new();
let mut cancels = Vec::new();
let mut denials = Vec::new();
for entry in entries {
if entry.cancel {
cancels.push(entry);
} else if let Some(ref filter) = self.peer_block_request_filter {
if (filter)(peer, &entry.cid) {
wants.push(entry);
} else {
denials.push(entry);
}
} else {
wants.push(entry);
}
}
(wants, cancels, denials)
}
pub async fn received_blocks(&self, from: PeerId, blocks: Vec<Block>) {
if blocks.is_empty() {
return;
}
let l = self.find_or_create(&from).await;
let ledger = l.lock().await;
for block in blocks {
self.score_ledger
.add_to_recv_bytes(ledger.partner(), block.data().len())
.await;
}
}
pub async fn notify_new_blocks(&self, blocks: &[Block]) {
if blocks.is_empty() {
return;
}
// get the sizes of each block
let block_sizes: AHashMap<_, _> = blocks
.iter()
.map(|block| (block.cid(), block.data().len()))
.collect();
let mut work = false;
let mut missing_wants: AHashMap<PeerId, Vec<Cid>> = AHashMap::new();
for block in blocks {
let cid = block.cid();
let peer_ledger = self.peer_ledger.lock().await;
let peers = peer_ledger.peers(cid);
if peers.is_none() {
continue;
}
for peer in peers.unwrap() {
let l = self.ledger_map.read().await.get(peer).cloned();
if l.is_none() {
missing_wants.entry(*peer).or_default().push(*cid);
continue;
}
let l = l.unwrap();
let ledger = l.lock().await;
let entry = ledger.wantlist_get(cid);
if entry.is_none() {
missing_wants.entry(*peer).or_default().push(*cid);
continue;
}
let entry = entry.unwrap();
work = true;
let block_size = block_sizes.get(cid).copied().unwrap_or_default();
let is_want_block = self.send_as_block(entry.want_type, block_size);
let entry_size = if is_want_block {
block_size
} else {
BlockPresence::encoded_len_for_cid(*cid)
};
self.peer_task_queue
.push_task(
*peer,
Task {
topic: entry.cid,
priority: entry.priority as isize,
work: entry_size,
data: TaskData {
block_size,
have_block: true,
is_want_block,
send_dont_have: false,
},
},
)
.await;
self.update_metrics().await;
}
}
// If we found missing wants remove them from the list
if !missing_wants.is_empty() {
let ledger_map = self.ledger_map.read().await;
let mut peer_ledger = self.peer_ledger.lock().await;
for (peer, wants) in missing_wants.into_iter() {
if let Some(l) = ledger_map.get(&peer) {
let ledger = l.lock().await;
for cid in wants {
if ledger.wantlist_get(&cid).is_some() {
continue;
}
peer_ledger.cancel_want(&peer, &cid);
}
} else {
for cid in wants {
peer_ledger.cancel_want(&peer, &cid);
}
}
}
}
if work {
self.signal_new_work();
}
}src/client/session_interest_manager.rs (line 147)
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
pub async fn split_wanted_unwanted<'a>(
&self,
blocks: &'a [Block],
) -> (Vec<&'a Block>, Vec<&'a Block>) {
debug!("split_wanted_unwantedn",);
let wants = &*self.wants.read().await;
let mut wanted_keys = AHashSet::new();
for block in blocks {
let cid = block.cid();
if let Some(wants) = wants.get(cid) {
for wanted in wants.values() {
if *wanted {
wanted_keys.insert(*cid);
}
}
}
}
let mut wanted_blocks = Vec::new();
let mut not_wanted_blocks = Vec::new();
for block in blocks {
if wanted_keys.contains(block.cid()) {
wanted_blocks.push(block);
} else {
not_wanted_blocks.push(block);
}
}
(wanted_blocks, not_wanted_blocks)
}src/client.rs (line 97)
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
pub async fn new(
network: Network,
store: S,
blocks_received_cb: Option<Box<BlocksReceivedCb>>,
config: Config,
) -> Self {
let self_id = *network.self_id();
let (notify, mut default_receiver): (
async_broadcast::Sender<Block>,
async_broadcast::Receiver<Block>,
) = async_broadcast::broadcast(4096);
// ensure no blocking is generated
// TODO: track task
tokio::task::spawn(async move {
debug!("starting default receiver");
loop {
inc!(BitswapMetrics::ClientLoopTick);
match default_receiver.recv().await {
Ok(block) => {
debug!("received block {}", block.cid());
}
Err(async_broadcast::RecvError::Closed) => {
debug!("shutting down default receiver");
break;
}
Err(e) => {
warn!("broadcast error: {:?}", e);
}
}
}
});
let session_manager = SessionManager::new(self_id, network.clone(), notify.clone()).await;
Client {
network,
store,
session_manager,
provider_search_delay: config.provider_search_delay,
rebroadcast_delay: config.rebroadcast_delay,
simulate_dont_haves_on_timeout: config.simluate_donthaves_on_timeout,
blocks_received_cb: blocks_received_cb.map(Arc::new),
notify,
}
}
pub async fn stop(self) -> Result<()> {
self.session_manager.stop().await?;
Ok(())
}
/// Attempts to retrieve a particular block from peers.
pub async fn get_block(&self, key: &Cid) -> Result<Block> {
let session = self.new_session().await;
let block = session.get_block(key).await;
session.stop().await?;
block
}
pub async fn get_block_with_session_id(
&self,
session_id: u64,
key: &Cid,
providers: &[PeerId],
) -> Result<Block> {
let session = self.get_or_create_session(session_id).await;
for provider in providers {
session.add_provider(key, *provider).await;
}
session.get_block(key).await
}
pub async fn get_blocks_with_session_id(
&self,
session_id: u64,
keys: &[Cid],
) -> Result<BlockReceiver> {
let session = self.get_or_create_session(session_id).await;
session.get_blocks(keys).await
}
/// Returns a channel where the caller may receive blocks that correspond to the
/// provided `keys`.
pub async fn get_blocks(&self, keys: &[Cid]) -> Result<BlockReceiver> {
let session = self.new_session().await;
session.get_blocks(keys).await
}
/// Announces the existence of blocks to this bitswap service.
/// Bitswap itself doesn't store new blocks. It's the caller responsibility to ensure
/// that those blocks are available in the blockstore before calling this function.
pub async fn notify_new_blocks(&self, blocks: &[Block]) -> Result<()> {
let block_cids: Vec<Cid> = blocks.iter().map(|b| *b.cid()).collect();
// Send all block keys (including duplicates) to any session that wants them.
self.session_manager
.receive_from(None, &block_cids, &[][..], &[][..])
.await;
// Publish the block to any Bitswap clients that had requested blocks.
// (the sessions use this pubsub mechanism to inform clients of incoming blocks)
for block in blocks {
if let Err(err) = self.notify.broadcast(block.clone()).await {
error!("failed to broadcast block {}: {:?}", block.cid(), err);
}
}
Ok(())
}
/// Process blocks received from the network.
async fn receive_blocks_from(
&self,
from: &PeerId,
incoming: &BitswapMessage,
haves: &[Cid],
dont_haves: &[Cid],
) -> Result<()> {
info!("recv_msg start");
let all_keys: Vec<Cid> = incoming.blocks().map(|b| *b.cid()).collect();
// Determine wanted and unwanted blocks
let blocks = incoming.blocks().cloned().collect::<Vec<_>>();
let (wanted, not_wanted) = self
.session_manager
.session_interest_manager()
.split_wanted_unwanted(&blocks)
.await;
for block in not_wanted {
debug!("recv block not in wantlist: {} from {}", block.cid(), from);
}
// Inform the PeerManager so that we can calculate per-peer latency.
let mut combined = all_keys.clone();
combined.extend_from_slice(haves);
combined.extend_from_slice(dont_haves);
info!("recv_msg peer_manager");
self.peer_manager().response_received(from, &combined).await;
info!("recv_msg session_manager");
// Send all block keys (including duplicates to any sessions that want them for accounting purposes).
self.session_manager
.receive_from(Some(*from), &all_keys, haves, dont_haves)
.await;
info!("recv_msg broadcast");
// Publish the blocks
for block in &wanted {
if let Err(err) = self.notify.broadcast((*block).clone()).await {
error!("failed to broadcast block {}: {:?}", block.cid(), err);
}
}
if let Some(ref cb) = self.blocks_received_cb {
(cb)(*from, wanted.into_iter().cloned().collect()).await;
}
info!("recv_msg end");
Ok(())
}
/// Called by the network interface when a new message is received.
pub async fn receive_message(&self, peer: &PeerId, incoming: &BitswapMessage) {
inc!(BitswapMetrics::MessagesProcessingClient);
if incoming.blocks_len() > 0 {
debug!("client::receive_message {} blocks", incoming.blocks_len());
for block in incoming.blocks() {
debug!("recv block; {} from {}", block.cid(), peer);
inc!(BitswapMetrics::BlocksIn);
record!(
BitswapMetrics::ReceivedBlockBytes,
block.data().len() as u64
);
}
}
// TODO: investigate if the allocations below can be avoided.
let haves: Vec<Cid> = incoming.haves().copied().collect();
let dont_haves: Vec<Cid> = incoming.dont_haves().copied().collect();
if incoming.blocks_len() > 0 || !haves.is_empty() || !dont_haves.is_empty() {
// Process blocks
if let Err(err) = self
.receive_blocks_from(peer, incoming, &haves, &dont_haves)
.await
{
warn!("ReceiveMessage recvBlockFrom error: {:?}", err);
}
}
}src/client/session.rs (line 319)
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
pub async fn get_blocks(&self, keys: &[Cid]) -> Result<BlockReceiver> {
ensure!(!keys.is_empty(), "missing keys");
debug!("get blocks: {:?}", keys);
let (s, r) = async_channel::bounded(8);
let mut remaining: AHashSet<Cid> = keys.iter().copied().collect();
let mut block_channel = self.inner.notify.new_receiver();
let incoming = self.inner.incoming.clone();
let (closer_s, mut closer_r) = oneshot::channel();
let worker = tokio::task::spawn(async move {
loop {
inc!(BitswapMetrics::SessionGetBlockLoopTick);
tokio::select! {
biased;
_ = &mut closer_r => {
// shutting down
break;
}
maybe_block = block_channel.recv() => {
match maybe_block {
Ok(block) => {
let cid = *block.cid();
if remaining.contains(&cid) {
debug!("received wanted block {}", cid);
match s.send(block).await {
Ok(_) => {
remaining.remove(&cid);
}
Err(_) => {
// receiver dropped, shutdown
break;
}
}
}
if remaining.is_empty() {
debug!("found all requested blocks");
break;
}
}
Err(async_broadcast::RecvError::Closed) => {
break;
}
Err(async_broadcast::RecvError::Overflowed(n)) => {
warn!("receiver is overflowing by {}", n);
continue;
}
}
}
}
}
// cancel all remaining
if !remaining.is_empty() {
if let Err(err) = incoming
.send(Op::Cancel(remaining.into_iter().collect()))
.await
{
warn!("failed to send cancel: {:?}", err);
}
}
});
self.inner.incoming.send(Op::Want(keys.to_vec())).await?;
Ok(BlockReceiver {
receiver: r,
guard: BlockReceiverGuard {
closer: Some(closer_s),
_worker: worker,
},
})
}sourcepub fn data(&self) -> &Bytes
pub fn data(&self) -> &Bytes
Examples found in repository?
src/message.rs (line 391)
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
pub fn encode_as_proto_v0(&self) -> pb::Message {
let mut message = pb::Message::default();
// wantlist
let mut wantlist = pb::message::Wantlist::default();
for entry in self.wantlist.values() {
wantlist.entries.push(entry.into());
}
wantlist.full = self.full;
message.wantlist = Some(wantlist);
// blocks
for block in self.blocks.values() {
message.blocks.push(block.data().clone());
}
message
}
pub fn encode_as_proto_v1(&self) -> pb::Message {
let mut message = pb::Message::default();
// wantlist
let mut wantlist = pb::message::Wantlist::default();
for entry in self.wantlist.values() {
wantlist.entries.push(entry.into());
}
wantlist.full = self.full;
message.wantlist = Some(wantlist);
// blocks
for block in self.blocks.values() {
message.payload.push(pb::message::Block {
prefix: Prefix::from(block.cid()).to_bytes(),
data: block.data().clone(),
});
}
// block presences
for (cid, typ) in &self.block_presences {
message.block_presences.push(pb::message::BlockPresence {
cid: cid.to_bytes(),
r#type: (*typ).into(),
});
}
message.pending_bytes = self.pending_bytes();
message
}More examples
src/server/decision.rs (line 463)
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
pub async fn message_sent(&self, peer: &PeerId, message: &BitswapMessage) {
let l = self.find_or_create(peer).await;
let mut ledger = l.lock().await;
// remove sent blocks from the want list for the peer
for block in message.blocks() {
self.score_ledger
.add_to_sent_bytes(ledger.partner(), block.data().len())
.await;
ledger
.wantlist_mut()
.remove_type(block.cid(), WantType::Block);
}
// remove sent block presences from the wantlist for the peer
for bp in message.block_presences() {
// don't record sent data, we reserve that for data blocks
if bp.typ == BlockPresenceType::Have {
ledger.wantlist_mut().remove_type(&bp.cid, WantType::Have);
}
}
}
fn split_wants<'a>(
&self,
peer: &PeerId,
entries: impl Iterator<Item = &'a Entry>,
) -> (Vec<&'a Entry>, Vec<&'a Entry>, Vec<&'a Entry>) {
let mut wants = Vec::new();
let mut cancels = Vec::new();
let mut denials = Vec::new();
for entry in entries {
if entry.cancel {
cancels.push(entry);
} else if let Some(ref filter) = self.peer_block_request_filter {
if (filter)(peer, &entry.cid) {
wants.push(entry);
} else {
denials.push(entry);
}
} else {
wants.push(entry);
}
}
(wants, cancels, denials)
}
pub async fn received_blocks(&self, from: PeerId, blocks: Vec<Block>) {
if blocks.is_empty() {
return;
}
let l = self.find_or_create(&from).await;
let ledger = l.lock().await;
for block in blocks {
self.score_ledger
.add_to_recv_bytes(ledger.partner(), block.data().len())
.await;
}
}
pub async fn notify_new_blocks(&self, blocks: &[Block]) {
if blocks.is_empty() {
return;
}
// get the sizes of each block
let block_sizes: AHashMap<_, _> = blocks
.iter()
.map(|block| (block.cid(), block.data().len()))
.collect();
let mut work = false;
let mut missing_wants: AHashMap<PeerId, Vec<Cid>> = AHashMap::new();
for block in blocks {
let cid = block.cid();
let peer_ledger = self.peer_ledger.lock().await;
let peers = peer_ledger.peers(cid);
if peers.is_none() {
continue;
}
for peer in peers.unwrap() {
let l = self.ledger_map.read().await.get(peer).cloned();
if l.is_none() {
missing_wants.entry(*peer).or_default().push(*cid);
continue;
}
let l = l.unwrap();
let ledger = l.lock().await;
let entry = ledger.wantlist_get(cid);
if entry.is_none() {
missing_wants.entry(*peer).or_default().push(*cid);
continue;
}
let entry = entry.unwrap();
work = true;
let block_size = block_sizes.get(cid).copied().unwrap_or_default();
let is_want_block = self.send_as_block(entry.want_type, block_size);
let entry_size = if is_want_block {
block_size
} else {
BlockPresence::encoded_len_for_cid(*cid)
};
self.peer_task_queue
.push_task(
*peer,
Task {
topic: entry.cid,
priority: entry.priority as isize,
work: entry_size,
data: TaskData {
block_size,
have_block: true,
is_want_block,
send_dont_have: false,
},
},
)
.await;
self.update_metrics().await;
}
}
// If we found missing wants remove them from the list
if !missing_wants.is_empty() {
let ledger_map = self.ledger_map.read().await;
let mut peer_ledger = self.peer_ledger.lock().await;
for (peer, wants) in missing_wants.into_iter() {
if let Some(l) = ledger_map.get(&peer) {
let ledger = l.lock().await;
for cid in wants {
if ledger.wantlist_get(&cid).is_some() {
continue;
}
peer_ledger.cancel_want(&peer, &cid);
}
} else {
for cid in wants {
peer_ledger.cancel_want(&peer, &cid);
}
}
}
}
if work {
self.signal_new_work();
}
}src/client.rs (line 252)
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
pub async fn receive_message(&self, peer: &PeerId, incoming: &BitswapMessage) {
inc!(BitswapMetrics::MessagesProcessingClient);
if incoming.blocks_len() > 0 {
debug!("client::receive_message {} blocks", incoming.blocks_len());
for block in incoming.blocks() {
debug!("recv block; {} from {}", block.cid(), peer);
inc!(BitswapMetrics::BlocksIn);
record!(
BitswapMetrics::ReceivedBlockBytes,
block.data().len() as u64
);
}
}
// TODO: investigate if the allocations below can be avoided.
let haves: Vec<Cid> = incoming.haves().copied().collect();
let dont_haves: Vec<Cid> = incoming.dont_haves().copied().collect();
if incoming.blocks_len() > 0 || !haves.is_empty() || !dont_haves.is_empty() {
// Process blocks
if let Err(err) = self
.receive_blocks_from(peer, incoming, &haves, &dont_haves)
.await
{
warn!("ReceiveMessage recvBlockFrom error: {:?}", err);
}
}
}Trait Implementations§
source§impl Ord for Block
impl Ord for Block
source§impl PartialEq<Block> for Block
impl PartialEq<Block> for Block
source§impl PartialOrd<Block> for Block
impl PartialOrd<Block> for Block
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self and other) and is used by the <=
operator. Read moreimpl Eq for Block
impl StructuralEq for Block
impl StructuralPartialEq for Block
Auto Trait Implementations§
impl RefUnwindSafe for Block
impl Send for Block
impl Sync for Block
impl Unpin for Block
impl UnwindSafe for Block
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request