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
mod errors;
use crate::{MessageId, MessageType, WireMsg};
use bytes::Bytes;
pub use errors::Error;
use serde::{Deserialize, Serialize};
use sn_data_types::{PublicKey, ReplicaPublicKeySet};
use std::{collections::BTreeMap, fmt, net::SocketAddr};
use threshold_crypto::PublicKey as BlsPublicKey;
use xor_name::{Prefix, XorName};
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum Message {
GetSectionQuery {
public_key: PublicKey,
is_node: bool,
},
GetSectionResponse(GetSectionResponse),
SectionInfoUpdate(ErrorResponse),
}
#[derive(Serialize, Deserialize, Hash, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub struct SectionInfo {
pub prefix: Prefix,
pub pk_set: ReplicaPublicKeySet,
pub elders: BTreeMap<XorName, SocketAddr>,
pub joins_allowed: bool,
}
impl fmt::Debug for SectionInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"SectionInfo {{ prefix: {:?}, pk_set: PkSet {{ public_key: {:?} }}, elders: {:?}, joins_allowed: {:?}}}",
self.prefix,
self.pk_set.public_key(),
self.elders,
self.joins_allowed
)
}
}
#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub struct ErrorResponse {
pub correlation_id: MessageId,
pub error: Error,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum GetSectionResponse {
Success(SectionInfo),
NodeNotReachable,
Redirect(Vec<(XorName, SocketAddr)>),
SectionInfoUpdate(Error),
}
impl Message {
pub fn from(bytes: Bytes) -> crate::Result<Self> {
let deserialized = WireMsg::deserialize(bytes)?;
if let MessageType::SectionInfo { msg, .. } = deserialized {
Ok(msg)
} else {
Err(crate::Error::FailedToParse(
"bytes as a network info message".to_string(),
))
}
}
pub fn serialize(&self, dest: XorName, dest_section_pk: BlsPublicKey) -> crate::Result<Bytes> {
WireMsg::serialize_section_info_msg(self, dest, dest_section_pk)
}
}