@@ -167,11 +167,16 @@ namespace libp2p::protocol::kademlia {
167167 return ;
168168 }
169169
170- if (requests_in_progress_ == 0 ) {
170+ if (requests_in_progress_ != 0 ) {
171+ return ;
172+ }
173+ if (received_records_->empty ()) {
171174 done_ = true ;
172175 log_.debug (" done" );
173176 handler_ (Error::VALUE_NOT_FOUND);
177+ return ;
174178 }
179+ finish ();
175180 }
176181
177182 void GetValueExecutor::onConnected (StreamAndProtocolOrError stream_res) {
@@ -310,41 +315,45 @@ namespace libp2p::protocol::kademlia {
310315 received_records_->insert ({remote_peer_id, value});
311316
312317 if (received_records_->size () >= config_.valueLookupsQuorum ) {
313- std::vector<Value> values;
314- std::transform (received_records_->begin (),
315- received_records_->end (),
316- std::back_inserter (values),
317- [](auto &record) { return record.value ; });
318-
319- auto index_res = validator_->select (key_, values);
320- if (not index_res.has_value ()) {
321- log_.debug (" Can't select best value of {} provided" , values.size ());
322- return ;
323- }
324- auto &best = values[index_res.value ()];
325-
326- // Return result to upstear
327- done_ = true ;
328- log_.debug (" done" );
329- handler_ (best);
330-
331- // Inform peer of new value
332- std::vector<PeerId> addressees;
333- auto &idx_by_value = received_records_->get <ByValue>();
334- for (auto &[peer, value] : idx_by_value) {
335- if (value != best) {
336- addressees.emplace_back (peer);
337- } else {
338- content_routing_table_->addProvider (key_, peer);
339- }
340- }
318+ finish ();
319+ }
320+ }
321+ }
341322
342- if (not addressees.empty ()) {
343- auto put_value_executor = executor_factory_->createPutValueExecutor (
344- key_, std::move (value), std::move (addressees));
345- [[maybe_unused]] auto res = put_value_executor->start ();
346- }
323+ void GetValueExecutor::finish () {
324+ std::vector<Value> values;
325+ std::transform (received_records_->begin (),
326+ received_records_->end (),
327+ std::back_inserter (values),
328+ [](auto &record) { return record.value ; });
329+
330+ auto index_res = validator_->select (key_, values);
331+ if (not index_res.has_value ()) {
332+ log_.debug (" Can't select best value of {} provided" , values.size ());
333+ return ;
334+ }
335+ auto &best = values[index_res.value ()];
336+
337+ // Return result to upstear
338+ done_ = true ;
339+ log_.debug (" done" );
340+ handler_ (best);
341+
342+ // Inform peer of new value
343+ std::vector<PeerId> addressees;
344+ auto &idx_by_value = received_records_->get <ByValue>();
345+ for (auto &[peer, value] : idx_by_value) {
346+ if (value != best) {
347+ addressees.emplace_back (peer);
348+ } else {
349+ content_routing_table_->addProvider (key_, peer);
347350 }
348351 }
352+
353+ if (not addressees.empty ()) {
354+ auto put_value_executor = executor_factory_->createPutValueExecutor (
355+ key_, best, std::move (addressees));
356+ [[maybe_unused]] auto res = put_value_executor->start ();
357+ }
349358 }
350359} // namespace libp2p::protocol::kademlia
0 commit comments