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

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

use super::subring::Subring;
use crate::consts::VNODE_DATA_MAX_LEN;
use crate::dht::Did;
use crate::ecc::HashStr;
use crate::error::Error;
use crate::error::Result;
use crate::message::Encoded;
use crate::message::Encoder;
use crate::message::MessagePayload;
use crate::message::MessageVerificationExt;

/// VNode Types
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum VNodeType {
    /// Encoded data stored in DHT
    Data,
    /// Finger table of a Subring
    Subring,
    /// A relayed but unreached message, which should be stored on
    /// the successor of the destination Did.
    RelayMessage,
}

/// VNode Operations
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum VNodeOperation {
    /// Create or update a VirtualNode
    Overwrite(VirtualNode),
    /// Extend data to a Data type VirtualNode.
    /// This operation will create VirtualNode if it's not existed.
    Extend(VirtualNode),
    /// Extend data to a Data type VirtualNode uniquely.
    /// If any element is already existed, move it to the end of the data vector.
    /// This operation will create VirtualNode if it's not existed.
    Touch(VirtualNode),
    /// Join subring.
    JoinSubring(String, Did),
}

/// A `VirtualNode` is a piece of data with [VNodeType] and [Did]. You can save it to
/// [PeerRing](super::PeerRing) by [ChordStorage](super::ChordStorage) protocol.
///
/// The Did of a Virtual Node is in the following format:
/// * If type value is [VNodeType::Data], it's sha1 of data topic.
/// * If type value is [VNodeType::Subring], it's sha1 of Subring name.
/// * If type value is [VNodeType::RelayMessage], it's the destination Did of
/// message plus 1 (to ensure that the message is sent to the successor of destination),
/// thus while destination node going online, it will sync message from its successor.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct VirtualNode {
    /// The did of `VirtualNode` make it unique, and can be stored and retrieved on DHT.
    pub did: Did,
    /// The data entity of `VirtualNode`, encoded by [Encoder].
    pub data: Vec<Encoded>,
    /// The type indicates how the data is encoded and how the Did is generated.
    pub kind: VNodeType,
}

impl VirtualNode {
    /// Generate did from topic.
    pub fn gen_did(topic: &str) -> Result<Did> {
        let hash: HashStr = topic.into();
        let did = Did::from_str(&hash.inner());
        tracing::debug!("gen_did: topic: {}, did: {:?}", topic, did);
        did
    }
}

impl VNodeOperation {
    /// Extract the did of target VirtualNode.
    pub fn did(&self) -> Result<Did> {
        Ok(match self {
            VNodeOperation::Overwrite(vnode) => vnode.did,
            VNodeOperation::Extend(vnode) => vnode.did,
            VNodeOperation::Touch(vnode) => vnode.did,
            VNodeOperation::JoinSubring(name, _) => VirtualNode::gen_did(name)?,
        })
    }

    /// Extract the kind of target VirtualNode.
    pub fn kind(&self) -> VNodeType {
        match self {
            VNodeOperation::Overwrite(vnode) => vnode.kind,
            VNodeOperation::Extend(vnode) => vnode.kind,
            VNodeOperation::Touch(vnode) => vnode.kind,
            VNodeOperation::JoinSubring(..) => VNodeType::Subring,
        }
    }

    /// Generate a target VirtualNode when it is not existed.
    pub fn gen_default_vnode(self) -> Result<VirtualNode> {
        match self {
            VNodeOperation::JoinSubring(name, did) => Subring::new(&name, did)?.try_into(),
            _ => Ok(VirtualNode {
                did: self.did()?,
                data: vec![],
                kind: self.kind(),
            }),
        }
    }
}

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

impl TryFrom<(String, Encoded)> for VirtualNode {
    type Error = Error;
    fn try_from((topic, e): (String, Encoded)) -> Result<Self> {
        Ok(Self {
            did: Self::gen_did(&topic)?,
            data: vec![e],
            kind: VNodeType::Data,
        })
    }
}

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

impl TryFrom<String> for VirtualNode {
    type Error = Error;
    fn try_from(topic: String) -> Result<Self> {
        (topic.clone(), topic).try_into()
    }
}

impl VirtualNode {
    /// Affine Transport vnode to a list of affined did
    pub fn affine(&self, scalar: u16) -> Vec<VirtualNode> {
        self.did
            .rotate_affine(scalar)
            .iter()
            .map(|did| self.clone_with_did(did.to_owned()))
            .collect()
    }

    /// Clone and setup with new DID
    pub fn clone_with_did(&self, did: Did) -> Self {
        let mut vnode = self.clone();
        vnode.did = did;
        vnode
    }

    /// The entry point of [VNodeOperation].
    /// Will dispatch to different operation handlers according to the variant.
    pub fn operate(&self, op: VNodeOperation) -> Result<Self> {
        match op {
            VNodeOperation::Overwrite(vnode) => self.overwrite(vnode),
            VNodeOperation::Extend(vnode) => self.extend(vnode),
            VNodeOperation::Touch(vnode) => self.touch(vnode),
            VNodeOperation::JoinSubring(_, did) => self.join_subring(did),
        }
    }

    /// Overwrite current data with new data.
    /// The handler of [VNodeOperation::Overwrite].
    pub fn overwrite(&self, other: Self) -> Result<Self> {
        if self.kind != VNodeType::Data {
            return Err(Error::VNodeNotOverwritable);
        }
        if self.kind != other.kind {
            return Err(Error::VNodeKindNotEqual);
        }
        if self.did != other.did {
            return Err(Error::VNodeDidNotEqual);
        }
        Ok(other)
    }

    /// This method is used to extend data to a Data type VirtualNode.
    /// The handler of [VNodeOperation::Extend].
    pub fn extend(&self, other: Self) -> Result<Self> {
        if self.kind != VNodeType::Data {
            return Err(Error::VNodeNotAppendable);
        }
        if self.kind != other.kind {
            return Err(Error::VNodeKindNotEqual);
        }
        if self.did != other.did {
            return Err(Error::VNodeDidNotEqual);
        }

        let trim_num = max(
            0,
            (self.data.len() + other.data.len()) as i64 - VNODE_DATA_MAX_LEN as i64,
        ) as usize;

        let mut data = self.data.iter().skip(trim_num).cloned().collect::<Vec<_>>();
        data.extend_from_slice(&other.data);

        Ok(Self {
            did: self.did,
            data,
            kind: self.kind,
        })
    }

    /// This method is used to extend data to a Data type VirtualNode uniquely.
    /// If any element is already existed, move it to the end of the data vector.
    /// The handler of [VNodeOperation::Touch].
    pub fn touch(&self, other: Self) -> Result<Self> {
        if self.kind != VNodeType::Data {
            return Err(Error::VNodeNotAppendable);
        }
        if self.kind != other.kind {
            return Err(Error::VNodeKindNotEqual);
        }
        if self.did != other.did {
            return Err(Error::VNodeDidNotEqual);
        }

        let remains = self
            .data
            .iter()
            .filter(|e| !other.data.contains(e))
            .collect::<Vec<_>>();

        let trim_num = max(
            0,
            (remains.len() + other.data.len()) as i64 - VNODE_DATA_MAX_LEN as i64,
        ) as usize;

        let mut data = remains
            .into_iter()
            .skip(trim_num)
            .cloned()
            .collect::<Vec<_>>();
        data.extend_from_slice(&other.data);

        Ok(Self {
            did: self.did,
            data,
            kind: self.kind,
        })
    }

    /// This method is used to join a subring.
    /// The handler of [VNodeOperation::JoinSubring].
    pub fn join_subring(&self, did: Did) -> Result<Self> {
        if self.kind != VNodeType::Subring {
            return Err(Error::VNodeNotJoinable);
        }

        let mut subring: Subring = self.clone().try_into()?;
        subring.finger.join(did);
        subring.try_into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_vnode_extend_over_max_len() {
        let topic = "test0".to_string();
        let mut vnode: VirtualNode = topic.clone().try_into().unwrap();
        assert_eq!(vnode.data.len(), 1);

        for i in 1..VNODE_DATA_MAX_LEN {
            let topic = topic.clone();
            let data = format!("test{}", i);

            let other = (topic, data).try_into().unwrap();
            vnode = vnode.extend(other).unwrap();

            assert_eq!(vnode.data.len(), i + 1);
        }

        for i in VNODE_DATA_MAX_LEN..VNODE_DATA_MAX_LEN + 10 {
            let topic = topic.clone();
            let data = format!("test{}", i);

            let other = (topic, data.clone()).try_into().unwrap();
            vnode = vnode.extend(other).unwrap();

            // The length should be trimmed to max length.
            assert_eq!(vnode.data.len(), VNODE_DATA_MAX_LEN);

            // The first data should be trimmed.
            assert_eq!(
                vnode.data[0].decode::<String>().unwrap(),
                format!("test{}", i - VNODE_DATA_MAX_LEN + 1)
            );

            // The last data should be the latest one.
            assert_eq!(
                vnode.data[VNODE_DATA_MAX_LEN - 1]
                    .decode::<String>()
                    .unwrap(),
                data
            );
        }
    }
}