nucypher_core/
fleet_state.rs

1use core::fmt;
2
3use serde::{Deserialize, Serialize};
4use sha3::{digest::Update, Digest, Keccak256};
5use umbral_pre::serde_bytes;
6
7use crate::node_metadata::NodeMetadata;
8use crate::versioning::ProtocolObject;
9
10/// An identifier of the fleet state.
11#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)]
12pub struct FleetStateChecksum(#[serde(with = "serde_bytes::as_hex")] [u8; 32]);
13
14impl FleetStateChecksum {
15    /// Creates a checksum from the given list of node metadata, and, possibly,
16    /// also the metadata of the requesting node.
17    pub fn from_nodes(other_nodes: &[NodeMetadata], this_node: Option<&NodeMetadata>) -> Self {
18        let mut nodes = other_nodes.to_vec();
19        match this_node {
20            None => {}
21            Some(node) => nodes.push(node.clone()),
22        }
23
24        // We do not expect node metadata with equal checksum addresses,
25        // so we use the unstable sort which is faster and has a lower memory profile.
26        nodes.sort_unstable_by(|node1, node2| {
27            node1
28                .payload
29                .staking_provider_address
30                .cmp(&node2.payload.staking_provider_address)
31        });
32
33        let checksum = nodes
34            .iter()
35            .fold(Keccak256::new(), |digest, node| {
36                // NodeMetadata has a payload signature, which is randomized,
37                // so this may lead to unnecessary fleet state update.
38                // But, unlike ProtocolObject::to_bytes(), payload serialization
39                // is not standardized, so it is better not to rely on it.
40                digest.chain(node.to_bytes())
41            })
42            .finalize();
43
44        Self(checksum.into())
45    }
46}
47
48impl AsRef<[u8]> for FleetStateChecksum {
49    fn as_ref(&self) -> &[u8] {
50        self.0.as_ref()
51    }
52}
53
54impl fmt::Display for FleetStateChecksum {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        let hex_repr = hex::encode(&self.0[..8]);
57        write!(f, "FleetStateChecksum:{hex_repr}...")
58    }
59}