Skip to content

Commit d85fdc6

Browse files
authored
kad put (#274)
Signed-off-by: turuslan <[email protected]>
1 parent 883d3a2 commit d85fdc6

16 files changed

Lines changed: 89 additions & 54 deletions

File tree

example/02-kademlia/rendezvous_chat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int main(int argc, char *argv[]) {
337337
auto peer_id =
338338
libp2p::peer::PeerId::fromHash(cid.content_address).value();
339339

340-
[[maybe_unused]] auto res = kademlia->findPeer(peer_id, [&](auto) {
340+
[[maybe_unused]] auto res = kademlia->findPeer(peer_id, [&](auto, auto) {
341341
// Say to world about his providing
342342
provide();
343343

include/libp2p/protocol/kademlia/common.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ namespace libp2p::protocol::kademlia {
2626
using Time = std::chrono::milliseconds;
2727
using ValueAndTime = std::pair<Value, Time>;
2828

29-
using FoundPeerInfoHandler = std::function<void(outcome::result<PeerInfo>)>;
29+
using FoundPeerInfoHandler =
30+
std::function<void(outcome::result<PeerInfo>, std::vector<PeerId>)>;
3031
using FoundProvidersHandler =
3132
std::function<void(outcome::result<std::vector<PeerInfo>>)>;
3233
using FoundValueHandler = std::function<void(outcome::result<Value>)>;

include/libp2p/protocol/kademlia/config.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ namespace libp2p::protocol::kademlia {
148148

149149
// https://github.com/libp2p/rust-libp2p/blob/c6cf7fec6913aa590622aeea16709fce6e9c99a5/protocols/kad/src/query/peers/closest.rs#L110-L120
150150
size_t query_initial_peers = K_VALUE;
151+
152+
// https://github.com/libp2p/rust-libp2p/blob/9a45db3f82b760c93099e66ec77a7a772d1f6cd3/protocols/kad/src/query/peers/closest.rs#L336-L346
153+
size_t replication_factor = K_VALUE;
151154
};
152155

153156
} // namespace libp2p::protocol::kademlia

include/libp2p/protocol/kademlia/impl/executors_factory.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace libp2p::protocol::kademlia {
4646
ContentId sought_key, FoundProvidersHandler handler) = 0;
4747

4848
virtual std::shared_ptr<FindPeerExecutor> createFindPeerExecutor(
49-
PeerId peer_id, FoundPeerInfoHandler handler) = 0;
49+
HashedKey key, FoundPeerInfoHandler handler) = 0;
5050
};
5151

5252
} // namespace libp2p::protocol::kademlia

include/libp2p/protocol/kademlia/impl/find_peer_executor.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace libp2p::protocol::kademlia {
3535
std::shared_ptr<basic::Scheduler> scheduler,
3636
std::shared_ptr<SessionHost> session_host,
3737
const std::shared_ptr<PeerRoutingTable> &peer_routing_table,
38-
PeerId peer_id,
38+
HashedKey target,
3939
FoundPeerInfoHandler handler);
4040

4141
~FindPeerExecutor() override;
@@ -70,9 +70,9 @@ namespace libp2p::protocol::kademlia {
7070
std::shared_ptr<SessionHost> session_host_;
7171

7272
// Secondary
73-
const PeerId sought_peer_id_;
74-
const NodeId target_;
73+
HashedKey target_;
7574
std::unordered_set<PeerId> nearest_peer_ids_;
75+
std::vector<PeerId> succeeded_peers_;
7676
FoundPeerInfoHandler handler_;
7777

7878
// Auxiliary

include/libp2p/protocol/kademlia/impl/kademlia_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace libp2p::protocol::kademlia {
112112
ContentId content_id, FoundProvidersHandler handler) override;
113113

114114
std::shared_ptr<FindPeerExecutor> createFindPeerExecutor(
115-
PeerId peer_id, FoundPeerInfoHandler handler) override;
115+
HashedKey key, FoundPeerInfoHandler handler) override;
116116

117117
outcome::result<void> findRandomPeer() override;
118118
void randomWalk();

include/libp2p/protocol/kademlia/message.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace libp2p::protocol::kademlia {
7777
Message createGetProvidersRequest(const Key &key,
7878
boost::optional<PeerInfo> self_announce);
7979

80-
Message createFindNodeRequest(const PeerId &node,
80+
Message createFindNodeRequest(Key key,
8181
boost::optional<PeerInfo> self_announce);
8282

8383
} // namespace libp2p::protocol::kademlia

include/libp2p/protocol/kademlia/node_id.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <climits>
1111
#include <cstring>
1212
#include <memory>
13+
#include <optional>
1314
#include <span>
1415
#include <vector>
1516

@@ -106,4 +107,17 @@ namespace libp2p::protocol::kademlia {
106107
Hash256 data_;
107108
};
108109

110+
struct HashedKey {
111+
HashedKey(Key key, std::optional<PeerId> peer)
112+
: key{std::move(key)},
113+
hash{NodeId::hash(this->key)},
114+
peer{std::move(peer)} {}
115+
// NOLINTNEXTLINE(google-explicit-constructor)
116+
HashedKey(Key key) : HashedKey{std::move(key), std::nullopt} {}
117+
// NOLINTNEXTLINE(google-explicit-constructor)
118+
HashedKey(const PeerId &peer) : HashedKey{peer.toVector(), peer} {}
119+
Key key;
120+
NodeId hash;
121+
std::optional<PeerId> peer;
122+
};
109123
} // namespace libp2p::protocol::kademlia

src/peer/address_repository/inmem_address_repository.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ namespace libp2p::peer {
123123
auto &addresses = *peer_it->second;
124124

125125
auto expires_at = Clock::now() + ttl;
126-
std::for_each(addresses.begin(), addresses.end(), [expires_at](auto &item) {
126+
for (auto &item : addresses) {
127127
item.second = expires_at;
128-
});
128+
}
129129

130130
return outcome::success();
131131
} // namespace libp2p::peer

src/protocol/kademlia/impl/add_provider_executor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ namespace libp2p::protocol::kademlia {
3636
nearest_peer_ids_.insert(std::move_iterator(nearest_peer_ids.begin()),
3737
std::move_iterator(nearest_peer_ids.end()));
3838

39-
std::for_each(nearest_peer_ids_.begin(),
40-
nearest_peer_ids_.end(),
41-
[this](auto &peer_id) { queue_.emplace(peer_id, target_); });
39+
for (auto &peer_id : nearest_peer_ids_) {
40+
queue_.emplace(peer_id, target_);
41+
}
4242

4343
log_.debug("created");
4444
}

0 commit comments

Comments
 (0)