Skip to content

Commit

Permalink
Add RPC block_hash action (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower authored and clemahieu committed Apr 30, 2018
1 parent e09e93d commit 52a053d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rai/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3328,6 +3328,30 @@ TEST (rpc, block_create_state_request_work)
}
}

TEST (rpc, block_hash)
{
rai::system system (24000, 1);
rai::keypair key;
auto latest (system.nodes[0]->latest (rai::test_genesis_key.pub));
auto & node1 (*system.nodes[0]);
rai::send_block send (latest, key.pub, 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, node1.generate_work (latest));
rai::rpc rpc (system.service, node1, rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "block_hash");
std::string json;
send.serialize_json (json);
request.put ("block", json);
test_response response (request, rpc, system.service);
while (response.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response.status);
std::string send_hash (response.json.get<std::string> ("hash"));
ASSERT_EQ (send.hash ().to_string (), send_hash);
}

TEST (rpc, wallet_lock)
{
rai::system system (24000, 1);
Expand Down
25 changes: 25 additions & 0 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,27 @@ void rai::rpc_handler::block_create ()
}
}

void rai::rpc_handler::block_hash ()
{
std::string block_text (request.get<std::string> ("block"));
boost::property_tree::ptree block_l;
std::stringstream block_stream (block_text);
boost::property_tree::read_json (block_stream, block_l);
block_l.put ("signature", "0");
block_l.put ("work", "0");
auto block (rai::deserialize_block_json (block_l));
if (block != nullptr)
{
boost::property_tree::ptree response_l;
response_l.put ("hash", block->hash ().to_string ());
response (response_l);
}
else
{
error_response (response, "Block is invalid");
}
}

void rai::rpc_handler::successors ()
{
std::string block_text (request.get<std::string> ("block"));
Expand Down Expand Up @@ -4696,6 +4717,10 @@ void rai::rpc_handler::process_request ()
{
block_create ();
}
else if (action == "block_hash")
{
block_hash ();
}
else if (action == "successors")
{
successors ();
Expand Down
1 change: 1 addition & 0 deletions rai/node/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class rpc_handler : public std::enable_shared_from_this<rai::rpc_handler>
void block_count ();
void block_count_type ();
void block_create ();
void block_hash ();
void bootstrap ();
void bootstrap_any ();
void chain ();
Expand Down

0 comments on commit 52a053d

Please sign in to comment.