-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloading.rs
169 lines (157 loc) · 5.56 KB
/
loading.rs
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
use std::str::FromStr;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use futures::channel::mpsc;
use futures::channel::oneshot;
use keyhive_core::keyhive::Keyhive;
use keyhive_core::store::ciphertext::memory::MemoryCiphertextStore;
use crate::doc_state::DocState;
use crate::io::{IoHandle, IoResult};
use crate::state::State;
use crate::task_context::Storage;
use crate::{driver, DocumentId, PeerId, Signer};
use crate::{io::IoTask, task_context::DocStorage, Beelay, StorageKey, UnixTimestampMillis};
pub struct Loading<R: rand::Rng + rand::CryptoRng + Clone + 'static> {
driver: driver::Driver,
result: oneshot::Receiver<LoadedParts<R>>,
}
pub(crate) struct LoadedParts<R: rand::Rng + rand::CryptoRng> {
pub(crate) state: Rc<RefCell<State<R>>>,
pub(crate) peer_id: PeerId,
}
pub enum Step<R: rand::Rng + rand::CryptoRng + Clone + 'static> {
Loading(Loading<R>, Vec<IoTask>),
Loaded(Beelay<R>, Vec<IoTask>),
}
impl<R: rand::Rng + rand::CryptoRng + Clone + 'static> Loading<R> {
pub(crate) fn new(
now: UnixTimestampMillis,
driver: driver::Driver,
rx_loaded: oneshot::Receiver<LoadedParts<R>>,
) -> Step<R> {
let loading = Self {
result: rx_loaded,
driver,
};
loading.step(now)
}
fn step(mut self, now: UnixTimestampMillis) -> Step<R> {
let new_events = self.driver.step(now);
if let Ok(Some(parts)) = self.result.try_recv() {
Step::Loaded(Beelay::loaded(parts, self.driver), new_events.new_tasks)
} else {
Step::Loading(self, new_events.new_tasks)
}
}
pub fn handle_io_complete(mut self, now: UnixTimestampMillis, result: IoResult) -> Step<R> {
self.driver.handle_io_complete(result);
self.step(now)
}
}
pub(crate) async fn load_keyhive<R: rand::Rng + rand::CryptoRng + Clone + 'static>(
io: IoHandle,
rng: R,
signer: Signer,
) -> (
Keyhive<
Signer,
crate::CommitHash,
Vec<u8>,
MemoryCiphertextStore<crate::CommitHash, Vec<u8>>,
crate::keyhive::Listener,
R,
>,
mpsc::UnboundedReceiver<
keyhive_core::event::Event<Signer, crate::CommitHash, crate::keyhive::Listener>,
>,
) {
let (tx, rx) = mpsc::unbounded();
let listener = crate::keyhive::Listener::new(tx);
// let signer = Signer::new(verifying_key, io.clone());
let mut keyhive = if let Some(archive) = crate::keyhive_storage::load_archive(io.clone()).await
{
tracing::trace!("keyhive archive found on disk, attempting to load");
match keyhive_core::keyhive::Keyhive::try_from_archive(
&archive,
signer.clone(),
MemoryCiphertextStore::new(), // FIXME use exitsing!
listener.clone(),
rng.clone(),
) {
Ok(k) => {
tracing::debug!("loaded keyhive archive");
k
}
Err(e) => {
tracing::error!(err=?e, "failed to load keyhive archive");
keyhive_core::keyhive::Keyhive::generate(
signer,
MemoryCiphertextStore::new(/* FIXME use something else! */),
listener.clone(),
rng.clone(),
)
.await
.unwrap()
}
}
} else {
tracing::trace!("no archive found on disk, creating a new one");
let result = keyhive_core::keyhive::Keyhive::generate(
signer,
MemoryCiphertextStore::new(/* FIXME */),
listener.clone(),
rng.clone(),
)
.await
.unwrap();
let archived = result.into_archive();
crate::keyhive_storage::save_archive(io.clone(), archived).await;
result
};
let events = crate::keyhive_storage::load_events(io).await;
tracing::trace!(num_events = events.len(), ?events, "loading keyhive events");
if let Err(e) = keyhive.ingest_unsorted_static_events(events).await {
tracing::error!(err=?e, "failed to ingest keyhive events");
}
// for event in events {
// tracing::trace!(event=?event, "processing loaded event");
// if let Err(e) = keyhive.receive_static_event(event) {
// tracing::error!(err=?e, "failed to handle keyhive event");
// }
// }
(keyhive, rx)
}
pub(crate) async fn load_docs(io: IoHandle) -> HashMap<DocumentId, DocState> {
let docs = Storage::new(&io)
.list_one_level(StorageKey::sedimentrees())
.await;
tracing::debug!(num_docs = docs.len(), "loading documents");
let load_futs = docs.into_iter().filter_map(|doc_id_key| {
let Some(name) = doc_id_key.name() else {
return None;
};
let doc_id = match DocumentId::from_str(name) {
Ok(d) => d,
Err(e) => {
tracing::warn!(?doc_id_key, err=?e, "failed to parse stored document id");
return None;
}
};
let doc_storage = DocStorage::new(io.clone(), doc_id);
Some(async move {
let doc = crate::sedimentree::storage::load(doc_storage).await;
(doc_id, doc)
})
});
futures::future::join_all(load_futs)
.await
.into_iter()
.filter_map(|(doc_id, doc_result)| match doc_result {
Ok(Some(doc)) => Some((doc_id, DocState::new(doc))),
Ok(None) => None,
Err(e) => {
tracing::error!(err=?e, %doc_id, "failed to load document");
None
}
})
.collect()
}