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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use automerge::{ObjId, Patch};
use compact_encoding::{CompactEncoding, State};
#[cfg(not(target_arch = "wasm32"))]
use random_access_disk::RandomAccessDisk;
use random_access_memory::RandomAccessMemory;
use random_access_storage::RandomAccess;
use std::fmt::Debug;
#[cfg(not(target_arch = "wasm32"))]
use std::path::{Path, PathBuf};

#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
use async_std::fs::remove_dir_all;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
use tokio::fs::remove_dir_all;

use crate::{
    common::state::{DocumentState, PeermergeState},
    crdt::{
        add_child_document, get_child_document_decoded_url, AutomergeDoc, DocsChangeResult,
        UnappliedEntries,
    },
    document::DocumentSettings,
    DocumentId, FeedDiscoveryKey, NameDescription, PeerId, PeermergeError,
};

use super::{
    cipher::{encode_document_secret_to_bytes, DecodedDocUrl, DocumentSecret},
    entry::Entry,
    keys::{discovery_key_from_public_key, document_id_from_discovery_key},
    state::{
        ChangeDocumentFeedsStateResult, ChildDocumentInfo, ChildDocumentStatus, DocumentContent,
        DocumentFeedInfo, DocumentFeedsState, DocumentIdWithParents,
    },
};
#[derive(Debug)]
pub(crate) struct PeermergeStateWrapper<T>
where
    T: RandomAccess + Debug + Send,
{
    pub(crate) state: PeermergeState,
    storage: T,
}

impl<T> PeermergeStateWrapper<T>
where
    T: RandomAccess + Debug + Send,
{
    pub(crate) async fn add_document_id_to_state(
        &mut self,
        document_id: DocumentId,
        parent_document_id: Option<DocumentId>,
    ) {
        let parent_document_ids: Vec<DocumentId> =
            parent_document_id.map(|id| vec![id]).unwrap_or_default();
        if let Some(document_id_with_parents) = self
            .state
            .document_ids
            .iter_mut()
            .find(|id_with_parents| id_with_parents.document_id == document_id)
        {
            document_id_with_parents
                .parent_document_ids
                .extend(parent_document_ids);
        } else {
            self.state.document_ids.push(DocumentIdWithParents {
                document_id,
                parent_document_ids,
            });
        }
        write_repo_state(&self.state, &mut self.storage).await;
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn state(&self) -> &PeermergeState {
        &self.state
    }
}

impl PeermergeStateWrapper<RandomAccessMemory> {
    pub(crate) async fn new_memory(
        peer_header: &NameDescription,
        document_write_settings: DocumentSettings,
        peer_id: Option<PeerId>,
    ) -> Self {
        let state = PeermergeState::new(peer_header, vec![], document_write_settings, peer_id);
        let mut storage = RandomAccessMemory::default();
        write_repo_state(&state, &mut storage).await;
        Self { state, storage }
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl PeermergeStateWrapper<RandomAccessDisk> {
    pub(crate) async fn new_disk(
        peer_header: &NameDescription,
        data_root_dir: &Path,
        document_write_settings: DocumentSettings,
    ) -> Result<Self, PeermergeError> {
        let state = PeermergeState::new(peer_header, vec![], document_write_settings, None);
        let state_path = get_peermerge_state_path(data_root_dir);
        let mut storage = RandomAccessDisk::builder(state_path.clone())
            .build()
            .await
            .map_err(|err| PeermergeError::InvalidOperation {
                context: format!("Could not create file to path {state_path:?}, {err}"),
            })?;
        write_repo_state(&state, &mut storage).await;
        Ok(Self { state, storage })
    }

    pub(crate) async fn open_disk(data_root_dir: &Path) -> Result<Option<Self>, PeermergeError> {
        let state_path = get_peermerge_state_path(data_root_dir);
        if state_path.exists() {
            let mut storage = RandomAccessDisk::builder(state_path).build().await.unwrap();
            let len = storage.len().await.expect("Could not get file length");
            let buffer = storage
                .read(0, len)
                .await
                .expect("Could not read file content");
            let mut dec_state = State::from_buffer(&buffer);
            let state: PeermergeState = dec_state.decode(&buffer)?;
            Ok(Some(Self { state, storage }))
        } else {
            Ok(None)
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
fn get_peermerge_state_path(data_root_dir: &Path) -> PathBuf {
    data_root_dir.join(PathBuf::from("peermerge_state.bin"))
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) async fn destroy_path_disk(hypercore_dir: &PathBuf) -> Result<(), PeermergeError> {
    remove_dir_all(hypercore_dir)
        .await
        .map_err(|err| PeermergeError::IO {
            context: Some(format!(
                "Could not delete hypercore directory at {:?}",
                hypercore_dir
            )),
            source: err,
        })
}

#[derive(Debug)]
pub(crate) struct DocStateWrapper<T>
where
    T: RandomAccess + Debug + Send,
{
    state: DocumentState,
    unapplied_entries: UnappliedEntries,
    watched_ids: Option<Vec<ObjId>>,
    storage: T,
}

impl<T> DocStateWrapper<T>
where
    T: RandomAccess + Debug + Send,
{
    pub(crate) async fn merge_new_remote_feeds(
        &mut self,
        new_remote_feeds: &[DocumentFeedInfo],
    ) -> Result<ChangeDocumentFeedsStateResult, PeermergeError> {
        let result = self.state.feeds_state.merge_new_feeds(new_remote_feeds)?;
        if result.changed {
            write_document_state(&self.state, &mut self.storage).await;
        }
        Ok(result)
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn content_mut(&mut self) -> Option<&mut DocumentContent> {
        if let Some(content) = self.state.content.as_mut() {
            Some(content)
        } else {
            None
        }
    }

    pub(crate) fn content_feeds_state_and_unapplied_entries_mut(
        &mut self,
    ) -> Option<(
        &mut DocumentContent,
        &mut DocumentFeedsState,
        &mut UnappliedEntries,
    )> {
        if let Some(content) = self.state.content.as_mut() {
            Some((
                content,
                &mut self.state.feeds_state,
                &mut self.unapplied_entries,
            ))
        } else {
            None
        }
    }

    pub(crate) async fn set_verified_and_persist(
        &mut self,
        discovery_key: &FeedDiscoveryKey,
        peer_id: &Option<PeerId>,
    ) -> bool {
        let changed = self.state.feeds_state.verify_feed(discovery_key, peer_id);
        if changed {
            write_document_state(&self.state, &mut self.storage).await;
        }
        changed
    }

    pub(crate) async fn set_removed_and_persist(
        &mut self,
        discovery_key: &FeedDiscoveryKey,
    ) -> bool {
        let changed = self.state.feeds_state.set_removed(discovery_key);
        if changed {
            write_document_state(&self.state, &mut self.storage).await;
        }
        changed
    }

    pub(crate) fn filter_watched_patches(&self, patches: &mut Vec<Patch>) {
        if let Some(ids) = &self.watched_ids {
            patches.retain(|patch| ids.contains(&patch.obj));
        }
    }

    pub(crate) async fn persist_content(&mut self) {
        write_document_state(&self.state, &mut self.storage).await;
    }

    pub(crate) async fn set_cursor_and_save_data(
        &mut self,
        discovery_key: &FeedDiscoveryKey,
        length: u64,
        change_result: DocsChangeResult,
    ) {
        if let Some(content) = self.state.content.as_mut() {
            content.set_cursor_and_save_data(*discovery_key, length, change_result);
            write_document_state(&self.state, &mut self.storage).await;
        } else {
            unimplemented!("This shouldn't happen")
        }
    }

    pub(crate) fn write_discovery_key(&self) -> Option<FeedDiscoveryKey> {
        self.state
            .feeds_state
            .write_feed
            .as_ref()
            .map(|key| discovery_key_from_public_key(&key.public_key))
    }

    pub(crate) fn state(&self) -> &DocumentState {
        &self.state
    }

    pub(crate) fn state_mut(&mut self) -> &mut DocumentState {
        &mut self.state
    }

    pub(crate) async fn add_created_child_document(
        &mut self,
        child_document_info: ChildDocumentInfo,
        child_document_url: &str,
        child_document_secret: DocumentSecret,
        max_entry_data_size_bytes: usize,
    ) -> Result<Vec<Entry>, PeermergeError> {
        let entries = if let Some(content) = self.state.content.as_mut() {
            if let Some(meta_automerge_doc) = content.meta_automerge_doc.as_mut() {
                let child_document_secret: Vec<u8> =
                    encode_document_secret_to_bytes(&child_document_secret).to_vec();
                let child_document_discovery_key =
                    discovery_key_from_public_key(&child_document_info.doc_public_key);
                let child_document_id =
                    document_id_from_discovery_key(&child_document_discovery_key);
                add_child_document(
                    meta_automerge_doc,
                    child_document_id,
                    child_document_url,
                    child_document_secret,
                    max_entry_data_size_bytes,
                )?
            } else {
                panic!("Can not add a child to a document without an initialized meta doc");
            }
        } else {
            panic!("Can not add a child to a document without content");
        };

        self.state.child_documents.push(child_document_info);
        Ok(entries)
    }

    /// Merge incoming child document info with that of the stored child
    /// document infos. If found returns document secret for the child document and marks
    /// the child document into ChildDocumentStatus::Creating. If child
    /// document not found, or given info already Created/Creating, returns
    /// None.
    pub(crate) async fn merge_child_document_and_persist(
        &mut self,
        child_document_info: &mut ChildDocumentInfo,
        is_proxy: bool,
    ) -> Option<DecodedDocUrl> {
        // First check that creation isn't ongoing already from some other protocol
        let already_stored = if let Some(info) = self
            .state
            .child_documents
            .iter()
            .find(|info| *info == child_document_info)
        {
            if info.status != ChildDocumentStatus::NotCreated {
                // NB: if the status is NotCreated, we want to recheck if secret can be
                // found now, and not return.
                return None;
            }
            true
        } else {
            false
        };

        // Get a mutable reference
        let stored_child_document_info = if already_stored {
            self.state
                .child_documents
                .iter_mut()
                .find(|info| *info == child_document_info)
        } else {
            None
        };

        // Reset status so that value from remote isn't carried over here
        child_document_info.status = ChildDocumentStatus::NotCreated;

        let decoded_url = if is_proxy {
            // Create a proxy url
            let child_document_discovery_key =
                discovery_key_from_public_key(&child_document_info.doc_public_key);
            let child_document_id = document_id_from_discovery_key(&child_document_discovery_key);
            Some(DecodedDocUrl::new_proxy(
                child_document_info.doc_public_key,
                child_document_discovery_key,
                child_document_id,
                child_document_info.doc_signature_verifying_key,
                true,
            ))
        } else if let Some(content) = self.state.content.as_ref() {
            // Search for the URL and secret from the meta doc
            if let Some(meta_automerge_doc) = content.meta_automerge_doc.as_ref() {
                let child_document_discovery_key =
                    discovery_key_from_public_key(&child_document_info.doc_public_key);
                let child_document_id =
                    document_id_from_discovery_key(&child_document_discovery_key);
                get_child_document_decoded_url(meta_automerge_doc, child_document_id)
            } else {
                None
            }
        } else {
            None
        };

        if decoded_url.is_some() {
            // Was able to create the url, mark this as creating
            if let Some(info) = stored_child_document_info {
                info.status = ChildDocumentStatus::Creating;
            } else {
                child_document_info.status = ChildDocumentStatus::Creating;
            }
        }

        if !already_stored {
            self.state.child_documents.push(child_document_info.clone());
        }
        if !already_stored || decoded_url.is_some() {
            write_document_state(&self.state, &mut self.storage).await;
        }
        decoded_url
    }

    pub(crate) async fn set_child_document_created_and_persist(
        &mut self,
        child_document_info: &ChildDocumentInfo,
    ) -> ChildDocumentInfo {
        // Get a mutable reference
        let stored_child_document_info = self
            .state
            .child_documents
            .iter_mut()
            .find(|info| *info == child_document_info)
            .expect("Child document must be set to Creating before persisting");
        stored_child_document_info.status = ChildDocumentStatus::Created;
        let return_info = stored_child_document_info.clone();
        write_document_state(&self.state, &mut self.storage).await;
        return_info
    }

    pub(crate) fn user_automerge_doc(&self) -> Option<&AutomergeDoc> {
        self.state
            .content
            .as_ref()
            .and_then(|content| content.user_automerge_doc.as_ref())
    }

    pub(crate) fn user_automerge_doc_mut(&mut self) -> Option<&mut AutomergeDoc> {
        self.state
            .content
            .as_mut()
            .and_then(|content| content.user_automerge_doc.as_mut())
    }

    // pub(crate) fn meta_automerge_doc(&self) -> Option<&AutomergeDoc> {
    //     self.state
    //         .content
    //         .as_ref()
    //         .and_then(|content| content.meta_automerge_doc.as_ref())
    // }

    // pub(crate) fn meta_automerge_doc_mut(&mut self) -> Option<&mut AutomergeDoc> {
    //     self.state
    //         .content
    //         .as_mut()
    //         .and_then(|content| content.meta_automerge_doc.as_mut())
    // }

    pub(crate) fn watch(&mut self, ids: Option<Vec<ObjId>>) {
        self.watched_ids = ids;
    }

    pub(crate) fn reserve_object<O: AsRef<ObjId>>(&mut self, obj: O) {
        let obj = obj.as_ref();
        if !self.unapplied_entries.reserved_ids.contains(obj) {
            self.unapplied_entries.reserved_ids.insert(obj.clone());
        }
    }

    pub(crate) fn unreserve_object<O: AsRef<ObjId>>(&mut self, obj: O) {
        self.unapplied_entries.reserved_ids.remove(obj.as_ref());
    }
}

impl DocStateWrapper<RandomAccessMemory> {
    pub(crate) async fn new_memory(state: DocumentState) -> Self {
        let storage = RandomAccessMemory::default();
        Self {
            state,
            storage,
            unapplied_entries: UnappliedEntries::new(),
            watched_ids: None,
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl DocStateWrapper<RandomAccessDisk> {
    pub(crate) async fn new_disk(state: DocumentState, data_root_dir: &Path) -> Self {
        let state_path = get_document_state_path(data_root_dir);
        let mut storage = RandomAccessDisk::builder(state_path).build().await.unwrap();
        write_document_state(&state, &mut storage).await;
        Self {
            state,
            storage,
            unapplied_entries: UnappliedEntries::new(),
            watched_ids: None,
        }
    }

    pub(crate) async fn open_disk(data_root_dir: &Path) -> Result<Self, PeermergeError> {
        let state_path = get_document_state_path(data_root_dir);
        let mut storage = RandomAccessDisk::builder(state_path).build().await.unwrap();
        let len = storage.len().await.expect("Could not get file length");
        let buffer = storage
            .read(0, len)
            .await
            .expect("Could not read file content");
        let mut dec_state = State::from_buffer(&buffer);
        let state: DocumentState = dec_state.decode(&buffer)?;
        Ok(Self {
            state,
            storage,
            unapplied_entries: UnappliedEntries::new(),
            watched_ids: None,
        })
    }
}

#[cfg(not(target_arch = "wasm32"))]
fn get_document_state_path(data_root_dir: &Path) -> PathBuf {
    data_root_dir.join(PathBuf::from("document_state.bin"))
}

async fn write_repo_state<T>(repo_state: &PeermergeState, storage: &mut T)
where
    T: RandomAccess + Debug + Send,
{
    let mut enc_state = State::new();
    enc_state
        .preencode(repo_state)
        .expect("Pre-encoding repo state should not fail");
    let mut buffer = enc_state.create_buffer();
    enc_state
        .encode(repo_state, &mut buffer)
        .expect("Encoding repo state should not fail");
    storage.write(0, &buffer).await.unwrap();
}

async fn write_document_state<T>(document_state: &DocumentState, storage: &mut T)
where
    T: RandomAccess + Debug + Send,
{
    let mut enc_state = State::new();
    enc_state
        .preencode(document_state)
        .expect("Pre-encoding document state should not fail");
    let mut buffer = enc_state.create_buffer();
    enc_state
        .encode(document_state, &mut buffer)
        .expect("Encoding document state should not fail");
    storage.write(0, &buffer).await.unwrap();
}