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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//  Copyright (C) 2017  The Duniter Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Module defining the format of network heads v2 and how to handle them.

use crate::NodeId;
use dubp_documents::blockstamp::*;
use dup_crypto::bases::BaseConvertionError;
use dup_crypto::keys::*;
use std::cmp::Ordering;
use std::num::ParseIntError;
use std::ops::Deref;
use std::str::FromStr;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// Head Message V2
pub struct NetworkHeadMessageV2 {
    /// API details
    pub api: String,
    /// Head version
    pub version: usize,
    /// Head pubkey
    pub pubkey: PubKey,
    /// Head blockstamp
    pub blockstamp: Blockstamp,
    /// Head node id
    pub node_uuid: NodeId,
    /// Issuer node software
    pub software: String,
    /// Issuer node soft version
    pub soft_version: String,
    /// Issuer node prefix
    pub prefix: usize,
    /// Issuer node free member room
    pub free_member_room: Option<usize>,
    /// Issuer node free mirror room
    pub free_mirror_room: Option<usize>,
}

impl PartialOrd for NetworkHeadMessageV2 {
    fn partial_cmp(&self, other: &NetworkHeadMessageV2) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for NetworkHeadMessageV2 {
    fn cmp(&self, other: &NetworkHeadMessageV2) -> Ordering {
        self.blockstamp.cmp(&other.blockstamp)
    }
}

impl NetworkHeadMessageV2 {
    /// To human readable string
    pub fn to_human_string(&self, max_len: usize, uid: Option<String>) -> String {
        let short_api = &self.api[4..];

        if max_len > 85 && uid.is_some() {
            format!(
                "{node_id:8}-{pubkey:.8} {blockstamp:.16} {soft:7}:{ver:14} {pre:3} [{api:5}]  {mer:02}:{mir:02} {uid}",
                node_id = self.node_uuid.to_string(),
                pubkey = self.pubkey.to_string(),
                blockstamp = self.blockstamp.to_string(),
                soft = self.software,
                ver = self.soft_version,
                pre = self.prefix,
                api = short_api,
                mer = self.free_member_room.unwrap_or(0),
                mir = self.free_mirror_room.unwrap_or(0),
                uid = uid.unwrap(),
            )
        } else if max_len > 75 {
            format!(
                "{node_id:8}-{pubkey:.8} {blockstamp:.16} {soft:7}:{ver:14} {pre:3} [{api:5}]  {mer:02}:{mir:02}",
                node_id = self.node_uuid.to_string(),
                pubkey = self.pubkey.to_string(),
                blockstamp = self.blockstamp.to_string(),
                soft = self.software,
                ver = self.soft_version,
                pre = self.prefix,
                api = short_api,
                mer = self.free_member_room.unwrap_or(0),
                mir = self.free_mirror_room.unwrap_or(0),
            )
        } else if max_len > 70 {
            format!(
                "{node_id:8}-{pubkey:.8} {blockstamp:.16} {soft:7}:{ver:14} [{api:5}]  {mer:02}:{mir:02}",
                node_id = self.node_uuid.to_string(),
                pubkey = self.pubkey.to_string(),
                blockstamp = self.blockstamp.to_string(),
                soft = self.software,
                ver = self.soft_version,
                api = short_api,
                mer = self.free_member_room.unwrap_or(0),
                mir = self.free_mirror_room.unwrap_or(0),
            )
        } else if max_len > 47 {
            format!(
                "{node_id:8}-{pubkey:.8} {blockstamp:.16} [{api:5}]  {mer:02}:{mir:02}",
                node_id = self.node_uuid.to_string(),
                pubkey = self.pubkey.to_string(),
                blockstamp = self.blockstamp.to_string(),
                api = short_api,
                mer = self.free_member_room.unwrap_or(0),
                mir = self.free_mirror_room.unwrap_or(0),
            )
        } else if max_len > 41 {
            format!(
                "{node_id:8}-{pubkey:.8} {blockstamp:.16} [{api:5}]",
                node_id = self.node_uuid.to_string(),
                pubkey = self.pubkey.to_string(),
                blockstamp = self.blockstamp.to_string(),
                api = short_api,
            )
        } else {
            String::from("Term width insufficient")
        }
    }
}

#[derive(Debug, Clone, Eq, Ord, PartialOrd, PartialEq, Hash, Serialize, Deserialize)]
/// Head Message
pub enum NetworkHeadMessage {
    /// Head Message V2
    V2(NetworkHeadMessageV2),
    /// Do not use
    Other(),
}

/// NetworkHeadMessage parse error
#[derive(Debug)]
pub enum NetworkHeadMessageParseErr {
    /// BaseConvertionError
    BaseConvertionError(BaseConvertionError),
    /// ParseIntError
    ParseIntError(ParseIntError),
    /// BlockstampParseError
    BlockstampParseError(BlockstampParseError),
}

impl From<BaseConvertionError> for NetworkHeadMessageParseErr {
    fn from(e: BaseConvertionError) -> Self {
        NetworkHeadMessageParseErr::BaseConvertionError(e)
    }
}

impl From<BlockstampParseError> for NetworkHeadMessageParseErr {
    fn from(e: BlockstampParseError) -> Self {
        NetworkHeadMessageParseErr::BlockstampParseError(e)
    }
}

impl From<ParseIntError> for NetworkHeadMessageParseErr {
    fn from(e: ParseIntError) -> Self {
        NetworkHeadMessageParseErr::ParseIntError(e)
    }
}

impl FromStr for NetworkHeadMessage {
    type Err = NetworkHeadMessageParseErr;
    fn from_str(source: &str) -> Result<Self, Self::Err> {
        let source_array: Vec<&str> = source.split(':').collect();
        Ok(NetworkHeadMessage::V2(NetworkHeadMessageV2 {
            api: source_array[0].to_string(),
            version: source_array[2].parse()?,
            pubkey: PubKey::Ed25519(ed25519::PublicKey::from_base58(
                &source_array[3].to_string(),
            )?),
            blockstamp: Blockstamp::from_string(source_array[4])?,
            node_uuid: NodeId(u32::from_str_radix(source_array[5], 16)?),
            software: source_array[6].to_string(),
            soft_version: source_array[7].to_string(),
            prefix: source_array[8].parse()?,
            free_member_room: if let Some(field) = source_array.get(9) {
                Some(field.parse()?)
            } else {
                None
            },
            free_mirror_room: if let Some(field) = source_array.get(10) {
                Some(field.parse()?)
            } else {
                None
            },
        }))
    }
}

impl NetworkHeadMessage {
    /// To human readable string
    pub fn to_human_string(&self, max_len: usize, uid: Option<String>) -> String {
        match *self {
            NetworkHeadMessage::V2(ref mess_v2) => mess_v2.deref().to_human_string(max_len, uid),
            _ => panic!("NetworkHead version not supported !"),
        }
    }
    /// Get head blockcstamp
    pub fn blockstamp(&self) -> Blockstamp {
        match *self {
            NetworkHeadMessage::V2(ref head_message_v2) => head_message_v2.blockstamp,
            _ => panic!("This HEAD version is not supported !"),
        }
    }
    /// Get head node id
    pub fn node_uuid(&self) -> NodeId {
        match *self {
            NetworkHeadMessage::V2(ref head_message_v2) => head_message_v2.node_uuid,
            _ => panic!("This HEAD version is not supported !"),
        }
    }
    /// Get head issuer public key
    fn _pubkey(&self) -> PubKey {
        match *self {
            NetworkHeadMessage::V2(ref head_message_v2) => head_message_v2.pubkey,
            _ => panic!("This HEAD version is not supported !"),
        }
    }
}

impl ToString for NetworkHeadMessageV2 {
    fn to_string(&self) -> String {
        match self.version {
            1 => format!(
                "{}:HEAD:1:{}:{}:{}:{}:{}:{}",
                self.api,
                self.pubkey,
                self.blockstamp,
                self.node_uuid,
                self.software,
                self.soft_version,
                self.prefix
            ),
            2 => format!(
                "{}:HEAD:2:{}:{}:{}:{}:{}:{}:{}:{}",
                self.api,
                self.pubkey,
                self.blockstamp,
                self.node_uuid,
                self.software,
                self.soft_version,
                self.prefix,
                self.free_member_room.unwrap(),
                self.free_mirror_room.unwrap()
            ),
            _ => panic!("NetworkHeadMessage is wrongly parsed !"),
        }
    }
}

impl ToString for NetworkHeadMessage {
    fn to_string(&self) -> String {
        match *self {
            NetworkHeadMessage::V2(ref head_message) => head_message.to_string(),
            _ => panic!("This HEADMessage version is not supported !"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// Head V2
pub struct NetworkHeadV2 {
    /// Head V1 Message
    pub message: NetworkHeadMessage,
    /// signature of V1 Message
    pub sig: Sig,
    /// Head V2 Message
    pub message_v2: NetworkHeadMessage,
    /// signature of V2 Message
    pub sig_v2: Sig,
    /// Head step
    pub step: u32,
    /// Head issuer uid
    pub uid: Option<String>,
}

impl ToString for NetworkHeadV2 {
    fn to_string(&self) -> String {
        self.message_v2.to_string()
    }
}

impl PartialOrd for NetworkHeadV2 {
    fn partial_cmp(&self, other: &NetworkHeadV2) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for NetworkHeadV2 {
    fn cmp(&self, other: &NetworkHeadV2) -> Ordering {
        self.message.cmp(&other.message)
    }
}

impl NetworkHeadV2 {
    /// To human readable string
    pub fn to_human_string(&self, max_len: usize) -> String {
        if max_len > 2 {
            format!(
                "{} {}",
                self.step,
                self.message_v2
                    .to_human_string(max_len - 2, self.uid.clone())
            )
        } else {
            String::from(".")
        }
    }
    /// Get uid of head issuer
    pub fn uid(&self) -> Option<String> {
        self.uid.clone()
    }
}