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
|
//! What a change of a swarm holding a store writes (`docs/store.md`, What a change writes),
//! judged by every node by the change and its history alone, in place of rules by path
//! (`rules::judge`). It writes nothing but:
//!
//! * events: at `_continuum/events/<ID>` the bytes whose ID (SHA-256) it is, no more than
//! the fold allows, at a key held nowhere at the change's history;
//! * its node's entry (`_continuum/members/<node ID>`), and the member list's map where
//! there is none.
//!
//! Everything else is closed: whatever isn't under `_continuum`, the policy, the swarm's
//! ID, users, bans, the swarm's topology, remakes and their acks. Nothing deletes or
//! replaces an event. A change names its node, as signed (signatures are required): the
//! genesis change's writer (`document::events`) names none, so no change under it is
//! taken but the genesis change, which the epoch's root is. Which events the store takes
//! is the store's (`federation::held`); this judges none.
//!
//! In a swarm not holding a store, no change writes a policy naming one (`turns_store`):
//! a swarm holds a store from its genesis change, or not at all.
use sha2::{Digest, Sha256};
use super::rules::{Change, Refusal};
use crate::document::at::At;
use crate::document::inspect::{Action, Op, Path};
use crate::document::{meta, store};
/// The most bytes an event of the fold `fold` may have.
fn largest(fold: &str) -> usize {
use crate::federation::store::Fold;
match fold {
crate::federation::fold::Federation::NAME => crate::federation::fold::Federation::LARGEST,
_ => 0,
}
}
/// Judges `change`, of a swarm holding a store of the fold `fold`, at its history `at`.
pub(crate) fn judge(at: &At<'_>, change: &Change<'_>, fold: &str) -> Result<(), Refusal> {
let refuse = |why: String| Err(Refusal::Rule(why));
let Some(node) = change.committer else {
return refuse("it names no node".to_owned());
};
let events = meta::path(&[meta::EVENTS]);
let members = meta::path(&[meta::MEMBERS]);
let entry = meta::path(&[meta::MEMBERS, node]);
for op in change.ops {
let allowed = if op.path.starts_with(&events) {
event(at, op, fold)
} else if op.path.starts_with(&entry) {
Ok(())
} else if op.path == members {
// The member list's map, made where there is none.
if op.action == Action::MakeMap && !store::held_anywhere(at, &members) {
Ok(())
} else {
Err("it writes the member list".to_owned())
}
} else {
Err(format!("it writes {}, closed", shown(&op.path)))
};
allowed.or_else(|why| refuse(format!("{node}: {why}")))?;
}
Ok(())
}
/// Whether `op`, under `_continuum/events`, puts an event at its ID, held nowhere at `at`,
/// no larger than the fold `fold` allows.
fn event(at: &At<'_>, op: &Op, fold: &str) -> Result<(), String> {
let [_, _, key] = op.path.as_slice() else {
return Err(format!("it writes {}, no event's place", shown(&op.path)));
};
let Action::PutBytes(bytes) = &op.action else {
return Err(format!("it writes {}, no event's bytes", shown(&op.path)));
};
if bytes.len() > largest(fold) {
return Err(format!(
"an event of {} bytes, more than {} allows",
bytes.len(),
fold
));
}
if hex(&Sha256::digest(bytes)) != *key {
return Err(format!("an event at {key}, which isn't its ID"));
}
if store::held_anywhere(at, &op.path) {
return Err(format!("an event at {key}, held there already"));
}
Ok(())
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
fn shown(path: &Path) -> String {
format!("/{}", path.join("/"))
}
/// Whether `ops` write a policy naming a store: anything at its `store`, or an envelope
/// at its place naming one.
pub(crate) fn turns_store(ops: &[Op]) -> bool {
let policy = super::policy::path();
let mut store = policy.clone();
store.push("store".to_owned());
ops.iter().any(|op| match &op.action {
_ if op.path.starts_with(&store) => true,
Action::PutString(text) if op.path == policy => crate::document::signed::read(text)
.is_some_and(|payload| payload.value.get("store").is_some()),
_ => false,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::clock::UnixMillis;
use crate::document::replica::{Epoch, Writer, transact};
use crate::document::store::StoreError;
use crate::document::typed::{TimestampFormat, Val};
use crate::document::{Document, ScalarValue, Transaction, events};
use crate::federation::store::Id;
use serde_json::json;
const ROOT: &[u8] = b"{\"a root\":true}";
/// A swarm holding a store, from its genesis change.
fn swarm() -> Document {
events::started("federation", ROOT).unwrap()
}
/// The change `node` writes to `doc` with `f`, judged as every node judges it.
fn judged(
doc: &mut Document,
node: &str,
f: impl FnOnce(&mut Transaction<'_>) -> Result<(), StoreError>,
) -> Result<(), String> {
let before = doc.get_heads();
transact(doc, UnixMillis(1), &Writer { node, user: None }, f).unwrap();
let after = doc.get_heads();
assert_ne!(after, before, "no change written");
super::super::rules::judge_held(doc, Epoch::GENESIS, &after[0]).unwrap()
}
fn put(doc: &mut Transaction<'_>, key: &str, bytes: &[u8]) -> Result<(), StoreError> {
store::write_val_path(
doc,
&meta::path(&[meta::EVENTS, key]),
&Val::Scalar(ScalarValue::Bytes(bytes.to_vec())),
store::Writing::sticky(TimestampFormat::default()),
)
.map(|_| ())
}
#[test]
fn a_change_puts_events_at_their_ids() {
let event = b"an event".to_vec();
let id = Id::of(&event);
let key = id.to_string();
let mut doc = swarm();
let mut beside = doc.fork();
assert_eq!(
judged(&mut doc, "node-a", |doc| put(doc, &key, &event)),
Ok(())
);
// The same, by another at once: one event.
assert_eq!(
judged(&mut beside, "node-b", |doc| put(doc, &key, &event)),
Ok(())
);
// Again, at a key held already: refused. Automerge writes no put of the value a
// key holds, so the change is one made by hand.
let heads = doc.get_heads();
let at = At::new(&doc, &heads).unwrap();
let path = meta::path(&[meta::EVENTS, &key]);
let again = Change {
hash: crate::document::ChangeHash([0; 32]),
deps: &heads,
seen: &heads,
committer: Some("node-b"),
user: None,
paths: &std::collections::BTreeSet::from([path.clone()]),
ops: &[Op {
path,
action: Action::PutBytes(event.clone()),
}],
};
assert!(judge(&at, &again, "federation").is_err());
// At another key, or too large: refused.
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| put(
doc,
&Id::of(b"else").to_string(),
&event
))
.is_err()
);
let mut doc = swarm();
let large = vec![b' '; crate::federation::event::LARGEST + 1];
assert!(
judged(&mut doc, "node-a", |doc| put(
doc,
&Id::of(&large).to_string(),
&large
))
.is_err()
);
// A string, not bytes: refused.
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| {
store::write_path(doc, &meta::path(&[meta::EVENTS, &key]), &json!("an event"))
})
.is_err()
);
}
#[test]
fn nothing_deletes_or_replaces_an_event() {
let root = Id::of(ROOT).to_string();
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| {
store::delete_path(doc, &meta::path(&[meta::EVENTS, &root]), false)
})
.is_err()
);
let mut doc = swarm();
assert!(judged(&mut doc, "node-a", |doc| put(doc, &root, b"other bytes")).is_err());
}
#[test]
fn a_node_writes_its_own_entry_and_nothing_else() {
let entry = |node: &str| meta::path(&[meta::MEMBERS, node]);
let mut doc = swarm();
assert_eq!(
judged(&mut doc, "node-a", |doc| {
store::write_path(doc, &entry("node-a"), &json!({"alias": "a"}))
}),
Ok(())
);
assert!(
judged(&mut doc, "node-a", |doc| {
store::write_path(doc, &entry("node-b"), &json!({}))
})
.is_err()
);
for closed in [
vec!["config".to_owned()],
meta::path(&[meta::USERS, "alice"]),
meta::path(&[meta::SWARM]),
meta::path(&[meta::NEIGHBOURS]),
meta::path(&[meta::VOTES, "node-a"]),
crate::access::policy::path(),
] {
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| store::write_path(
doc,
&closed,
&json!(1)
))
.is_err(),
"{closed:?}"
);
}
}
#[test]
fn a_swarm_turns_into_no_store() {
let mut doc = Document::new();
transact(
&mut doc,
UnixMillis(1),
&Writer {
node: "node-a",
user: None,
},
|doc| store::write_path(doc, &meta::path(&[meta::SWARM]), &json!("s")),
)
.unwrap();
assert!(
judged(&mut doc, "node-a", |doc| {
store::write_path(
doc,
&crate::access::policy::path(),
&events::policy("federation"),
)
})
.is_err()
);
}
}
|