|
| 1 | +use anyhow::Result; |
| 2 | +use iroh::{protocol::Router, Endpoint}; |
| 3 | +use iroh_blobs::{ |
| 4 | + api::{blobs::Blobs, Store}, |
| 5 | + format::collection::Collection, |
| 6 | + hashseq::HashSeq, |
| 7 | + store::mem::MemStore, |
| 8 | + BlobsProtocol, Hash, |
| 9 | +}; |
| 10 | + |
| 11 | +struct Node { |
| 12 | + store: Store, |
| 13 | + blobs: Blobs, |
| 14 | + router: Router, |
| 15 | +} |
| 16 | + |
| 17 | +impl Node { |
| 18 | + async fn new() -> Result<Self> { |
| 19 | + let endpoint = Endpoint::builder().discovery_n0().bind().await?; |
| 20 | + |
| 21 | + // We initialize an in-memory backing store for iroh-blobs |
| 22 | + let store = MemStore::new(); |
| 23 | + // Then we initialize a struct that can accept blobs requests over iroh connections |
| 24 | + let blobs = BlobsProtocol::new(&store, endpoint.clone(), None); |
| 25 | + |
| 26 | + let router = Router::builder(endpoint) |
| 27 | + .accept(iroh_blobs::ALPN, blobs.clone()) |
| 28 | + .spawn(); |
| 29 | + |
| 30 | + Ok(Self { |
| 31 | + store, |
| 32 | + blobs: blobs.blobs().clone(), |
| 33 | + router, |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + async fn list_hashes(&self) -> Result<Vec<Hash>> { |
| 38 | + todo!(); |
| 39 | + } |
| 40 | + |
| 41 | + async fn create_collection(&self) -> Result<Hash> { |
| 42 | + let tx = self.store.batch().await?; |
| 43 | + let a = tx.add_slice(b"a").await?; |
| 44 | + let b = tx.add_slice(b"b").await?; |
| 45 | + let c = tx.add_slice(b"c").await?; |
| 46 | + |
| 47 | + let collection = Collection::from_iter([ |
| 48 | + ("a", a.hash().clone()), |
| 49 | + ("b", b.hash().clone()), |
| 50 | + ("c", c.hash().clone()), |
| 51 | + ]); |
| 52 | + |
| 53 | + let collection = collection.store(&self.store).await?; |
| 54 | + let tag = self.store.tags().create(collection).await?; |
| 55 | + Ok(tag.hash()) |
| 56 | + } |
| 57 | + |
| 58 | + async fn get_collection(&self, hash: Hash) -> Result<()> { |
| 59 | + todo!(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[tokio::main] |
| 64 | +async fn main() -> anyhow::Result<()> { |
| 65 | + let send_node = Node::new().await?; |
| 66 | + let hash = send_node.create_collection().await?; |
| 67 | + |
| 68 | + let recv_node = Node::new().await?; |
| 69 | + recv_node.get_collection(hash).await?; |
| 70 | + |
| 71 | + let send_hashes = send_node.list_hashes().await?; |
| 72 | + let recv_hashes = recv_node.list_hashes().await?; |
| 73 | + assert_eq!(send_hashes, recv_hashes); |
| 74 | + |
| 75 | + Ok(()) |
| 76 | +} |
0 commit comments