ckb_network/peer_store/
mod.rs

1//! Peer store manager
2//!
3//! This module implements a locally managed node information set, which is used for
4//! booting into the network when the node is started, real-time update detection/timing
5//! saving at runtime, and saving data when stopping
6//!
7//! The validity and screening speed of the data set are very important to the entire network,
8//! and the address information collected on the network cannot be blindly trusted
9
10pub mod addr_manager;
11pub mod ban_list;
12#[cfg(target_family = "wasm")]
13pub(crate) mod browser;
14mod peer_store_db;
15mod peer_store_impl;
16pub mod types;
17
18pub(crate) use crate::Behaviour;
19pub use crate::SessionType;
20use p2p::multiaddr::{Multiaddr, Protocol};
21pub use peer_store_impl::PeerStore;
22pub(crate) use peer_store_impl::required_flags_filter;
23
24/// peer store evict peers after reach this limitation
25pub(crate) const ADDR_COUNT_LIMIT: usize = 16384;
26/// Consider we never seen a peer if peer's last_connected_at beyond this timeout
27const ADDR_TIMEOUT_MS: u64 = 7 * 24 * 3600 * 1000;
28/// The timeout that peer's address should be added to the feeler list again
29pub(crate) const ADDR_TRY_TIMEOUT_MS: u64 = 3 * 24 * 3600 * 1000;
30/// When obtaining the list of selectable nodes for identify,
31/// the node that has just been disconnected needs to be excluded
32pub(crate) const DIAL_INTERVAL: u64 = 15 * 1000;
33const ADDR_MAX_RETRIES: u32 = 3;
34const ADDR_MAX_FAILURES: u32 = 10;
35
36/// Alias score
37pub type Score = i32;
38
39/// PeerStore Scoring configuration
40#[derive(Copy, Clone, Debug)]
41pub struct PeerScoreConfig {
42    /// Default score
43    pub default_score: Score,
44    /// Ban score
45    pub ban_score: Score,
46    /// Ban time
47    pub ban_timeout_ms: u64,
48}
49
50impl Default for PeerScoreConfig {
51    fn default() -> Self {
52        PeerScoreConfig {
53            default_score: 100,
54            ban_score: 40,
55            ban_timeout_ms: 24 * 3600 * 1000, // 1 day
56        }
57    }
58}
59
60/// Peer Status
61#[derive(Debug, Copy, Clone, Eq, PartialEq)]
62pub enum Status {
63    /// Connected
64    Connected,
65    /// The peer is disconnected
66    Disconnected,
67}
68
69/// Report result
70#[derive(Debug, Copy, Clone, Eq, PartialEq)]
71pub enum ReportResult {
72    /// Ok
73    Ok,
74    /// The peer is banned
75    Banned,
76}
77
78impl ReportResult {
79    /// Whether ban
80    pub fn is_banned(self) -> bool {
81        self == ReportResult::Banned
82    }
83
84    /// Whether ok
85    pub fn is_ok(self) -> bool {
86        self == ReportResult::Ok
87    }
88}
89
90// Remove unnecessary protocols
91pub(crate) fn base_addr(addr: &Multiaddr) -> Multiaddr {
92    addr.iter()
93        .filter_map(|p| {
94            if matches!(
95                p,
96                Protocol::Ws
97                    | Protocol::Wss
98                    | Protocol::Memory(_)
99                    | Protocol::Tls(_)
100                    | Protocol::Onion3(_)
101            ) {
102                None
103            } else {
104                Some(p)
105            }
106        })
107        .collect()
108}