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