aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/federation/party.rs117
1 files changed, 115 insertions, 2 deletions
diff --git a/src/federation/party.rs b/src/federation/party.rs
index ecd75a9..563fbb4 100644
--- a/src/federation/party.rs
+++ b/src/federation/party.rs
@@ -22,9 +22,18 @@
//! start its swarm from, to standard output;
//! * `check FILE`: what the node finds of the fork whose root `FILE` holds: whether it
//! builds it, what it holds that its heads lack, and the parties whose keys it gives
-//! otherwise.
+//! otherwise;
+//! * `kartei rules FILE KARTEI`: the version `FILE` holds with the rules for krebs made
+//! from kartei's checkout `KARTEI` (`registry::rules`), the namespaces of no party the
+//! group's (`--group`), to standard output; what kartei gives a party that they don't,
+//! to standard error;
+//! * `kartei in DIR...`: signs in the party's kartei namespaces `DIR...`: a write at each
+//! place of its namespace that doesn't hold its file's text, `null` at each that no
+//! file is at (`registry::writes`);
+//! * `kartei out DIR`: what kartei's consumers read, written out from what counts at the
+//! node (`registry::out`) to the new directory `DIR`.
-use std::collections::BTreeSet;
+use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, anyhow, bail};
@@ -36,6 +45,7 @@ use super::client::{self, Record};
use super::event::{self, Does, Event, Signed};
use super::fold::Federation;
use super::jcs::Json;
+use super::registry::{self, Files};
use super::store::Id;
use super::store::memory::Store;
use super::version::Version;
@@ -129,6 +139,33 @@ fn command() -> Command {
.arg(id("file")),
)
.subcommand(
+ Command::new("kartei")
+ .about("The krebs registry: kartei's files signed in and written out")
+ .subcommand_required(true)
+ .subcommand(
+ Command::new("rules")
+ .about("The version a file holds with the rules for krebs, made from kartei")
+ .arg(id("file"))
+ .arg(id("kartei"))
+ .arg(
+ Arg::new("group")
+ .long("group")
+ .value_name("PARTY")
+ .help("The party holding the namespaces of no party"),
+ ),
+ )
+ .subcommand(
+ Command::new("in")
+ .about("Signs in the party's kartei namespaces")
+ .arg(Arg::new("dirs").num_args(1..).required(true)),
+ )
+ .subcommand(
+ Command::new("out")
+ .about("Writes out what kartei's consumers read, to a new directory")
+ .arg(id("dir")),
+ ),
+ )
+ .subcommand(
Command::new("twice")
.about("Names, from records, a party that voted twice")
.arg(
@@ -170,7 +207,19 @@ pub(crate) async fn main() -> Result<()> {
}
return out(&super::fork::root_file(&root, &[]));
}
+ let kartei = (name == "kartei").then(|| sub.subcommand().expect("a subcommand is required"));
+ if let Some(("rules", sub)) = kartei {
+ return kartei_rules(sub);
+ }
let node = Node::new(&matches)?;
+ if let Some(("out", sub)) = kartei {
+ let dir = sub.get_one::<String>("dir").expect("required");
+ let counts = node.counts().await?;
+ let files = registry::out(&current(&counts)?, &counted(&counts)?);
+ registry::write(Path::new(dir), &files).map_err(|why| anyhow!(why))?;
+ println!("{} files written to {dir}", files.len());
+ return Ok(());
+ }
if name == "fork" {
let version = version_in(sub.get_one::<String>("file").expect("required"))?;
let heads = strings(sub, "heads");
@@ -278,6 +327,10 @@ pub(crate) async fn main() -> Result<()> {
vote(&node, &signer, &mut record, &path, proposal, yes).await
}
("again", _) => again(&node, &signer, &mut record, &path).await,
+ ("kartei", _) => {
+ let (_, sub) = kartei.expect("a subcommand of kartei");
+ kartei_in(&node, &signer, &mut record, &path, &strings(sub, "dirs")).await
+ }
_ => unreachable!("every subcommand handled"),
}
}
@@ -438,6 +491,66 @@ async fn again(node: &Node, signer: &Signer, record: &mut Record, path: &Path) -
Ok(())
}
+/// The version a file holds, with the rules for krebs made from kartei's checkout.
+fn kartei_rules(sub: &ArgMatches) -> Result<()> {
+ let version = version_in(sub.get_one::<String>("file").expect("required"))?;
+ let version = Version::read(&version).map_err(|why| anyhow!("no version: {why}"))?;
+ let kartei = registry::read(Path::new(
+ sub.get_one::<String>("kartei").expect("required"),
+ ))
+ .map_err(|why| anyhow!(why))?;
+ let group = sub.get_one::<String>("group").map(String::as_str);
+ let krebs = registry::rules(&kartei, &version, group).map_err(|why| anyhow!(why))?;
+ for not_given in &krebs.not_given {
+ eprintln!("{not_given}");
+ }
+ let json = registry::with_rules(&version, &krebs.given);
+ Version::read(&json).map_err(|why| anyhow!("no version with the rules: {why}"))?;
+ let json: JsonValue = serde_json::from_slice(&json.bytes())?;
+ println!("{}", serde_json::to_string_pretty(&json)?);
+ Ok(())
+}
+
+/// Signs in the party's kartei namespaces, the directories `dirs`.
+async fn kartei_in(
+ node: &Node,
+ signer: &Signer,
+ record: &mut Record,
+ path: &Path,
+ dirs: &[String],
+) -> Result<()> {
+ let mut files = Files::new();
+ for dir in dirs {
+ for (at, text) in registry::read(Path::new(dir)).map_err(|why| anyhow!(why))? {
+ if files.get(&at).is_some_and(|held| *held != text) {
+ bail!("{at}: two of the directories hold it otherwise");
+ }
+ files.insert(at, text);
+ }
+ }
+ let writes = registry::writes(&signer.party, &files, &counted(&node.counts().await?)?);
+ if writes.is_empty() {
+ println!("its namespace holds these files");
+ }
+ for (place, value) in writes {
+ sign(node, signer, record, path, event::write(&place, value)).await?;
+ }
+ Ok(())
+}
+
+/// The values at places, as `counts` names them.
+fn counted(counts: &JsonValue) -> Result<BTreeMap<String, Json>> {
+ counts["values"]
+ .as_object()
+ .into_iter()
+ .flatten()
+ .map(|(place, value)| -> Result<(String, Json)> {
+ let value = client::json(value).map_err(|why| anyhow!(why))?;
+ Ok((place.clone(), value))
+ })
+ .collect()
+}
+
/// The current version, as `counts` names it, checked: its hash its own.
fn current(counts: &JsonValue) -> Result<Version> {
let json = client::json(&counts["version"]["version"]).map_err(|why| anyhow!(why))?;