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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use crate::{chunk_store::UsedSpace, Network, Result};
use bls::{PublicKeySet, PublicKeyShare};
use ed25519_dalek::PublicKey as Ed25519PublicKey;
use itertools::Itertools;
use serde::Serialize;
use sn_data_types::{PublicKey, Signature, SignatureShare};
use sn_messaging::client::TransientElderKey;
use sn_routing::SectionChain;
use std::{
collections::BTreeSet,
net::SocketAddr,
path::{Path, PathBuf},
};
use xor_name::{Prefix, XorName};
#[derive(Clone)]
pub enum NodeState {
Infant(Ed25519PublicKey),
Adult(AdultState),
Elder(ElderState),
}
impl NodeState {
pub fn node_id(&self) -> Ed25519PublicKey {
match self {
Self::Infant(id) => *id,
Self::Adult(state) => state.node_id,
Self::Elder(state) => state.node_id,
}
}
pub fn node_name(&self) -> XorName {
PublicKey::Ed25519(self.node_id()).into()
}
}
#[derive(Clone)]
pub struct AdultState {
prefix: Prefix,
node_name: XorName,
node_id: Ed25519PublicKey,
section_proof_chain: SectionChain,
elders: Vec<(XorName, SocketAddr)>,
adult_reader: AdultReader,
node_signing: NodeSigning,
}
impl AdultState {
#[allow(clippy::eval_order_dependence)]
pub async fn new(network: Network) -> Result<Self> {
Ok(Self {
prefix: network.our_prefix().await,
node_name: network.our_name().await,
node_id: network.public_key().await,
section_proof_chain: network.our_history().await,
elders: network.our_elder_addresses().await,
adult_reader: AdultReader::new(network.clone()),
node_signing: NodeSigning::new(network),
})
}
pub fn node_name(&self) -> XorName {
self.node_name
}
pub fn node_id(&self) -> Ed25519PublicKey {
self.node_id
}
pub fn section_proof_chain(&self) -> &SectionChain {
&self.section_proof_chain
}
pub async fn sign_as_node<T: Serialize>(&self, data: &T) -> Result<Signature> {
self.node_signing.sign_as_node(&data).await
}
}
#[derive(Clone)]
pub struct ElderState {
prefix: Prefix,
node_name: XorName,
node_id: Ed25519PublicKey,
key_index: usize,
public_key_set: PublicKeySet,
section_proof_chain: SectionChain,
elders: Vec<(XorName, SocketAddr)>,
adult_reader: AdultReader,
interaction: NodeInteraction,
node_signing: NodeSigning,
}
impl ElderState {
#[allow(clippy::eval_order_dependence)]
pub async fn new(network: Network) -> Result<Self> {
Ok(Self {
prefix: network.our_prefix().await,
node_name: network.our_name().await,
node_id: network.public_key().await,
key_index: network.our_index().await?,
public_key_set: network.public_key_set().await?,
section_proof_chain: network.our_history().await,
elders: network.our_elder_addresses().await,
adult_reader: AdultReader::new(network.clone()),
interaction: NodeInteraction::new(network.clone()),
node_signing: NodeSigning::new(network),
})
}
pub async fn set_joins_allowed(&mut self, joins_allowed: bool) -> Result<()> {
self.interaction.set_joins_allowed(joins_allowed).await
}
pub async fn adults(&self) -> Vec<XorName> {
self.adult_reader.our_adults().await
}
pub async fn adults_sorted_by_distance_to(&self, name: &XorName, count: usize) -> Vec<XorName> {
self.adult_reader
.our_adults_sorted_by_distance_to(name, count)
.await
}
pub fn prefix(&self) -> &Prefix {
&self.prefix
}
pub fn node_name(&self) -> XorName {
self.node_name
}
pub fn node_id(&self) -> Ed25519PublicKey {
self.node_id
}
pub fn key_index(&self) -> usize {
self.key_index
}
pub fn section_public_key(&self) -> PublicKey {
PublicKey::Bls(self.public_key_set().public_key())
}
pub fn public_key_set(&self) -> &PublicKeySet {
&self.public_key_set
}
pub fn public_key_share(&self) -> PublicKeyShare {
self.public_key_set.public_key_share(self.key_index)
}
pub fn section_proof_chain(&self) -> &SectionChain {
&self.section_proof_chain
}
pub fn elder_names(&self) -> BTreeSet<&XorName> {
self.elders.iter().map(|(name, _)| name).collect()
}
pub fn elders(&self) -> &Vec<(XorName, SocketAddr)> {
&self.elders
}
pub fn elders_sorted_by_distance_to(&self, name: &XorName) -> Vec<&(XorName, SocketAddr)> {
self.elders
.iter()
.sorted_by(|(lhs, _), (rhs, _)| name.cmp_distance(lhs, rhs))
.collect()
}
pub async fn sign_as_elder<T: Serialize>(&self, data: &T) -> Result<SignatureShare> {
let share = self
.node_signing
.sign_as_elder(data, &self.public_key_set().public_key())
.await?;
Ok(SignatureShare {
share,
index: self.key_index,
})
}
pub async fn sign_as_node<T: Serialize>(&self, data: &T) -> Result<Signature> {
self.node_signing.sign_as_node(&data).await
}
pub fn elder_key(&self) -> TransientElderKey {
TransientElderKey {
node_id: self.node_id,
bls_key: self.public_key_share(),
bls_share_index: self.key_index(),
bls_public_key_set: self.public_key_set().clone(),
}
}
}
#[derive(Clone)]
struct AdultReader {
network: Network,
}
impl AdultReader {
pub fn new(network: Network) -> Self {
Self { network }
}
pub async fn our_adults(&self) -> Vec<XorName> {
self.network.our_adults().await
}
pub async fn our_adults_sorted_by_distance_to(
&self,
name: &XorName,
count: usize,
) -> Vec<XorName> {
self.network
.our_adults_sorted_by_distance_to(name, count)
.await
}
}
#[derive(Clone)]
pub struct NodeSigning {
network: Network,
}
impl NodeSigning {
pub fn new(network: Network) -> Self {
Self { network }
}
pub async fn sign_as_node<T: Serialize>(&self, data: &T) -> Result<Signature> {
self.network.sign_as_node(&data).await
}
pub async fn sign_as_elder<T: Serialize>(
&self,
data: &T,
public_key: &bls::PublicKey,
) -> Result<bls::SignatureShare> {
self.network.sign_as_elder(data, public_key).await
}
}
#[derive(Clone)]
pub struct NodeInteraction {
network: Network,
}
impl NodeInteraction {
pub fn new(network: Network) -> Self {
Self { network }
}
pub async fn set_joins_allowed(&mut self, joins_allowed: bool) -> Result<()> {
self.network.set_joins_allowed(joins_allowed).await
}
}
#[derive(Clone)]
pub struct NodeInfo {
pub genesis: bool,
pub root_dir: PathBuf,
pub used_space: UsedSpace,
pub reward_key: PublicKey,
}
impl NodeInfo {
pub fn path(&self) -> &Path {
self.root_dir.as_path()
}
}