Skip to main content

hyprstream_p2p/
service.rs

1//! GitTorrent Service - Core P2P Git Repository Service
2//!
3//! This module implements the main GitTorrent service that orchestrates:
4//! - Git repository operations using git2
5//! - Content-addressed object storage/transfer via iroh-blobs + the mainline
6//!   locator (the libp2p Kademlia DHT was retired in F3, #901)
7//! - CID-based content addressing and distribution
8
9use crate::crypto::hash::sha256_git;
10use crate::{ContentCid, Error, GitTorrentUrl, Result, Sha256Hash};
11
12use crate::blobs::IrohBlobStore;
13use crate::locator::{Cid512, MainlineLocator, PeerContact, CID512_LEN};
14use async_trait::async_trait;
15use iroh_blobs::Hash as Blake3Hash;
16
17use serde::{Deserialize, Serialize};
18use std::collections::HashMap;
19use std::path::{Path, PathBuf};
20use std::sync::Arc;
21use tokio::sync::RwLock;
22
23/// Configuration for GitTorrent service
24#[derive(Debug, Clone, Serialize, Deserialize)]
25#[serde(default)]
26pub struct GitTorrentConfig {
27    /// Storage directory for repositories
28    pub storage_dir: PathBuf,
29    /// Bootstrap nodes for P2P network
30    pub bootstrap_nodes: Vec<String>,
31    /// Enable automatic object discovery
32    pub auto_discovery: bool,
33    /// Local bind address for daemon
34    pub bind_address: String,
35    /// Local bind port for daemon
36    pub bind_port: u16,
37    /// Port advertised to mainline as this node's iroh-blobs object-service
38    /// port (0 = let remote nodes infer via BEP5 implied_port).
39    pub p2p_port: u16,
40}
41
42impl Default for GitTorrentConfig {
43    fn default() -> Self {
44        Self {
45            storage_dir: dirs::cache_dir()
46                .unwrap_or_else(std::env::temp_dir)
47                .join("gittorrent"),
48            bootstrap_nodes: vec![
49                // No default bootstrap nodes - user must specify them
50                // or run in standalone mode for the first network node
51            ],
52            auto_discovery: true,
53            bind_address: "127.0.0.1".to_owned(),
54            bind_port: 8080,
55            p2p_port: 0, // 0 = let remote nodes infer the port (BEP5 implied_port)
56        }
57    }
58}
59
60impl GitTorrentConfig {
61    /// Get the configuration directory
62    fn config_dir() -> Result<PathBuf> {
63        dirs::config_dir()
64            .map(|dir| dir.join("gittorrent"))
65            .ok_or_else(|| Error::other("Unable to determine config directory"))
66    }
67
68    /// Create a configuration builder with the standard priority stack
69    ///
70    /// Returns a `config::ConfigBuilder` that developers can extend with additional sources
71    /// before building the final configuration.
72    ///
73    /// **Default Priority Order (highest to lowest):**
74    /// 1. Environment variables (GITTORRENT_* prefix)
75    /// 2. Config file (~/.config/gittorrent/git-remote-gittorrent.*)
76    /// 3. Default values
77    ///
78    /// # Basic Usage
79    /// ```ignore
80    /// use hyprstream_p2p::service::GitTorrentConfig;
81    ///
82    /// // Simple case - use defaults
83    /// let config: GitTorrentConfig = GitTorrentConfig::builder()?
84    ///     .build()?
85    ///     .try_deserialize()?;
86    /// ```
87    ///
88    /// # Extended Usage - Add Custom Sources
89    /// ```ignore
90    /// use hyprstream_p2p::service::GitTorrentConfig;
91    /// use config::File;
92    ///
93    /// // Add custom config source with highest priority
94    /// let config: GitTorrentConfig = GitTorrentConfig::builder()?
95    ///     .add_source(File::with_name("/custom/config"))  // Highest priority
96    ///     .build()?
97    ///     .try_deserialize()?;
98    /// ```
99    ///
100    /// # Programmatic Overrides
101    /// ```ignore
102    /// use hyprstream_p2p::service::GitTorrentConfig;
103    ///
104    /// // Override specific values programmatically
105    /// let config: GitTorrentConfig = GitTorrentConfig::builder()?
106    ///     .set_override("p2p_port", 4001)?  // Highest priority
107    ///     .build()?
108    ///     .try_deserialize()?;
109    /// ```
110    ///
111    /// # Supported Environment Variables
112    /// - `GITTORRENT_P2P_PORT`: port advertised to the mainline locator
113    /// - `GITTORRENT_BOOTSTRAP_NODES`: Comma-separated list of bootstrap nodes
114    /// - `GITTORRENT_STORAGE_DIR`: Storage directory for repositories
115    /// - `GITTORRENT_BIND_ADDRESS`: Local bind address for daemon
116    /// - `GITTORRENT_BIND_PORT`: Local bind port for daemon
117    /// - `GITTORRENT_AUTO_DISCOVERY`: Enable/disable automatic discovery (true/false)
118    pub fn builder() -> Result<config::ConfigBuilder<config::builder::DefaultState>> {
119        let config_dir = Self::config_dir()?;
120        let config_file = config_dir.join("git-remote-gittorrent");
121
122        Ok(config::Config::builder()
123            // Start with defaults (lowest priority)
124            .add_source(config::Config::try_from(&Self::default())?)
125            // Layer config file (optional, medium priority)
126            .add_source(config::File::from(config_file).required(false))
127            // Layer environment variables (high priority)
128            .add_source(
129                config::Environment::with_prefix("GITTORRENT")
130                    .separator("_")
131                    .list_separator(","),
132            ))
133    }
134
135    /// Convenience method to load configuration with defaults
136    ///
137    /// This is equivalent to `GitTorrentConfig::builder()?.build()?.try_deserialize()?`
138    ///
139    /// # Basic Usage
140    /// ```ignore
141    /// use hyprstream_p2p::service::GitTorrentConfig;
142    ///
143    /// let config = GitTorrentConfig::load()?;
144    /// ```
145    pub fn load() -> Result<Self> {
146        let config = Self::builder()?
147            .build()
148            .map_err(|e| Error::other(format!("Failed to build config: {e}")))?;
149
150        config
151            .try_deserialize()
152            .map_err(|e| Error::other(format!("Failed to deserialize config: {e}")))
153    }
154}
155
156/// Statistics about the GitTorrent service
157#[derive(Debug, Clone, Serialize, Deserialize)]
158pub struct ServiceStats {
159    /// Number of known repositories
160    pub repository_count: usize,
161    /// Number of stored Git objects
162    pub object_count: usize,
163    /// Total bytes stored
164    pub total_bytes: u64,
165    /// Number of connected peers
166    pub peer_count: usize,
167}
168
169/// Repository metadata for object-based storage
170#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct RepositoryMetadata {
172    /// Repository name
173    pub name: String,
174    /// Description
175    pub description: Option<String>,
176    /// Remote URL (if any)
177    pub remote_url: Option<String>,
178    /// List of branches and their HEAD commits
179    pub branches: HashMap<String, Sha256Hash>,
180
181    // Object-based fields
182    /// All object hashes in this repository
183    pub object_hashes: Vec<Sha256Hash>,
184    /// Root commit objects for each branch
185    pub root_commits: HashMap<String, Sha256Hash>,
186    /// Repository format version (for compatibility)
187    pub format_version: u32,
188    /// Object count by type
189    pub object_stats: ObjectStats,
190
191    /// Total size in bytes
192    pub size_bytes: u64,
193    /// Last updated timestamp
194    pub last_updated: u64,
195    /// Repository publisher/owner (optional)
196    pub publisher: Option<String>,
197    /// Git references information
198    pub refs: Vec<String>,
199    /// LFS chunks if any (for future LFS support)
200    pub lfs_chunks: Vec<String>,
201}
202
203/// The bytes of an object fetched by CID, paired with their commit
204/// provenance so callers can decide whether persistence is safe.
205///
206/// `committed == true` means the bytes are already durable in the local
207/// validated store (the in-memory cache or the blob plane) or were
208/// self-verified against the CID and committed during this fetch — safe
209/// for the caller to treat as validated.
210///
211/// `committed == false` means the bytes were returned by a remote
212/// provider under a CID that cannot self-verify them (e.g. an XET Merkle
213/// CID, which addresses the reconstruction DAG rather than the raw file).
214/// The service deliberately does NOT commit such bytes (it cannot prove
215/// they match the CID); the caller MUST validate them end-to-end (e.g.
216/// pointer size + SHA-256) before persisting or treating them as
217/// committed. Conflating this case with `committed == true` lets
218/// unvalidated provider bytes leak past a caller's persistence gate.
219#[derive(Debug, Clone)]
220pub struct ObjectFetch {
221    /// The fetched object bytes.
222    pub data: Vec<u8>,
223    /// Whether the bytes are committed to the local validated store.
224    pub committed: bool,
225}
226
227/// Statistics about objects in a repository
228#[derive(Debug, Clone, Serialize, Deserialize, Default)]
229pub struct ObjectStats {
230    pub commit_count: usize,
231    pub tree_count: usize,
232    pub blob_count: usize,
233    pub tag_count: usize,
234}
235
236/// Main GitTorrent service
237pub struct GitTorrentService {
238    /// iroh-blobs/mainline object plane backing the `put_object`/`get_object`
239    /// facade (F1–F3, epic #880 Track F).
240    object_plane: ObjectPlane,
241    /// Local repository metadata
242    repositories: Arc<RwLock<HashMap<String, RepositoryMetadata>>>,
243    /// Object cache keyed by the complete content CID.
244    object_cache: Arc<RwLock<HashMap<ContentCid, Vec<u8>>>>,
245}
246
247struct ObjectPlane {
248    blobs: Arc<IrohBlobStore>,
249    locator: Arc<dyn ObjectLocator>,
250    fetcher: Arc<dyn RemoteBlobFetcher>,
251    announce_port: Option<u16>,
252}
253
254#[async_trait]
255trait ObjectLocator: Send + Sync {
256    async fn announce(&self, cid: &Cid512, port: Option<u16>) -> Result<()>;
257    async fn providers(&self, cid: &Cid512) -> Result<Vec<PeerContact>>;
258}
259
260#[async_trait]
261impl ObjectLocator for MainlineLocator {
262    async fn announce(&self, cid: &Cid512, port: Option<u16>) -> Result<()> {
263        MainlineLocator::announce(self, cid, port).await
264    }
265
266    async fn providers(&self, cid: &Cid512) -> Result<Vec<PeerContact>> {
267        MainlineLocator::providers(self, cid).await
268    }
269}
270
271#[async_trait]
272trait RemoteBlobFetcher: Send + Sync {
273    async fn fetch(&self, cid: &ContentCid, providers: Vec<PeerContact>)
274        -> Result<Option<Vec<u8>>>;
275}
276
277#[derive(Debug, Default)]
278struct IrohMainlineBlobFetcher;
279
280#[cfg(test)]
281#[derive(Debug, Default)]
282struct NoopObjectLocator;
283
284#[cfg(test)]
285#[async_trait]
286impl ObjectLocator for NoopObjectLocator {
287    async fn announce(&self, _cid: &Cid512, _port: Option<u16>) -> Result<()> {
288        Ok(())
289    }
290
291    async fn providers(&self, _cid: &Cid512) -> Result<Vec<PeerContact>> {
292        Ok(Vec::new())
293    }
294}
295
296#[async_trait]
297impl RemoteBlobFetcher for IrohMainlineBlobFetcher {
298    async fn fetch(
299        &self,
300        cid: &ContentCid,
301        providers: Vec<PeerContact>,
302    ) -> Result<Option<Vec<u8>>> {
303        // C2 currently exposes BEP5 socket contacts. The iroh-blobs downloader
304        // needs authenticated iroh EndpointIds, so this stays the single adapter
305        // point until the locator carries endpoint identity metadata.
306        if !providers.is_empty() {
307            tracing::debug!(
308                "found {} mainline providers for content CID {cid}, but endpoint-id fetch is not wired yet",
309                providers.len(),
310            );
311        }
312        Ok(None)
313    }
314}
315
316fn locator_cid_from_blake3(hash: Blake3Hash) -> Cid512 {
317    let mut hasher = blake3::Hasher::new();
318    hasher.update(b"hyprstream.gittorrent.iroh-blobs.locator.v1");
319    hasher.update(hash.as_bytes());
320
321    let mut cid = [0u8; CID512_LEN];
322    hasher.finalize_xof().fill(&mut cid);
323    Cid512::from_bytes(cid)
324}
325
326fn locator_cid_from_content(cid: &ContentCid) -> Cid512 {
327    let mut hasher = blake3::Hasher::new();
328    hasher.update(b"hyprstream.gittorrent.content-cid.locator.v1");
329    // Hash the complete canonical CID, including its codec and multihash
330    // algorithm. Equal-width digests from different domains cannot alias.
331    hasher.update(cid.as_str().as_bytes());
332
333    let mut cid = [0u8; CID512_LEN];
334    hasher.finalize_xof().fill(&mut cid);
335    Cid512::from_bytes(cid)
336}
337
338impl GitTorrentService {
339    /// Create a new GitTorrent service
340    pub async fn new(config: GitTorrentConfig) -> Result<Self> {
341        // Ensure storage directory exists
342        tokio::fs::create_dir_all(&config.storage_dir).await?;
343
344        // iroh-blobs + mainline object plane (the sole backend after F3).
345        // Tests use an in-memory store + a no-op locator so they never touch
346        // the network or the filesystem outside `storage_dir`.
347        let object_plane = ObjectPlane {
348            #[cfg(not(test))]
349            blobs: Arc::new(IrohBlobStore::open_fs(config.storage_dir.join("iroh-blobs")).await?),
350            #[cfg(test)]
351            blobs: Arc::new(IrohBlobStore::new_memory()),
352            #[cfg(not(test))]
353            locator: Arc::new(MainlineLocator::new()?),
354            #[cfg(test)]
355            locator: Arc::new(NoopObjectLocator),
356            fetcher: Arc::new(IrohMainlineBlobFetcher),
357            announce_port: config.auto_discovery.then_some(config.p2p_port),
358        };
359
360        // `bootstrap_nodes` configured a libp2p Kademlia bootstrap that no
361        // longer exists; the mainline locator bootstraps itself against the
362        // default BEP5 routers, so there is nothing to dial here.
363        if !config.bootstrap_nodes.is_empty() {
364            tracing::info!(
365                "{} bootstrap node(s) configured; mainline locator self-bootstraps against default BEP5 routers",
366                config.bootstrap_nodes.len()
367            );
368        }
369
370        Ok(Self {
371            object_plane,
372            repositories: Arc::new(RwLock::new(HashMap::new())),
373            object_cache: Arc::new(RwLock::new(HashMap::new())),
374        })
375    }
376
377    #[cfg(any(test, feature = "testing"))]
378    async fn new_with_object_plane(
379        config: GitTorrentConfig,
380        locator: Arc<dyn ObjectLocator>,
381        fetcher: Arc<dyn RemoteBlobFetcher>,
382    ) -> Result<Self> {
383        tokio::fs::create_dir_all(&config.storage_dir).await?;
384        let object_plane = ObjectPlane {
385            blobs: Arc::new(IrohBlobStore::open_fs(config.storage_dir.join("iroh-blobs")).await?),
386            locator,
387            fetcher,
388            announce_port: config.auto_discovery.then_some(config.p2p_port),
389        };
390
391        Ok(Self {
392            object_plane,
393            repositories: Arc::new(RwLock::new(HashMap::new())),
394            object_cache: Arc::new(RwLock::new(HashMap::new())),
395        })
396    }
397
398    /// Add a repository to the service
399    pub async fn add_repository(&self, name: String, path: &Path) -> Result<()> {
400        // Scan repository for SHA256 objects
401        let metadata = self.scan_repository(path).await?;
402
403        // Store repository metadata
404        let mut repos = self.repositories.write().await;
405        repos.insert(name.clone(), metadata);
406
407        tracing::info!("Added repository: {}", name);
408        Ok(())
409    }
410
411    /// Get a Git object by SHA256 hash
412    pub async fn get_object(&self, hash: &Sha256Hash) -> Result<Option<Vec<u8>>> {
413        let cid = ContentCid::git_sha256(hash)?;
414        self.get_object_by_cid(&cid).await
415    }
416
417    /// Get an object by its canonical content CID.
418    ///
419    /// Discards commit provenance. Callers that persist fetched bytes
420    /// (e.g. the XET smudge path, which must validate before persisting)
421    /// must use [`get_object_by_cid_with_provenance`] instead; treating a
422    /// provider-fetched, non-self-verifying result as already-validated
423    /// bypasses the caller's persistence gate.
424    pub async fn get_object_by_cid(&self, cid: &ContentCid) -> Result<Option<Vec<u8>>> {
425        Ok(self
426            .get_object_by_cid_with_provenance(cid)
427            .await?
428            .map(|fetch| fetch.data))
429    }
430
431    /// Get an object by its canonical content CID, along with its commit
432    /// provenance.
433    ///
434    /// See [`ObjectFetch`] for the meaning of `committed`. The provenance
435    /// is structural: it is derived from which store produced the bytes
436    /// and whether the CID can self-verify them, never from a caller
437    /// assertion. A non-`committed` result must not be persisted without
438    /// end-to-end validation.
439    pub async fn get_object_by_cid_with_provenance(
440        &self,
441        cid: &ContentCid,
442    ) -> Result<Option<ObjectFetch>> {
443        // Check local cache first. The cache only ever holds committed
444        // bytes (see `put_object_by_cid` and the self-verifying provider
445        // path below), so a hit is committed by construction.
446        {
447            let cache = self.object_cache.read().await;
448            if let Some(data) = cache.get(cid) {
449                return Ok(Some(ObjectFetch {
450                    data: data.clone(),
451                    committed: true,
452                }));
453            }
454        }
455
456        if let Some(data) = self.object_plane.blobs.get_object_by_cid(cid).await? {
457            let mut cache = self.object_cache.write().await;
458            cache.insert(cid.clone(), data.clone());
459            return Ok(Some(ObjectFetch {
460                data,
461                committed: true,
462            }));
463        }
464
465        let rendezvous = locator_cid_from_content(cid);
466        let providers = self.object_plane.locator.providers(&rendezvous).await?;
467        if providers.is_empty() {
468            return Ok(None);
469        }
470
471        if let Some(data) = self.object_plane.fetcher.fetch(cid, providers).await? {
472            // A raw Git CID self-verifies inside `put_object_by_cid`, so
473            // provider bytes can be committed here. A XET Merkle CID addresses
474            // the reconstruction DAG rather than the raw file, so this layer
475            // cannot verify the fetched bytes: return them uncommitted and
476            // leave persistence to the caller, which must validate them
477            // (pointer size + SHA-256) before calling `put_object_by_cid`.
478            let decoded = cid.decoded()?;
479            let self_verifying = matches!(
480                (decoded.codec, decoded.multihash.algo),
481                (
482                    hyprstream_rpc::cid::Codec::GitRaw,
483                    hyprstream_rpc::cid::HashAlgo::Sha2_256
484                )
485            );
486            if !self_verifying {
487                return Ok(Some(ObjectFetch {
488                    data,
489                    committed: false,
490                }));
491            }
492            self.object_plane
493                .blobs
494                .put_object_by_cid(cid.clone(), data.clone())
495                .await?;
496            let mut cache = self.object_cache.write().await;
497            cache.insert(cid.clone(), data.clone());
498            return Ok(Some(ObjectFetch {
499                data,
500                committed: true,
501            }));
502        }
503
504        Ok(None)
505    }
506
507    /// Store a Git object, returning its SHA256 hash (the consumer-facing key).
508    pub async fn put_object(&self, data: Vec<u8>) -> Result<Sha256Hash> {
509        let hash = sha256_git(&data)?;
510        let cid = ContentCid::git_sha256(&hash)?;
511        self.put_object_by_cid(cid, data).await?;
512        Ok(hash)
513    }
514
515    /// Store an object under a canonical content CID.
516    pub async fn put_object_by_cid(&self, cid: ContentCid, data: Vec<u8>) -> Result<()> {
517        self.object_plane
518            .blobs
519            .put_object_by_cid(cid.clone(), data.clone())
520            .await?;
521        let blake3 = self
522            .object_plane
523            .blobs
524            .blake3_of_cid(&cid)
525            .await
526            .ok_or_else(|| Error::not_found(format!("missing blake3 index for {cid}")))?;
527        let blob_rendezvous = locator_cid_from_blake3(blake3);
528
529        if let Some(port) = self.object_plane.announce_port {
530            let content_rendezvous = locator_cid_from_content(&cid);
531            self.object_plane
532                .locator
533                .announce(&blob_rendezvous, Some(port))
534                .await?;
535            self.object_plane
536                .locator
537                .announce(&content_rendezvous, Some(port))
538                .await?;
539        }
540
541        let mut cache = self.object_cache.write().await;
542        cache.insert(cid.clone(), data);
543
544        tracing::debug!("Stored object with CID: {cid}");
545        Ok(())
546    }
547
548    /// Store all objects from a repository individually through the object
549    /// facade (iroh-blobs + mainline announce).
550    pub async fn store_repository_objects(&self, repo_path: &Path) -> Result<Vec<Sha256Hash>> {
551        use crate::git::objects::extract_objects;
552
553        let objects = extract_objects(repo_path).await?;
554        let mut stored_hashes = Vec::new();
555
556        tracing::info!("Storing {} objects from repository", objects.len());
557
558        for obj in objects {
559            self.put_object(obj.data).await?;
560            stored_hashes.push(obj.hash);
561        }
562
563        tracing::info!("Successfully stored {} objects", stored_hashes.len());
564        Ok(stored_hashes)
565    }
566
567    /// Store objects for a specific commit and its history through the object
568    /// facade.
569    pub async fn store_commit_objects(
570        &self,
571        repo_path: &Path,
572        commit_hash: &str,
573    ) -> Result<Vec<Sha256Hash>> {
574        use crate::git::objects::extract_commit_objects;
575
576        let objects = extract_commit_objects(repo_path, commit_hash).await?;
577        let mut stored_hashes = Vec::new();
578
579        tracing::info!(
580            "Storing {} objects for commit {}",
581            objects.len(),
582            commit_hash
583        );
584
585        for obj in objects {
586            self.put_object(obj.data).await?;
587            stored_hashes.push(obj.hash);
588        }
589
590        Ok(stored_hashes)
591    }
592
593    /// Reconstruct repository from individual objects
594    pub async fn reconstruct_repository(
595        &self,
596        object_hashes: &[Sha256Hash],
597        target_path: &Path,
598    ) -> Result<()> {
599        use crate::git::objects::{write_objects, GitObject, GitObjectType};
600
601        // Create repository directory
602        tokio::fs::create_dir_all(target_path).await?;
603
604        // Initialize git repository
605        let _repo = git2::Repository::init(target_path)?;
606
607        // Collect all objects
608        let mut git_objects = Vec::new();
609        let mut missing_objects = Vec::new();
610
611        for hash in object_hashes {
612            if let Some(data) = self.get_object(hash).await? {
613                // Parse object type from git format
614                if let Some(null_pos) = data.iter().position(|&b| b == 0) {
615                    let header = &data[..null_pos];
616                    if let Ok(header_str) = std::str::from_utf8(header) {
617                        let parts: Vec<&str> = header_str.split(' ').collect();
618                        if parts.len() == 2 {
619                            let obj_type = match parts[0] {
620                                "commit" => GitObjectType::Commit,
621                                "tree" => GitObjectType::Tree,
622                                "tag" => GitObjectType::Tag,
623                                // "blob" or unknown types default to Blob
624                                _ => GitObjectType::Blob,
625                            };
626
627                            let size = parts[1].parse().unwrap_or(0);
628
629                            git_objects.push(GitObject {
630                                hash: hash.clone(),
631                                object_type: obj_type,
632                                data,
633                                size,
634                            });
635                        }
636                    }
637                }
638            } else {
639                missing_objects.push(hash);
640            }
641        }
642
643        if !missing_objects.is_empty() {
644            tracing::warn!(
645                "Missing {} objects during reconstruction",
646                missing_objects.len()
647            );
648            // The object facade (`get_object`) already consults the local
649            // iroh-blobs store plus the mainline locator before reporting an
650            // object missing, so there is no separate peer-fetch path here.
651        }
652
653        // Write objects to repository
654        write_objects(target_path, &git_objects).await?;
655
656        tracing::info!(
657            "Reconstructed repository with {} objects at {:?}",
658            git_objects.len(),
659            target_path
660        );
661        Ok(())
662    }
663
664    /// Get objects for a specific commit (recursive)
665    pub async fn get_commit_objects(&self, commit_hash: &Sha256Hash) -> Result<Vec<Vec<u8>>> {
666        use crate::types::GitHash;
667
668        let mut objects = Vec::new();
669        let mut visited = std::collections::HashSet::new();
670
671        // Convert Sha256Hash to GitHash for internal use
672        let mut bytes = [0u8; 32];
673        bytes.copy_from_slice(&commit_hash.to_bytes());
674        let initial_hash = GitHash::Sha256(bytes);
675        let mut to_fetch = vec![initial_hash];
676
677        while let Some(hash) = to_fetch.pop() {
678            if visited.contains(&hash) {
679                continue;
680            }
681            visited.insert(hash.clone());
682
683            // Convert GitHash back to Sha256Hash for the object facade.
684            let sha256_hash = match &hash {
685                GitHash::Sha256(b) => Sha256Hash::from_bytes(b)?,
686                GitHash::Sha1(_) => {
687                    // For SHA1 hashes, we need to use the DHT key derivation
688                    // For now, skip SHA1 objects in this legacy function
689                    continue;
690                }
691            };
692
693            if let Some(data) = self.get_object(&sha256_hash).await? {
694                objects.push(data.clone());
695
696                // Parse object to find referenced objects (returns GitHash).
697                // The object we just fetched was keyed by `hash`, whose variant
698                // tells us the repository object format for binary tree entries.
699                let referenced = self.parse_object_references(&data, hash.object_format())?;
700                to_fetch.extend(referenced);
701            }
702        }
703
704        Ok(objects)
705    }
706
707    /// Parse an object to find hash references to other objects.
708    ///
709    /// `format` is the repository's object format (from the fetched object's
710    /// own hash), used to slice binary tree entries at the correct OID width.
711    fn parse_object_references(
712        &self,
713        object_data: &[u8],
714        format: crate::types::ObjectFormat,
715    ) -> Result<Vec<crate::types::GitHash>> {
716        use crate::git::objects::parse_object_references;
717        parse_object_references(object_data, format)
718    }
719
720    /// Clone a repository from a GitTorrent URL
721    pub async fn clone_repository(&self, url: &GitTorrentUrl, local_path: &Path) -> Result<()> {
722        match url {
723            GitTorrentUrl::Commit { hash } | GitTorrentUrl::CommitWithRefs { hash } => {
724                tracing::info!("Cloning repository with commit hash {}", hash);
725
726                // Try to find the repository metadata object
727                if let Some(metadata_data) = self.get_object(hash).await? {
728                    // Parse repository metadata (implement based on your format)
729                    let metadata: RepositoryMetadata = serde_json::from_slice(&metadata_data)?;
730
731                    // Create local repository
732                    self.create_local_repository(local_path, &metadata).await?;
733
734                    tracing::info!("Successfully cloned repository to {:?}", local_path);
735                } else {
736                    return Err(Error::not_found(format!(
737                        "Repository metadata not found for hash {hash}"
738                    )));
739                }
740            }
741            GitTorrentUrl::GitServer { server: _, repo: _ } => {
742                return Err(Error::other("Git server URLs not yet supported"));
743            }
744            GitTorrentUrl::Username { username: _ } => {
745                return Err(Error::other("Username URLs not yet supported"));
746            }
747        }
748
749        Ok(())
750    }
751
752    /// Get service statistics
753    pub async fn stats(&self) -> ServiceStats {
754        let repos = self.repositories.read().await;
755        let cache = self.object_cache.read().await;
756
757        ServiceStats {
758            repository_count: repos.len(),
759            object_count: cache.len(),
760            total_bytes: cache.values().map(|data| data.len() as u64).sum(),
761            peer_count: 0, // TODO: Get from DHT
762        }
763    }
764
765    /// List all repositories
766    pub async fn list_repositories(&self) -> Vec<(String, RepositoryMetadata)> {
767        let repos = self.repositories.read().await;
768        repos
769            .iter()
770            .map(|(name, meta)| (name.clone(), meta.clone()))
771            .collect()
772    }
773
774    /// Announce repository to P2P network (wrapper for store_repository_objects)
775    pub async fn announce_repository(&self, repo_path: &Path) -> Result<()> {
776        self.store_repository_objects(repo_path).await?;
777        tracing::info!("Announced repository at {:?} to P2P network", repo_path);
778        Ok(())
779    }
780
781    /// Query for repository metadata by identifier
782    pub async fn query_repository(&self, identifier: &str) -> Result<Option<RepositoryMetadata>> {
783        // Try to parse identifier as SHA256 hash and look up repository metadata
784        if let Ok(hash) = Sha256Hash::new(identifier) {
785            if let Some(data) = self.get_object(&hash).await? {
786                // Try to parse as repository metadata JSON
787                if let Ok(metadata) = serde_json::from_slice::<RepositoryMetadata>(&data) {
788                    return Ok(Some(metadata));
789                }
790            }
791        }
792
793        // Also check local repositories by name
794        let repos = self.repositories.read().await;
795        if let Some(metadata) = repos.get(identifier) {
796            return Ok(Some(metadata.clone()));
797        }
798
799        Ok(None)
800    }
801
802    /// Scan a repository and extract metadata with object information
803    async fn scan_repository(&self, repo_path: &Path) -> Result<RepositoryMetadata> {
804        use crate::git::objects::{extract_objects, GitObjectType};
805
806        // Open repository in spawn_blocking (git2::Repository is !Send)
807        let path = repo_path.to_path_buf();
808        let (branches, root_commits) = crate::git::with_repo_blocking(path, |repo| {
809            let mut branches = HashMap::new();
810            let mut root_commits = HashMap::new();
811
812            let refs = repo.references()?;
813            for reference in refs {
814                let reference = reference?;
815                if let Some(name) = reference.shorthand() {
816                    if let Some(target) = reference.target() {
817                        let hash_str = format!("{:0>64}", target.to_string());
818                        if let Ok(sha256) = Sha256Hash::new(hash_str) {
819                            branches.insert(name.to_owned(), sha256.clone());
820
821                            if reference.is_branch() {
822                                root_commits.insert(name.to_owned(), sha256);
823                            }
824                        }
825                    }
826                }
827            }
828
829            Ok((branches, root_commits))
830        })
831        .await?;
832
833        // Extract all objects to get complete metadata (async)
834        let objects = extract_objects(repo_path).await?;
835        let mut object_hashes = Vec::new();
836        let mut object_stats = ObjectStats::default();
837        let mut total_size = 0u64;
838
839        for obj in &objects {
840            object_hashes.push(obj.hash.clone());
841            total_size += obj.size as u64;
842
843            // Count objects by type
844            match obj.object_type {
845                GitObjectType::Commit => object_stats.commit_count += 1,
846                GitObjectType::Tree => object_stats.tree_count += 1,
847                GitObjectType::Blob => object_stats.blob_count += 1,
848                GitObjectType::Tag => object_stats.tag_count += 1,
849            }
850        }
851
852        let repo_name = repo_path
853            .file_name()
854            .and_then(|n| n.to_str())
855            .unwrap_or("unknown")
856            .to_owned();
857
858        // Collect ref names
859        let ref_names: Vec<String> = branches.keys().cloned().collect();
860
861        Ok(RepositoryMetadata {
862            name: repo_name,
863            description: None,
864            remote_url: None,
865            branches,
866            object_hashes,
867            root_commits,
868            format_version: 1, // Current format version
869            object_stats,
870            size_bytes: total_size,
871            // SAFETY: Only fails if system time is before Unix epoch (1970)
872            last_updated: std::time::SystemTime::now()
873                .duration_since(std::time::UNIX_EPOCH)
874                .map(|d| d.as_secs())
875                .unwrap_or(0),
876            publisher: None, // Could be set from git config in future
877            refs: ref_names,
878            lfs_chunks: vec![], // No LFS support yet
879        })
880    }
881
882    /// Create a local repository from metadata using object reconstruction
883    async fn create_local_repository(
884        &self,
885        local_path: &Path,
886        metadata: &RepositoryMetadata,
887    ) -> Result<()> {
888        // Reconstruct repository from individual objects
889        self.reconstruct_repository(&metadata.object_hashes, local_path)
890            .await?;
891
892        // Set up refs in spawn_blocking (git2::Repository is !Send)
893        let path = local_path.to_path_buf();
894        let branches = metadata.branches.clone();
895        let object_count = metadata.object_hashes.len();
896        crate::git::with_repo_blocking(path, move |repo| {
897            for (branch_name, commit_hash) in &branches {
898                if let Ok(oid) = git2::Oid::from_str(commit_hash.as_str()) {
899                    let ref_name = format!("refs/heads/{branch_name}");
900                    if let Err(e) = repo.reference(&ref_name, oid, false, "Initial clone") {
901                        tracing::warn!("Failed to create ref {}: {}", ref_name, e);
902                    }
903                }
904            }
905
906            if let Some(main_commit) = branches.get("main").or_else(|| branches.get("master")) {
907                if let Ok(oid) = git2::Oid::from_str(main_commit.as_str()) {
908                    if let Err(e) = repo.set_head_detached(oid) {
909                        tracing::warn!("Failed to set HEAD: {}", e);
910                    }
911                }
912            }
913
914            Ok(())
915        })
916        .await?;
917
918        tracing::info!(
919            "Created local repository at {:?} with {} objects",
920            local_path,
921            object_count
922        );
923        Ok(())
924    }
925}
926
927/// Test infrastructure for downstream crates that need to exercise
928/// `GitTorrentService` against an in-memory, deterministic object plane.
929///
930/// Compiled under `cfg(test)` and the `testing` cargo feature. The mock
931/// store and fetcher simulate a remote provider advertising bytes; the
932/// service is the real production type, so provenance and persistence
933/// flow through the same code paths as in deployment.
934#[cfg(any(test, feature = "testing"))]
935pub mod testing {
936    use super::*;
937    use std::collections::HashMap as StdHashMap;
938    use tokio::sync::Mutex;
939
940    /// In-memory `ObjectLocator` that hands back whatever providers were
941    /// registered for a rendezvous CID. Never touches the network.
942    #[derive(Default)]
943    pub struct MockObjectLocator {
944        announcements: Mutex<Vec<(Cid512, Option<u16>)>>,
945        providers: Mutex<StdHashMap<Cid512, Vec<PeerContact>>>,
946    }
947
948    impl MockObjectLocator {
949        /// Register a provider for a rendezvous CID, mirroring a BEP5
950        /// `get_peers` hit.
951        pub async fn add_provider(&self, cid: Cid512, provider: PeerContact) {
952            self.providers
953                .lock()
954                .await
955                .entry(cid)
956                .or_default()
957                .push(provider);
958        }
959
960        /// Convenience wrapper: register a provider for a content CID,
961        /// mapping it to its rendezvous CID the way the real mainline
962        /// locator would. Hides the private content→rendezvous mapping
963        /// from downstream test code.
964        pub async fn add_provider_for_content(
965            &self,
966            content_cid: &ContentCid,
967            provider: PeerContact,
968        ) {
969            self.add_provider(locator_cid_from_content(content_cid), provider)
970                .await;
971        }
972
973        /// Announcements recorded by `announce`, in insertion order.
974        pub async fn announcements(&self) -> Vec<(Cid512, Option<u16>)> {
975            self.announcements.lock().await.clone()
976        }
977    }
978
979    #[async_trait]
980    impl ObjectLocator for MockObjectLocator {
981        async fn announce(&self, cid: &Cid512, port: Option<u16>) -> Result<()> {
982            self.announcements.lock().await.push((*cid, port));
983            Ok(())
984        }
985
986        async fn providers(&self, cid: &Cid512) -> Result<Vec<PeerContact>> {
987            Ok(self
988                .providers
989                .lock()
990                .await
991                .get(cid)
992                .cloned()
993                .unwrap_or_default())
994        }
995    }
996
997    /// In-memory `RemoteBlobFetcher` keyed by content CID. Simulates a
998    /// remote provider returning bytes (which may or may not match the
999    /// CID — the service cannot tell for non-self-verifying CIDs).
1000    #[derive(Default)]
1001    pub struct MockBlobFetcher {
1002        objects: Mutex<StdHashMap<ContentCid, Vec<u8>>>,
1003        calls: Mutex<usize>,
1004    }
1005
1006    impl MockBlobFetcher {
1007        /// Insert a Git object keyed by its SHA-256 hash (self-verifying).
1008        pub async fn insert(&self, hash: Sha256Hash, data: Vec<u8>) -> Result<()> {
1009            let cid = ContentCid::git_sha256(&hash)?;
1010            self.objects.lock().await.insert(cid, data);
1011            Ok(())
1012        }
1013
1014        /// Insert raw bytes keyed by an arbitrary CID. Used to simulate a
1015        /// provider returning bytes under a non-self-verifying CID (e.g.
1016        /// an XET Merkle CID), which is the case the service must NOT
1017        /// commit before caller validation.
1018        pub async fn insert_raw(&self, cid: ContentCid, data: Vec<u8>) {
1019            self.objects.lock().await.insert(cid, data);
1020        }
1021
1022        /// Number of `fetch` invocations observed.
1023        pub async fn calls(&self) -> usize {
1024            *self.calls.lock().await
1025        }
1026    }
1027
1028    #[async_trait]
1029    impl RemoteBlobFetcher for MockBlobFetcher {
1030        async fn fetch(
1031            &self,
1032            cid: &ContentCid,
1033            _providers: Vec<PeerContact>,
1034        ) -> Result<Option<Vec<u8>>> {
1035            *self.calls.lock().await += 1;
1036            Ok(self.objects.lock().await.get(cid).cloned())
1037        }
1038    }
1039
1040    /// Build a `GitTorrentConfig` whose storage lives under `path` and
1041    /// whose auto-discovery is off, so tests never reach the network.
1042    pub fn test_config(path: &std::path::Path) -> GitTorrentConfig {
1043        GitTorrentConfig {
1044            storage_dir: path.to_path_buf(),
1045            bootstrap_nodes: vec![],
1046            auto_discovery: false,
1047            ..Default::default()
1048        }
1049    }
1050
1051    /// Construct a real `GitTorrentService` backed by the supplied mocks.
1052    ///
1053    /// The private locator/fetcher trait bounds stay internal to this
1054    /// module so downstream callers configure the service purely through
1055    /// the public mock methods.
1056    pub async fn new_service_with_mocks(
1057        config: GitTorrentConfig,
1058        locator: Arc<MockObjectLocator>,
1059        fetcher: Arc<MockBlobFetcher>,
1060    ) -> Result<GitTorrentService> {
1061        GitTorrentService::new_with_object_plane(config, locator, fetcher).await
1062    }
1063}
1064
1065#[cfg(test)]
1066mod tests {
1067    use super::*;
1068    use crate::service::testing::{
1069        new_service_with_mocks, test_config, MockBlobFetcher, MockObjectLocator,
1070    };
1071    use std::net::{Ipv4Addr, SocketAddrV4};
1072    use tempfile::TempDir;
1073
1074    #[tokio::test]
1075    async fn test_service_creation() -> crate::error::Result<()> {
1076        let temp_dir = TempDir::new()?;
1077        let config = test_config(temp_dir.path());
1078
1079        let service = GitTorrentService::new(config).await?;
1080        let stats = service.stats().await;
1081
1082        assert_eq!(stats.repository_count, 0);
1083        assert_eq!(stats.object_count, 0);
1084        Ok(())
1085    }
1086
1087    #[tokio::test]
1088    async fn test_object_storage() -> crate::error::Result<()> {
1089        let temp_dir = TempDir::new()?;
1090        let config = test_config(temp_dir.path());
1091
1092        let service = GitTorrentService::new(config).await?;
1093
1094        let test_data = b"hello world".to_vec();
1095        let hash = service.put_object(test_data.clone()).await?;
1096
1097        let retrieved = service.get_object(&hash).await?;
1098        let retrieved = retrieved.ok_or_else(|| crate::Error::not_found("Object not found"))?;
1099        assert_eq!(retrieved, test_data);
1100        Ok(())
1101    }
1102
1103    #[test]
1104    fn test_content_rendezvous_preserves_cid_domain() -> crate::error::Result<()> {
1105        let digest = [0x5a; 32];
1106        let git = ContentCid::git_sha256(&Sha256Hash::from_bytes(&digest)?)?;
1107        let xet = ContentCid::xet_merkle(&digest)?;
1108
1109        assert_ne!(git, xet);
1110        assert_ne!(
1111            locator_cid_from_content(&git),
1112            locator_cid_from_content(&xet),
1113            "rendezvous derivation must include CID codec and hash algorithm"
1114        );
1115        Ok(())
1116    }
1117
1118    #[tokio::test]
1119    async fn test_object_storage_announces_blake3_and_content_cids() -> crate::error::Result<()> {
1120        let temp_dir = TempDir::new()?;
1121        let locator = Arc::new(MockObjectLocator::default());
1122        let fetcher = Arc::new(MockBlobFetcher::default());
1123        let config = GitTorrentConfig {
1124            auto_discovery: true,
1125            p2p_port: 4242,
1126            ..test_config(temp_dir.path())
1127        };
1128
1129        let service =
1130            GitTorrentService::new_with_object_plane(config, locator.clone(), fetcher).await?;
1131
1132        let data = b"announced object".to_vec();
1133        let hash = service.put_object(data.clone()).await?;
1134        let content_cid = ContentCid::git_sha256(&hash)?;
1135        let blake3 = service
1136            .object_plane
1137            .blobs
1138            .blake3_of(&hash)
1139            .await
1140            .ok_or_else(|| Error::not_found("missing blake3 index"))?;
1141        let announcements = locator.announcements().await;
1142
1143        assert!(announcements.contains(&(locator_cid_from_blake3(blake3), Some(4242))));
1144        assert!(announcements.contains(&(locator_cid_from_content(&content_cid), Some(4242))));
1145        assert_eq!(
1146            service.get_object(&hash).await?.as_deref(),
1147            Some(data.as_slice())
1148        );
1149        Ok(())
1150    }
1151
1152    #[tokio::test]
1153    async fn test_get_object_fetches_from_provider_and_stores_locally() -> crate::error::Result<()>
1154    {
1155        let temp_dir = TempDir::new()?;
1156        let locator = Arc::new(MockObjectLocator::default());
1157        let fetcher = Arc::new(MockBlobFetcher::default());
1158        let service = GitTorrentService::new_with_object_plane(
1159            test_config(temp_dir.path()),
1160            locator.clone(),
1161            fetcher.clone(),
1162        )
1163        .await?;
1164
1165        let data = b"remote object".to_vec();
1166        let hash = sha256_git(&data)?;
1167        let content_cid = ContentCid::git_sha256(&hash)?;
1168        locator
1169            .add_provider(
1170                locator_cid_from_content(&content_cid),
1171                PeerContact::untrusted(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 6881)),
1172            )
1173            .await;
1174        fetcher.insert(hash.clone(), data.clone()).await?;
1175
1176        assert_eq!(
1177            service.get_object(&hash).await?.as_deref(),
1178            Some(data.as_slice())
1179        );
1180        assert_eq!(fetcher.calls().await, 1);
1181
1182        assert_eq!(
1183            service.get_object(&hash).await?.as_deref(),
1184            Some(data.as_slice())
1185        );
1186        assert_eq!(fetcher.calls().await, 1);
1187        Ok(())
1188    }
1189
1190    #[tokio::test]
1191    async fn test_get_object_returns_remote_xet_bytes_uncommitted() -> crate::error::Result<()> {
1192        let temp_dir = TempDir::new()?;
1193        let locator = Arc::new(MockObjectLocator::default());
1194        let fetcher = Arc::new(MockBlobFetcher::default());
1195        let service = new_service_with_mocks(
1196            test_config(temp_dir.path()),
1197            locator.clone(),
1198            fetcher.clone(),
1199        )
1200        .await?;
1201
1202        let data = b"xet reconstruction bytes".to_vec();
1203        let cid = ContentCid::xet_merkle(&[0x7f; 32])?;
1204        locator
1205            .add_provider(
1206                locator_cid_from_content(&cid),
1207                PeerContact::untrusted(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 6881)),
1208            )
1209            .await;
1210        fetcher.insert_raw(cid.clone(), data.clone()).await;
1211
1212        assert_eq!(
1213            service.get_object_by_cid(&cid).await?.as_deref(),
1214            Some(data.as_slice())
1215        );
1216        // A Merkle CID cannot verify raw bytes, so provider bytes must not be
1217        // committed to the local store or cache before the caller validates.
1218        assert!(service
1219            .object_plane
1220            .blobs
1221            .get_object_by_cid(&cid)
1222            .await?
1223            .is_none());
1224        assert_eq!(
1225            service.get_object_by_cid(&cid).await?.as_deref(),
1226            Some(data.as_slice())
1227        );
1228        assert_eq!(fetcher.calls().await, 2, "uncommitted bytes are re-fetched");
1229        Ok(())
1230    }
1231
1232    #[tokio::test]
1233    async fn test_get_object_by_cid_provenance_distinguishes_committed_from_provider_xet_bytes(
1234    ) -> crate::error::Result<()> {
1235        // Regression for #1115: provider-fetched XET (non-self-verifying)
1236        // bytes must surface as `committed == false` so the caller's
1237        // persistence gate fires. A cache/blob hit and a self-verifying
1238        // provider fetch must surface as `committed == true`.
1239        let temp_dir = TempDir::new()?;
1240
1241        // --- Provider-fetched XET Merkle bytes: NOT committed. ---
1242        let locator = Arc::new(MockObjectLocator::default());
1243        let fetcher = Arc::new(MockBlobFetcher::default());
1244        let service = new_service_with_mocks(
1245            test_config(temp_dir.path()),
1246            locator.clone(),
1247            fetcher.clone(),
1248        )
1249        .await?;
1250        let xet_data = b"xet reconstruction bytes".to_vec();
1251        let xet_cid = ContentCid::xet_merkle(&[0x7f; 32])?;
1252        locator
1253            .add_provider(
1254                locator_cid_from_content(&xet_cid),
1255                PeerContact::untrusted(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 6881)),
1256            )
1257            .await;
1258        fetcher.insert_raw(xet_cid.clone(), xet_data.clone()).await;
1259
1260        let fetch = service
1261            .get_object_by_cid_with_provenance(&xet_cid)
1262            .await?
1263            .ok_or_else(|| crate::Error::not_found("provider did not return the XET object"))?;
1264        assert_eq!(fetch.data, xet_data);
1265        assert!(
1266            !fetch.committed,
1267            "provider-fetched XET bytes must not be reported committed"
1268        );
1269
1270        // --- Self-verifying Git CID provider fetch: committed. ---
1271        let git_data = b"self-verifying git object".to_vec();
1272        let git_hash = sha256_git(&git_data)?;
1273        let git_cid = ContentCid::git_sha256(&git_hash)?;
1274        locator
1275            .add_provider(
1276                locator_cid_from_content(&git_cid),
1277                PeerContact::untrusted(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 6882)),
1278            )
1279            .await;
1280        fetcher.insert(git_hash, git_data.clone()).await?;
1281        let fetch = service
1282            .get_object_by_cid_with_provenance(&git_cid)
1283            .await?
1284            .ok_or_else(|| crate::Error::not_found("provider did not return the Git object"))?;
1285        assert_eq!(fetch.data, git_data);
1286        assert!(
1287            fetch.committed,
1288            "self-verifying provider bytes are committed during fetch"
1289        );
1290
1291        // --- Local cache hit (the prior line cached it): committed. ---
1292        let fetch = service
1293            .get_object_by_cid_with_provenance(&git_cid)
1294            .await?
1295            .ok_or_else(|| crate::Error::not_found("object was not cached"))?;
1296        assert!(fetch.committed, "cache hits are committed");
1297        // No additional provider fetch for the cached path.
1298        assert_eq!(fetcher.calls().await, 2);
1299        Ok(())
1300    }
1301
1302    #[tokio::test]
1303    async fn test_get_object_rejects_provider_bytes_with_wrong_sha256() -> crate::error::Result<()>
1304    {
1305        let temp_dir = TempDir::new()?;
1306        let locator = Arc::new(MockObjectLocator::default());
1307        let fetcher = Arc::new(MockBlobFetcher::default());
1308        let service = GitTorrentService::new_with_object_plane(
1309            test_config(temp_dir.path()),
1310            locator.clone(),
1311            fetcher.clone(),
1312        )
1313        .await?;
1314
1315        let requested_hash = sha256_git(b"wanted")?;
1316        let content_cid = ContentCid::git_sha256(&requested_hash)?;
1317        locator
1318            .add_provider(
1319                locator_cid_from_content(&content_cid),
1320                PeerContact::untrusted(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 6881)),
1321            )
1322            .await;
1323        fetcher
1324            .insert(requested_hash.clone(), b"poisoned".to_vec())
1325            .await?;
1326
1327        assert!(service.get_object(&requested_hash).await.is_err());
1328        assert!(service
1329            .object_plane
1330            .blobs
1331            .get_object(&requested_hash)
1332            .await?
1333            .is_none());
1334        Ok(())
1335    }
1336}