safe_network/node/api/event.rs
1// Copyright 2022 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use sn_interface::messaging::{
10 data::ServiceMsg,
11 system::{NodeCmd, NodeQuery, NodeQueryResponse},
12 AuthorityProof, DstLocation, EndUser, MsgId, ServiceAuth, SrcLocation,
13};
14
15use bls::PublicKey as BlsPublicKey;
16use ed25519_dalek::Keypair;
17use std::{collections::BTreeSet, sync::Arc};
18use xor_name::{Prefix, XorName};
19
20/// A flag in EldersChanged event, indicating
21/// whether the node got promoted, demoted or did not change.
22#[derive(Debug)]
23pub enum NodeElderChange {
24 /// The node was promoted to Elder.
25 Promoted,
26 /// The node was demoted to Adult.
27 Demoted,
28 /// There was no change to the node.
29 None,
30}
31
32/// Bound name of elders and section_key, section_prefix info together.
33#[derive(Debug, Clone, PartialEq)]
34pub struct Elders {
35 /// The prefix of the section.
36 pub prefix: Prefix,
37 /// The BLS public key of a section.
38 pub key: BlsPublicKey,
39 /// Remaining Elders in our section.
40 pub remaining: BTreeSet<XorName>,
41 /// New Elders in our section.
42 pub added: BTreeSet<XorName>,
43 /// Removed Elders in our section.
44 pub removed: BTreeSet<XorName>,
45}
46
47/// An Event raised by a `Node` or `Client` via its event sender.
48///
49/// These are sent by sn_routing to the library's user. It allows the user to handle requests and
50/// responses, and to react to changes in the network.
51///
52/// `Request` and `Response` events from section locations are only raised once the majority has
53/// been reached, i.e. enough members of the section have sent the same message.
54#[allow(clippy::large_enum_variant)]
55#[derive(custom_debug::Debug)]
56pub enum Event {
57 /// Received a message from another Node.
58 MessageReceived {
59 /// The message ID
60 msg_id: MsgId,
61 /// Source location
62 src: SrcLocation,
63 /// Destination location
64 dst: DstLocation,
65 /// The message.
66 msg: Box<MessageReceived>,
67 },
68 /// A new peer joined our section.
69 MemberJoined {
70 /// Name of the node
71 name: XorName,
72 /// Previous name before relocation or `None` if it is a new node.
73 previous_name: Option<XorName>,
74 /// Age of the node
75 age: u8,
76 },
77 /// A node left our section.
78 MemberLeft {
79 /// Name of the node
80 name: XorName,
81 /// Age of the node
82 age: u8,
83 },
84 /// Our section has split.
85 SectionSplit {
86 /// The Elders of our section.
87 elders: Elders,
88 /// Promoted, demoted or no change?
89 self_status_change: NodeElderChange,
90 },
91 /// The set of elders in our section has changed.
92 EldersChanged {
93 /// The Elders of our section.
94 elders: Elders,
95 /// Promoted, demoted or no change?
96 self_status_change: NodeElderChange,
97 },
98 /// This node has started relocating to other section. Will be followed by
99 /// `Relocated` when the node finishes joining the destination section.
100 RelocationStarted {
101 /// Previous name before relocation
102 previous_name: XorName,
103 },
104 /// This node has completed relocation to other section.
105 Relocated {
106 /// Old name before the relocation.
107 previous_name: XorName,
108 /// New keypair to be used after relocation.
109 #[debug(skip)]
110 new_keypair: Arc<Keypair>,
111 },
112 /// Received a message from a peer.
113 ServiceMsgReceived {
114 /// The message ID
115 msg_id: MsgId,
116 /// The content of the message.
117 msg: Box<ServiceMsg>,
118 /// Data authority
119 auth: AuthorityProof<ServiceAuth>,
120 /// The end user that sent the message.
121 /// Its xorname is derived from the client public key,
122 /// and the socket_id maps against the actual socketaddr
123 user: EndUser,
124 /// DstLocation for the message
125 dst_location: DstLocation,
126 },
127 /// Notify the current list of adult nodes, in case of churning.
128 AdultsChanged {
129 /// Remaining Adults in our section.
130 remaining: BTreeSet<XorName>,
131 /// New Adults in our section.
132 added: BTreeSet<XorName>,
133 /// Removed Adults in our section.
134 removed: BTreeSet<XorName>,
135 },
136}
137
138/// Type of messages that are received from a peer
139#[derive(Debug, Clone)]
140#[allow(clippy::large_enum_variant)]
141pub enum MessageReceived {
142 /// Cmds only sent a among Nodes in the network.
143 NodeCmd(NodeCmd),
144 /// Queries is a read-only operation.
145 NodeQuery(NodeQuery),
146 /// The response to a query, containing the query result.
147 NodeQueryResponse {
148 /// QueryResponse.
149 response: NodeQueryResponse,
150 /// ID of causing query.
151 correlation_id: MsgId,
152 },
153}