gossip fetch and response handling implemented

pull/2232/head
dr7ana 5 months ago
parent cc97fe1f5f
commit 89975a0b01

@ -668,20 +668,28 @@ namespace llarp
}
void
LinkManager::gossip_rc(const RouterID& rc_rid, std::string serialized_rc)
LinkManager::gossip_rc(
const RouterID& gossip_src, const RouterID& last_sender, std::string serialized_rc)
{
for (auto& [rid, conn] : ep.active_conns)
{
// don't send back to the owner...
if (rid == rc_rid)
// don't send back to the gossip source or the last sender
if (rid == gossip_src or rid == last_sender)
continue;
// don't gossip RCs to clients
if (not conn->remote_is_relay)
continue;
send_control_message(rid, "gossip_rc", serialized_rc, [](oxen::quic::message) mutable {
log::critical(logcat, "PLACEHOLDER FOR GOSSIP RC RESPONSE HANDLER");
});
log::critical(logcat, "Dispatching gossip_rc to {}", rid);
send_control_message(
rid,
"gossip_rc"s,
GossipRCMessage::serialize(gossip_src, last_sender, serialized_rc),
[](oxen::quic::message) mutable {
log::critical(logcat, "PLACEHOLDER FOR GOSSIP RC RESPONSE HANDLER");
});
}
}
@ -689,15 +697,31 @@ namespace llarp
LinkManager::handle_gossip_rc(oxen::quic::message m)
{
// RemoteRC constructor wraps deserialization in a try/catch
RemoteRC rc{m.body()};
RemoteRC rc;
RouterID src, sender;
try
{
oxenc::bt_dict_consumer btdc{m.body()};
btdc.required("rc");
rc = RemoteRC{btdc.consume_dict_data()};
src.from_string(btdc.require<std::string>("sender"));
sender.from_string(btdc.require<std::string>("src"));
}
catch (const std::exception& e)
{
log::info(link_cat, "Exception handling GossipRC request: {}", e.what());
return;
}
if (node_db->put_rc_if_newer(rc))
if (node_db->verify_store_gossip_rc(rc))
{
log::info(link_cat, "Received updated RC, forwarding to relay peers.");
gossip_rc(rc.router_id(), m.body_str());
log::critical(link_cat, "Received updated RC, forwarding to relay peers.");
gossip_rc(src, _router.local_rid(), std::string{rc.view()});
}
else
log::debug(link_cat, "Received known or old RC, not storing or forwarding.");
log::critical(link_cat, "Received known or old RC, not storing or forwarding.");
}
void
@ -744,9 +768,7 @@ namespace llarp
{
oxenc::bt_dict_consumer btdc{m.body()};
btdc.required("local");
auto rc_dict = btdc.consume_dict_data();
// log::critical(logcat, "incoming dict data: {}", oxenc::to_hex(rc_dict));
remote = RemoteRC{rc_dict};
remote = RemoteRC{btdc.consume_dict_data()};
quantity = btdc.require<size_t>("quantity");
}
catch (const std::exception& e)

@ -234,7 +234,7 @@ namespace llarp
}
void
gossip_rc(const RouterID& rc_rid, std::string serialized_rc);
gossip_rc(const RouterID& gossip_src, const RouterID& last_sender, std::string serialized_rc);
void
handle_gossip_rc(oxen::quic::message m);

@ -6,6 +6,28 @@
namespace llarp
{
namespace GossipRCMessage
{
inline static std::string
serialize(const RouterID& gossip_src, const RouterID& last_sender, std::string rc)
{
oxenc::bt_dict_producer btdp;
try
{
btdp.append_encoded("rc", rc);
btdp.append("sender", last_sender.ToView());
btdp.append("src", gossip_src.ToView());
}
catch (...)
{
log::error(link_cat, "Error: GossipRCMessage failed to bt encode contents");
}
return std::move(btdp).str();
}
} // namespace GossipRCMessage
namespace FetchRCMessage
{
inline const auto INVALID_REQUEST =

@ -945,7 +945,13 @@ namespace llarp
}
bool
NodeDB::has_rc(RouterID pk) const
NodeDB::has_rc(const RemoteRC& rc) const
{
return known_rcs.count(rc);
}
bool
NodeDB::has_rc(const RouterID& pk) const
{
return rc_lookup.count(pk);
}
@ -1020,11 +1026,19 @@ namespace llarp
return known_rids.size();
}
bool
NodeDB::verify_store_gossip_rc(const RemoteRC& rc)
{
if (not router_whitelist.count(rc.router_id()))
return put_rc_if_newer(rc);
return false;
}
bool
NodeDB::put_rc_if_newer(RemoteRC rc, rc_time now)
{
if (auto itr = rc_lookup.find(rc.router_id());
itr == rc_lookup.end() or itr->second.other_is_newer(rc))
if (not has_rc(rc))
return put_rc(std::move(rc), now);
return false;

@ -425,7 +425,10 @@ namespace llarp
/// return true if we have an rc by its ident pubkey
bool
has_rc(RouterID pk) const;
has_rc(const RouterID& pk) const;
bool
has_rc(const RemoteRC& rc) const;
/// maybe get an rc by its ident pubkey
std::optional<RemoteRC>
@ -584,6 +587,9 @@ namespace llarp
/// returns true if the rc was inserted
bool
put_rc_if_newer(RemoteRC rc, rc_time now = time_point_now());
bool
verify_store_gossip_rc(const RemoteRC& rc);
};
} // namespace llarp

@ -869,10 +869,10 @@ namespace llarp
router_contact.resign();
save_rc();
auto view = router_contact.view();
_link_manager->gossip_rc(
pubkey(), std::string{reinterpret_cast<const char*>(view.data()), view.size()});
router_contact.router_id(),
router_contact.router_id(),
std::string{oxen::quic::to_sv(router_contact.view())});
last_rc_gossip = now_timepoint;

Loading…
Cancel
Save