1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use std::{
    io::{Cursor, Read, Write},
    sync::Arc,
};

use borsh::{BorshDeserialize, BorshSerialize};
use futures::TryStreamExt;
use iroh::{
    base::node_addr::AddrInfoOptions,
    docs::{store::Query, Author, AuthorId, Capability, NamespaceSecret},
};
use once_cell::sync::Lazy;

use crate::{
    store::LeafStore,
    types::{EntityPath, NamespaceSecretKey, PathSegment, SubspaceId},
    Digest, ExactLink,
};

pub type LeafIroh = crate::Leaf<LeafIrohStore>;

pub const LEAF_GC_PREFIX_STR: &str = "_leaf_gc_";
pub static LEAF_GC_PREFIX: Lazy<PathSegment> =
    Lazy::new(|| PathSegment::String(LEAF_GC_PREFIX_STR.into()));

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct LeafGcPathPrefix {
    pub leaf_gc_prefix_str: String,
    pub subspace: [u8; 32],
    pub entity_path: Vec<PathSegment>,
    pub entity_snapshot_id: Digest,
}

impl LeafGcPathPrefix {
    pub fn new(link: &ExactLink, entity_snapshot_id: Digest) -> Self {
        LeafGcPathPrefix {
            leaf_gc_prefix_str: LEAF_GC_PREFIX_STR.into(),
            subspace: link.subspace,
            entity_path: link.path.0.clone(),
            entity_snapshot_id,
        }
    }
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        self.serialize(&mut buf).unwrap();
        buf
    }
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct LeafGcPath {
    pub prefix: LeafGcPathPrefix,
    pub entity_snapshot_id_plus_blob_hash: Digest,
}

impl std::fmt::Debug for LeafGcPath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "LeafGCPath(")?;
        f.debug_list()
            .entry(&self.prefix.leaf_gc_prefix_str)
            .entry(&iroh::base::base32::fmt(self.prefix.subspace))
            .entries(&self.prefix.entity_path)
            .entry(&self.prefix.entity_snapshot_id)
            .entry(&self.entity_snapshot_id_plus_blob_hash)
            .finish()?;
        write!(f, ")")
    }
}

impl LeafGcPath {
    pub fn new(link: &ExactLink, entity_snapshot_id: Digest, blob_hash: Digest) -> Self {
        let entity_snapshot_id_plus_blob_hash =
            Digest::new(&[*entity_snapshot_id.as_bytes(), *blob_hash.as_bytes()].concat());
        Self {
            prefix: LeafGcPathPrefix::new(link, entity_snapshot_id),
            entity_snapshot_id_plus_blob_hash,
        }
    }
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        self.serialize(&mut buf).unwrap();
        buf
    }
    pub fn from_bytes(mut bytes: &[u8]) -> std::io::Result<Self> {
        Self::deserialize(&mut bytes)
    }
}

#[derive(Debug, Clone)]
pub struct LeafIrohStore {
    pub client: iroh::client::Iroh,
    pub docs: Arc<quick_cache::sync::Cache<iroh::docs::NamespaceId, iroh::client::Doc>>,
}
pub struct IrohDocumentKeyFormat {
    pub path: Vec<PathSegment>,
}
impl IrohDocumentKeyFormat {
    pub fn new(path: Vec<PathSegment>) -> Self {
        Self { path }
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        let mut segment_bytes = Vec::new();
        for segment in &self.path {
            segment.serialize(&mut segment_bytes).unwrap();
            let len: u32 = segment_bytes.len().try_into().unwrap();
            buf.write_all(&len.to_le_bytes()[..]).unwrap();
            buf.write_all(&segment_bytes).unwrap();
            segment_bytes.clear();
        }
        buf
    }

    pub fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self> {
        let len = bytes.len();
        if len == 0 {
            return Ok(Self { path: Vec::new() });
        }
        let mut reader = Cursor::new(bytes);
        let mut path = Vec::new();
        let mut segment_bytes = Vec::new();
        loop {
            if reader.position() as usize == len {
                break;
            }
            let mut segment_len_bytes = [0u8; 4];
            reader.read_exact(&mut segment_len_bytes)?;
            let segment_len = u32::from_le_bytes(segment_len_bytes);

            segment_bytes.extend(std::iter::repeat(0u8).take(segment_len as _));
            reader.read_exact(&mut segment_bytes)?;

            path.push(PathSegment::deserialize(&mut &segment_bytes[..])?);
            segment_bytes.clear();
        }
        Ok(Self { path })
    }
}

impl LeafIrohStore {
    pub fn new(client: iroh::client::Iroh) -> Self {
        Self {
            client,
            docs: Arc::new(quick_cache::sync::Cache::new(10)),
        }
    }

    /// Open a document using the local document cache.
    pub async fn open(&self, ns: iroh::docs::NamespaceId) -> anyhow::Result<iroh::client::Doc> {
        self.docs
            .get_or_insert_async(&ns, async {
                self.client
                    .docs()
                    .import_namespace(iroh::docs::Capability::Read(ns))
                    .await
            })
            .await
    }

    pub fn get_entity_key(subspace: SubspaceId, path: &[PathSegment]) -> Vec<u8> {
        assert_ne!(
            path.first(),
            Some(&*LEAF_GC_PREFIX),
            "Cannot write entities to reserved prefix: {}",
            LEAF_GC_PREFIX_STR
        );
        let mut path = path.to_vec();
        path.insert(0, PathSegment::Bytes(subspace.to_vec()));
        IrohDocumentKeyFormat::new(path).to_bytes()
    }
}

impl LeafStore for LeafIrohStore {
    fn key_resolvers(&self) -> Box<dyn Iterator<Item = &dyn super::KeyResolverImpl<Digest>> + '_> {
        Box::new([].into_iter())
    }

    fn encryption_algorithms(
        &self,
    ) -> Box<dyn Iterator<Item = &dyn super::EncryptionAlgorithmImpl<Digest>> + '_> {
        Box::new([].into_iter())
    }

    async fn store_blob(
        &self,
        data: &[u8],
        link: &ExactLink,
        entity_snapshot_id: Digest,
    ) -> anyhow::Result<Digest> {
        let doc = self.open(link.namespace.into()).await?;
        let hash = self.client.blobs().add_bytes(data.to_vec()).await?.hash;

        let key = LeafGcPath::new(link, entity_snapshot_id, Digest(hash));
        let doc_key = key.to_bytes();
        let author_id = self.client.authors().default().await?;
        doc.set_hash(author_id, doc_key, hash, data.len() as u64)
            .await?;
        Ok(Digest(hash))
    }

    async fn del_blobs(
        &self,
        link: &ExactLink,
        entity_snapshot_id: Digest,
    ) -> anyhow::Result<usize> {
        let doc = self.open(link.namespace.into()).await?;

        let path_prefix = LeafGcPathPrefix::new(link, entity_snapshot_id).to_bytes();
        let author_id = self.client.authors().default().await?;
        let deleted = doc.del(author_id, path_prefix).await?;

        Ok(deleted)
    }

    async fn get_blob(&self, digest: Digest) -> anyhow::Result<Vec<u8>> {
        Ok(self.client.blobs().read_to_bytes(digest.0).await?.to_vec())
    }

    async fn store_entity(&self, link: &ExactLink, data: Vec<u8>) -> anyhow::Result<Digest> {
        let doc = self.open(link.namespace.into()).await?;
        let key = Self::get_entity_key(link.subspace, &link.path.0);
        let digest = doc.set_bytes(link.subspace.into(), key, data).await?;
        Ok(Digest(digest))
    }
    async fn del_entity(&self, link: &ExactLink) -> anyhow::Result<()> {
        let doc = self.open(link.namespace.into()).await?;
        let key = Self::get_entity_key(link.subspace, &link.path.0);
        doc.del(link.subspace.into(), key).await?;
        Ok(())
    }

    async fn get_entity(&self, link: &ExactLink) -> anyhow::Result<Option<Digest>> {
        let doc = self.open(link.namespace.into()).await?;
        let key = Self::get_entity_key(link.subspace, &link.path.0);
        let entity = doc.get_exact(link.subspace.into(), key, false).await?;
        let entity = entity.map(|entry| Digest(entry.content_hash()));
        Ok(entity)
    }

    async fn list(
        &self,
        link: ExactLink,
        limit: Option<u64>,
        offset: Option<u64>,
    ) -> anyhow::Result<impl futures::Stream<Item = anyhow::Result<ExactLink>>> {
        let link = link.clone();
        let doc = self.open(link.namespace.into()).await?;

        let mut path = vec![PathSegment::Bytes(link.subspace.to_vec())];
        path.extend(link.path.0.iter().cloned());
        let path_bytes = IrohDocumentKeyFormat::new(path).to_bytes();

        let mut query = Query::key_prefix(path_bytes).author(link.subspace.into());
        if let Some(limit) = limit {
            query = query.limit(limit);
        }
        if let Some(offset) = offset {
            query = query.limit(offset);
        }
        let stream = doc.get_many(query).await?;

        let s = stream.and_then(move |x| async move {
            let mut key = IrohDocumentKeyFormat::from_bytes(x.key())?;
            key.path.remove(0); // Remove the subspace path segment

            Ok(ExactLink {
                namespace: link.namespace,
                subspace: link.subspace,
                path: EntityPath(key.path),
            })
        });

        Ok(s)
    }

    async fn create_subspace(&self) -> anyhow::Result<SubspaceId> {
        let author = self.client.authors().create().await?;
        Ok(*author.as_bytes())
    }

    async fn import_subspace_secret(&self, author_secret: [u8; 32]) -> anyhow::Result<SubspaceId> {
        let author = Author::from_bytes(&author_secret);
        let id = *author.public_key().as_bytes();
        self.client.authors().import(author).await?;
        Ok(id)
    }

    async fn get_subspace_secret(
        &self,
        author: SubspaceId,
    ) -> anyhow::Result<Option<crate::prelude::SubspaceSecretKey>> {
        let author = self.client.authors().export(AuthorId::from(author)).await?;
        Ok(author.map(|x| x.to_bytes()))
    }

    async fn create_namespace(&self) -> anyhow::Result<crate::prelude::NamespaceId> {
        let doc = self.client.docs().create().await?;
        Ok(doc.id().to_bytes())
    }

    async fn import_namespace_secret(
        &self,
        namespace_secret: [u8; 32],
    ) -> anyhow::Result<crate::prelude::NamespaceId> {
        let secret = NamespaceSecret::from_bytes(&namespace_secret);
        let id = *secret.id().as_bytes();
        self.client
            .docs()
            .import_namespace(iroh::docs::Capability::Write(secret))
            .await?;
        Ok(id)
    }

    async fn get_namespace_secret(
        &self,
        namespace: crate::prelude::NamespaceId,
    ) -> anyhow::Result<Option<NamespaceSecretKey>> {
        let doc = self.open(namespace.into()).await?;
        let capability = doc
            .share(iroh::client::docs::ShareMode::Write, AddrInfoOptions::Id)
            .await
            .map(|x| x.capability)
            .ok()
            .and_then(|x| {
                if let Capability::Write(secret) = x {
                    Some(secret.to_bytes())
                } else {
                    None
                }
            });
        Ok(capability)
    }
}