safe_nd/messaging/
transfer.rs

1// Copyright 2020 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 super::{
11    AuthorisationKind, CmdError, MiscAuthKind, MoneyAuthKind, QueryResponse, TransferError,
12};
13#[cfg(feature = "simulated-payouts")]
14use crate::Transfer;
15use crate::{DebitAgreementProof, Error, PublicKey, SignedTransfer, XorName};
16use serde::{Deserialize, Serialize};
17use std::fmt;
18
19/// Money cmd that is sent to network.
20#[allow(clippy::large_enum_variant)]
21#[derive(Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
22pub enum TransferCmd {
23    #[cfg(feature = "simulated-payouts")]
24    /// Cmd to simulate a farming payout
25    SimulatePayout(Transfer),
26    /// The cmd to validate a transfer.
27    ValidateTransfer(SignedTransfer),
28    /// The cmd to register the consensused transfer.
29    RegisterTransfer(DebitAgreementProof),
30}
31
32/// Money query that is sent to network.
33#[allow(clippy::large_enum_variant)]
34#[derive(Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
35pub enum TransferQuery {
36    /// Get the PublicKeySet for replicas of a given PK
37    GetReplicaKeys(PublicKey),
38    /// Get key balance.
39    GetBalance(PublicKey),
40    /// Get key transfers since specified version.
41    GetHistory {
42        /// The balance key.
43        at: PublicKey,
44        /// The last version of transfers we know of.
45        since_version: usize,
46    },
47}
48
49impl TransferCmd {
50    /// Creates a Response containing an error, with the Response variant corresponding to the
51    /// Request variant.
52    pub fn error(&self, error: Error) -> CmdError {
53        use CmdError::*;
54        use TransferCmd::*;
55        use TransferError::*;
56        match *self {
57            ValidateTransfer(_) => Transfer(TransferValidation(error)),
58            RegisterTransfer(_) => Transfer(TransferRegistration(error)),
59            #[cfg(feature = "simulated-payouts")]
60            SimulatePayout(_) => Transfer(TransferRegistration(error)),
61        }
62    }
63
64    /// Returns the type of authorisation needed for the request.
65    pub fn authorisation_kind(&self) -> AuthorisationKind {
66        use TransferCmd::*;
67        match self {
68            RegisterTransfer(_) => AuthorisationKind::None, // the proof has the authority within it
69            ValidateTransfer(_) => AuthorisationKind::Misc(MiscAuthKind::WriteAndTransfer),
70            #[cfg(feature = "simulated-payouts")]
71            SimulatePayout(_) => AuthorisationKind::None,
72        }
73    }
74
75    /// Returns the address of the destination for `request`.
76    pub fn dst_address(&self) -> XorName {
77        use TransferCmd::*;
78        match self {
79            RegisterTransfer(ref proof) => XorName::from(proof.from()), // this is handled where the debit is made
80            ValidateTransfer(ref signed_transfer) => XorName::from(signed_transfer.from()), // this is handled where the debit is made
81            #[cfg(feature = "simulated-payouts")]
82            SimulatePayout(ref transfer) => XorName::from(transfer.from()), // this is handled where the debit is made
83        }
84    }
85}
86
87impl fmt::Debug for TransferCmd {
88    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
89        use TransferCmd::*;
90        write!(
91            formatter,
92            "TransferCmd::{}",
93            match *self {
94                RegisterTransfer { .. } => "RegisterTransfer",
95                ValidateTransfer { .. } => "ValidateTransfer",
96                #[cfg(feature = "simulated-payouts")]
97                SimulatePayout { .. } => "SimulatePayout",
98            }
99        )
100    }
101}
102
103impl TransferQuery {
104    // /// Get the variant of this query.
105    // pub fn get_type(&self) -> Type {
106    //     use TransferQuery::*;
107    //     match *self {
108    //         // TODO: This should this be private
109    //         GetReplicaKeys(_) => Type::PublicRead,
110    //         GetBalance(_) => Type::PrivateRead,
111    //         GetHistory { .. } => Type::PrivateRead,
112    //     }
113    // }
114
115    /// Creates a QueryResponse containing an error, with the QueryResponse variant corresponding to the
116    /// Request variant.
117    pub fn error(&self, error: Error) -> QueryResponse {
118        use TransferQuery::*;
119        match *self {
120            GetReplicaKeys(_) => QueryResponse::GetReplicaKeys(Err(error)),
121            GetBalance(_) => QueryResponse::GetBalance(Err(error)),
122            GetHistory { .. } => QueryResponse::GetHistory(Err(error)),
123        }
124    }
125
126    /// Returns the type of authorisation needed for the query.
127    pub fn authorisation_kind(&self) -> AuthorisationKind {
128        use TransferQuery::*;
129        match self {
130            GetBalance(_) => AuthorisationKind::Money(MoneyAuthKind::ReadBalance), // current state
131            GetReplicaKeys(_) => AuthorisationKind::None, // current replica keys
132            GetHistory { .. } => AuthorisationKind::Money(MoneyAuthKind::ReadHistory), // history of incoming transfers
133        }
134    }
135
136    /// Returns the address of the destination for the query.
137    pub fn dst_address(&self) -> XorName {
138        use TransferQuery::*;
139        match self {
140            GetBalance(at) | GetReplicaKeys(at) | GetHistory { at, .. } => XorName::from(*at),
141        }
142    }
143}
144
145impl fmt::Debug for TransferQuery {
146    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
147        use TransferQuery::*;
148        write!(
149            formatter,
150            "TransferQuery::{}",
151            match *self {
152                GetBalance(_) => "GetBalance",
153                GetReplicaKeys(_) => "GetReplicaKeys",
154                GetHistory { .. } => "GetHistory",
155            }
156        )
157    }
158}