protocols/kad: Support multiple protocol names (#2846)

Add support for multiple Kademlia protocol names to allow
protocol name upgrades.
This commit is contained in:
Dmitry Markin
2022-09-03 03:46:50 +03:00
committed by GitHub
parent 89f898c69f
commit cee199afca
3 changed files with 39 additions and 12 deletions

View File

@ -142,21 +142,28 @@ impl From<KadPeer> for proto::message::Peer {
// `OutboundUpgrade` to be just a single message
#[derive(Debug, Clone)]
pub struct KademliaProtocolConfig {
protocol_name: Cow<'static, [u8]>,
protocol_names: Vec<Cow<'static, [u8]>>,
/// Maximum allowed size of a packet.
max_packet_size: usize,
}
impl KademliaProtocolConfig {
/// Returns the configured protocol name.
pub fn protocol_name(&self) -> &[u8] {
&self.protocol_name
pub fn protocol_names(&self) -> &[Cow<'static, [u8]>] {
&self.protocol_names
}
/// Modifies the protocol name used on the wire. Can be used to create incompatibilities
/// Modifies the protocol names used on the wire. Can be used to create incompatibilities
/// between networks on purpose.
pub fn set_protocol_names(&mut self, names: Vec<Cow<'static, [u8]>>) {
self.protocol_names = names;
}
/// Sets single protocol name used on the wire. Can be used to create incompatibilities
/// between networks on purpose.
#[deprecated(since = "0.40.0", note = "use `set_protocol_names()` instead")]
pub fn set_protocol_name(&mut self, name: impl Into<Cow<'static, [u8]>>) {
self.protocol_name = name.into();
self.set_protocol_names(std::iter::once(name.into()).collect());
}
/// Modifies the maximum allowed size of a single Kademlia packet.
@ -168,7 +175,7 @@ impl KademliaProtocolConfig {
impl Default for KademliaProtocolConfig {
fn default() -> Self {
KademliaProtocolConfig {
protocol_name: Cow::Borrowed(DEFAULT_PROTO_NAME),
protocol_names: iter::once(Cow::Borrowed(DEFAULT_PROTO_NAME)).collect(),
max_packet_size: DEFAULT_MAX_PACKET_SIZE,
}
}
@ -176,10 +183,10 @@ impl Default for KademliaProtocolConfig {
impl UpgradeInfo for KademliaProtocolConfig {
type Info = Cow<'static, [u8]>;
type InfoIter = iter::Once<Self::Info>;
type InfoIter = std::vec::IntoIter<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {
iter::once(self.protocol_name.clone())
self.protocol_names.clone().into_iter()
}
}