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
use crate::native::{commands::serve::tracing::initialize_tracing, workspace::Workspace};
use anyhow::{anyhow, Result};
use noosphere_api::{
client::Client,
data::{FetchParameters, FetchResponse, PushBody, PushResponse},
};
use noosphere_core::{
authority::{Authorization, SUPPORTED_KEYS},
view::Sphere,
};
use noosphere_storage::{db::SphereDb, interface::Store, memory::MemoryStore};
use ucan::crypto::{did::DidParser, KeyMaterial};
pub async fn sync(workspace: &Workspace) -> Result<()> {
initialize_tracing();
workspace.expect_local_directories()?;
let mut db = workspace.get_local_db().await?;
let mut memory_store = MemoryStore::default();
match workspace
.get_local_content_changes(None, &db, &mut memory_store)
.await?
{
Some((_, content_changes)) if !content_changes.is_empty() => {
return Err(anyhow!(
"You have unsaved local changes; save or revert them before syncing!"
));
}
_ => (),
};
let gateway_url = workspace.get_local_gateway_url().await?;
let authorization = workspace.get_local_authorization().await?;
let key = workspace.get_local_key().await?;
let sphere_identity = workspace.get_local_identity().await?;
let mut did_parser = DidParser::new(SUPPORTED_KEYS);
println!("Handshaking with gateway at {}...", gateway_url);
let client = Client::identify(
&sphere_identity,
&gateway_url,
&key,
&authorization,
&mut did_parser,
db.clone(),
)
.await?;
sync_remote_changes(
&sphere_identity,
&client,
&key,
Some(&authorization),
&mut db,
)
.await?;
push_local_changes(&sphere_identity, &client, &mut db).await?;
println!("Sync complete, rendering updated workspace...");
workspace.render(&mut db).await?;
println!("Done!");
Ok(())
}
pub async fn push_local_changes<'a, S, K>(
local_sphere_identity: &str,
client: &Client<'a, K, SphereDb<S>>,
db: &mut SphereDb<S>,
) -> Result<()>
where
K: KeyMaterial,
S: Store,
{
let local_sphere_identity = local_sphere_identity.to_string();
let counterpart_sphere_identity = client.session.sphere_identity.clone();
let local_sphere_tip = db
.get_version(&local_sphere_identity)
.await?
.ok_or_else(|| {
anyhow!(
"The history of local sphere {} is missing!",
local_sphere_identity
)
})?;
let counterpart_sphere_tip = db
.get_version(&counterpart_sphere_identity)
.await?
.ok_or_else(|| {
anyhow!(
"No local history for counterpart sphere {}; did you forget to fetch?",
counterpart_sphere_identity
)
})?;
let local_sphere_base = Sphere::at(&counterpart_sphere_tip, db)
.try_get_links()
.await?
.get(&local_sphere_identity)
.await?
.cloned();
if local_sphere_base == Some(local_sphere_tip) {
println!("Gateway is already up to date!");
return Ok(());
}
println!("Collecting blocks from new local history...");
let bundle = Sphere::at(&local_sphere_tip, db)
.try_bundle_until_ancestor(local_sphere_base.as_ref())
.await?;
println!(
"Pushing new local history to gateway {}...",
client.session.gateway_identity
);
let result = client
.push(&PushBody {
sphere: local_sphere_identity,
base: local_sphere_base,
tip: local_sphere_tip,
blocks: bundle,
})
.await?;
let (counterpart_sphere_updated_tip, new_blocks) = match result {
PushResponse::Accepted { new_tip, blocks } => (new_tip, blocks),
PushResponse::NoChange => {
return Err(anyhow!("Gateway already up to date!"));
}
};
println!("Saving updated counterpart sphere history...");
new_blocks.load_into(db).await?;
Sphere::try_hydrate_range(
Some(&counterpart_sphere_tip),
&counterpart_sphere_updated_tip,
db,
)
.await?;
db.set_version(
&counterpart_sphere_identity,
&counterpart_sphere_updated_tip,
)
.await?;
Ok(())
}
pub async fn sync_remote_changes<'a, K, S>(
local_sphere_identity: &str,
client: &Client<'a, K, SphereDb<S>>,
credential: &'a K,
authorization: Option<&Authorization>,
db: &mut SphereDb<S>,
) -> Result<()>
where
K: KeyMaterial,
S: Store,
{
let local_sphere_identity = local_sphere_identity.to_string();
let counterpart_sphere_identity = client.session.sphere_identity.clone();
let counterpart_sphere_base = db.get_version(&counterpart_sphere_identity).await?;
println!(
"Fetching latest changes from gateway {}...",
client.session.gateway_identity
);
let fetch_result = client
.fetch(&FetchParameters {
since: counterpart_sphere_base,
})
.await?;
let (counterpart_sphere_tip, new_blocks) = match fetch_result {
FetchResponse::NewChanges { tip, blocks } => (tip, blocks),
FetchResponse::UpToDate => {
println!("Local history is already up to date...");
return Ok(());
}
};
println!("Saving blocks to local database...");
new_blocks.load_into(db).await?;
println!("Hydrating received counterpart sphere revisions...");
Sphere::try_hydrate_range(
counterpart_sphere_base.as_ref(),
&counterpart_sphere_tip,
db,
)
.await?;
db.set_version(&counterpart_sphere_identity, &counterpart_sphere_tip)
.await?;
let local_sphere_tip = db.get_version(&local_sphere_identity).await?;
let local_sphere_old_base = match counterpart_sphere_base {
Some(counterpart_sphere_base) => Sphere::at(&counterpart_sphere_base, db)
.try_get_links()
.await?
.get(&local_sphere_identity)
.await?
.cloned(),
None => None,
};
let local_sphere_new_base = Sphere::at(&counterpart_sphere_tip, db)
.try_get_links()
.await?
.get(&local_sphere_identity)
.await?
.cloned();
match (
local_sphere_tip,
local_sphere_old_base,
local_sphere_new_base,
) {
(Some(current_tip), Some(old_base), Some(new_base)) => {
println!("Syncing received local sphere revisions...");
let new_tip = Sphere::at(¤t_tip, db)
.try_sync(&old_base, &new_base, credential, authorization)
.await?;
db.set_version(&local_sphere_identity, &new_tip).await?;
}
(None, old_base, Some(new_base)) => {
println!("Hydrating received local sphere revisions...");
Sphere::try_hydrate_range(old_base.as_ref(), &new_base, db).await?;
db.set_version(&local_sphere_identity, &new_base).await?;
}
_ => {
println!("Nothing to sync!");
return Ok(());
}
};
Ok(())
}