2020-05-18 16:06:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/types.hpp>
|
|
|
|
#include <llarp/dht/key.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/router_id.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/service/name.hpp>
|
2020-05-18 16:06:52 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <oxenmq/address.h>
|
|
|
|
#include <oxenmq/oxenmq.h>
|
|
|
|
|
2020-05-18 16:06:52 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
struct Router;
|
2020-05-19 18:53:03 +00:00
|
|
|
|
2020-05-18 16:06:52 +00:00
|
|
|
namespace rpc
|
|
|
|
{
|
|
|
|
/// The LokidRpcClient uses loki-mq to talk to make API requests to lokid.
|
2020-05-19 18:53:03 +00:00
|
|
|
struct LokidRpcClient : public std::enable_shared_from_this<LokidRpcClient>
|
2020-05-21 14:09:45 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
explicit LokidRpcClient(std::shared_ptr<oxenmq::OxenMQ> lmq, std::weak_ptr<Router> r);
|
2020-05-18 16:06:52 +00:00
|
|
|
|
2020-05-19 18:53:03 +00:00
|
|
|
/// Connect to lokid async
|
2020-05-18 16:06:52 +00:00
|
|
|
void
|
2021-02-03 18:12:21 +00:00
|
|
|
ConnectAsync(oxenmq::address url);
|
2020-05-18 16:06:52 +00:00
|
|
|
|
2020-05-20 11:41:42 +00:00
|
|
|
/// blocking request identity key from lokid
|
2020-06-30 16:02:29 +00:00
|
|
|
/// throws on failure
|
|
|
|
SecretKey
|
2020-05-20 11:41:42 +00:00
|
|
|
ObtainIdentityKey();
|
|
|
|
|
2021-05-03 20:53:00 +00:00
|
|
|
/// get what the current block height is according to oxend
|
|
|
|
uint64_t
|
|
|
|
BlockHeight() const
|
|
|
|
{
|
|
|
|
return m_BlockHeight;
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
void
|
2023-09-29 17:28:30 +00:00
|
|
|
lookup_ons_hash(
|
2023-09-27 14:09:48 +00:00
|
|
|
std::string namehash,
|
2020-09-17 19:18:08 +00:00
|
|
|
std::function<void(std::optional<service::EncryptedName>)> resultHandler);
|
|
|
|
|
2021-06-06 12:32:23 +00:00
|
|
|
/// inform that if connected to a router successfully
|
2021-06-04 20:46:51 +00:00
|
|
|
void
|
2021-06-06 12:32:23 +00:00
|
|
|
InformConnection(RouterID router, bool success);
|
2021-06-04 20:46:51 +00:00
|
|
|
|
2020-05-19 18:53:03 +00:00
|
|
|
void
|
2023-01-29 23:24:49 +00:00
|
|
|
StartPings();
|
2020-05-19 18:53:03 +00:00
|
|
|
|
2023-01-29 23:24:49 +00:00
|
|
|
private:
|
2020-05-19 18:53:03 +00:00
|
|
|
/// do a lmq command on the current connection
|
|
|
|
void
|
|
|
|
Command(std::string_view cmd);
|
|
|
|
|
2022-10-14 21:02:53 +00:00
|
|
|
/// triggers a service node list refresh from oxend; thread-safe and will do nothing if an
|
|
|
|
/// update is already in progress.
|
2020-05-20 11:41:42 +00:00
|
|
|
void
|
2021-06-07 18:26:51 +00:00
|
|
|
UpdateServiceNodeList();
|
2020-05-20 11:41:42 +00:00
|
|
|
|
2020-05-19 18:53:03 +00:00
|
|
|
template <typename HandlerFunc_t, typename Args_t>
|
|
|
|
void
|
|
|
|
Request(std::string_view cmd, HandlerFunc_t func, const Args_t& args)
|
|
|
|
{
|
|
|
|
m_lokiMQ->request(*m_Connection, std::move(cmd), std::move(func), args);
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:41:42 +00:00
|
|
|
template <typename HandlerFunc_t>
|
|
|
|
void
|
|
|
|
Request(std::string_view cmd, HandlerFunc_t func)
|
|
|
|
{
|
|
|
|
m_lokiMQ->request(*m_Connection, std::move(cmd), std::move(func));
|
|
|
|
}
|
|
|
|
|
2022-10-14 21:02:53 +00:00
|
|
|
// Handles a service node list update; takes the "service_node_states" object of an oxend
|
|
|
|
// "get_service_nodes" rpc request.
|
2020-05-19 18:53:03 +00:00
|
|
|
void
|
2022-10-14 21:02:53 +00:00
|
|
|
HandleNewServiceNodeList(const nlohmann::json& json);
|
2020-05-19 18:53:03 +00:00
|
|
|
|
2021-04-12 11:39:07 +00:00
|
|
|
// Handles notification of a new block
|
|
|
|
void
|
|
|
|
HandleNewBlock(oxenmq::Message& msg);
|
|
|
|
|
2021-02-03 18:12:21 +00:00
|
|
|
std::optional<oxenmq::ConnectionID> m_Connection;
|
2023-09-15 14:55:32 +00:00
|
|
|
std::shared_ptr<oxenmq::OxenMQ> m_lokiMQ;
|
2020-05-19 18:53:03 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::weak_ptr<Router> m_Router;
|
2021-04-26 10:41:23 +00:00
|
|
|
std::atomic<bool> m_UpdatingList;
|
2022-10-14 21:02:53 +00:00
|
|
|
std::string m_LastUpdateHash;
|
2021-05-03 20:53:00 +00:00
|
|
|
|
2021-06-04 20:46:51 +00:00
|
|
|
std::unordered_map<RouterID, PubKey> m_KeyMap;
|
|
|
|
|
2021-05-03 20:53:00 +00:00
|
|
|
uint64_t m_BlockHeight;
|
2020-05-18 16:06:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rpc
|
|
|
|
} // namespace llarp
|