From ac2eec9b959a24ca2c294adc4cf8997947e6b08f Mon Sep 17 00:00:00 2001 From: Ruslan Tushov Date: Thu, 12 Oct 2023 16:33:24 +0300 Subject: [PATCH] kademlia multiple protocols (#219) Signed-off-by: turuslan --- include/libp2p/protocol/kademlia/common.hpp | 4 +--- include/libp2p/protocol/kademlia/config.hpp | 5 +++-- include/libp2p/protocol/kademlia/impl/kademlia_impl.hpp | 1 - src/protocol/kademlia/impl/add_provider_executor.cpp | 2 +- src/protocol/kademlia/impl/find_peer_executor.cpp | 2 +- src/protocol/kademlia/impl/find_providers_executor.cpp | 2 +- src/protocol/kademlia/impl/get_value_executor.cpp | 2 +- src/protocol/kademlia/impl/kademlia_impl.cpp | 3 +-- src/protocol/kademlia/impl/put_value_executor.cpp | 2 +- 9 files changed, 10 insertions(+), 13 deletions(-) diff --git a/include/libp2p/protocol/kademlia/common.hpp b/include/libp2p/protocol/kademlia/common.hpp index 8f28f9cfc..7f29bd3be 100644 --- a/include/libp2p/protocol/kademlia/common.hpp +++ b/include/libp2p/protocol/kademlia/common.hpp @@ -8,10 +8,9 @@ #include +#include #include #include -#include -#include #include #include @@ -21,7 +20,6 @@ namespace libp2p::protocol::kademlia { using peer::PeerId; using peer::PeerInfo; - using peer::ProtocolName; using Key = ContentId; using Value = ContentValue; diff --git a/include/libp2p/protocol/kademlia/config.hpp b/include/libp2p/protocol/kademlia/config.hpp index 188c68de3..c8798a0b8 100644 --- a/include/libp2p/protocol/kademlia/config.hpp +++ b/include/libp2p/protocol/kademlia/config.hpp @@ -7,6 +7,7 @@ #define LIBP2P_PROTOCOL_KADEMLIA_CONFIG #include +#include #include namespace libp2p::protocol::kademlia { @@ -51,10 +52,10 @@ namespace libp2p::protocol::kademlia { Config() = default; /** - * @returns kademlia protocol id + * @returns kademlia protocol ids * @note Default: "ipfs/kad/1.0" */ - peer::ProtocolName protocolId = "/ipfs/kad/1.0.0"; + StreamProtocols protocols = {"/ipfs/kad/1.0.0"}; /** * True if application is not announce himself diff --git a/include/libp2p/protocol/kademlia/impl/kademlia_impl.hpp b/include/libp2p/protocol/kademlia/impl/kademlia_impl.hpp index ad8c759d0..d830ada9c 100644 --- a/include/libp2p/protocol/kademlia/impl/kademlia_impl.hpp +++ b/include/libp2p/protocol/kademlia/impl/kademlia_impl.hpp @@ -128,7 +128,6 @@ namespace libp2p::protocol::kademlia { // --- Secondary --- - const ProtocolName &protocol_; const PeerId self_id_; // --- Auxiliary --- diff --git a/src/protocol/kademlia/impl/add_provider_executor.cpp b/src/protocol/kademlia/impl/add_provider_executor.cpp index e207fe6f0..26e2f4156 100644 --- a/src/protocol/kademlia/impl/add_provider_executor.cpp +++ b/src/protocol/kademlia/impl/add_provider_executor.cpp @@ -138,7 +138,7 @@ namespace libp2p::protocol::kademlia { config_.connectionTimeout); host_->newStream( - peer_info, {config_.protocolId}, + peer_info, config_.protocols, [holder](auto &&stream_res) { if (holder->first) { holder->second.cancel(); diff --git a/src/protocol/kademlia/impl/find_peer_executor.cpp b/src/protocol/kademlia/impl/find_peer_executor.cpp index aa3d63a96..60cfbe127 100644 --- a/src/protocol/kademlia/impl/find_peer_executor.cpp +++ b/src/protocol/kademlia/impl/find_peer_executor.cpp @@ -148,7 +148,7 @@ namespace libp2p::protocol::kademlia { config_.connectionTimeout); host_->newStream( - peer_info, {config_.protocolId}, + peer_info, config_.protocols, [holder](auto &&stream_res) { if (holder->first) { holder->second.cancel(); diff --git a/src/protocol/kademlia/impl/find_providers_executor.cpp b/src/protocol/kademlia/impl/find_providers_executor.cpp index 7c61310fe..830178b38 100644 --- a/src/protocol/kademlia/impl/find_providers_executor.cpp +++ b/src/protocol/kademlia/impl/find_providers_executor.cpp @@ -157,7 +157,7 @@ namespace libp2p::protocol::kademlia { config_.connectionTimeout); host_->newStream( - peer_info, {config_.protocolId}, + peer_info, config_.protocols, [holder](auto &&stream_res) { if (holder->first) { holder->second.cancel(); diff --git a/src/protocol/kademlia/impl/get_value_executor.cpp b/src/protocol/kademlia/impl/get_value_executor.cpp index 9ab534567..c6fcf939e 100644 --- a/src/protocol/kademlia/impl/get_value_executor.cpp +++ b/src/protocol/kademlia/impl/get_value_executor.cpp @@ -145,7 +145,7 @@ namespace libp2p::protocol::kademlia { config_.connectionTimeout); host_->newStream( - peer_info, {config_.protocolId}, + peer_info, config_.protocols, [holder](auto &&stream_res) { if (holder->first) { holder->second.cancel(); diff --git a/src/protocol/kademlia/impl/kademlia_impl.cpp b/src/protocol/kademlia/impl/kademlia_impl.cpp index 4afec2e71..b43a9515a 100644 --- a/src/protocol/kademlia/impl/kademlia_impl.cpp +++ b/src/protocol/kademlia/impl/kademlia_impl.cpp @@ -43,7 +43,6 @@ namespace libp2p::protocol::kademlia { scheduler_(std::move(scheduler)), bus_(std::move(bus)), random_generator_(std::move(random_generator)), - protocol_(config_.protocolId), self_id_(host_->getId()), log_("Kademlia", "kademlia") { BOOST_ASSERT(host_ != nullptr); @@ -68,7 +67,7 @@ namespace libp2p::protocol::kademlia { // handle streams for observed protocol host_->setProtocolHandler( - {protocol_}, [wp = weak_from_this()](StreamAndProtocol stream) { + config_.protocols, [wp = weak_from_this()](StreamAndProtocol stream) { if (auto self = wp.lock()) { self->handleProtocol(std::move(stream)); } diff --git a/src/protocol/kademlia/impl/put_value_executor.cpp b/src/protocol/kademlia/impl/put_value_executor.cpp index f5ba8b9f1..0217f17f9 100644 --- a/src/protocol/kademlia/impl/put_value_executor.cpp +++ b/src/protocol/kademlia/impl/put_value_executor.cpp @@ -90,7 +90,7 @@ namespace libp2p::protocol::kademlia { config_.connectionTimeout); host_->newStream( - peer_info, {config_.protocolId}, + peer_info, config_.protocols, [holder](auto &&stream_res) { if (holder->first) { holder->second.cancel();