safe_nd/messaging/
network.rs

1// Copyright 2019 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
4// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
5// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
6// modified, or distributed except according to those terms. Please review the Licences for the
7// specific language governing permissions and limitations relating to use of the SAFE Network
8// Software.
9
10use crate::{
11    AccountId, Address, Blob, BlobAddress, DebitAgreementProof, Error, PublicKey, ReplicaEvent,
12    Result, Signature, SignedTransfer, TransferId, TransferValidated, XorName,
13};
14use serde::{Deserialize, Serialize};
15use std::collections::BTreeSet;
16
17// -------------- Node Cmds --------------
18
19///
20#[allow(clippy::large_enum_variant)]
21#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
22pub enum NodeCmd {
23    /// Cmds related to the running of a node.
24    System(NodeSystemCmd),
25    ///
26    Data(NodeDataCmd),
27    ///
28    Transfers(NodeTransferCmd),
29}
30
31/// Cmds related to the running of a node.
32#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
33pub enum NodeSystemCmd {
34    /// Register a wallet for reward payouts.
35    RegisterWallet {
36        /// The wallet to which rewards will be paid out by the network.
37        wallet: PublicKey,
38        /// The section where this wallet is to be registered (NB: this is the section of the node id).
39        section: XorName,
40    },
41}
42
43///
44#[allow(clippy::large_enum_variant)]
45#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
46pub enum NodeTransferCmd {
47    ///
48    PropagateTransfer(DebitAgreementProof),
49    ///
50    ValidateSectionPayout(SignedTransfer),
51    ///
52    RegisterSectionPayout(DebitAgreementProof),
53}
54
55///
56#[allow(clippy::large_enum_variant)]
57#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
58pub enum NodeDataCmd {
59    ///
60    DuplicateChunk {
61        ///
62        new_holder: XorName,
63        ///
64        address: BlobAddress,
65        ///
66        fetch_from_holders: BTreeSet<XorName>,
67    },
68}
69
70// -------------- Node Events --------------
71
72///
73#[allow(clippy::large_enum_variant)]
74#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
75pub enum NodeEvent {
76    /// Wrapper for a duplicate completion response, from a node to elders.
77    DuplicationComplete {
78        ///
79        chunk: BlobAddress,
80        /// The Elder's accumulated signature
81        /// over the chunk address. This is sent back
82        /// to them so that any uninformed Elder knows
83        /// that this is all good.
84        proof: Signature,
85    },
86    ///
87    SectionPayoutValidated(TransferValidated),
88}
89
90///
91#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
92pub enum NodeQuery {
93    ///
94    Data(NodeDataQuery),
95    ///
96    Rewards(NodeRewardQuery),
97    ///
98    Transfers(NodeTransferQuery),
99}
100
101/// Reward query that is sent between sections.
102#[allow(clippy::large_enum_variant)]
103#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
104pub enum NodeRewardQuery {
105    /// Sent by the new section to the
106    /// old section after node relocation.
107    GetAccountId {
108        /// The id of the node
109        /// in the old section.
110        old_node_id: XorName,
111        /// The id of the node
112        /// in the new section.
113        new_node_id: XorName,
114    },
115}
116
117///
118#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
119pub enum NodeTransferQuery {
120    /// Replicas starting up
121    /// need to query for events of
122    /// the existing Replicas.
123    GetReplicaEvents(PublicKey),
124}
125
126///
127#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
128pub enum NodeDataQuery {
129    /// Elder to Adult Get.
130    GetChunk {
131        /// The holder id.
132        holder: XorName,
133        /// The chunk address.
134        address: BlobAddress,
135    },
136    /// Adult to Adult Get
137    GetChunks {
138        /// The holder id.
139        holder: XorName,
140        /// The chunk addresses.
141        addresses: BTreeSet<BlobAddress>,
142    },
143}
144
145///
146#[allow(clippy::large_enum_variant)]
147#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
148pub enum NodeQueryResponse {
149    ///
150    Data(NodeDataQueryResponse),
151    ///
152    Rewards(NodeRewardQueryResponse),
153    ///
154    Transfers(NodeTransferQueryResponse),
155}
156
157///
158#[allow(clippy::large_enum_variant)]
159#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
160pub enum NodeRewardQueryResponse {
161    /// Returns the account id
162    /// together with the new node id,
163    /// that followed with the original query.
164    GetAccountId(Result<(PublicKey, XorName)>),
165}
166
167///
168#[allow(clippy::large_enum_variant)]
169#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
170pub enum NodeTransferQueryResponse {
171    /// Replicas starting up
172    /// need to query for events of
173    /// the existing Replicas.
174    GetReplicaEvents(Result<Vec<ReplicaEvent>>),
175}
176
177///
178#[allow(clippy::large_enum_variant)]
179#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
180pub enum NodeDataQueryResponse {
181    /// Elder to Adult Get.
182    GetChunk(Result<Blob>),
183    /// Adult to Adult Get
184    GetChunks(Result<Vec<Blob>>),
185}
186
187///
188#[allow(clippy::large_enum_variant)]
189#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
190pub enum NodeCmdError {
191    ///
192    Data(NodeDataError),
193    ///
194    Rewards(NodeRewardError),
195    ///
196    Transfers(NodeTransferError),
197}
198
199///
200#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
201pub enum NodeDataError {
202    ///
203    ChunkDuplication {
204        ///
205        address: BlobAddress,
206        ///
207        error: Error,
208    },
209}
210
211///
212#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
213pub enum NodeTransferError {
214    /// The error of propagation of TransferRegistered event.
215    TransferPropagation(Error),
216}
217
218///
219#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
220pub enum NodeRewardError {
221    ///
222    RewardClaiming {
223        ///
224        account_id: AccountId,
225        ///
226        error: Error,
227    },
228    ///
229    RewardPayoutInitiation {
230        ///
231        id: TransferId,
232        ///
233        account: AccountId,
234        ///
235        error: Error,
236    },
237    ///
238    RewardPayoutFinalisation {
239        ///
240        id: TransferId,
241        ///
242        account: AccountId,
243        ///
244        error: Error,
245    },
246}
247
248impl NodeCmd {
249    /// Returns the address of the destination for `request`.
250    pub fn dst_address(&self) -> Address {
251        use Address::*;
252        use NodeCmd::*;
253        use NodeDataCmd::*;
254        use NodeTransferCmd::*;
255        match self {
256            System(NodeSystemCmd::RegisterWallet { section, .. }) => Section(*section),
257            Data(DuplicateChunk { new_holder, .. }) => Node(*new_holder),
258            Transfers(cmd) => match cmd {
259                ValidateSectionPayout(signed_transfer) => Section(signed_transfer.from().into()),
260                RegisterSectionPayout(debit_agreement) => Section(debit_agreement.from().into()),
261                PropagateTransfer(debit_agreement) => Section(debit_agreement.to().into()),
262            },
263        }
264    }
265}
266
267impl NodeEvent {
268    /// Returns the address of the destination for `request`.
269    pub fn dst_address(&self) -> Address {
270        use Address::*;
271        use NodeEvent::*;
272        match self {
273            DuplicationComplete { chunk, .. } => Section(*chunk.name()),
274            SectionPayoutValidated(event) => Section(event.from().into()),
275        }
276    }
277}
278
279impl NodeQuery {
280    /// Returns the address of the destination for the query.
281    pub fn dst_address(&self) -> Address {
282        use Address::*;
283        use NodeDataQuery::*;
284        use NodeQuery::*;
285        use NodeRewardQuery::*;
286        use NodeTransferQuery::*;
287        match self {
288            Data(data_query) => match data_query {
289                GetChunk { holder, .. } | GetChunks { holder, .. } => Node(*holder),
290            },
291            Transfers(transfer_query) => match transfer_query {
292                GetReplicaEvents(section_key) => Section((*section_key).into()),
293            },
294            Rewards(GetAccountId { old_node_id, .. }) => Section(*old_node_id),
295        }
296    }
297}