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
// Copyright 2020 Netwarps Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io;
use std::io::{Read, Write};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use crate::{PeerId, PublicKey};
use libp2prs_multiaddr::Multiaddr;

pub const ADDRESS_TTL: Duration = Duration::from_secs(60 * 60);
pub const TEMP_ADDR_TTL: Duration = Duration::from_secs(2 * 60);
pub const PROVIDER_ADDR_TTL: Duration = Duration::from_secs(10 * 60);
pub const RECENTLY_CONNECTED_ADDR_TTL: Duration = Duration::from_secs(10 * 60);
pub const OWN_OBSERVED_ADDR_TTL: Duration = Duration::from_secs(10 * 60);

pub const PERMANENT_ADDR_TTL: Duration = Duration::from_secs(u64::MAX - 1);
pub const CONNECTED_ADDR_TTL: Duration = Duration::from_secs(u64::MAX - 2);

#[derive(Default, Clone)]
pub struct PeerStore {
    inner: Arc<Mutex<HashMap<PeerId, PeerRecord>>>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PeerSaved {
    addr: Multiaddr,
    ttl: Duration,
}

/// The PeerInfo represents a remote peer and its elements.
#[derive(Clone)]
struct PeerRecord {
    /// Indicates if this record is currently pinned in peer store.
    ///
    /// PeerStore GC will not recycle a pinned record.
    pinned: bool,
    /// The multiaddr owned by this peer.
    addrs: Vec<AddrBookRecord>,
    /// The public key of the peer.
    key: Option<PublicKey>,
    /// The protocols supported by the peer.
    protos: HashSet<String>,
}

impl PeerRecord {
    fn new(addrs: Vec<AddrBookRecord>, key: Option<PublicKey>, protos: HashSet<String>) -> Self {
        Self {
            pinned: false,
            addrs,
            key,
            protos,
        }
    }
}

#[derive(Clone, Debug)]
struct AddrBookRecord {
    addr: Multiaddr,
    ttl: Duration,
    expiry: Instant,
}

impl Into<Multiaddr> for AddrBookRecord {
    fn into(self) -> Multiaddr {
        self.addr
    }
}

impl PeerStore {
    /// Save addr_book when closing swarm
    pub fn save_data(&self) -> io::Result<()> {
        let mut ds_addr_book = HashMap::new();

        {
            let guard = self.inner.lock().unwrap();
            // Transfer peer_id to String and insert into a new HashMap
            for (peer_id, value) in guard.iter() {
                let key = peer_id.to_string();
                let mut v = Vec::new();
                // save address info
                for item in value.addrs.to_vec() {
                    v.push(PeerSaved {
                        addr: item.addr,
                        ttl: item.ttl,
                    })
                }
                ds_addr_book.insert(key, v);
            }
        }
        let json_addrbook = serde_json::to_string(&ds_addr_book)?;

        let mut file = File::create("./ds_addr_book.txt")?;
        file.write_all(json_addrbook.as_bytes())
    }

    /// Load addr_book when initializing swarm
    pub fn load_data(&self) -> io::Result<()> {
        let mut file = match File::open("./ds_addr_book.txt") {
            Ok(file) => file,
            Err(e) => {
                if e.kind() == io::ErrorKind::NotFound {
                    File::create("./ds_addr_book.txt")?
                } else {
                    return Err(e);
                }
            }
        };
        let metadata = file.metadata()?;
        let length = metadata.len() as usize;
        if length == 0 {
            return Ok(());
        }
        let mut buf = vec![0u8; length];

        // Read data from file and deserialize
        let _ = file.read_exact(buf.as_mut())?;
        let json_data: HashMap<String, Vec<PeerSaved>> =
            serde_json::from_slice(&buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.to_string()))?;

        // Iter and insert into hashmap
        let mut guard = self.inner.lock().unwrap();
        for (key, value) in json_data {
            let peer_id = PeerId::from_str(&key).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
            let mut v = Vec::new();
            for item in value {
                v.push(AddrBookRecord::new(item.addr, item.ttl));
            }
            guard.insert(peer_id, PeerRecord::new(v, None, Default::default()));
        }

        Ok(())
    }

    /// Gets all peer Ids in peer store.
    pub fn get_peers(&self) -> Vec<PeerId> {
        let guard = self.inner.lock().unwrap();
        guard.keys().cloned().collect()
    }

    /// Pins the peer Id so that GC wouldn't recycle the multiaddr of the peer.
    pub fn pin(&self, peer_id: &PeerId) {
        let mut guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get_mut(peer_id) {
            pr.pinned = true;
        }
    }

    /// Unpins the peer Id.
    pub fn unpin(&self, peer_id: &PeerId) {
        let mut guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get_mut(peer_id) {
            pr.pinned = false;
        }
    }

    /// Checks if the peer is currently being pinned in peer store.
    pub fn pinned(&self, peer_id: &PeerId) -> bool {
        let guard = self.inner.lock().unwrap();
        guard.get(peer_id).map_or(false, |pr| pr.pinned)
    }

    /// Adds public key by peer_id.
    pub fn add_key(&self, peer_id: &PeerId, key: PublicKey) {
        let mut guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get_mut(peer_id) {
            pr.key = Some(key);
        }
    }

    /// Gets public key by peer_id.
    pub fn get_key(&self, peer_id: &PeerId) -> Option<PublicKey> {
        let guard = self.inner.lock().unwrap();
        guard.get(peer_id).and_then(|pr| pr.key.clone())
    }

    /// Add address to address_book by peer_id, if exists, update rtt.
    pub fn add_addr(&self, peer_id: &PeerId, addr: Multiaddr, ttl: Duration) {
        self.add_addrs(peer_id, vec![addr], ttl)
    }

    /// Adds many new addresses if they're not already in the Address Book.
    pub fn add_addrs(&self, peer_id: &PeerId, addrs: Vec<Multiaddr>, ttl: Duration) {
        let mut guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get_mut(peer_id) {
            for addr in addrs {
                if let Some(record) = pr.addrs.iter_mut().find(|item| item.addr == addr) {
                    // addr exists, update ttl & expiry
                    record.set_ttl(ttl);
                } else {
                    pr.addrs.push(AddrBookRecord::new(addr, ttl));
                }
            }
        } else {
            // Peer_id non-exists, create a new PeerRecord and fill with a new AddrBookRecord.
            let vec = addrs.into_iter().map(|addr| AddrBookRecord::new(addr, ttl)).collect();
            guard.insert(*peer_id, PeerRecord::new(vec, None, Default::default()));
        }
    }

    /// Removes all multiaddr of a peer from peer store.
    pub fn clear_addrs(&self, peer_id: &PeerId) {
        let mut guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get_mut(peer_id) {
            pr.addrs.clear();
        }
    }

    /// Retrieves the all multiaddr of a peer from the peer store.
    pub fn get_addrs(&self, peer_id: &PeerId) -> Option<Vec<Multiaddr>> {
        let guard = self.inner.lock().unwrap();
        guard.get(peer_id).map(|pr| pr.addrs.iter().map(|a| a.clone().into()).collect())
    }

    /// Updates the ttl of the multiaddr of the peer.
    pub fn update_addr(&self, peer_id: &PeerId, new_ttl: Duration) {
        let mut guard = self.inner.lock().unwrap();

        if let Some(pr) = guard.get_mut(peer_id) {
            for record in pr.addrs.iter_mut() {
                record.set_ttl(new_ttl);
            }
        }
    }

    /// Removes all expired address.
    pub fn remove_expired_addrs(&self) {
        let mut to_remove = vec![];
        let mut guard = self.inner.lock().unwrap();
        for (peer, pr) in guard.iter_mut() {
            if !pr.pinned {
                log::debug!("GC attempt for {:?}", peer);
                pr.addrs.retain(|record| record.expiry.elapsed() < record.ttl);
                // delete this peer if no addr at all
                if pr.addrs.is_empty() {
                    log::debug!("remove {:?} from peerstore", peer);
                    to_remove.push(*peer);
                }
            }
        }

        for peer in to_remove {
            guard.remove(&peer);
        }
    }

    /// Adds the supported protocols of a peer to the peer store.
    pub fn add_protocols(&self, peer_id: &PeerId, protos: Vec<String>) {
        let mut guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get_mut(peer_id) {
            pr.protos.extend(protos);
        } else {
            let mut s = HashSet::new();
            s.extend(protos);
            guard.insert(*peer_id, PeerRecord::new(Default::default(), None, s));
        }
    }

    /// Clears the protocols by peer_id
    pub fn clear_protocols(&self, peer_id: &PeerId) {
        let mut guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get_mut(peer_id) {
            pr.protos.clear();
        }
    }

    /// Gets the protocols by peer_id.
    pub fn get_protocols(&self, peer_id: &PeerId) -> Option<Vec<String>> {
        let guard = self.inner.lock().unwrap();
        guard.get(peer_id).map(|pr| pr.protos.iter().cloned().collect())
    }

    /// Get the first protocol which is matched by the given protocols.
    pub fn first_supported_protocol(&self, peer_id: &PeerId, protos: Vec<String>) -> Option<String> {
        let guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get(peer_id) {
            for proto in protos {
                if pr.protos.contains(&proto) {
                    return Some(proto);
                }
            }
        }
        None
    }

    /// Searches all protocols and return an option that matches by the given protocols.
    pub fn support_protocols(&self, peer_id: &PeerId, protos: Vec<String>) -> Option<Vec<String>> {
        let guard = self.inner.lock().unwrap();
        if let Some(pr) = guard.get(peer_id) {
            let mut proto_list = Vec::with_capacity(protos.len());
            for item in protos {
                if pr.protos.contains(&item) {
                    proto_list.push(item)
                }
            }
            Some(proto_list)
        } else {
            None
        }
    }
}

#[allow(dead_code)]
impl AddrBookRecord {
    pub fn new(addr: Multiaddr, ttl: Duration) -> Self {
        Self {
            addr,
            ttl,
            expiry: Instant::now(),
        }
    }
    /// Get the multiaddr.
    pub fn get_addr(&self) -> &Multiaddr {
        &self.addr
    }

    /// Set the time-to-live. It would also reset the 'expiry'.
    pub fn set_ttl(&mut self, ttl: Duration) {
        self.ttl = ttl;
        self.expiry = Instant::now();
    }
}

#[cfg(test)]
mod tests {
    use crate::identity::Keypair;
    use crate::peerstore::{PeerStore, ADDRESS_TTL};
    use crate::PeerId;
    use libp2prs_multiaddr::Multiaddr;
    use std::time::Duration;

    #[test]
    fn addr_basic() {
        let keypair = Keypair::generate_secp256k1();
        let peer_id = PeerId::from_public_key(keypair.public());

        let peerstore = PeerStore::default();

        peerstore.add_key(&peer_id, keypair.public());
        peerstore.add_addr(&peer_id, "/memory/123456".parse().unwrap(), Duration::from_secs(1));

        assert_eq!(
            peerstore.get_addrs(&peer_id).unwrap().first().unwrap(),
            &"/memory/123456".parse::<Multiaddr>().unwrap()
        );

        peerstore.add_addr(&peer_id, "/memory/654321".parse().unwrap(), Duration::from_secs(1));
        let addrs = peerstore.get_addrs(&peer_id).unwrap();
        assert_eq!(addrs.len(), 2);

        peerstore.add_addr(&peer_id, "/memory/654321".parse().unwrap(), Duration::from_secs(1));
        let addrs = peerstore.get_addrs(&peer_id).unwrap();
        assert_eq!(addrs.len(), 2);

        peerstore.clear_addrs(&peer_id);
        assert_eq!(peerstore.get_addrs(&peer_id).unwrap().len(), 0);
    }

    #[test]
    fn proto_basic() {
        let keypair = Keypair::generate_secp256k1();
        let peer_id = PeerId::from_public_key(keypair.public());

        let peerstore = PeerStore::default();

        let proto_list = vec!["/libp2p/secio/1.0.0".to_string(), "/libp2p/yamux/1.0.0".to_string()];

        peerstore.add_key(&peer_id, keypair.public());
        peerstore.add_protocols(&peer_id, proto_list.clone());

        let p = peerstore.get_protocols(&peer_id).unwrap();
        // let p = peerstore.get_protocol(&peer_id).unwrap();

        for i in proto_list {
            if p.contains(&i) {
                continue;
            } else {
                unreachable!()
            }
        }

        let optional_list = vec!["/libp2p/noise/1.0.0".to_string(), "/libp2p/yamux/1.0.0".to_string()];
        let protocol = peerstore.first_supported_protocol(&peer_id, optional_list);
        assert_eq!(protocol.unwrap(), "/libp2p/yamux/1.0.0");

        let option_support_list = vec![
            "/libp2p/secio/1.0.0".to_string(),
            "/libp2p/noise/1.0.0".to_string(),
            "/libp2p/yamux/1.0.0".to_string(),
        ];
        let support_protocol = peerstore.support_protocols(&peer_id, option_support_list);
        assert_eq!(
            support_protocol.unwrap(),
            vec!["/libp2p/secio/1.0.0".to_string(), "/libp2p/yamux/1.0.0".to_string()]
        );
    }

    #[test]
    fn peerstore_basic() {
        let keypair = Keypair::generate_secp256k1();
        let peer_id = PeerId::from_public_key(keypair.public());

        let addrs = vec!["/memory/123456".parse().unwrap(), "/memory/123456".parse().unwrap()];
        let protos = vec!["/libp2p/secio/1.0.0".to_string(), "/libp2p/yamux/1.0.0".to_string()];

        let ps = PeerStore::default();
        ps.add_key(&peer_id, keypair.public());
        ps.add_addrs(&peer_id, addrs, ADDRESS_TTL);
        ps.add_protocols(&peer_id, protos);

        let optional_list = vec!["/libp2p/noise/1.0.0".to_string(), "/libp2p/yamux/1.0.0".to_string()];
        let protocol = ps.first_supported_protocol(&peer_id, optional_list);
        assert_eq!(protocol.unwrap(), "/libp2p/yamux/1.0.0");

        let option_support_list = vec![
            "/libp2p/secio/1.0.0".to_string(),
            "/libp2p/noise/1.0.0".to_string(),
            "/libp2p/yamux/1.0.0".to_string(),
        ];
        let support_protocol = ps.support_protocols(&peer_id, option_support_list);
        assert_eq!(
            support_protocol.unwrap(),
            vec!["/libp2p/secio/1.0.0".to_string(), "/libp2p/yamux/1.0.0".to_string()]
        );
    }

    #[test]
    fn peerstore_gc() {
        let peer_id = PeerId::random();
        let addrs = vec!["/memory/123456".parse().unwrap()];

        let ps = PeerStore::default();
        ps.add_addrs(&peer_id, addrs, Duration::from_secs(5));
        ps.pin(&peer_id);
        assert!(ps.get_addrs(&peer_id).is_some());

        std::thread::sleep(Duration::from_secs(5));
        ps.remove_expired_addrs();
        assert!(ps.get_addrs(&peer_id).is_some());

        ps.unpin(&peer_id);
        ps.remove_expired_addrs();
        assert!(ps.get_addrs(&peer_id).is_none());
    }
}