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

Fix error with work_cancel RPC request #2916

Merged
merged 2 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions nano/core_test/fakes/work_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ enum class work_peer_type
class work_peer_connection : public std::enable_shared_from_this<work_peer_connection>
{
const std::string generic_error = "Unable to parse JSON";
const std::string empty_response = "Empty response";

public:
work_peer_connection (asio::io_context & ioc_a, work_peer_type const type_a, nano::work_version const version_a, nano::work_pool & pool_a, std::function<void(bool const)> on_generation_a, std::function<void()> on_cancel_a) :
Expand Down Expand Up @@ -126,6 +125,16 @@ class work_peer_connection : public std::enable_shared_from_this<work_peer_conne
beast::ostream (response.body ()) << ostream.str ();
}

void handle_cancel ()
{
on_cancel ();
ptree::ptree message_l;
message_l.put ("success", "");
std::stringstream ostream;
ptree::write_json (ostream, message_l);
beast::ostream (response.body ()) << ostream.str ();
}

void handle_generate (nano::block_hash const & hash_a)
{
if (type == work_peer_type::good)
Expand Down Expand Up @@ -176,9 +185,7 @@ class work_peer_connection : public std::enable_shared_from_this<work_peer_conne
}
else if (action_text == "work_cancel")
{
error (empty_response);
on_cancel ();
write_response ();
handle_cancel ();
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions nano/lib/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ std::string nano::error_rpc_messages::message (int ev) const
{
case nano::error_rpc::generic:
return "Unknown error";
case nano::error_rpc::empty_response:
return "Empty response";
case nano::error_rpc::bad_destination:
return "Bad destination account";
case nano::error_rpc::bad_difficulty_format:
Expand Down
1 change: 1 addition & 0 deletions nano/lib/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ enum class error_blocks
enum class error_rpc
{
generic = 1,
empty_response,
bad_destination,
bad_difficulty_format,
bad_key,
Expand Down
10 changes: 8 additions & 2 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,15 @@ void nano::json_handler::process_request (bool unsafe_a)

void nano::json_handler::response_errors ()
{
if (ec || response_l.empty ())
if (!ec && response_l.empty ())
{
// Return an error code if no response data was given
ec = nano::error_rpc::empty_response;
}
if (ec)
{
boost::property_tree::ptree response_error;
response_error.put ("error", ec ? ec.message () : "Empty response");
response_error.put ("error", ec.message ());
std::stringstream ostream;
boost::property_tree::write_json (ostream, response_error);
response (ostream.str ());
Expand Down Expand Up @@ -4861,6 +4866,7 @@ void nano::json_handler::work_cancel ()
if (!ec)
{
node.observers.work_cancel.notify (hash);
response_l.put ("success", "");
}
response_errors ();
}
Expand Down
2 changes: 2 additions & 0 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2999,6 +2999,8 @@ TEST (rpc, work_cancel)
ASSERT_TIMELY (10s, response1.status != 0);
ASSERT_EQ (200, response1.status);
ASSERT_NO_ERROR (ec);
std::string success (response1.json.get<std::string> ("success"));
ASSERT_TRUE (success.empty ());
}
}

Expand Down