Skip to main content

coven_replication/sync/
status.rs

1//! Per-device activity derived from the device heads a pull fetched: what every
2//! other device in the store has published, for a host to render "which devices
3//! synced, and how far".
4
5use super::store::VerifiedStoreDeviceHead;
6
7/// Activity summary for a single remote device.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct DeviceActivity {
10    pub device_id: String,
11    /// Hex-encoded Ed25519 public key the device's head verified against — the
12    /// member the device belongs to. Empty only for a head that carried no author.
13    pub author: String,
14    /// The device's highest published head sequence.
15    pub last_seq: u64,
16}
17
18/// The activity of every device other than this one, read off the heads a pull
19/// fetched. `our_device_id` identifies the local device so its own head is left
20/// out; each remaining device is reported once, at its highest head sequence.
21pub(crate) fn other_device_activity(
22    heads: &[VerifiedStoreDeviceHead],
23    our_device_id: &str,
24) -> Vec<DeviceActivity> {
25    let mut other_devices: Vec<DeviceActivity> = Vec::new();
26
27    for head in heads {
28        if head.author.device_id.to_string() == our_device_id {
29            continue;
30        }
31
32        let activity = DeviceActivity {
33            device_id: head.author.device_id.to_string(),
34            author: head.author.author_pubkey.clone(),
35            last_seq: head.head.slot_sequence(),
36        };
37        match other_devices
38            .iter_mut()
39            .find(|current| current.device_id == activity.device_id)
40        {
41            Some(current) if current.last_seq < activity.last_seq => *current = activity,
42            Some(_) => {}
43            None => other_devices.push(activity),
44        }
45    }
46
47    other_devices
48}