diff options
| author | tv <tv@krebsco.de> | 2026-11-29 13:16:06 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-29 13:16:06 +0000 |
| commit | f0c0cec4358db039b49dbf1b38bdc17117ba69cf (patch) | |
| tree | 3cff3556fb7f2dd2ec8bd52017de67ab78b9c5d3 /src/federation/store/entry.rs | |
| parent | 8dd47f8e4bc838d22ab5b888eafb1733c25e64ef (diff) | |
federation: a member's entry in a set of events
What a node says of itself (store::entry::Entry): a JSON map of no more
than 16 KiB, with its count, signed with its node key for its store. A
set holds each member's newest (Set::put_entry): the highest count, of
two of one count the lower name; none of a node the store doesn't name
a member as it reads now, and those it held go with their node. Their
names are among the set's, to catch up by; and a set says what of a
peer's names it would have (Set::wants, Store::knows), which leaves out
what its store refused, waits for or forgot, and a stranger's entry
until who is a member changes.
The store's test fold names members, by what counts at node:<node ID>.
Diffstat (limited to 'src/federation/store/entry.rs')
| -rw-r--r-- | src/federation/store/entry.rs | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/src/federation/store/entry.rs b/src/federation/store/entry.rs new file mode 100644 index 0000000..f19c44f --- /dev/null +++ b/src/federation/store/entry.rs @@ -0,0 +1,176 @@ +//! A member's entry in a set of events (`docs/document.md`, A set of events): what a +//! node says of itself, where it is reached and what else its peers read of it, written +//! by that node alone and signed with its node key. Of a node's entries the one with the +//! highest count is its entry; of two of one count, which only a node that says two +//! things at once makes, the one with the lower name. + +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +use super::Id; +use crate::document::ranges::Name; +use crate::identity::{self, NodeKey}; + +/// The most bytes an entry's value may have. +pub(crate) const LARGEST: usize = 16 << 10; + +/// What a node signs of its entry, ahead of the store, itself, the count and the value. +const SIGNS: &[u8] = b"continuum-entry\0"; + +/// A node's entry, as passed on and as kept. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct Entry { + /// The node's public key: its ID is of it. + #[serde(with = "serde_bytes")] + key: [u8; 32], + /// Which of the node's entries: each later one counts higher. + count: u64, + /// The entry: a JSON map, as bytes. + #[serde(with = "serde_bytes")] + value: Vec<u8>, + /// The node's signature (`SIGNS`). + #[serde(with = "serde_bytes")] + signature: [u8; 64], +} + +fn signed(store: &Id, key: &[u8; 32], count: u64, value: &[u8]) -> Vec<u8> { + let mut message = Vec::with_capacity(SIGNS.len() + 32 + 32 + 8 + value.len()); + message.extend_from_slice(SIGNS); + message.extend_from_slice(&store.0); + message.extend_from_slice(key); + message.extend_from_slice(&count.to_le_bytes()); + message.extend_from_slice(value); + message +} + +impl Entry { + /// The entry of the node with `key` in the store `store`, its `count`th, holding + /// `value`. + pub(crate) fn new(store: &Id, key: &NodeKey, count: u64, value: &serde_json::Value) -> Self { + let value = serde_json::to_vec(value).expect("JSON encodes"); + let public = key.public(); + let signature = key.sign(&signed(store, &public, count, &value)); + Self { + key: public, + count, + value, + signature, + } + } + + /// The node whose entry it is, by its ID. + pub(crate) fn node(&self) -> String { + identity::node_id_of(&self.key) + } + + pub(crate) fn count(&self) -> u64 { + self.count + } + + /// What it holds: a JSON map, where the entry checks. + pub(crate) fn value(&self) -> Option<serde_json::Map<String, serde_json::Value>> { + match serde_json::from_slice(&self.value) { + Ok(serde_json::Value::Object(map)) => Some(map), + _ => None, + } + } + + /// Why the entry isn't one of the store `store`, where it isn't: too large, no JSON + /// map, or not signed by its node for that store. + pub(crate) fn checks(&self, store: &Id) -> Result<(), &'static str> { + if self.value.len() > LARGEST { + return Err("an entry larger than an entry may be"); + } + if self.value().is_none() { + return Err("an entry that is no JSON map"); + } + let message = signed(store, &self.key, self.count, &self.value); + if !identity::verify(&self.key, &message, &self.signature) { + return Err("an entry its node didn't sign for this store"); + } + Ok(()) + } + + /// The entry as bytes, as it is passed on. + pub(crate) fn encode(&self) -> Vec<u8> { + rmp_serde::to_vec(self).expect("an entry encodes") + } + + /// An entry from its bytes, where they are one's and nothing else. + pub(crate) fn decode(bytes: &[u8]) -> Option<Self> { + let entry: Self = rmp_serde::from_slice(bytes).ok()?; + (entry.encode() == bytes).then_some(entry) + } + + /// The entry's name, to catch up by: of its bytes, as an event's is. + pub(crate) fn name(&self) -> Name { + Sha256::digest(self.encode()).into() + } +} + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::*; + + fn key(seed: u8) -> NodeKey { + NodeKey::from_seed([seed; 32]) + } + + #[test] + fn an_entry_checks_for_its_store_alone() { + let store = Id::of(b"a store"); + let entry = Entry::new(&store, &key(1), 3, &json!({"addresses": ["a"]})); + assert_eq!(entry.checks(&store), Ok(())); + assert_eq!(entry.node(), key(1).id()); + assert_eq!(entry.count(), 3); + assert_eq!(entry.value().expect("a map")["addresses"], json!(["a"])); + assert!(entry.checks(&Id::of(b"another")).is_err()); + } + + #[test] + fn an_entry_changed_checks_no_more() { + let store = Id::of(b"a store"); + let entry = Entry::new(&store, &key(1), 3, &json!({"alias": "a"})); + let mut counted = entry.clone(); + counted.count = 4; + assert!(counted.checks(&store).is_err()); + let mut valued = entry.clone(); + valued.value = br#"{"alias":"b"}"#.to_vec(); + assert!(valued.checks(&store).is_err()); + let mut keyed = entry; + keyed.key = key(2).public(); + assert!(keyed.checks(&store).is_err()); + } + + #[test] + fn an_entry_is_a_map_of_no_more_than_its_size() { + let store = Id::of(b"a store"); + assert!( + Entry::new(&store, &key(1), 0, &json!([1])) + .checks(&store) + .is_err() + ); + let large = "a".repeat(LARGEST); + assert!( + Entry::new(&store, &key(1), 0, &json!({"alias": large})) + .checks(&store) + .is_err() + ); + } + + #[test] + fn an_entry_reads_back_from_its_bytes() { + let store = Id::of(b"a store"); + let entry = Entry::new(&store, &key(1), 3, &json!({"alias": "a"})); + let bytes = entry.encode(); + assert_eq!(Entry::decode(&bytes), Some(entry.clone())); + assert_eq!(entry.name(), <Name>::from(Sha256::digest(&bytes))); + // Bytes that read as one and aren't its own are none. + let mut longer = bytes; + longer.push(0); + assert_eq!(Entry::decode(&longer), None); + assert_eq!(Entry::decode(b"no entry"), None); + } +} |
