aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/party.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/party.rs')
-rw-r--r--src/federation/party.rs117
1 files changed, 108 insertions, 9 deletions
diff --git a/src/federation/party.rs b/src/federation/party.rs
index cfcc569..148bd20 100644
--- a/src/federation/party.rs
+++ b/src/federation/party.rs
@@ -54,6 +54,7 @@ use super::event::{self, Does, Event, Signed};
use super::fold::Federation;
use super::jcs::Json;
use super::keys::{self, Key};
+use super::revision::{FEATURES, Revision};
use super::store::Id;
use super::store::memory::Store;
use super::version::Version;
@@ -117,10 +118,23 @@ fn command() -> Command {
.args(changing("NODE", "nodes", "node list"))
.group(changes()),
)
- .subcommand(Command::new("next").about(
- "The version after the current one, to change and propose: numbered on, naming the \
- current one's hash, without its checkpoint",
- ))
+ .subcommand(
+ Command::new("next")
+ .about(
+ "The version after the current one, to change and propose: numbered on, \
+ naming the current one's hash, without its checkpoint",
+ )
+ .arg(
+ Arg::new("revision")
+ .long("revision")
+ .value_name("FEATURE")
+ .help(
+ "Following the revision that adds FEATURE (federation.md, Changing \
+ the federation): pass it once every party's nodes run a continuum \
+ that knows it",
+ ),
+ ),
+ )
.subcommand(
Command::new("propose")
.about("Proposes the version a file holds (JSON), and says the proposal's hash")
@@ -268,7 +282,28 @@ pub(crate) async fn main() -> Result<()> {
return proposals(&node).await;
}
if name == "next" {
- let next = next(&node.current().await?)?;
+ let current = node.current().await?;
+ let feature = sub.get_one::<String>("revision");
+ let next = next(&current, feature.map(String::as_str))?;
+ if feature.is_none() {
+ // A later revision this client and the node know, said.
+ let known = node.revisions().await?;
+ if let Some(later) = Revision::known()
+ .filter(|revision| *revision > current.revision && known.contains(&revision.id()))
+ .last()
+ {
+ let name = later
+ .features()
+ .last()
+ .expect("a later revision adds one")
+ .name();
+ eprintln!(
+ "The node knows {later}, the current version follows {}: --revision {name} \
+ moves to it, once every party's nodes run a continuum that knows it",
+ current.revision
+ );
+ }
+ }
println!("{}", serde_json::to_string_pretty(&next)?);
return Ok(());
}
@@ -708,6 +743,19 @@ async fn vote(
})) if hash == proposal => version,
_ => bail!("the node's {proposing} isn't the proposal it names"),
};
+ // A later revision: the node knows it, or it stops at the version once it passes.
+ if yes
+ && let Ok(version) = Version::read(&proposed)
+ && version.revision != current.revision
+ && !node.revisions().await?.contains(&version.revision.id())
+ {
+ bail!(
+ "{proposal} moves to {}, which the node doesn't know: once it passed, a node that \
+ doesn't stops at it, and votes nothing till upgraded; upgrade this node and every \
+ other of the party to a continuum that knows it, then back it",
+ version.revision
+ );
+ }
// The votes on its party's earlier proposals it backed, for the nays among them.
let mut nays = Vec::new();
let backed: BTreeSet<Id> = record
@@ -842,8 +890,9 @@ async fn proposals(node: &Node) -> Result<()> {
/// The version after `current`, to change and propose: `current` numbered on, naming its
/// hash as the version it changes, and without the checkpoint it names, whose heads are
-/// behind: a version naming them again is never written.
-fn next(current: &Version) -> Result<JsonValue> {
+/// behind: a version naming them again is never written. With `feature`, following the
+/// revision that adds it, where that isn't earlier than the current one's.
+fn next(current: &Version, feature: Option<&str>) -> Result<JsonValue> {
let mut next: JsonValue = serde_json::from_slice(&current.json.bytes())?;
let fields = next
.as_object_mut()
@@ -851,6 +900,28 @@ fn next(current: &Version) -> Result<JsonValue> {
fields.insert("number".to_owned(), (current.number + 1).into());
fields.insert("changes".to_owned(), current.hash.to_string().into());
fields.remove("checkpoint");
+ if let Some(name) = feature {
+ let Some(feature) = FEATURES.iter().find(|feature| feature.name() == name) else {
+ let names: Vec<&str> = FEATURES.iter().map(|feature| feature.name()).collect();
+ bail!(
+ "no feature {name:?} this continuum knows: it knows {}",
+ if names.is_empty() {
+ "none".to_owned()
+ } else {
+ names.join(", ")
+ }
+ );
+ };
+ let revision = feature.revision();
+ if revision < current.revision {
+ bail!(
+ "the current version follows {}, later than {revision}: a version never \
+ follows an earlier revision than the one it changes",
+ current.revision
+ );
+ }
+ fields.insert("revision".to_owned(), revision.id().to_string().into());
+ }
Ok(next)
}
@@ -937,6 +1008,18 @@ impl Node {
current(&self.counts().await?)
}
+ /// The IDs of the revisions the node knows: none where it says none, as one too old to
+ /// know the base revision by name.
+ async fn revisions(&self) -> Result<Vec<Id>> {
+ let summary = self.get("/events").await?;
+ Ok(summary["revisions"]
+ .as_array()
+ .into_iter()
+ .flatten()
+ .filter_map(|revision| revision["id"].as_str().and_then(Id::from_hex))
+ .collect())
+ }
+
/// The bytes of an event the node's store holds.
async fn event(&self, id: &Id) -> Result<Vec<u8>> {
let response = self
@@ -1104,22 +1187,38 @@ mod tests {
}
/// The next version is the current one numbered on, naming its hash, without its
- /// checkpoint: a version changing it, as its parties may pass.
+ /// checkpoint: a version changing it, as its parties may pass. With a feature, it
+ /// follows the revision adding it, never an earlier one than the current version's.
#[test]
fn the_next_version_changes_the_current_one() {
use crate::federation::harness::{Party, Spec};
+ use crate::federation::revision::{Feature, Revision};
use crate::federation::store::Id;
use crate::federation::version::Version;
let first = Spec::first(&[&Party::new("tv", 1), &Party::new("lass", 2)], &[]);
let mut second = first.after(first.hash());
second.checkpoint = Some(vec![Id::of(b"a head")]);
let current = Version::read(&second.json()).unwrap();
- let next = super::next(&current).unwrap();
+ let next = super::next(&current, None).unwrap();
assert_eq!(next["number"], current.number + 1);
assert_eq!(next["changes"], current.hash.to_string());
assert!(next.get("checkpoint").is_none());
+ assert!(next.get("revision").is_none());
let next = Version::read(&crate::federation::client::json(&next).unwrap()).unwrap();
next.changes_validly(Some(&current)).unwrap();
+ let revised = super::next(&current, Some("merge-events")).unwrap();
+ let revised = Version::read(&crate::federation::client::json(&revised).unwrap()).unwrap();
+ assert_eq!(revised.revision, Feature::MergeEvents.revision());
+ revised.changes_validly(Some(&current)).unwrap();
+ // Kept on, where nothing is said.
+ let kept = super::next(&revised, None).unwrap();
+ let kept = Version::read(&crate::federation::client::json(&kept).unwrap()).unwrap();
+ assert_eq!(kept.revision, revised.revision);
+ let why = super::next(&current, Some("elsewhere"))
+ .unwrap_err()
+ .to_string();
+ assert!(why.contains("merge-events"), "{why}");
+ assert_ne!(revised.revision, Revision::default());
}
/// Its options come from configuration files as the node's do, arguments winning.