nucypher_core/
fleet_state.rs1use 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#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize)]
12pub struct FleetStateChecksum(#[serde(with = "serde_bytes::as_hex")] [u8; 32]);
13
14impl FleetStateChecksum {
15 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 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 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}