radicle_protocol/service/
gossip.rs

1pub mod store;
2
3use std::str::FromStr;
4use std::sync::LazyLock;
5
6use super::*;
7use crate::bounded::BoundedVec;
8use radicle::node::UserAgent;
9use radicle::node::PROTOCOL_VERSION;
10
11pub use store::{AnnouncementId, Error, RelayStatus, Store};
12
13/// This node's user agent string.
14pub static PROTOCOL_VERSION_STRING: LazyLock<UserAgent> = LazyLock::new(|| {
15    FromStr::from_str(format!("/radicle:{PROTOCOL_VERSION}/").as_str())
16        .expect("user agent is valid")
17});
18
19pub fn node(config: &Config, timestamp: Timestamp) -> NodeAnnouncement {
20    let features = config.features();
21    let alias = config.alias.clone();
22    let addresses: BoundedVec<_, ADDRESS_LIMIT> = config
23        .external_addresses
24        .clone()
25        .try_into()
26        .expect("external addresses are within the limit");
27    let agent = PROTOCOL_VERSION_STRING.clone();
28    let version = PROTOCOL_VERSION;
29
30    NodeAnnouncement {
31        features,
32        version,
33        timestamp,
34        alias,
35        addresses,
36        nonce: 0,
37        agent,
38    }
39}
40
41pub fn inventory(
42    timestamp: Timestamp,
43    inventory: impl IntoIterator<Item = RepoId>,
44) -> InventoryAnnouncement {
45    let inventory = inventory.into_iter().collect::<Vec<_>>();
46    if inventory.len() > INVENTORY_LIMIT {
47        error!(
48            target: "service",
49            "inventory announcement limit ({}) exceeded, other nodes will see only some of your projects",
50            inventory.len()
51        );
52    }
53
54    InventoryAnnouncement {
55        inventory: BoundedVec::truncate(inventory),
56        timestamp,
57    }
58}