diff options
| -rw-r--r-- | docs/lifecycle.md | 5 | ||||
| -rw-r--r-- | src/access/screen.rs | 76 | ||||
| -rw-r--r-- | src/document/signatures.rs | 11 |
3 files changed, 71 insertions, 21 deletions
diff --git a/docs/lifecycle.md b/docs/lifecycle.md index 3843646..e6ac31b 100644 --- a/docs/lifecycle.md +++ b/docs/lifecycle.md @@ -101,8 +101,9 @@ document file holding a document is an error with no override. started on copies of one file would both write on as that actor, and each one's changes would be refused as forged by peers holding the other's: don't start a copy of a node's data directory while the node runs, or after it, with `--keep-actor`. A - node receiving a change of its own actor that it didn't make writes on as a fresh - actor at once, and logs a warning; what both wrote before stays refused where the + node receiving a change of its own actor that it didn't make, signed with its own + key, writes on as a fresh actor at once, and logs a warning (unsigned, it is + anyone's forgery: refused, and nothing switches); what both wrote before stays refused where the other's came first. Fewer actors mean fewer signatures in every transfer of the epoch's history (a writer each); a crash, a drop, or a switch or a rejoin still starts a fresh one. diff --git a/src/access/screen.rs b/src/access/screen.rs index 92cc360..cb088d5 100644 --- a/src/access/screen.rs +++ b/src/access/screen.rs @@ -721,17 +721,32 @@ pub(crate) fn apply_bytes( } let mut changes = crate::document::changes::changes_in(bytes) .ok_or_else(|| anyhow::anyhow!("Refused changes that don't load"))?; - // A change of the actor this node writes as, and not one it made: another node writes - // as it too (two started from one document file with `--keep-actor`, a copied data - // directory). Each would make changes the other's peers refuse as forged; the node - // writes on as a fresh actor once these are judged. They are judged as any other, - // under the actor still: one at a seq the node made a change at is forged - // (`Signatures::forks`). + // A change of the actor this node writes as, not one it made, and signed with this + // node's key: another node writes as it too (two started from one document file with + // `--keep-actor`, a copied data directory, which hold the key). Each would make + // changes the other's peers refuse as forged; the node writes on as a fresh actor + // once these are judged. They are judged as any other, under the actor still: one at + // a seq the node made a change at is forged (`Signatures::forks`). One nobody signed + // with this node's key is anyone's forgery, and switches nothing: else any peer + // would make the node start writers at will. + let own_key = doc + .get_author_for_actor(doc.get_actor()) + .and_then(|author| { + std::str::from_utf8(author.as_bytes()) + .ok() + .map(str::to_owned) + }) + .and_then(|author| crate::identity::public_of(&author)); let twin = changes .iter() .find(|change| { change.actor_id() == doc.get_actor() && !crate::document::changes::holds(&*doc, &change.hash()) + && own_key.is_some_and(|key| { + signatures + .as_deref() + .is_some_and(|signatures| signatures.signed_by(&change.hash(), &key)) + }) }) .map(Change::seq); let switch = |doc: &mut Document| { @@ -1146,13 +1161,31 @@ mod tests { /// A change of the node's own actor that it didn't make: refused as forged where the /// node made one at its seq, and it writes on as a fresh actor. Its own changes /// coming back change nothing. + /// A change of this node's actor it didn't make, signed with its key: another node + /// writes as it (its files copied), and it switches to a fresh actor. Unsigned, it is + /// anyone's forgery, and switches nothing. #[test] fn another_writing_as_this_node() { - let mut theirs = genesis(); + let key = crate::identity::NodeKey::from_seed([5; 32]); + let node = key.id(); + let mut theirs = Document::new(); + transact( + &mut theirs, + UnixMillis(1), + &Writer { + node: &node, + user: None, + }, + |doc| store::write_path(doc, &path("_continuum/swarm"), &json!("s")), + ) + .unwrap(); let mut ours = theirs.fork().with_actor(theirs.get_actor().clone()); let screening = std::sync::Mutex::new(Screening::default()); let mut signatures = Signatures::default(); - let mut take = |doc: &mut Document, from: &Document, since: &[ChangeHash]| { + let take = |doc: &mut Document, + signatures: &mut Signatures, + from: &Document, + since: &[ChangeHash]| { let bytes: Vec<u8> = received(from, since) .iter() .flat_map(|change| change.raw_bytes().to_vec()) @@ -1163,27 +1196,32 @@ mod tests { 0, &bytes, &screening, - Some(&mut signatures), + Some(signatures), &mut crate::document::PatchLog::inactive(), ) .unwrap() }; let heads = ours.get_heads(); - write(&mut ours, "a", None, "_continuum/members/a/alive", json!(1)); + write(&mut ours, &node, None, "notes/ours", json!(1)); let echo = ours.clone(); - assert!(take(&mut ours, &echo, &heads).fresh_actor.is_none()); + assert!( + take(&mut ours, &mut signatures, &echo, &heads) + .fresh_actor + .is_none() + ); let actor = ours.get_actor().clone(); let before = theirs.get_heads(); - write( - &mut theirs, - "a", - None, - "_continuum/members/a/alive", - json!(2), - ); - let outcome = take(&mut ours, &theirs, &before); + write(&mut theirs, &node, None, "notes/theirs", json!(2)); + let twin = theirs.get_last_local_change().unwrap().hash(); + // Nobody signed it with this node's key: forged, and nothing switches. + let outcome = take(&mut ours, &mut signatures, &theirs, &before); assert_eq!(outcome.forged, 1); + assert!(outcome.fresh_actor.is_none()); + assert_eq!(ours.get_actor(), &actor); + // Signed with it: the node's files were copied. + signatures.sign(&theirs, twin, &key); + let outcome = take(&mut ours, &mut signatures, &theirs, &before); assert_eq!(outcome.fresh_actor.as_ref(), Some(ours.get_actor())); assert_ne!(ours.get_actor(), &actor); } diff --git a/src/document/signatures.rs b/src/document/signatures.rs index 79c8569..f9dd94b 100644 --- a/src/document/signatures.rs +++ b/src/document/signatures.rs @@ -594,6 +594,17 @@ impl Signatures { signed } + /// Whether a signature of `hash` by the node whose public key is `key` is kept or + /// waiting for its change (`pending`): checked here, the change not needed. + pub(crate) fn signed_by(&self, hash: &ChangeHash, key: &[u8; 32]) -> bool { + let message = message(hash); + self.by_change + .get(hash) + .into_iter() + .chain(self.pending.by_change.get(hash).into_iter().flatten()) + .any(|signature| crate::identity::verify(key, &message, signature)) + } + /// Whether a valid signature of `hash` is kept: it vouches for its change and the /// changes of its actor it builds on. pub(crate) fn signed(&self, hash: &ChangeHash) -> bool { |
