aboutsummaryrefslogtreecommitdiffstats
path: root/src/swarm
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-19 13:50:25 +0000
committertv <tv@krebsco.de>2026-11-19 14:53:47 +0100
commit7d91c37ec1c606f70ec7a90ce9be4b0aa5c518c4 (patch)
tree9116ed3a2fbc69cba32500d2b8b48f85c6d46c51 /src/swarm
parentc1bf987314774d135316d95480baeb6eb7e1dc70 (diff)
peers: what each peer said it speaks and knows, in GET /peers
GET /peers shows, of each peer, what it said with its handshakes (SPEAKS): its `knows`, the wire versions it speaks, the oldest and the newest, and the newest revision of the federation it knows; null where it said nothing, a build from before SPEAKS or no handshake from it since the node started. `localKnows` is the node's own. So every swarm, federated or not, shows which builds its nodes run, as far as each heard. `swarm::speaking::shown` shapes it, for GET /events/counts too, whose `knows` is the same; docs/openapi.yaml has it once, as `Knows`. Without the federation feature, a revision reads as one this continuum doesn't know. `Speaking::of` is now used in every build, not only federated ones, which warned of it as dead code.
Diffstat (limited to 'src/swarm')
-rw-r--r--src/swarm/speaking.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/swarm/speaking.rs b/src/swarm/speaking.rs
index e1a8d2c..4626fb1 100644
--- a/src/swarm/speaking.rs
+++ b/src/swarm/speaking.rs
@@ -30,6 +30,44 @@ fn newest_revision() -> Option<[u8; 32]> {
None
}
+/// `speaks` as the API shows it (`knows` in `GET /peers` and `GET /events/counts`): the
+/// wire versions, the oldest and the newest, and the newest revision of the federation,
+/// by number and ID and as people read it, its number null where this continuum doesn't
+/// know it; null where there is none.
+pub(crate) fn shown(speaks: Speaks) -> serde_json::Value {
+ serde_json::json!({
+ "wire": [speaks.oldest, speaks.newest],
+ "revision": speaks.revision.map(revision_shown),
+ })
+}
+
+#[cfg(feature = "federation")]
+fn revision_shown(id: [u8; 32]) -> serde_json::Value {
+ use crate::federation::revision::Revision;
+ let id = crate::federation::store::Id(id);
+ match Revision::of(&id) {
+ Some(revision) => serde_json::json!({
+ "number": revision.0,
+ "id": id.to_string(),
+ "named": revision.to_string(),
+ }),
+ None => unknown(&id.to_string()),
+ }
+}
+
+#[cfg(not(feature = "federation"))]
+fn revision_shown(id: [u8; 32]) -> serde_json::Value {
+ unknown(&crate::transport::Hex(&id).to_string())
+}
+
+fn unknown(id: &str) -> serde_json::Value {
+ serde_json::json!({
+ "number": null,
+ "id": id,
+ "named": format!("{id}, a revision this continuum doesn't know"),
+ })
+}
+
/// The SPEAKS each peer sent last, by node ID.
#[derive(Default)]
pub(crate) struct Speaking(std::sync::Mutex<BTreeMap<String, Speaks>>);
@@ -68,5 +106,18 @@ mod tests {
speaking.heard("a", older);
assert_eq!(speaking.of("a"), Some(older));
assert_eq!(own().newest, wire::VERSION);
+ let unknown = shown(Speaks {
+ revision: Some([7; 32]),
+ ..older
+ });
+ assert_eq!(unknown["wire"], serde_json::json!([1, 1]));
+ assert_eq!(unknown["revision"]["number"], serde_json::Value::Null);
+ assert_eq!(unknown["revision"]["id"], "07".repeat(32));
+ assert_eq!(shown(older)["revision"], serde_json::Value::Null);
+ #[cfg(feature = "federation")]
+ assert_eq!(
+ shown(own())["revision"]["number"],
+ crate::federation::revision::FEATURES.len()
+ );
}
}