1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
//! What the peers' builds speak and know, as each says with its handshakes (SPEAKS,
//! `transport::Speaks`): the wire versions it speaks and the newest revision of the
//! federation it knows. A peer that sends none runs a build from before SPEAKS, or isn't
//! heard from yet.
use std::collections::BTreeMap;
use crate::locks::MutexExt;
use crate::transport::{Speaks, wire};
/// How many peers' SPEAKS are kept: anyone can send one.
const MOST: usize = 4096;
/// What this build speaks and knows.
pub(crate) fn own() -> Speaks {
Speaks {
oldest: wire::OLDEST,
newest: wire::VERSION,
revision: newest_revision(),
}
}
#[cfg(feature = "federation")]
fn newest_revision() -> Option<[u8; 32]> {
Some(crate::federation::revision::Revision::newest().id().0)
}
#[cfg(not(feature = "federation"))]
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>>);
impl Speaking {
/// `peer` said `speaks`.
pub(crate) fn heard(&self, peer: &str, speaks: Speaks) {
let mut heard = self.0.lock_or_recover();
if heard.len() >= MOST && !heard.contains_key(peer) {
heard.clear();
}
heard.insert(peer.to_owned(), speaks);
}
/// What `peer` said last, if it said anything.
pub(crate) fn of(&self, peer: &str) -> Option<Speaks> {
self.0.lock_or_recover().get(peer).copied()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_peer_is_heard_as_it_spoke_last() {
let speaking = Speaking::default();
assert_eq!(speaking.of("a"), None);
speaking.heard("a", own());
let older = Speaks {
oldest: 1,
newest: 1,
revision: None,
};
speaking.heard("b", older);
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()
);
}
}
|