diff options
| author | tv <tv@krebsco.de> | 2026-11-16 21:42:18 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-16 23:07:19 +0100 |
| commit | d893a092c469688eaf98c98725fedf6dfad99614 (patch) | |
| tree | ccf493ef78721f32666ad068de892b6287566bf3 /src/federation/event.rs | |
| parent | 0023300b417b2375026750b1b0924adfd94c4e03 (diff) | |
federation: events made, as a party's client makes them
What makes a signed event (`event::signed`) and what each does (`write`, `keys`, `nodes`,
`proposal`, `vote`), and a version event (`event::version_event`), move from the tests'
harness into `federation::event`, for a party's client to make them as the tests do: the
harness makes its events through them.
Diffstat (limited to 'src/federation/event.rs')
| -rw-r--r-- | src/federation/event.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/federation/event.rs b/src/federation/event.rs index fc519d0..00c7018 100644 --- a/src/federation/event.rs +++ b/src/federation/event.rs @@ -13,6 +13,85 @@ pub(crate) const LARGEST: usize = 256 * 1024; /// Dependencies an event may name. pub(crate) const DEPENDENCIES: usize = 64; +/// An event `party` signs with `signer`, its key spelled `key`, on `deps`, doing `does` +/// (a field's name, and what it holds): its bytes, as JCS. +pub(crate) fn signed( + signer: &ed25519_dalek::SigningKey, + party: &str, + key: &str, + deps: &[Id], + does: (&str, Json), +) -> Vec<u8> { + let mut members = vec![ + ("deps".to_owned(), ids(deps)), + ("party".to_owned(), Json::Str(party.to_owned())), + ("key".to_owned(), Json::Str(key.to_owned())), + (does.0.to_owned(), does.1), + ]; + let unsigned = Json::object(members.clone()).expect("names once each"); + members.push(( + "sig".to_owned(), + Json::Str(keys::sign(signer, &unsigned.bytes())), + )); + Json::object(members).expect("names once each").bytes() +} + +/// What writing `value` at `path` does. +pub(crate) fn write(path: &str, value: Json) -> (&'static str, Json) { + let does = Json::object(vec![ + ("path".to_owned(), Json::Str(path.to_owned())), + ("value".to_owned(), value), + ]); + ("write", does.expect("names once each")) +} + +/// What writing a party's key set does. +pub(crate) fn keys(keys: &[String]) -> (&'static str, Json) { + ("keys", texts(keys)) +} + +/// What writing a party's node list does. +pub(crate) fn nodes(nodes: &[String]) -> (&'static str, Json) { + ("nodes", texts(nodes)) +} + +/// What proposing `version` does. +pub(crate) fn proposal(version: &Json) -> (&'static str, Json) { + ("proposal", version.clone()) +} + +/// What voting `yes` on `proposal`, on the version `on`, does. +pub(crate) fn vote(on: Id, proposal: Id, yes: bool) -> (&'static str, Json) { + let does = Json::object(vec![ + ("on".to_owned(), Json::Str(on.to_string())), + ("proposal".to_owned(), Json::Str(proposal.to_string())), + ("yes".to_owned(), Json::Bool(yes)), + ]); + ("vote", does.expect("names once each")) +} + +/// A version event, which nobody signs: its bytes. +pub(crate) fn version_event(deps: &[Id], version: Id) -> Vec<u8> { + Json::object(vec![ + ("deps".to_owned(), ids(deps)), + ("version".to_owned(), Json::Str(version.to_string())), + ]) + .expect("names once each") + .bytes() +} + +/// IDs as an event names them: ascending, each once. +fn ids(ids: &[Id]) -> Json { + let mut ids = ids.to_vec(); + ids.sort(); + ids.dedup(); + Json::Array(ids.iter().map(|id| Json::Str(id.to_string())).collect()) +} + +fn texts(texts: &[String]) -> Json { + Json::Array(texts.iter().map(|text| Json::Str(text.clone())).collect()) +} + /// An event, read from its bytes. #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) enum Event { |
