Skip to content
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

Refactor preparation for moving RPC out of process #1872

Merged
merged 4 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*.DS_Store
core_test
!core_test/
rpc_test
!rpc_test/
qt_test
nano_node
nano_wallet
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ add_subdirectory(nano/crypto_lib)
add_subdirectory(nano/secure)
add_subdirectory(nano/lib)
add_subdirectory(nano/node)
add_subdirectory(nano/rpc)
add_subdirectory(nano/nano_node)

if (NANO_TEST OR RAIBLOCKS_TEST)
Expand All @@ -315,6 +316,7 @@ if (NANO_TEST OR RAIBLOCKS_TEST)
"${CMAKE_SOURCE_DIR}/gtest/googletest/include")

add_subdirectory(nano/core_test)
add_subdirectory(nano/rpc_test)
add_subdirectory(nano/slow_test)
endif ()

Expand Down
4 changes: 4 additions & 0 deletions ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ run_tests() {
fi
done

xvfb_run_ ./rpc_test
rpc_test_res=${?}

xvfb_run_ ./qt_test
qt_test_res=${?}

${TIMEOUT_CMD} ${TIMEOUT_TIME_ARG} ${TIMEOUT_SEC-${TIMEOUT_DEFAULT}} ./load_test ./nano_node -s 150
load_test_res=${?}

echo "Core Test return code: ${core_test_res}"
echo "RPC Test return code: ${rpc_test_res}"
echo "QT Test return code: ${qt_test_res}"
echo "Load Test return code: ${load_test_res}"
return ${core_test_res}
Expand Down
9 changes: 5 additions & 4 deletions nano/core_test/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#include <gtest/gtest.h>
#include <memory>
#include <nano/core_test/testutil.hpp>
#include <nano/lib/ipc_client.hpp>
#include <nano/node/ipc.hpp>
#include <nano/node/rpc.hpp>
#include <nano/node/testing.hpp>
#include <nano/rpc/rpc.hpp>
#include <sstream>
#include <vector>

Expand All @@ -20,7 +21,7 @@ TEST (ipc, asynchronous)
nano::ipc::ipc_server ipc (*system.nodes[0], rpc);
nano::ipc::ipc_client client (system.nodes[0]->io_ctx);

auto req (client.prepare_request (nano::ipc::payload_encoding::json_legacy, std::string (R"({"action": "block_count"})")));
auto req (nano::ipc::prepare_request (nano::ipc::payload_encoding::json_legacy, std::string (R"({"action": "block_count"})")));
auto res (std::make_shared<std::vector<uint8_t>> ());
std::atomic<bool> call_completed{ false };
client.async_connect ("::1", 24077, [&client, &req, &res, &call_completed](nano::error err) {
Expand Down Expand Up @@ -61,13 +62,13 @@ TEST (ipc, synchronous)
system.nodes[0]->config.ipc_config.transport_tcp.enabled = true;
system.nodes[0]->config.ipc_config.transport_tcp.port = 24077;
nano::ipc::ipc_server ipc (*system.nodes[0], rpc);
nano::ipc::rpc_ipc_client client (system.nodes[0]->io_ctx);
nano::ipc::ipc_client client (system.nodes[0]->io_ctx);

// Start blocking IPC client in a separate thread
std::atomic<bool> call_completed{ false };
std::thread client_thread ([&client, &call_completed]() {
client.connect ("::1", 24077);
std::string response (client.request (std::string (R"({"action": "block_count"})")));
std::string response (nano::ipc::request (client, std::string (R"({"action": "block_count"})")));
std::stringstream ss;
ss << response;
// Make sure the response is valid json
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <nano/core_test/testutil.hpp>
#include <nano/lib/jsonconfig.hpp>
#include <nano/node/common.hpp>
#include <nano/node/rpc.hpp>
#include <nano/node/testing.hpp>
#include <nano/rpc/rpc.hpp>

using namespace std::chrono_literals;

Expand Down
17 changes: 12 additions & 5 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ add_library (nano_lib
errors.hpp
errors.cpp
expected.hpp
blockbuilders.cpp
blockbuilders.hpp
blocks.cpp
blockbuilders.cpp
blocks.hpp
blocks.cpp
config.hpp
config.cpp
interface.cpp
interface.h
interface.cpp
ipc.hpp
ipc.cpp
ipc_client.hpp
ipc_client.cpp
jsonconfig.hpp
numbers.cpp
logger_mt.hpp
rpcconfig.hpp
rpcconfig.cpp
numbers.hpp
numbers.cpp
timer.hpp
utility.cpp
utility.hpp
utility.cpp
work.hpp
work.cpp)

Expand Down
9 changes: 9 additions & 0 deletions nano/lib/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
#include <nano/lib/numbers.hpp>
#include <string>

#define xstr(a) ver_str (a)
#define ver_str(a) #a

/**
* Returns build version information
*/
static const char * NANO_MAJOR_MINOR_VERSION = xstr (NANO_VERSION_MAJOR) "." xstr (NANO_VERSION_MINOR);
static const char * NANO_MAJOR_MINOR_RC_VERSION = xstr (NANO_VERSION_MAJOR) "." xstr (NANO_VERSION_MINOR) "RC" xstr (NANO_VERSION_PATCH);

namespace nano
{
/**
Expand Down
43 changes: 43 additions & 0 deletions nano/lib/ipc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <nano/lib/ipc.hpp>

nano::ipc::socket_base::socket_base (boost::asio::io_context & io_ctx_a) :
io_timer (io_ctx_a)
{
}

void nano::ipc::socket_base::timer_start (std::chrono::seconds timeout_a)
{
if (timeout_a < std::chrono::seconds::max ())
{
io_timer.expires_from_now (boost::posix_time::seconds (static_cast<long> (timeout_a.count ())));
io_timer.async_wait ([this](const boost::system::error_code & ec) {
if (!ec)
{
this->timer_expired ();
}
});
}
}

void nano::ipc::socket_base::timer_expired ()
{
close ();
}

void nano::ipc::socket_base::timer_cancel ()
{
boost::system::error_code ec;
io_timer.cancel (ec);
assert (!ec);
}

nano::ipc::dsock_file_remover::dsock_file_remover (std::string const & file_a) :
filename (file_a)
{
std::remove (filename.c_str ());
}

nano::ipc::dsock_file_remover::~dsock_file_remover ()
{
std::remove (filename.c_str ());
}
83 changes: 83 additions & 0 deletions nano/lib/ipc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

#include <atomic>
#include <boost/asio.hpp>
#include <boost/property_tree/ptree.hpp>
#include <string>

namespace nano
{
namespace ipc
{
/**
* The IPC framing format is simple: preamble followed by an encoding specific payload.
* Preamble is uint8_t {'N', encoding_type, reserved, reserved}. Reserved bytes MUST be zero.
* @note This is intentionally not an enum class as the values are only used as vector indices.
*/
enum preamble_offset
{
/** Always 'N' */
lead = 0,
/** One of the payload_encoding values */
encoding = 1,
/** Always zero */
reserved_1 = 2,
/** Always zero */
reserved_2 = 3,
};

/** Abstract base type for sockets, implementing timer logic and a close operation */
class socket_base
{
public:
socket_base (boost::asio::io_context & io_ctx_a);
virtual ~socket_base () = default;

/** Close socket */
virtual void close () = 0;

/**
* Start IO timer.
* @param timeout_a Seconds to wait. To wait indefinitely, use std::chrono::seconds::max ()
*/
void timer_start (std::chrono::seconds timeout_a);
void timer_expired ();
void timer_cancel ();

private:
/** IO operation timer */
boost::asio::deadline_timer io_timer;
};

/**
* Payload encodings; add protobuf, flatbuffers and so on as needed.
*/
enum class payload_encoding : uint8_t
{
/**
* Request is preamble followed by 32-bit BE payload length and payload bytes.
* Response is 32-bit BE payload length followed by payload bytes.
*/
json_legacy = 1
};

/** IPC transport interface */
class transport
{
public:
virtual void stop () = 0;
virtual ~transport () = default;
};

/** The domain socket file is attempted to be removed at both startup and shutdown. */
class dsock_file_remover final
{
public:
dsock_file_remover (std::string const & file_a);
~dsock_file_remover ();

private:
std::string filename;
};
}
}
Loading