ckb_network/protocols/
support_protocols.rs

1use crate::ProtocolId;
2use p2p::{
3    builder::MetaBuilder,
4    service::{ProtocolHandle, ProtocolMeta},
5    traits::ServiceProtocol,
6};
7use tokio_util::codec::length_delimited;
8
9pub const LASTEST_VERSION: &str = "3";
10
11/// All supported protocols
12///
13/// The underlying network of CKB is flexible and complex. The flexibility lies in that it can support any number of protocols.
14/// Therefore, it is also relatively complex. Now, CKB has a bunch of protocols open by default,
15/// but not all protocols have to be open. In other words, if you want to interact with ckb nodes at the p2p layer,
16/// you only need to implement a few core protocols.
17///
18/// Core protocol: identify/discovery/sync/relay
19#[derive(Clone, Debug)]
20pub enum SupportProtocols {
21    /// Ping: as a health check for ping/pong
22    Ping,
23    /// Discovery: used to communicate with any node with any known node address,
24    /// to build a robust network topology as much as possible.
25    Discovery,
26    /// Identify: the first protocol opened when the nodes are interconnected,
27    /// used to obtain the features, versions, and observation addresses supported by the other node.
28    ///
29    /// [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0012-node-discovery/0012-node-discovery.md)
30    Identify,
31    /// Feeler: used to detect whether the address is valid.
32    ///
33    /// [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0007-scoring-system-and-network-security/0007-scoring-system-and-network-security.md#feeler-connection)
34    /// [Eclipse Attacks on Bitcoin's Peer-to-Peer Network](https://cryptographylab.bitbucket.io/slides/Eclipse%20Attacks%20on%20Bitcoin%27s%20Peer-to-Peer%20Network.pdf)
35    Feeler,
36    /// Disconnect message: used to give the remote node a debug message when the node decides to disconnect.
37    /// This message must be as quick as possible, otherwise the message may not be sent. So, use a separate protocol to support it.
38    DisconnectMessage,
39    /// Sync: ckb's main communication protocol for synchronize all blocks.
40    ///
41    /// [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0004-ckb-block-sync/0004-ckb-block-sync.md)
42    Sync,
43    /// Relay: ckb's main communication protocol for synchronizing latest transactions and blocks.
44    ///
45    /// [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0004-ckb-block-sync/0004-ckb-block-sync.md#new-block-announcement)
46    RelayV2,
47    /// Relay: ckb's main communication protocol for synchronizing latest transactions and blocks.
48    /// [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0004-ckb-block-sync/0004-ckb-block-sync.md#new-block-announcement)
49    RelayV3,
50    /// Time: A protocol used for node pairing that warns if there is a large gap between the local time and the remote node.
51    Time,
52    /// Alert: A protocol reserved by the Nervos Foundation to publish network-wide announcements.
53    /// Any information sent from the protocol is verified by multi-signature
54    Alert,
55    /// LightClient: A protocol used for light client.
56    LightClient,
57    /// Filter: A protocol used for client side block data filtering.
58    Filter,
59    /// HolePunching: A protocol used to connect peers behind firewalls or NAT routers.
60    HolePunching,
61}
62
63impl SupportProtocols {
64    /// Protocol id
65    pub fn protocol_id(&self) -> ProtocolId {
66        match self {
67            SupportProtocols::Ping => 0,
68            SupportProtocols::Discovery => 1,
69            SupportProtocols::Identify => 2,
70            SupportProtocols::Feeler => 3,
71            SupportProtocols::DisconnectMessage => 4,
72            SupportProtocols::Sync => 100,
73            SupportProtocols::RelayV2 => 101,
74            SupportProtocols::RelayV3 => 103,
75            SupportProtocols::Time => 102,
76            SupportProtocols::Alert => 110,
77            SupportProtocols::LightClient => 120,
78            SupportProtocols::Filter => 121,
79            SupportProtocols::HolePunching => 130,
80        }
81        .into()
82    }
83
84    /// Protocol name
85    pub fn name(&self) -> String {
86        match self {
87            SupportProtocols::Ping => "/ckb/ping",
88            SupportProtocols::Discovery => "/ckb/discovery",
89            SupportProtocols::Identify => "/ckb/identify",
90            SupportProtocols::Feeler => "/ckb/flr",
91            SupportProtocols::DisconnectMessage => "/ckb/disconnectmsg",
92            SupportProtocols::Sync => "/ckb/syn",
93            SupportProtocols::RelayV2 => "/ckb/relay",
94            SupportProtocols::RelayV3 => "/ckb/relay3",
95            SupportProtocols::Time => "/ckb/tim",
96            SupportProtocols::Alert => "/ckb/alt",
97            SupportProtocols::LightClient => "/ckb/lightclient",
98            SupportProtocols::Filter => "/ckb/filter",
99            SupportProtocols::HolePunching => "/ckb/holepunching",
100        }
101        .to_owned()
102    }
103
104    /// Support versions
105    pub fn support_versions(&self) -> Vec<String> {
106        // Here you have to make sure that the list of supported versions is sorted from smallest to largest
107        match self {
108            SupportProtocols::Ping => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
109            SupportProtocols::Discovery => {
110                vec!["2".to_owned(), "2.1".to_owned(), LASTEST_VERSION.to_owned()]
111            }
112            SupportProtocols::Identify => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
113            SupportProtocols::Feeler => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
114            SupportProtocols::DisconnectMessage => {
115                vec!["2".to_owned(), LASTEST_VERSION.to_owned()]
116            }
117            SupportProtocols::Sync => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
118            SupportProtocols::Time => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
119            SupportProtocols::Alert => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
120            SupportProtocols::RelayV2 => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
121            SupportProtocols::RelayV3 => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
122            SupportProtocols::LightClient => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
123            SupportProtocols::Filter => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
124            SupportProtocols::HolePunching => vec!["2".to_owned(), LASTEST_VERSION.to_owned()],
125        }
126    }
127
128    /// Protocol message max length
129    pub fn max_frame_length(&self) -> usize {
130        match self {
131            SupportProtocols::Ping => 1024,              // 1   KB
132            SupportProtocols::Discovery => 512 * 1024,   // 512 KB
133            SupportProtocols::Identify => 2 * 1024,      // 2   KB
134            SupportProtocols::Feeler => 1024,            // 1   KB
135            SupportProtocols::DisconnectMessage => 1024, // 1   KB
136            SupportProtocols::Sync => 2 * 1024 * 1024,   // 2   MB
137            SupportProtocols::RelayV2 | SupportProtocols::RelayV3 => 4 * 1024 * 1024, // 4   MB
138            SupportProtocols::Time => 1024,              // 1   KB
139            SupportProtocols::Alert => 128 * 1024,       // 128 KB
140            SupportProtocols::LightClient => 2 * 1024 * 1024, // 2 MB
141            SupportProtocols::Filter => 2 * 1024 * 1024, // 2   MB
142            SupportProtocols::HolePunching => 512 * 1024, // 512 KB
143        }
144    }
145
146    /// Builder with service handle
147    // a helper fn to build `ProtocolMeta`
148    pub fn build_meta_with_service_handle<
149        SH: FnOnce() -> ProtocolHandle<Box<dyn ServiceProtocol + Send + 'static + Unpin>>,
150    >(
151        self,
152        service_handle: SH,
153    ) -> ProtocolMeta {
154        let meta_builder: MetaBuilder = self.into();
155        meta_builder.service_handle(service_handle).build()
156    }
157}
158
159impl From<SupportProtocols> for MetaBuilder {
160    fn from(p: SupportProtocols) -> Self {
161        let max_frame_length = p.max_frame_length();
162        MetaBuilder::default()
163            .id(p.protocol_id())
164            .support_versions(p.support_versions())
165            .name(move |_| p.name())
166            .codec(move || {
167                Box::new(
168                    length_delimited::Builder::new()
169                        .max_frame_length(max_frame_length)
170                        .new_codec(),
171                )
172            })
173    }
174}