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

Support external RPC servers via IPC #1434

Merged
merged 15 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_executable (core_test
block.cpp
block_store.cpp
interface.cpp
ipc.cpp
conflicts.cpp
daemon.cpp
entry.cpp
Expand Down
87 changes: 87 additions & 0 deletions nano/core_test/ipc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <boost/property_tree/json_parser.hpp>
#include <chrono>
#include <gtest/gtest.h>
#include <memory>
#include <nano/core_test/testutil.hpp>
#include <nano/node/ipc.hpp>
#include <nano/node/rpc.hpp>
#include <nano/node/testing.hpp>
#include <sstream>
#include <vector>

using namespace std::chrono_literals;

TEST (ipc, asynchronous)
{
nano::system system (24000, 1);
nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true));
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::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 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) {
client.async_write (req, [&client, &req, &res, &call_completed](nano::error err_a, size_t size_a) {
ASSERT_NO_ERROR (static_cast<std::error_code> (err_a));
ASSERT_EQ (size_a, req->size ());
// Read length
client.async_read (res, sizeof (uint32_t), [&client, &res, &call_completed](nano::error err_read_a, size_t size_read_a) {
ASSERT_NO_ERROR (static_cast<std::error_code> (err_read_a));
ASSERT_EQ (size_read_a, sizeof (uint32_t));
uint32_t payload_size_l = boost::endian::big_to_native (*reinterpret_cast<uint32_t *> (res->data ()));
// Read json payload
client.async_read (res, payload_size_l, [&res, &call_completed](nano::error err_read_a, size_t size_read_a) {
std::string payload (res->begin (), res->end ());
std::stringstream ss;
ss << payload;

// Make sure the response is valid json
boost::property_tree::ptree blocks;
boost::property_tree::read_json (ss, blocks);
ASSERT_EQ (blocks.get<int> ("count"), 1);
call_completed = true;
});
});
});
});
system.deadline_set (5s);
while (!call_completed)
{
ASSERT_NO_ERROR (system.poll ());
}
}

TEST (ipc, synchronous)
{
nano::system system (24000, 1);
nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true));
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);

// 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::stringstream ss;
ss << response;
// Make sure the response is valid json
boost::property_tree::ptree blocks;
boost::property_tree::read_json (ss, blocks);
ASSERT_EQ (blocks.get<int> ("count"), 1);

call_completed = true;
});
client_thread.detach ();

system.deadline_set (5s);
while (!call_completed)
{
ASSERT_NO_ERROR (system.poll ());
}
}
13 changes: 7 additions & 6 deletions nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <nano/lib/jsonconfig.hpp>
#include <nano/lib/utility.hpp>
#include <nano/nano_node/daemon.hpp>

#include <boost/property_tree/json_parser.hpp>
#include <fstream>
#include <iostream>
#include <nano/lib/jsonconfig.hpp>
#include <nano/lib/utility.hpp>
#include <nano/nano_node/daemon.hpp>
#include <nano/node/ipc.hpp>
#include <nano/node/working.hpp>

nano_daemon::daemon_config::daemon_config () :
Expand Down Expand Up @@ -139,10 +139,11 @@ void nano_daemon::daemon::run (boost::filesystem::path const & data_path, nano::
node->flags = flags;
node->start ();
std::unique_ptr<nano::rpc> rpc = get_rpc (io_ctx, *node, config.rpc);
if (rpc && config.rpc_enable)
if (rpc)
{
rpc->start ();
rpc->start (config.rpc_enable);
}
nano::ipc::ipc_server ipc (*node, *rpc);
runner = std::make_unique<nano::thread_runner> (io_ctx, node->config.io_threads);
runner->join ();
}
Expand Down
7 changes: 5 additions & 2 deletions nano/nano_wallet/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <nano/lib/utility.hpp>
#include <nano/nano_wallet/icon.hpp>
#include <nano/node/cli.hpp>
#include <nano/node/ipc.hpp>
#include <nano/node/rpc.hpp>
#include <nano/node/working.hpp>
#include <nano/qt/qt.hpp>
Expand Down Expand Up @@ -277,12 +278,14 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost
update_config (config, config_path);
node->start ();
std::unique_ptr<nano::rpc> rpc = get_rpc (io_ctx, *node, config.rpc);
if (rpc && config.rpc_enable)
if (rpc)
{
rpc->start ();
rpc->start (config.rpc_enable);
}
nano::ipc::ipc_server ipc (*node, *rpc);
nano::thread_runner runner (io_ctx, node->config.io_threads);
QObject::connect (&application, &QApplication::aboutToQuit, [&]() {
ipc.stop ();
rpc->stop ();
node->stop ();
});
Expand Down
2 changes: 2 additions & 0 deletions nano/node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ add_library (node
cli.cpp
common.cpp
common.hpp
ipc.hpp
ipc.cpp
lmdb.cpp
lmdb.hpp
logging.cpp
Expand Down
Loading