|
|
@ -112,7 +112,7 @@ namespace libp2p::protocol::kademlia { |
|
|
|
} |
|
|
|
|
|
|
|
outcome::result<void> KademliaImpl::putValue(Key key, Value value) { |
|
|
|
log_.debug("CALL: PutValue ({})", multi::detail::encodeBase58(key.data)); |
|
|
|
log_.debug("CALL: PutValue ({})", multi::detail::encodeBase58(key)); |
|
|
|
|
|
|
|
if (auto res = storage_->putValue(key, std::move(value)); |
|
|
|
not res.has_value()) { |
|
|
@ -124,7 +124,7 @@ namespace libp2p::protocol::kademlia { |
|
|
|
|
|
|
|
outcome::result<void> KademliaImpl::getValue(const Key &key, |
|
|
|
FoundValueHandler handler) { |
|
|
|
log_.debug("CALL: GetValue ({})", multi::detail::encodeBase58(key.data)); |
|
|
|
log_.debug("CALL: GetValue ({})", multi::detail::encodeBase58(key)); |
|
|
|
|
|
|
|
// Check if has actual value locally
|
|
|
|
if (auto res = storage_->getValue(key); res.has_value()) { |
|
|
@ -144,7 +144,7 @@ namespace libp2p::protocol::kademlia { |
|
|
|
|
|
|
|
outcome::result<void> KademliaImpl::provide(const Key &key, |
|
|
|
bool need_notify) { |
|
|
|
log_.debug("CALL: Provide ({})", multi::detail::encodeBase58(key.data)); |
|
|
|
log_.debug("CALL: Provide ({})", multi::detail::encodeBase58(key)); |
|
|
|
|
|
|
|
content_routing_table_->addProvider(key, self_id_); |
|
|
|
|
|
|
@ -159,8 +159,7 @@ namespace libp2p::protocol::kademlia { |
|
|
|
|
|
|
|
outcome::result<void> KademliaImpl::findProviders( |
|
|
|
const Key &key, size_t limit, FoundProvidersHandler handler) { |
|
|
|
log_.debug("CALL: FindProviders ({})", |
|
|
|
multi::detail::encodeBase58(key.data)); |
|
|
|
log_.debug("CALL: FindProviders ({})", multi::detail::encodeBase58(key)); |
|
|
|
|
|
|
|
// Try to find locally
|
|
|
|
auto providers = content_routing_table_->getProvidersFor(key, limit); |
|
|
@ -304,7 +303,7 @@ namespace libp2p::protocol::kademlia { |
|
|
|
} |
|
|
|
auto &[key, value, ts] = msg.record.value(); |
|
|
|
|
|
|
|
log_.debug("MSG: PutValue ({})", multi::detail::encodeBase58(key.data)); |
|
|
|
log_.debug("MSG: PutValue ({})", multi::detail::encodeBase58(key)); |
|
|
|
|
|
|
|
auto validation_res = validator_->validate(key, value); |
|
|
|
if (not validation_res) { |
|
|
@ -318,6 +317,15 @@ namespace libp2p::protocol::kademlia { |
|
|
|
log_.warn("incoming PutValue failed: {}", res.error().message()); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// echo request
|
|
|
|
auto buffer = std::make_shared<std::vector<uint8_t>>(); |
|
|
|
if (not msg.serialize(*buffer)) { |
|
|
|
session->close(Error::MESSAGE_SERIALIZE_ERROR); |
|
|
|
BOOST_UNREACHABLE_RETURN(); |
|
|
|
} |
|
|
|
|
|
|
|
session->write(buffer, {}); |
|
|
|
} |
|
|
|
|
|
|
|
void KademliaImpl::onGetValue(const std::shared_ptr<Session> &session, |
|
|
@ -327,16 +335,9 @@ namespace libp2p::protocol::kademlia { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
auto cid_res = ContentId::fromWire(msg.key); |
|
|
|
if (!cid_res) { |
|
|
|
log_.warn("incoming GetValue failed: invalid key in message"); |
|
|
|
return; |
|
|
|
} |
|
|
|
auto &cid = cid_res.value(); |
|
|
|
|
|
|
|
log_.debug("MSG: GetValue ({})", multi::detail::encodeBase58(cid.data)); |
|
|
|
log_.debug("MSG: GetValue ({})", multi::detail::encodeBase58(msg.key)); |
|
|
|
|
|
|
|
if (auto providers = content_routing_table_->getProvidersFor(cid); |
|
|
|
if (auto providers = content_routing_table_->getProvidersFor(msg.key); |
|
|
|
not providers.empty()) { |
|
|
|
std::vector<Message::Peer> peers; |
|
|
|
peers.reserve(config_.closerPeerCount); |
|
|
@ -356,10 +357,10 @@ namespace libp2p::protocol::kademlia { |
|
|
|
msg.provider_peers = std::move(peers); |
|
|
|
} |
|
|
|
|
|
|
|
auto res = storage_->getValue(cid); |
|
|
|
auto res = storage_->getValue(msg.key); |
|
|
|
if (res) { |
|
|
|
auto &[value, expire] = res.value(); |
|
|
|
msg.record = Message::Record{std::move(cid), std::move(value), |
|
|
|
msg.record = Message::Record{std::move(msg.key), std::move(value), |
|
|
|
std::to_string(expire.count())}; |
|
|
|
} |
|
|
|
|
|
|
@ -379,21 +380,14 @@ namespace libp2p::protocol::kademlia { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
auto cid_res = ContentId::fromWire(msg.key); |
|
|
|
if (not cid_res) { |
|
|
|
log_.warn("AddProvider failed: invalid key in message"); |
|
|
|
return; |
|
|
|
} |
|
|
|
auto &cid = cid_res.value(); |
|
|
|
|
|
|
|
log_.debug("MSG: AddProvider ({})", multi::detail::encodeBase58(cid.data)); |
|
|
|
log_.debug("MSG: AddProvider ({})", multi::detail::encodeBase58(msg.key)); |
|
|
|
|
|
|
|
auto &providers = msg.provider_peers.value(); |
|
|
|
for (auto &provider : providers) { |
|
|
|
if (auto peer_id_res = session->stream()->remotePeerId()) { |
|
|
|
if (peer_id_res.value() == provider.info.id) { |
|
|
|
// Save providers who have provided themselves
|
|
|
|
content_routing_table_->addProvider(cid, provider.info.id); |
|
|
|
content_routing_table_->addProvider(msg.key, provider.info.id); |
|
|
|
addPeer(provider.info, false); |
|
|
|
} |
|
|
|
} |
|
|
@ -407,17 +401,10 @@ namespace libp2p::protocol::kademlia { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
auto cid_res = ContentId::fromWire(msg.key); |
|
|
|
if (not cid_res) { |
|
|
|
log_.warn("GetProviders failed: invalid key in message"); |
|
|
|
return; |
|
|
|
} |
|
|
|
auto &cid = cid_res.value(); |
|
|
|
|
|
|
|
log_.debug("MSG: GetProviders ({})", multi::detail::encodeBase58(cid.data)); |
|
|
|
log_.debug("MSG: GetProviders ({})", multi::detail::encodeBase58(msg.key)); |
|
|
|
|
|
|
|
auto peer_ids = content_routing_table_->getProvidersFor( |
|
|
|
cid, config_.closerPeerCount * 2); |
|
|
|
msg.key, config_.closerPeerCount * 2); |
|
|
|
|
|
|
|
if (not peer_ids.empty()) { |
|
|
|
std::vector<Message::Peer> peers; |
|
|
@ -441,7 +428,7 @@ namespace libp2p::protocol::kademlia { |
|
|
|
} |
|
|
|
|
|
|
|
peer_ids = peer_routing_table_->getNearestPeers( |
|
|
|
NodeId(cid), config_.closerPeerCount * 2); |
|
|
|
NodeId(msg.key), config_.closerPeerCount * 2); |
|
|
|
|
|
|
|
if (not peer_ids.empty()) { |
|
|
|
std::vector<Message::Peer> peers; |
|
|
@ -475,6 +462,11 @@ namespace libp2p::protocol::kademlia { |
|
|
|
|
|
|
|
void KademliaImpl::onFindNode(const std::shared_ptr<Session> &session, |
|
|
|
Message &&msg) { |
|
|
|
if (msg.key.empty()) { |
|
|
|
log_.warn("FindNode failed: empty key in message"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (msg.closer_peers) { |
|
|
|
for (auto &peer : msg.closer_peers.value()) { |
|
|
|
if (peer.conn_status != Message::Connectedness::CAN_NOT_CONNECT) { |
|
|
@ -493,17 +485,10 @@ namespace libp2p::protocol::kademlia { |
|
|
|
msg.closer_peers.reset(); |
|
|
|
} |
|
|
|
|
|
|
|
auto cid_res = ContentId::fromWire(msg.key); |
|
|
|
if (not cid_res) { |
|
|
|
log_.warn("FindNode failed: invalid key in message"); |
|
|
|
return; |
|
|
|
} |
|
|
|
auto &cid = cid_res.value(); |
|
|
|
|
|
|
|
log_.debug("MSG: FindNode ({})", multi::detail::encodeBase58(cid.data)); |
|
|
|
log_.debug("MSG: FindNode ({})", multi::detail::encodeBase58(msg.key)); |
|
|
|
|
|
|
|
auto ids = peer_routing_table_->getNearestPeers( |
|
|
|
NodeId(cid), config_.closerPeerCount * 2); |
|
|
|
NodeId(msg.key), config_.closerPeerCount * 2); |
|
|
|
|
|
|
|
std::vector<Message::Peer> peers; |
|
|
|
peers.reserve(config_.closerPeerCount); |
|
|
|