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
use serde::{Deserialize, Serialize};
use sha3::{Digest, Sha3_256};
use crate::arrays_as_bytes;
use crate::node_metadata::NodeMetadata;
use crate::versioning::ProtocolObject;
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
pub struct FleetStateChecksum(#[serde(with = "arrays_as_bytes")] [u8; 32]);
impl FleetStateChecksum {
pub fn from_nodes(this_node: Option<&NodeMetadata>, other_nodes: &[NodeMetadata]) -> Self {
let mut nodes = other_nodes.to_vec();
match this_node {
None => {}
Some(node) => nodes.push(node.clone()),
}
nodes.sort_unstable_by(|node1, node2| {
node1
.payload
.staking_provider_address
.cmp(&node2.payload.staking_provider_address)
});
let checksum = nodes
.iter()
.fold(Sha3_256::new(), |digest, node| {
digest.chain(&node.to_bytes())
})
.finalize();
Self(checksum.into())
}
}
impl AsRef<[u8]> for FleetStateChecksum {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}