Skip to content

[WIP] Updated code to use new active_nodes_bin endpoint for snode cache #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if(CCACHE_PROGRAM)
endif()

project(libsession-util
VERSION 1.4.0
VERSION 1.4.2
DESCRIPTION "Session client utility library"
LANGUAGES ${LANGS})

Expand Down
4 changes: 2 additions & 2 deletions include/session/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ std::ofstream open_for_writing(const fs::path& filename);
/// enabled for any failures. This also throws if the file cannot be opened.
std::ifstream open_for_reading(const fs::path& filename);

/// Reads a (binary) file from disk into the string `contents`.
std::string read_whole_file(const fs::path& filename);
/// Reads a (binary) file from disk.
std::vector<std::byte> read_whole_file(const fs::path& filename);

/// Dumps (binary) string contents to disk. The file is overwritten if it already exists.
void write_whole_file(const fs::path& filename, std::string_view contents = "");
Expand Down
26 changes: 18 additions & 8 deletions include/session/session_network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ struct request_info {
bool node_destination{detail::node_for_destination(destination).has_value()};
};

enum class response_type {
json = 0,
bencoded = 1,
binary = 2,
};

class Network {
private:
const bool use_testnet;
Expand All @@ -199,6 +205,7 @@ class Network {
// Values persisted to disk
std::optional<size_t> seed_node_cache_size;
std::vector<service_node> snode_cache;
std::vector<std::byte> snode_cache_bin;
std::chrono::system_clock::time_point last_snode_cache_update{};

std::thread disk_write_thread;
Expand Down Expand Up @@ -552,8 +559,9 @@ class Network {
/// updated cache to disk.
///
/// Inputs:
/// - 'nodes' - [in] the nodes to use as the updated cache.
void refresh_snode_cache_complete(std::vector<service_node> nodes);
/// - 'updated_snode_cache_bin' - [in] the nodes to use as the updated cache in binary form.
void refresh_snode_cache_complete(
std::string request_id, std::vector<std::byte> updated_snode_cache_bin);

/// API: network/refresh_snode_cache_from_seed_nodes
///
Expand Down Expand Up @@ -625,14 +633,15 @@ class Network {
/// - 'request_id' - [in] id for the request which triggered the call.
/// - `conn_info` -- [in] the connection info to retrieve service nodes from.
/// - `limit` -- [in, optional] the number of service nodes to retrieve.
/// - `callback` -- [in] callback to be triggered once we receive nodes. NOTE: If an error
/// occurs an empty list and an error will be provided.
/// - `callback` -- [in] callback to be triggered once we receive snode cache. NOTE: If an
/// error occurs an empty vector and an error will be provided.
void get_service_nodes(
std::string request_id,
connection_info conn_info,
std::optional<int> limit,
std::function<void(std::vector<service_node> nodes, std::optional<std::string> error)>
callback);
std::function<
void(std::vector<std::byte> updated_snode_cache_bin,
std::optional<std::string> error)> callback);

/// API: network/check_request_queue_timeouts
///
Expand Down Expand Up @@ -702,13 +711,14 @@ class Network {
///
/// Inputs:
/// - `resp` -- [in] the quic response.
/// - `is_bencoded` -- [in] flag indicating whether the response will be bencoded or JSON.
/// - `response_type` -- [in] enum indicating what response type to expect.
///
/// Returns:
/// - `std::pair<uint16_t, std::string>` -- the status code and response body (for a bencoded
/// response this is just the direct response body from quic as it simplifies consuming the
/// response elsewhere).
std::pair<uint16_t, std::string> validate_response(oxen::quic::message resp, bool is_bencoded);
std::pair<uint16_t, std::string> validate_response(
oxen::quic::message resp, response_type type);

/// API: network/drop_path_when_empty
///
Expand Down
13 changes: 9 additions & 4 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ std::ifstream open_for_reading(const fs::path& filename) {
return in;
}

std::string read_whole_file(const fs::path& filename) {
std::vector<std::byte> read_whole_file(const fs::path& filename) {
auto in = open_for_reading(filename);
std::string contents;
in.seekg(0, std::ios::end);
auto size = in.tellg();
in.seekg(0, std::ios::beg);
contents.resize(size);
in.read(contents.data(), size);

if (size <= 0)
return {};

std::vector<std::byte> contents(static_cast<size_t>(size));
if (!in.read(reinterpret_cast<char*>(contents.data()), size))
return {};

return contents;
}

Expand Down
Loading