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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#[cfg(not(feature = "use-mock-crust"))]
use crust::PeerId;
#[cfg(feature = "use-mock-crust")]
use mock_crust::crust::PeerId;
use core::GROUP_SIZE;
use kademlia_routing_table::Destination;
use rust_sodium::crypto::{hash, sign};
use std::fmt::{self, Debug, Formatter};
use xor_name::XorName;
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Eq, Ord, Clone, Hash)]
pub enum Authority {
ClientManager(XorName),
NaeManager(XorName),
NodeManager(XorName),
ManagedNode(XorName),
Client {
client_key: sign::PublicKey,
peer_id: PeerId,
proxy_node_name: XorName,
},
}
impl Authority {
pub fn is_group(&self) -> bool {
match *self {
Authority::ClientManager(_) |
Authority::NaeManager(_) |
Authority::NodeManager(_) => true,
Authority::ManagedNode(_) |
Authority::Client { .. } => false,
}
}
pub fn name(&self) -> &XorName {
match *self {
Authority::ClientManager(ref name) |
Authority::NaeManager(ref name) |
Authority::NodeManager(ref name) |
Authority::ManagedNode(ref name) => name,
Authority::Client { ref proxy_node_name, .. } => proxy_node_name,
}
}
pub fn to_destination(&self) -> Destination<XorName> {
if self.is_group() {
Destination::Group(*self.name(), GROUP_SIZE)
} else {
Destination::Node(*self.name())
}
}
}
impl Debug for Authority {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match *self {
Authority::ClientManager(ref name) => {
write!(formatter, "ClientManager(name: {})", name)
}
Authority::NaeManager(ref name) => write!(formatter, "NaeManager(name: {})", name),
Authority::NodeManager(ref name) => write!(formatter, "NodeManager(name: {})", name),
Authority::ManagedNode(ref name) => write!(formatter, "ManagedNode(name: {})", name),
Authority::Client { ref client_key, ref proxy_node_name, ref peer_id } => {
write!(formatter,
"Client {{ client_name: {}, proxy_node_name: {}, peer_id: {:?} }}",
XorName(hash::sha256::hash(&client_key[..]).0),
proxy_node_name,
peer_id)
}
}
}
}