ant_protocol/messages/
cmd.rs

1// Copyright 2024 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#![allow(clippy::mutable_key_type)] // for Bytes in NetworkAddress
9
10use crate::storage::DataTypes;
11use crate::{storage::ValidationType, NetworkAddress};
12use ant_evm::ProofOfPayment;
13use serde::{Deserialize, Serialize};
14
15/// Ant protocol cmds
16///
17/// See the [`protocol`] module documentation for more details of the types supported by the Safe
18/// Network, and their semantics.
19///
20/// [`protocol`]: crate
21#[derive(Eq, PartialEq, Clone, Serialize, Deserialize)]
22pub enum Cmd {
23    /// Write operation to notify peer fetch a list of [`NetworkAddress`] from the holder.
24    ///
25    /// [`NetworkAddress`]: crate::NetworkAddress
26    Replicate {
27        /// Holder of the replication keys.
28        holder: NetworkAddress,
29        /// Keys of copy that shall be replicated.
30        keys: Vec<(NetworkAddress, ValidationType)>,
31    },
32    /// Write operation to notify peer fetch a list of fresh [`NetworkAddress`] from the holder.
33    ///
34    /// [`NetworkAddress`]: crate::NetworkAddress
35    FreshReplicate {
36        /// Holder of the replication keys.
37        holder: NetworkAddress,
38        /// Keys of copy that shall be replicated.
39        keys: Vec<(
40            NetworkAddress,
41            DataTypes,
42            ValidationType,
43            Option<ProofOfPayment>,
44        )>,
45    },
46    /// Notify the peer it is now being considered as BAD due to the included behaviour
47    PeerConsideredAsBad {
48        detected_by: NetworkAddress,
49        bad_peer: NetworkAddress,
50        bad_behaviour: String,
51    },
52}
53
54impl std::fmt::Debug for Cmd {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        match self {
57            Cmd::Replicate { holder, keys } => {
58                let first_ten_keys: Vec<_> = keys.iter().take(10).collect();
59                f.debug_struct("Cmd::Replicate")
60                    .field("holder", holder)
61                    .field("keys_len", &keys.len())
62                    .field("first_ten_keys", &first_ten_keys)
63                    .finish()
64            }
65            Cmd::FreshReplicate { holder, keys } => {
66                let first_ten_keys: Vec<_> = keys.iter().take(10).collect();
67                f.debug_struct("Cmd::FreshReplicate")
68                    .field("holder", holder)
69                    .field("keys_len", &keys.len())
70                    .field("first_ten_keys", &first_ten_keys)
71                    .finish()
72            }
73            Cmd::PeerConsideredAsBad {
74                detected_by,
75                bad_peer,
76                bad_behaviour,
77            } => f
78                .debug_struct("Cmd::PeerConsideredAsBad")
79                .field("detected_by", detected_by)
80                .field("bad_peer", bad_peer)
81                .field("bad_behaviour", bad_behaviour)
82                .finish(),
83        }
84    }
85}
86
87impl Cmd {
88    /// Used to send a cmd to the close group of the address.
89    pub fn dst(&self) -> NetworkAddress {
90        match self {
91            Cmd::Replicate { holder, .. } => holder.clone(),
92            Cmd::FreshReplicate { holder, .. } => holder.clone(),
93            Cmd::PeerConsideredAsBad { bad_peer, .. } => bad_peer.clone(),
94        }
95    }
96}
97
98impl std::fmt::Display for Cmd {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        match self {
101            Cmd::Replicate { holder, keys } => {
102                write!(
103                    f,
104                    "Cmd::Replicate({:?} has {} keys)",
105                    holder.as_peer_id(),
106                    keys.len()
107                )
108            }
109            Cmd::FreshReplicate { holder, keys } => {
110                write!(
111                    f,
112                    "Cmd::Replicate({:?} has {} keys)",
113                    holder.as_peer_id(),
114                    keys.len()
115                )
116            }
117            Cmd::PeerConsideredAsBad {
118                detected_by,
119                bad_peer,
120                bad_behaviour,
121            } => {
122                write!(
123                    f,
124                    "Cmd::PeerConsideredAsBad({detected_by:?} consider peer {bad_peer:?} as bad, due to {bad_behaviour:?})")
125            }
126        }
127    }
128}