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
use async_trait::async_trait;

use crate::dht::vnode::VirtualNode;
use crate::dht::ChordStorage;
use crate::dht::Did;
use crate::dht::PeerRingAction;
use crate::dht::PeerRingRemoteAction;
use crate::err::Error;
use crate::err::Result;
use crate::message::types::FoundVNode;
use crate::message::types::Message;
use crate::message::types::SearchVNode;
use crate::message::types::StoreVNode;
use crate::message::types::SyncVNodeWithSuccessor;
use crate::message::HandleMsg;
use crate::message::MessageHandler;
use crate::message::MessagePayload;
use crate::message::PayloadSender;

/// TChordStorage should imply necessary method for DHT storage
#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
pub trait TChordStorage {
    /// check local cache of dht
    async fn check_cache(&self, id: &Did) -> Option<VirtualNode>;
    /// fetch virtual node from DHT
    async fn fetch(&self, id: &Did) -> Result<()>;
    /// store virtual node on DHT
    async fn store(&self, vnode: VirtualNode) -> Result<()>;
}

#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
impl TChordStorage for MessageHandler {
    /// Check local cache
    async fn check_cache(&self, id: &Did) -> Option<VirtualNode> {
        self.dht.fetch_cache(id)
    }

    /// Fetch virtual node, if exist in localstoreage, copy it to the cache,
    /// else Query Remote Node
    async fn fetch(&self, id: &Did) -> Result<()> {
        // If peer found that data is on it's localstore, copy it to the cache
        match self.dht.lookup(id).await? {
            PeerRingAction::SomeVNode(v) => {
                self.dht.cache(v);
                Ok(())
            }
            PeerRingAction::None => Ok(()),
            PeerRingAction::RemoteAction(next, _) => {
                self.send_direct_message(Message::SearchVNode(SearchVNode { id: *id }), next)
                    .await?;
                Ok(())
            }
            act => Err(Error::PeerRingUnexpectedAction(act)),
        }
    }

    /// Store VirtualNode, TryInto<VirtualNode> is implementated for alot of types
    async fn store(&self, vnode: VirtualNode) -> Result<()> {
        match self.dht.store(vnode).await? {
            PeerRingAction::None => Ok(()),
            PeerRingAction::RemoteAction(target, PeerRingRemoteAction::FindAndStore(vnode)) => {
                self.send_direct_message(
                    Message::StoreVNode(StoreVNode { data: vec![vnode] }),
                    target,
                )
                .await?;
                Ok(())
            }
            act => Err(Error::PeerRingUnexpectedAction(act)),
        }
    }
}

#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
impl HandleMsg<SearchVNode> for MessageHandler {
    /// Search VNode via successor
    /// If a VNode is storead local, it will response immediately.
    async fn handle(&self, ctx: &MessagePayload<Message>, msg: &SearchVNode) -> Result<()> {
        let mut relay = ctx.relay.clone();

        match self.dht.lookup(&msg.id).await {
            Ok(action) => match action {
                PeerRingAction::None => Ok(()),
                PeerRingAction::SomeVNode(v) => {
                    relay.relay(self.dht.id, None)?;
                    self.send_report_message(
                        Message::FoundVNode(FoundVNode { data: vec![v] }),
                        ctx.tx_id,
                        relay,
                    )
                    .await
                }
                PeerRingAction::RemoteAction(next, _) => {
                    relay.relay(self.dht.id, Some(next))?;
                    self.transpond_payload(ctx, relay).await
                }
                act => Err(Error::PeerRingUnexpectedAction(act)),
            },
            Err(e) => Err(e),
        }
    }
}

#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
impl HandleMsg<FoundVNode> for MessageHandler {
    async fn handle(&self, ctx: &MessagePayload<Message>, msg: &FoundVNode) -> Result<()> {
        let mut relay = ctx.relay.clone();

        relay.relay(self.dht.id, None)?;
        if relay.next_hop.is_some() {
            self.transpond_payload(ctx, relay).await
        } else {
            // When query successor, store in local cache
            for datum in msg.data.iter().cloned() {
                self.dht.cache(datum);
            }
            Ok(())
        }
    }
}

#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
impl HandleMsg<StoreVNode> for MessageHandler {
    async fn handle(&self, ctx: &MessagePayload<Message>, msg: &StoreVNode) -> Result<()> {
        let virtual_peer = msg.data.clone();
        for p in virtual_peer {
            match self.dht.store(p).await {
                Ok(action) => match action {
                    PeerRingAction::None => Ok(()),
                    PeerRingAction::RemoteAction(next, _) => {
                        let mut relay = ctx.relay.clone();
                        relay.reset_destination(next)?;
                        relay.relay(self.dht.id, Some(next))?;
                        self.transpond_payload(ctx, relay).await
                    }
                    act => Err(Error::PeerRingUnexpectedAction(act)),
                },
                Err(e) => Err(e),
            }?;
        }
        Ok(())
    }
}

#[cfg_attr(feature = "wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "wasm"), async_trait)]
impl HandleMsg<SyncVNodeWithSuccessor> for MessageHandler {
    // received remote sync vnode request
    async fn handle(
        &self,
        _ctx: &MessagePayload<Message>,
        msg: &SyncVNodeWithSuccessor,
    ) -> Result<()> {
        for data in msg.data.iter().cloned() {
            // only simply store here
            match self.dht.store(data).await {
                Ok(PeerRingAction::None) => Ok(()),
                Ok(PeerRingAction::RemoteAction(
                    next,
                    PeerRingRemoteAction::FindAndStore(peer),
                )) => {
                    self.send_direct_message(
                        Message::StoreVNode(StoreVNode { data: vec![peer] }),
                        next,
                    )
                    .await
                }
                Ok(_) => unreachable!(),
                Err(e) => Err(e),
            }?;
        }
        Ok(())
    }
}

#[cfg(not(feature = "wasm"))]
#[cfg(test)]
mod test {
    use std::sync::Arc;

    use super::*;
    use crate::dht::PeerRing;
    use crate::ecc::SecretKey;
    use crate::message::FindSuccessorThen;
    use crate::message::MessageHandler;
    use crate::prelude::RTCSdpType;
    use crate::session::SessionManager;
    use crate::storage::PersistenceStorage;
    use crate::storage::PersistenceStorageOperation;
    use crate::swarm::Swarm;
    use crate::swarm::TransportManager;
    use crate::types::ice_transport::IceTrickleScheme;

    #[tokio::test]
    async fn test_store_vnode() -> Result<()> {
        let stun = "stun://stun.l.google.com:19302";

        let mut key1 = SecretKey::random();
        let mut key2 = SecretKey::random();

        let mut v = vec![key1, key2];

        v.sort_by(|a, b| {
            if a.address() < b.address() {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Greater
            }
        });
        (key1, key2) = (v[0], v[1]);

        println!(
            "test with key1: {:?}, key2: {:?}",
            key1.address(),
            key2.address(),
        );

        let did1 = key1.address().into();
        let did2 = key2.address().into();

        let path1 = PersistenceStorage::random_path("./tmp");
        let path2 = PersistenceStorage::random_path("./tmp");

        let dht1 = Arc::new(PeerRing::new_with_storage(
            did1,
            Arc::new(
                PersistenceStorage::new_with_path(path1.as_str())
                    .await
                    .unwrap(),
            ),
        ));
        let dht2 = Arc::new(PeerRing::new_with_storage(
            did2,
            Arc::new(
                PersistenceStorage::new_with_path(path2.as_str())
                    .await
                    .unwrap(),
            ),
        ));

        let sm1 = SessionManager::new_with_seckey(&key1).unwrap();
        let sm2 = SessionManager::new_with_seckey(&key2).unwrap();

        let swarm1 = Arc::new(Swarm::new(stun, key1.address(), sm1.clone()));
        let swarm2 = Arc::new(Swarm::new(stun, key2.address(), sm2.clone()));

        let transport1 = swarm1.new_transport().await.unwrap();
        let transport2 = swarm2.new_transport().await.unwrap();

        let node1 = MessageHandler::new(Arc::clone(&dht1), Arc::clone(&swarm1));
        let node2 = MessageHandler::new(Arc::clone(&dht2), Arc::clone(&swarm2));

        // now we connect node1 and node2

        let handshake_info1 = transport1
            .get_handshake_info(&sm1, RTCSdpType::Offer)
            .await?;

        let addr1 = transport2.register_remote_info(handshake_info1).await?;

        let handshake_info2 = transport2
            .get_handshake_info(&sm2, RTCSdpType::Answer)
            .await?;

        let addr2 = transport1.register_remote_info(handshake_info2).await?;

        assert_eq!(addr1, key1.address());
        assert_eq!(addr2, key2.address());
        let promise_1 = transport1.connect_success_promise().await?;
        let promise_2 = transport2.connect_success_promise().await?;
        promise_1.await?;
        promise_2.await?;

        swarm1
            .register(&swarm2.address(), transport1.clone())
            .await
            .unwrap();
        swarm2
            .register(&swarm1.address(), transport2.clone())
            .await
            .unwrap();

        assert!(swarm1.get_transport(&key2.address()).is_some());
        assert!(swarm2.get_transport(&key1.address()).is_some());

        // JoinDHT
        let ev_1 = node1.listen_once().await.unwrap();
        if let Message::JoinDHT(x) = ev_1.data {
            assert_eq!(x.id, did2);
        } else {
            panic!();
        }

        let ev_2 = node2.listen_once().await.unwrap();
        if let Message::JoinDHT(x) = ev_2.data {
            assert_eq!(x.id, did1);
        } else {
            panic!();
        }
        let ev_1 = node1.listen_once().await.unwrap();
        if let Message::FindSuccessorSend(x) = ev_1.data {
            assert_eq!(x.id, did2);
            assert_eq!(x.then, FindSuccessorThen::Connect);
        } else {
            panic!();
        }
        let ev_2 = node2.listen_once().await.unwrap();
        if let Message::FindSuccessorSend(x) = ev_2.data {
            assert_eq!(x.id, did1);
            assert_eq!(x.then, FindSuccessorThen::Connect);
        } else {
            panic!();
        }
        let ev_1 = node1.listen_once().await.unwrap();
        if let Message::FindSuccessorReport(x) = ev_1.data {
            // for node2 there is no did is more closer to key1, so it response key1
            // and dht1 wont update
            assert!(!dht1.lock_successor()?.list().contains(&did1));
            assert_eq!(x.id, did1);
            assert_eq!(x.then, FindSuccessorThen::Connect);
        } else {
            panic!();
        }
        let ev_2 = node2.listen_once().await.unwrap();
        if let Message::FindSuccessorReport(x) = ev_2.data {
            // for key1 there is no did is more closer to key1, so it response key1
            // and dht2 wont update
            assert_eq!(x.id, did2);
            assert_eq!(x.then, FindSuccessorThen::Connect);
        } else {
            panic!();
        }

        // for now node1's successor is node 2, node2's successor is node 1
        // now we store adata on ndoe 2 and query it from node 1
        let data = "Across the Great Wall we can reach every corner in the world.".to_string();
        let vnode: VirtualNode = data.try_into().unwrap();
        let vid = vnode.did();
        assert!(dht1.cache.is_empty());
        assert!(dht2.cache.is_empty());
        assert!(node1.check_cache(&vid).await.is_none());
        assert!(node2.check_cache(&vid).await.is_none());

        // test remote store
        if vid.in_range(&did2, &did2, &did1) {
            node1.store(vnode.clone()).await.unwrap();
            // if vnode in range [node2, node1]
            // vnode should stored in node2
            let ev = node2.listen_once().await.unwrap();
            if let Message::StoreVNode(x) = ev.data {
                assert_eq!(x.data[0].did(), vid);
            } else {
                panic!();
            }
        } else {
            node2.store(vnode.clone()).await.unwrap();
            // if vnode in range [node2, node1]
            // vnode should stored in node1
            let ev = node1.listen_once().await.unwrap();
            if let Message::StoreVNode(x) = ev.data {
                assert_eq!(x.data[0].did(), vid);
            } else {
                panic!();
            }
        }
        assert!(node1.check_cache(&vid).await.is_none());
        assert!(node2.check_cache(&vid).await.is_none());
        if vid.in_range(&did2, &did2, &did1) {
            assert!(dht1.storage.count().await.unwrap() == 0);
            assert!(dht2.storage.count().await.unwrap() != 0);
        } else {
            assert!(dht1.storage.count().await.unwrap() != 0);
            assert!(dht2.storage.count().await.unwrap() == 0);
        }
        // test remote query
        if vid.in_range(&did2, &did2, &did1) {
            // vid is in node 2
            println!("vid is on node 2 {:?}", &did2);
            node1.fetch(&vid).await.unwrap();
            // it will send reqeust to node 2
            let ev = node2.listen_once().await.unwrap();
            // node 2 received search vnode request
            if let Message::SearchVNode(x) = ev.data {
                assert_eq!(x.id, vid);
            } else {
                panic!();
            }
            let ev = node1.listen_once().await.unwrap();
            if let Message::FoundVNode(x) = ev.data {
                assert_eq!(x.data[0].did(), vid);
            } else {
                panic!();
            }
            assert!(node1.check_cache(&vid).await.is_some());
        } else {
            // vid is in node 1
            println!("vid is on node 1 {:?}", &did1);
            node2.fetch(&vid).await.unwrap();
            let ev = node1.listen_once().await.unwrap();
            if let Message::SearchVNode(x) = ev.data {
                assert_eq!(x.id, vid);
            } else {
                panic!();
            }
            let ev = node2.listen_once().await.unwrap();
            if let Message::FoundVNode(x) = ev.data {
                assert_eq!(x.data[0].did(), vid);
            } else {
                panic!();
            }
            assert!(node2.check_cache(&vid).await.is_some());
        }

        tokio::fs::remove_dir_all("./tmp").await.ok();
        Ok(())
    }
}