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
#![warn(missing_docs)]
use std::str::FromStr;

use num_bigint::BigUint;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde::Serialize;

use crate::dht::subring::SubRing;
use crate::dht::Did;
use crate::ecc::HashStr;
use crate::err::Error;
use crate::err::Result;
use crate::message::Encoded;
use crate::message::Encoder;
use crate::message::MessagePayload;

/// VNode Types
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum VNodeType {
    /// Data: Encoded data stored in DHT
    Data,
    /// SubRing: Finger table of a SubRing
    SubRing,
    /// RelayMessage: A Relayed but unreach message, which is stored on it's successor
    RelayMessage,
}

/// A Virtual Node is a Node that dont have real network address.
/// The Address of a Virtual Node is virutal,
/// For Encoded Data, it's sha1 of data, for a SubRing, it's sha1 of SubRing's name,
/// and for the RelayedMessage, it's the target address of message plus 1 (for ensure that the message is
/// sent to the successor of target), thus while target Node going online, it will sync from it's successor.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct VirtualNode {
    /// address of vnode
    pub address: Did,
    /// all data of vnode should be encoded
    pub data: Vec<Encoded>,
    /// vnode type
    pub kind: VNodeType,
}

impl VirtualNode {
    /// VirtualNode should have it's virtual address.
    pub fn did(&self) -> Did {
        self.address
    }
}

impl<T> TryFrom<MessagePayload<T>> for VirtualNode
where T: Serialize + DeserializeOwned
{
    type Error = Error;
    fn try_from(msg: MessagePayload<T>) -> Result<Self> {
        let address = BigUint::from(Did::from(msg.addr)) + BigUint::from(1u16);
        let data = msg.encode()?;
        Ok(Self {
            address: address.into(),
            data: vec![data],
            kind: VNodeType::RelayMessage,
        })
    }
}

impl TryFrom<Encoded> for VirtualNode {
    type Error = Error;
    fn try_from(e: Encoded) -> Result<Self> {
        let address: HashStr = e.value().into();
        Ok(Self {
            address: Did::from_str(&address.inner())?,
            data: vec![e],
            kind: VNodeType::Data,
        })
    }
}

impl TryFrom<String> for VirtualNode {
    type Error = Error;
    fn try_from(s: String) -> Result<Self> {
        let encoded_message = s.encode()?;
        encoded_message.try_into()
    }
}

impl VirtualNode {
    /// concat data of a virtual Node
    /// We do not needs to check the type of VNode because two VNode with same address but
    /// has different Type is incapable
    pub fn concat(a: &Self, b: &Self) -> Result<Self> {
        match &a.kind {
            VNodeType::RelayMessage => {
                if a.address != b.address {
                    Err(Error::AddressNotEqual)
                } else {
                    Ok(Self {
                        address: a.address,
                        data: [&a.data[..], &b.data[..]].concat(),
                        kind: a.kind.clone(),
                    })
                }
            }
            VNodeType::Data => Ok(a.clone()),
            VNodeType::SubRing => {
                // if subring exists, just join creator to new subring
                let decoded_a: String = a.data[0].decode()?;
                let decoded_b: String = a.data[0].decode()?;
                let mut subring_a: SubRing =
                    serde_json::from_str(&decoded_a).map_err(Error::Deserialize)?;
                let subring_b: SubRing =
                    serde_json::from_str(&decoded_b).map_err(Error::Deserialize)?;
                subring_a.finger.join(subring_b.creator);
                subring_a.try_into()
            }
        }
    }
}