kad: Export QueryId and add Kademlia::protocol_name. (#1469)

The `QueryId` type should be exported as it is used in the
`NetworkBehaviour::ProtocolsHandler` type of `Kademlia`.

`Kademlia::protocol_name` is added for convenience.
This commit is contained in:
Toralf Wittner
2020-02-25 11:59:38 +01:00
committed by GitHub
parent 688aa5bc79
commit 2ea459dcdc
3 changed files with 12 additions and 1 deletions

View File

@ -222,6 +222,13 @@ where
Self::with_config(id, store, Default::default()) Self::with_config(id, store, Default::default())
} }
/// Get the protocol name of this kademlia instance.
pub fn protocol_name(&self) -> &[u8] {
self.protocol_name_override
.as_ref()
.map_or(crate::protocol::DEFAULT_PROTO_NAME.as_ref(), AsRef::as_ref)
}
/// Creates a new `Kademlia` network behaviour with the given configuration. /// Creates a new `Kademlia` network behaviour with the given configuration.
pub fn with_config(id: PeerId, store: TStore, config: KademliaConfig) -> Self { pub fn with_config(id: PeerId, store: TStore, config: KademliaConfig) -> Self {
let local_key = kbucket::Key::new(id.clone()); let local_key = kbucket::Key::new(id.clone());

View File

@ -65,6 +65,7 @@ pub use behaviour::{
GetProvidersOk, GetProvidersOk,
GetProvidersError, GetProvidersError,
}; };
pub use query::QueryId;
pub use protocol::KadConnectionType; pub use protocol::KadConnectionType;
pub use record::{store, Record, ProviderRecord}; pub use record::{store, Record, ProviderRecord};

View File

@ -40,6 +40,9 @@ use std::{io, iter};
use unsigned_varint::codec; use unsigned_varint::codec;
use wasm_timer::Instant; use wasm_timer::Instant;
/// The protocol name used for negotiating with multistream-select.
pub const DEFAULT_PROTO_NAME: &[u8] = b"/ipfs/kad/1.0.0";
/// Status of our connection to a node reported by the Kademlia protocol. /// Status of our connection to a node reported by the Kademlia protocol.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum KadConnectionType { pub enum KadConnectionType {
@ -152,7 +155,7 @@ impl KademliaProtocolConfig {
impl Default for KademliaProtocolConfig { impl Default for KademliaProtocolConfig {
fn default() -> Self { fn default() -> Self {
KademliaProtocolConfig { KademliaProtocolConfig {
protocol_name: Cow::Borrowed(b"/ipfs/kad/1.0.0"), protocol_name: Cow::Borrowed(DEFAULT_PROTO_NAME)
} }
} }
} }