1use 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#[derive(Debug, Clone, Serialize, Deserialize)]
25#[serde(default)]
26pub struct GitTorrentConfig {
27 pub storage_dir: PathBuf,
29 pub bootstrap_nodes: Vec<String>,
31 pub auto_discovery: bool,
33 pub bind_address: String,
35 pub bind_port: u16,
37 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 ],
52 auto_discovery: true,
53 bind_address: "127.0.0.1".to_owned(),
54 bind_port: 8080,
55 p2p_port: 0, }
57 }
58}
59
60impl GitTorrentConfig {
61 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 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 .add_source(config::Config::try_from(&Self::default())?)
125 .add_source(config::File::from(config_file).required(false))
127 .add_source(
129 config::Environment::with_prefix("GITTORRENT")
130 .separator("_")
131 .list_separator(","),
132 ))
133 }
134
135 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#[derive(Debug, Clone, Serialize, Deserialize)]
158pub struct ServiceStats {
159 pub repository_count: usize,
161 pub object_count: usize,
163 pub total_bytes: u64,
165 pub peer_count: usize,
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct RepositoryMetadata {
172 pub name: String,
174 pub description: Option<String>,
176 pub remote_url: Option<String>,
178 pub branches: HashMap<String, Sha256Hash>,
180
181 pub object_hashes: Vec<Sha256Hash>,
184 pub root_commits: HashMap<String, Sha256Hash>,
186 pub format_version: u32,
188 pub object_stats: ObjectStats,
190
191 pub size_bytes: u64,
193 pub last_updated: u64,
195 pub publisher: Option<String>,
197 pub refs: Vec<String>,
199 pub lfs_chunks: Vec<String>,
201}
202
203#[derive(Debug, Clone)]
220pub struct ObjectFetch {
221 pub data: Vec<u8>,
223 pub committed: bool,
225}
226
227#[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
236pub struct GitTorrentService {
238 object_plane: ObjectPlane,
241 repositories: Arc<RwLock<HashMap<String, RepositoryMetadata>>>,
243 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 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 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 pub async fn new(config: GitTorrentConfig) -> Result<Self> {
341 tokio::fs::create_dir_all(&config.storage_dir).await?;
343
344 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 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 pub async fn add_repository(&self, name: String, path: &Path) -> Result<()> {
400 let metadata = self.scan_repository(path).await?;
402
403 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 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 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 pub async fn get_object_by_cid_with_provenance(
440 &self,
441 cid: &ContentCid,
442 ) -> Result<Option<ObjectFetch>> {
443 {
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 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 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 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 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 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 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 tokio::fs::create_dir_all(target_path).await?;
603
604 let _repo = git2::Repository::init(target_path)?;
606
607 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 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 _ => 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 }
652
653 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 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 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 let sha256_hash = match &hash {
685 GitHash::Sha256(b) => Sha256Hash::from_bytes(b)?,
686 GitHash::Sha1(_) => {
687 continue;
690 }
691 };
692
693 if let Some(data) = self.get_object(&sha256_hash).await? {
694 objects.push(data.clone());
695
696 let referenced = self.parse_object_references(&data, hash.object_format())?;
700 to_fetch.extend(referenced);
701 }
702 }
703
704 Ok(objects)
705 }
706
707 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 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 if let Some(metadata_data) = self.get_object(hash).await? {
728 let metadata: RepositoryMetadata = serde_json::from_slice(&metadata_data)?;
730
731 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 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, }
763 }
764
765 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 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 pub async fn query_repository(&self, identifier: &str) -> Result<Option<RepositoryMetadata>> {
783 if let Ok(hash) = Sha256Hash::new(identifier) {
785 if let Some(data) = self.get_object(&hash).await? {
786 if let Ok(metadata) = serde_json::from_slice::<RepositoryMetadata>(&data) {
788 return Ok(Some(metadata));
789 }
790 }
791 }
792
793 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 async fn scan_repository(&self, repo_path: &Path) -> Result<RepositoryMetadata> {
804 use crate::git::objects::{extract_objects, GitObjectType};
805
806 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 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 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 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, object_stats,
870 size_bytes: total_size,
871 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, refs: ref_names,
878 lfs_chunks: vec![], })
880 }
881
882 async fn create_local_repository(
884 &self,
885 local_path: &Path,
886 metadata: &RepositoryMetadata,
887 ) -> Result<()> {
888 self.reconstruct_repository(&metadata.object_hashes, local_path)
890 .await?;
891
892 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#[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 #[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 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 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 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 #[derive(Default)]
1001 pub struct MockBlobFetcher {
1002 objects: Mutex<StdHashMap<ContentCid, Vec<u8>>>,
1003 calls: Mutex<usize>,
1004 }
1005
1006 impl MockBlobFetcher {
1007 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 pub async fn insert_raw(&self, cid: ContentCid, data: Vec<u8>) {
1019 self.objects.lock().await.insert(cid, data);
1020 }
1021
1022 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 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 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 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 let temp_dir = TempDir::new()?;
1240
1241 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 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 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 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}