fn schedule_download(
        &mut self,
        peer_id: NodeId,
        advert: &GossipAdvert,
        gossip_config: &GossipConfig,
        max_advertizing_peer: u32,
        artifact_manager: &dyn ArtifactManager,
    ) -> Option<&ArtifactTracker> {
        // Schedule a download of an artifact that is not currently being downloaded.
        if !self.artifacts.contains_key(&advert.integrity_hash) {
            let artifact_id = &advert.artifact_id;
            match artifact_manager.get_remaining_quota(artifact_id.into(), peer_id) {
                None => return None,
                Some(quota_size) if quota_size < advert.size => return None,
                Some(_) => { /* enough quota remaining */ }
            }

            if let Some(chunk_tracker) = artifact_manager.get_chunk_tracker(&advert.artifact_id) {
                let requested_instant = Instant::now();
                // Calculate the worst-case time estimate for the artifact download, which
                // assumes that all chunks for the artifact will time out for
                // each peer that has advertised the artifact.
                //
                // TODO: Revisit this in the context of subnets with many nodes: P2P-510
                let download_eta_ms =
                    std::cmp::max(advert.size as u64 / gossip_config.max_chunk_size as u64, 1)
                        * max_advertizing_peer as u64
                        * gossip_config.max_chunk_wait_ms as u64;
                let expiry_instant = requested_instant + Duration::from_millis(download_eta_ms);
                self.artifacts.insert(
                    advert.integrity_hash.clone(),
                    ArtifactTracker {
                        artifact_id: artifact_id.clone(),
                        expiry_instant,
                        chunkable: chunk_tracker,
                        peer_id,
                    },
                );
                self.expiry_index
                    .entry(expiry_instant)
                    .and_modify(|expired_artifacts| {
                        expired_artifacts.push(advert.integrity_hash.clone())
                    })
                    .or_insert_with(|| (vec![advert.integrity_hash.clone()]));
            } else {
                warn!(self.log, "Chunk tracker not found for advert {:?}", advert);
            }
        }
        self.artifacts.get(&advert.integrity_hash)
    }
/// The function checks if the download can be scheduled without violating
/// the IC download constraints.
The function checks if the download can be scheduled without violating
the IC download constraints.
The function checks if the download can be scheduled without violating the IC download constraints .
fn schedule_download(
        &mut self,
        peer_id: NodeId,
        advert: &GossipAdvert,
        gossip_config: &GossipConfig,
        max_advertizing_peer: u32,
        artifact_manager: &dyn ArtifactManager,
    ) -> Option<&ArtifactTracker> {
        // Schedule a download of an artifact that is not currently being downloaded.
        if !self.artifacts.contains_key(&advert.integrity_hash) {
            let artifact_id = &advert.artifact_id;
            match artifact_manager.get_remaining_quota(artifact_id.into(), peer_id) {
                None => return None,
                Some(quota_size) if quota_size < advert.size => return None,
                Some(_) => { /* enough quota remaining */ }
            }
            if let Some(chunk_tracker) = artifact_manager.get_chunk_tracker(&advert.artifact_id) {
                let requested_instant = Instant::now();
                // Calculate the worst-case time estimate for the artifact download, which
                // assumes that all chunks for the artifact will time out for
                // each peer that has advertised the artifact.
                //
                // TODO: Revisit this in the context of subnets with many nodes: P2P-510
                let download_eta_ms =
                    std::cmp::max(advert.size as u64 / gossip_config.max_chunk_size as u64, 1)
                        * max_advertizing_peer as u64
                        * gossip_config.max_chunk_wait_ms as u64;
                let expiry_instant = requested_instant + Duration::from_millis(download_eta_ms);
                self.artifacts.insert(
                    advert.integrity_hash.clone(),
                    ArtifactTracker {
                        artifact_id: artifact_id.clone(),
                        expiry_instant,
                        chunkable: chunk_tracker,
                        peer_id,
                    },
                );
                self.expiry_index
                    .entry(expiry_instant)
                    .and_modify(|expired_artifacts| {
                        expired_artifacts.push(advert.integrity_hash.clone())
                    })
                    .or_insert_with(|| (vec![advert.integrity_hash.clone()]));
            } else {
                warn!(self.log, "Chunk tracker not found for advert {:?}", advert);
            }
        }
        self.artifacts.get(&advert.integrity_hash)
    }
fn schedule_download ( & mut self , peer_id : NodeId , advert : & GossipAdvert , gossip_config : & GossipConfig , max_advertizing_peer : u32 , artifact_manager : & dyn ArtifactManager , ) -> Option < & ArtifactTracker > { // Schedule a download of an artifact that is not currently being downloaded. if ! self . artifacts . contains_key ( & advert . integrity_hash ) { let artifact_id = & advert . artifact_id ; match artifact_manager . get_remaining_quota ( artifact_id . into ( ) , peer_id ) { None => return None , Some ( quota_size ) if quota_size < advert . size => return None , Some ( _ ) => { /* enough quota remaining */ } } if let Some ( chunk_tracker ) = artifact_manager . get_chunk_tracker ( & advert . artifact_id ) { let requested_instant = Instant :: now ( ) ; // Calculate the worst-case time estimate for the artifact download, which // assumes that all chunks for the artifact will time out for // each peer that has advertised the artifact. // // TODO: Revisit this in the context of subnets with many nodes: P2P-510 let download_eta_ms = std :: cmp :: max ( advert . size as u64 / gossip_config . max_chunk_size as u64 , 1 ) * max_advertizing_peer as u64 * gossip_config . max_chunk_wait_ms as u64 ; let expiry_instant = requested_instant + Duration :: from_millis ( download_eta_ms ) ; self . artifacts . insert ( advert . integrity_hash . clone ( ) , ArtifactTracker { artifact_id : artifact_id . clone ( ) , expiry_instant , chunkable : chunk_tracker , peer_id , } , ) ; self . expiry_index . entry ( expiry_instant ) . and_modify ( | expired_artifacts | { expired_artifacts . push ( advert . integrity_hash . clone ( ) ) } ) . or_insert_with ( | | ( vec ! [ advert integrity_hash clone ( ) ] ) ) ; } else { warn ! ( self log " " advert ) ; } } self . artifacts . get ( & advert . integrity_hash ) }
The function checks if the download can be scheduled without violating
the IC download constraints.
The function checks if the download can be scheduled without violating the IC download constraints .
&mut self
peer_id
NodeId
advert
&GossipAdvert
gossip_config
&GossipConfig
max_advertizing_peer
u32
artifact_manager
&dyn ArtifactManager
&mut self
peer_id
NodeId
advert
&GossipAdvert
gossip_config
&GossipConfig
max_advertizing_peer
u32
artifact_manager
&dyn ArtifactManager