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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2021 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use serde::{Deserialize, Serialize};
use sn_data_types::PublicKey;
use xor_name::{Prefix, XorName};

type SocketId = XorName;

/// The planned route of a message.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub struct Itinerary {
    /// Source
    pub src: SrcLocation,
    /// Destionation
    pub dst: DstLocation,
    /// Wether this will be aggregated, and where.
    pub aggregation: Aggregation,
}

impl Itinerary {
    /// Elders will aggregate a group sig before
    /// they all all send one copy of it each to dst.
    pub fn aggregate_at_src(&self) -> bool {
        matches!(self.aggregation, Aggregation::AtSource)
    }

    /// Elders will send their signed message, which
    /// recipients aggregate.
    pub fn aggregate_at_dst(&self) -> bool {
        matches!(self.aggregation, Aggregation::AtDestination)
    }

    /// Name of the source
    pub fn src_name(&self) -> XorName {
        self.src.name()
    }

    /// Name of the destionation
    pub fn dst_name(&self) -> Option<XorName> {
        self.dst.name()
    }
}

/// Aggregation scheme
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum Aggregation {
    /// No aggregation is made, eg. when the payload contains full authority.
    None,
    /// Elders will aggregate a group sig before
    /// they all all send one copy of it each to dst.
    AtSource,
    /// Elders will send their signed message, which
    /// recipients aggregate.
    AtDestination,
}

/// An EndUser is repreented by a PublicKey.
/// It uses 1-n clients to access the network.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum EndUser {
    /// All clients of this end user.
    AllClients(PublicKey),
    /// An EndUser can instantiate multiple Clients.
    /// The Clients use the same PublicKey, but different SocketAddr.
    Client {
        /// The EndUser PublicKey
        public_key: PublicKey,
        /// A hash of the EndUser signature over their socket address.
        /// This maps to the SocketAddr at the Elders where the EndUser registered.
        socket_id: SocketId,
    },
}

impl EndUser {
    /// Returns the name of this location, or `None` if it is `Direct`.
    pub fn id(&self) -> &PublicKey {
        match self {
            Self::Client { public_key, .. } => public_key,
            Self::AllClients(public_key) => public_key,
        }
    }

    /// Returns the name of this location, or `None` if it is `Direct`.
    pub fn name(&self) -> XorName {
        (*self.id()).into()
    }

    /// Returns true if the provided name equals the enduser name.
    pub fn equals(&self, name: &XorName) -> bool {
        match self {
            Self::Client { public_key, .. } => name == &(*public_key).into(),
            Self::AllClients(public_key) => name == &(*public_key).into(),
        }
    }
}

/// Message source location.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum SrcLocation {
    /// An EndUser uses one or more Clients.
    EndUser(EndUser),
    /// A single node with the given name.
    Node(XorName),
    /// A section close to a name.
    Section(XorName),
}

impl SrcLocation {
    /// Returns whether this location is a section.
    pub fn is_section(&self) -> bool {
        matches!(self, Self::Section(_))
    }

    /// Returns whether this location is a section.
    pub fn is_user(&self) -> bool {
        matches!(self, Self::EndUser(_))
    }

    /// Returns whether the given name is part of this location
    pub fn equals(&self, name: &XorName) -> bool {
        match self {
            Self::EndUser(user) => user.equals(name),
            Self::Node(self_name) => name == self_name,
            Self::Section(some_name) => name == some_name,
        }
    }

    /// Returns the name of this location, or `None` if it is `Direct`.
    pub fn name(&self) -> XorName {
        match self {
            Self::EndUser(user) => user.name(),
            Self::Node(name) => *name,
            Self::Section(name) => *name,
        }
    }

    /// Returns this location as `DstLocation`
    pub fn to_dst(&self) -> DstLocation {
        match self {
            Self::EndUser(user) => DstLocation::EndUser(*user),
            Self::Node(name) => DstLocation::Node(*name),
            Self::Section(name) => DstLocation::Section(*name),
        }
    }
}

/// Message destination location.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum DstLocation {
    /// An EndUser uses one or more Clients.
    EndUser(EndUser),
    /// Destination is a single node with the given name.
    Node(XorName),
    /// Destination are the nodes of the section whose prefix matches the given name.
    Section(XorName),
    /// Destination is the node at the `ConnectionInfo` the message is directly sent to.
    Direct,
}

impl DstLocation {
    /// Returns whether this location is a section.
    pub fn is_section(&self) -> bool {
        matches!(self, Self::Section(_))
    }

    /// Returns whether this location is a section.
    pub fn is_user(&self) -> bool {
        matches!(self, Self::EndUser(_))
    }

    /// Returns whether the given name of the given prefix is part of this location.
    ///
    /// Returns None if `prefix` does not match `name`.
    pub fn contains(&self, name: &XorName, prefix: &Prefix) -> bool {
        if !prefix.matches(name) {
            return false;
        }

        match self {
            Self::EndUser(user) => prefix.matches(&user.name()),
            Self::Node(self_name) => name == self_name,
            Self::Section(self_name) => prefix.matches(self_name),
            Self::Direct => true,
        }
    }

    /// Returns the name of this location, or `None` if it is `Direct`.
    pub fn name(&self) -> Option<XorName> {
        match self {
            Self::EndUser(user) => Some(user.name()),
            Self::Node(name) => Some(*name),
            Self::Section(name) => Some(*name),
            Self::Direct => None,
        }
    }
}