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 /// [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0004-ckb-block-sync/0004-ckb-block-sync.md#new-block-announcement)
45 RelayV3,
46 /// Time: A protocol used for node pairing that warns if there is a large gap between the local time and the remote node.
47 Time,
48 /// Alert: A protocol reserved by the Nervos Foundation to publish network-wide announcements.
49 /// Any information sent from the protocol is verified by multi-signature
50 Alert,
51 /// LightClient: A protocol used for light client.
52 LightClient,
53 /// Filter: A protocol used for client side block data filtering.
54 Filter,
55 /// HolePunching: A protocol used to connect peers behind firewalls or NAT routers.
56 HolePunching,
57}
58
59impl SupportProtocols {
60 /// Protocol id
61 pub fn protocol_id(&self) -> ProtocolId {
62 match self {
63 SupportProtocols::Ping => 0,
64 SupportProtocols::Discovery => 1,
65 SupportProtocols::Identify => 2,
66 SupportProtocols::Feeler => 3,
67 SupportProtocols::DisconnectMessage => 4,
68 SupportProtocols::Sync => 100,
69 SupportProtocols::RelayV3 => 101,
70 SupportProtocols::Time => 102,
71 SupportProtocols::Alert => 110,
72 SupportProtocols::LightClient => 120,
73 SupportProtocols::Filter => 121,
74 SupportProtocols::HolePunching => 130,
75 }
76 .into()
77 }
78
79 /// Protocol name
80 pub fn name(&self) -> String {
81 match self {
82 SupportProtocols::Ping => "/ckb/ping",
83 SupportProtocols::Discovery => "/ckb/discovery",
84 SupportProtocols::Identify => "/ckb/identify",
85 SupportProtocols::Feeler => "/ckb/flr",
86 SupportProtocols::DisconnectMessage => "/ckb/disconnectmsg",
87 SupportProtocols::Sync => "/ckb/syn",
88 SupportProtocols::RelayV3 => "/ckb/relay3",
89 SupportProtocols::Time => "/ckb/tim",
90 SupportProtocols::Alert => "/ckb/alt",
91 SupportProtocols::LightClient => "/ckb/lightclient",
92 SupportProtocols::Filter => "/ckb/filter",
93 SupportProtocols::HolePunching => "/ckb/holepunching",
94 }
95 .to_owned()
96 }
97
98 /// Support versions
99 pub fn support_versions(&self) -> Vec<String> {
100 // Here you have to make sure that the list of supported versions is sorted from smallest to largest
101 match self {
102 SupportProtocols::Ping => vec![LASTEST_VERSION.to_owned()],
103 SupportProtocols::Discovery => {
104 vec![LASTEST_VERSION.to_owned()]
105 }
106 SupportProtocols::Identify => vec![LASTEST_VERSION.to_owned()],
107 SupportProtocols::Feeler => vec![LASTEST_VERSION.to_owned()],
108 SupportProtocols::DisconnectMessage => {
109 vec![LASTEST_VERSION.to_owned()]
110 }
111 SupportProtocols::Sync => vec![LASTEST_VERSION.to_owned()],
112 SupportProtocols::Time => vec![LASTEST_VERSION.to_owned()],
113 SupportProtocols::Alert => vec![LASTEST_VERSION.to_owned()],
114 SupportProtocols::RelayV3 => vec![LASTEST_VERSION.to_owned()],
115 SupportProtocols::LightClient => vec![LASTEST_VERSION.to_owned()],
116 SupportProtocols::Filter => vec![LASTEST_VERSION.to_owned()],
117 SupportProtocols::HolePunching => vec![LASTEST_VERSION.to_owned()],
118 }
119 }
120
121 /// Protocol message max length
122 pub fn max_frame_length(&self) -> usize {
123 match self {
124 SupportProtocols::Ping => 1024, // 1 KB
125 SupportProtocols::Discovery => 512 * 1024, // 512 KB
126 SupportProtocols::Identify => 2 * 1024, // 2 KB
127 SupportProtocols::Feeler => 1024, // 1 KB
128 SupportProtocols::DisconnectMessage => 1024, // 1 KB
129 SupportProtocols::Sync => 2 * 1024 * 1024, // 2 MB
130 SupportProtocols::RelayV3 => 4 * 1024 * 1024, // 4 MB
131 SupportProtocols::Time => 1024, // 1 KB
132 SupportProtocols::Alert => 128 * 1024, // 128 KB
133 SupportProtocols::LightClient => 2 * 1024 * 1024, // 2 MB
134 SupportProtocols::Filter => 2 * 1024 * 1024, // 2 MB
135 SupportProtocols::HolePunching => 512 * 1024, // 512 KB
136 }
137 }
138
139 /// Builder with service handle
140 // a helper fn to build `ProtocolMeta`
141 pub fn build_meta_with_service_handle<
142 SH: FnOnce() -> ProtocolHandle<Box<dyn ServiceProtocol + Send + 'static + Unpin>>,
143 >(
144 self,
145 service_handle: SH,
146 ) -> ProtocolMeta {
147 let meta_builder: MetaBuilder = self.into();
148 meta_builder.service_handle(service_handle).build()
149 }
150}
151
152impl From<SupportProtocols> for MetaBuilder {
153 fn from(p: SupportProtocols) -> Self {
154 let max_frame_length = p.max_frame_length();
155 MetaBuilder::default()
156 .id(p.protocol_id())
157 .support_versions(p.support_versions())
158 .name(move |_| p.name())
159 .codec(move || {
160 Box::new(
161 length_delimited::Builder::new()
162 .max_frame_length(max_frame_length)
163 .new_codec(),
164 )
165 })
166 }
167}