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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#![warn(missing_docs)]

use itertools::izip;
use serde::Deserialize;
use serde::Serialize;

use crate::dht::Did;
use crate::err::Error;
use crate::err::Result;

/// `MessageRelay` divides messages into two types by method: SEND and REPORT.
/// And will enable different behaviors when handling SEND and REPORT messages.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub enum RelayMethod {
    /// When a node want to send message to another node, it will send a message with SEND method.
    SEND,
    /// A node that got a SEND message will either forward it to another node or respond with a REPORT message.
    REPORT,
}

/// MessageRelay guide message passing on rings network by relay.
///
/// All messages should be sent with `MessageRelay`.
/// By calling `relay` method in correct place, `MessageRelay` help to do things:
/// - Infer the next_hop of a message.
/// - Get the sender and origin sender of a message.
/// - Record the whole transport path for inspection.
///
/// # Examples
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct MessageRelay {
    /// The method of message. SEND or REPORT.
    pub method: RelayMethod,

    /// A push only stack. Record routes when handling sending messages.
    pub path: Vec<Did>,

    /// Move this cursor to flag the top of the stack when reporting.
    /// Notice that this cursor is not the index of current.
    /// It's `path.len() - <index of current> - 1`, which means count down to head of vector.
    /// It will always be 0 while handling sending messages in this way.
    pub path_end_cursor: usize,

    /// The next node to handle the message.
    /// When and only when located at the end of the message propagation, the `next_hop` is none.
    /// A message handler will pick transport by this field.
    pub next_hop: Option<Did>,

    /// The destination of the message. It may be customized when sending. It cannot be changed when reporting.
    /// It may help the handler to find out `next_hop` in some situations.
    pub destination: Did,
}

impl MessageRelay {
    /// Create a new `MessageRelay`.
    /// Will set `path_end_cursor` to 0 if pass None as parameter.
    pub fn new(
        method: RelayMethod,
        path: Vec<Did>,
        path_end_cursor: Option<usize>,
        next_hop: Option<Did>,
        destination: Did,
    ) -> Self {
        Self {
            method,
            path,
            path_end_cursor: path_end_cursor.unwrap_or(0),
            next_hop,
            destination,
        }
    }

    /// Check current did, update path and its end cursor, then infer next_hop.
    ///
    /// When handling a SEND message, will push `current` to the `self.path` stack, and set `next_hop` parameter to `self.next_node`.
    ///
    /// When handling a REPORT message, will move forward `self.path_end_cursor` to the position of `current` in `self.path`.
    /// If `next_hop` parameter is none, it will also pick the previous node in `self.path` as `self.next_hop`.
    /// (With this feature, one can always pass None as `next_hop` parameter when handling a REPORT message.)
    pub fn relay(&mut self, current: Did, next_hop: Option<Did>) -> Result<()> {
        self.validate()?;

        // If self.next_hop is set, it should be current
        if self.next_hop.is_some() && self.next_hop.unwrap() != current {
            return Err(Error::InvalidNextHop);
        }

        match self.method {
            RelayMethod::SEND => {
                self.path.push(current);
                self.next_hop = next_hop;
                Ok(())
            }

            RelayMethod::REPORT => {
                // The final hop
                if self.next_hop == Some(self.destination) {
                    self.path_end_cursor = self.path.len() - 1;
                    self.next_hop = None;
                    return Ok(());
                }

                let pos = self
                    .path
                    .iter()
                    .rev()
                    .skip(self.path_end_cursor)
                    .position(|&x| x == current);

                if let (None, None) = (pos, next_hop) {
                    return Err(Error::CannotInferNextHop);
                }

                if let Some(pos) = pos {
                    self.path_end_cursor += pos;
                }

                // `self.path_prev()` should never return None here, because we have handled final hop
                self.next_hop = next_hop.or_else(|| self.path_prev());

                Ok(())
            }
        }
    }

    /// Construct a `MessageRelay` of method REPORT from a `MessageRelay` of method REPORT.
    /// It will return Error if method is not SEND.
    /// It will return Error if `self.path.len()` is less than 2.
    pub fn report(&self) -> Result<Self> {
        if self.method != RelayMethod::SEND {
            return Err(Error::ReportNeedSend);
        }

        if self.path.len() < 2 {
            return Err(Error::CannotInferNextHop);
        }

        Ok(Self {
            method: RelayMethod::REPORT,
            path: self.path.clone(),
            path_end_cursor: 0,
            next_hop: self.path_prev(),
            destination: self.sender(),
        })
    }

    /// A SEND message can change its destination.
    /// Call with REPORT method will get an error imeediately.
    pub fn reset_destination(&mut self, destination: Did) -> Result<()> {
        if self.method == RelayMethod::SEND {
            self.destination = destination;
            Ok(())
        } else {
            Err(Error::ResetDestinationNeedSend)
        }
    }

    /// Check if path and destination is valid.
    /// It will be automatically called at relay started.
    pub fn validate(&self) -> Result<()> {
        // Adjacent elements in self.path cannot be equal
        if self.path.windows(2).any(|w| w[0] == w[1]) {
            return Err(Error::InvalidRelayPath);
        }

        // The destination of report message should always be the first element of path
        if self.method == RelayMethod::REPORT && self.path[0] != self.destination {
            return Err(Error::InvalidRelayDestination);
        }

        // Prevent infinite loop
        if self.method == RelayMethod::SEND && has_infinite_loop(&self.path) {
            return Err(Error::InfiniteRelayPath);
        }

        Ok(())
    }

    /// Get the original sender of current message.
    /// Should always be the first element of path.
    pub fn origin(&self) -> Did {
        *self.path.first().unwrap()
    }

    /// Get sender of current message.
    /// With SEND method, it will be the `origin()` of the message.
    /// With REPORT method, it will be the last element of path.
    pub fn sender(&self) -> Did {
        match self.method {
            RelayMethod::SEND => self.origin(),
            RelayMethod::REPORT => *self.path.last().unwrap(),
        }
    }

    /// Get the previous element of the element pointed by path_end_cursor.
    pub fn path_prev(&self) -> Option<Did> {
        if self.path.len() < self.path_end_cursor + 2 {
            None
        } else {
            Some(self.path[self.path.len() - 2 - self.path_end_cursor])
        }
    }
}

// Since rust cannot zip N iterators, when you change this number,
// you should also change the code of `has_infinite_loop` below.
const INFINITE_LOOP_TOLERANCE: usize = 3;

fn has_infinite_loop<T>(path: &[T]) -> bool
where T: PartialEq + std::fmt::Debug {
    if let Some(last) = path.last() {
        let indexes = path
            .iter()
            .rev()
            .enumerate()
            .filter(|(_, r)| r == &last)
            .map(|(index, _)| index)
            .take(INFINITE_LOOP_TOLERANCE)
            .collect::<Vec<_>>();

        if indexes.len() >= INFINITE_LOOP_TOLERANCE {
            let p1 = path.iter().rev().skip(indexes[0]);
            let p2 = path.iter().rev().skip(indexes[1]);
            let p3 = path.iter().rev().skip(indexes[2]);

            let lens = vec![
                indexes[1] - indexes[0],
                indexes[2] - indexes[1],
                path.len() - indexes[2],
            ];

            let min_len = lens.iter().min().unwrap();

            for (i, (x, y, z)) in izip!(p1, p2, p3).enumerate() {
                if !(x == y && y == z) {
                    return false;
                }

                if i == min_len - 1 {
                    break;
                }
            }

            if lens[0] == lens[1] {
                return true;
            }
        }
    }

    false
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::ecc::SecretKey;

    #[test]
    fn test_path_end_cursor() {
        let origin_sender = SecretKey::random().address().into();
        let next_hop1 = SecretKey::random().address().into();
        let next_hop2 = SecretKey::random().address().into();
        let next_hop3 = SecretKey::random().address().into();

        let mut send_relay = MessageRelay {
            method: RelayMethod::SEND,
            path: vec![origin_sender],
            path_end_cursor: 0,
            next_hop: None,
            destination: next_hop3,
        };

        // node0 -> node1
        send_relay.relay(next_hop1, None).unwrap();
        assert_eq!(send_relay.path_end_cursor, 0);

        // node0 -> node1 -> node2
        send_relay.relay(next_hop2, None).unwrap();
        assert_eq!(send_relay.path_end_cursor, 0);

        // node0 -> node1 -> node2 -> node3
        send_relay.relay(next_hop3, None).unwrap();
        assert_eq!(send_relay.path_end_cursor, 0);

        // Node3 make REPORT, destination is node0.
        let mut report_relay = send_relay.report().unwrap();
        assert_eq!(report_relay.path_end_cursor, 0);

        // node0 -> node1 -> node2 -> node3 -> node2
        report_relay.relay(next_hop2, None).unwrap();
        assert_eq!(report_relay.path_end_cursor, 1);

        // node0 -> node1 -> node2 -> node3 -> node2 -> node1
        report_relay.relay(next_hop1, None).unwrap();
        assert_eq!(report_relay.path_end_cursor, 2);
    }

    #[test]
    fn test_jump_to_previous_node_when_reporting() {
        let origin_sender = SecretKey::random().address().into();
        let next_hop1 = SecretKey::random().address().into();
        let next_hop2 = SecretKey::random().address().into();
        let next_hop3 = SecretKey::random().address().into();
        let next_hop4 = SecretKey::random().address().into();

        let mut send_relay = MessageRelay {
            method: RelayMethod::SEND,
            path: vec![origin_sender],
            path_end_cursor: 0,
            next_hop: None,
            destination: next_hop4,
        };

        // node0 -> node1 -> node2 -> node3 -> node4
        send_relay.relay(next_hop1, None).unwrap();
        send_relay.relay(next_hop2, None).unwrap();
        send_relay.relay(next_hop3, None).unwrap();
        send_relay.relay(next_hop4, None).unwrap();
        assert_eq!(send_relay.path, vec![
            origin_sender,
            next_hop1,
            next_hop2,
            next_hop3,
            next_hop4
        ]);
        assert_eq!(send_relay.path_end_cursor, 0);

        // Node4 make REPORT, destination is node0.
        let mut report_relay = send_relay.report().unwrap();
        assert_eq!(report_relay.path, vec![
            origin_sender,
            next_hop1,
            next_hop2,
            next_hop3,
            next_hop4
        ]);
        assert_eq!(report_relay.next_hop, Some(next_hop3));
        assert_eq!(report_relay.path_end_cursor, 0);

        // And node4 find that node2 is connected, so it will skip node3 and will jump to node2.
        report_relay.next_hop = Some(next_hop2);

        // And node2 find that node0 is connected, so it will skip node1 and will jump to node0.
        // node0 -> node1 -> node2 -> node3 -> node4 -> node2
        report_relay.relay(next_hop2, Some(origin_sender)).unwrap();
        assert_eq!(report_relay.path, vec![
            origin_sender,
            next_hop1,
            next_hop2,
            next_hop3,
            next_hop4
        ]);
        assert_eq!(report_relay.next_hop, Some(origin_sender));
        assert_eq!(report_relay.path_end_cursor, 2);

        // node0 -> node1 -> node2 -> node3 -> node4 -> node2 -> node0
        report_relay.relay(origin_sender, None).unwrap();
    }

    #[test]
    fn test_path_prev() {
        let origin_sender = SecretKey::random().address().into();
        let next_hop1 = SecretKey::random().address().into();
        let next_hop2 = SecretKey::random().address().into();

        let mut relay = MessageRelay {
            method: RelayMethod::SEND,
            path: vec![origin_sender],
            path_end_cursor: 0,
            next_hop: None,
            destination: next_hop2,
        };

        assert!(relay.path_prev().is_none());

        relay.relay(next_hop1, None).unwrap();
        assert_eq!(relay.path_prev(), Some(origin_sender));

        relay.relay(next_hop2, None).unwrap();
        assert_eq!(relay.path_prev(), Some(next_hop1));
    }

    #[test]
    #[rustfmt::skip]
    fn test_has_infinite_loop() {
        assert!(!has_infinite_loop(&Vec::<u8>::new()));

        assert!(!has_infinite_loop(&[
            1, 2, 3,
        ]));

        assert!(!has_infinite_loop(&[
            1, 2, 3,
            1, 2, 3,
        ]));

        assert!(has_infinite_loop(&[
            1, 2, 3,
            1, 2, 3,
            1, 2, 3,
        ]));

        assert!(has_infinite_loop(&[
            1, 1, 2, 3,
               1, 2, 3,
               1, 2, 3,
        ]));

        assert!(!has_infinite_loop(&[
               1, 2, 3,
            1, 1, 2, 3,
               1, 2, 3,
        ]));

        assert!(has_infinite_loop(&[
            1, 2, 1, 2, 3,
                  1, 2, 3,
                  1, 2, 3,
        ]));

        assert!(has_infinite_loop(&[
            4, 5, 1, 2, 3,
                  1, 2, 3,
                  1, 2, 3,
        ]));

        assert!(!has_infinite_loop(&[
            1, 2, 3,
                  3,
            1, 2, 3,
                  3,
            1, 2, 3,
        ]));

        assert!(!has_infinite_loop(&[
                  1,
            1, 2, 3,
                  3,
            1, 2, 3,
                  3,
            1, 2, 3,
        ]));

        // TODO: try to detect this earlier.
        assert!(!has_infinite_loop(&[
                  3,
            1, 2, 3,
                  3,
            1, 2, 3,
                  3,
            1, 2, 3,
        ]));

        // TODO: try to detect this earlier.
        assert!(!has_infinite_loop(&[
            1, 2, 3,
            1, 2, 3,
                  3,
            1, 2, 3,
                  3,
            1, 2, 3,
        ]));

        // The above two cases will be detected finally.
        assert!(has_infinite_loop(&[
                  1, 2,
               3, 1, 2,
            3, 3, 1, 2,
            3, 3, 1, 2,
            3, 3, 1, 2,
        ]));

        assert!(!has_infinite_loop(&[
               2, 3,
               4, 3,
            1, 2, 3,
               4, 3,
            1, 2, 3,
               4, 3,
        ]));

        // TODO: try to detect this earlier.
        assert!(!has_infinite_loop(&[
            1, 2, 3,
               4, 3,
            1, 2, 3,
               4, 3,
            1, 2, 3,
               4, 3,
        ]));

        // The above case will be detected finally.
        assert!(has_infinite_loop(&[
               1, 2, 3, 4,
            3, 1, 2, 3, 4,
            3, 1, 2, 3, 4,
            3, 1, 2, 3, 4,
        ]));
    }
}