Skip to content

Commit

Permalink
Merging pull request to add republish as an RPC.
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu committed May 29, 2017
1 parent 52c7f3e commit 094708d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
28 changes: 28 additions & 0 deletions rai/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1931,3 +1931,31 @@ TEST (rpc, bootstrap_any)
std::string success (response.json.get <std::string> ("success"));
ASSERT_TRUE (success.empty());
}

TEST (rpc, republish)
{
rai::system system (24000, 2);
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));
system.nodes [0]->process (send);
rai::rpc rpc (system.service, node1, rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "republish");
request.put ("hash", send.hash ().to_string ());
test_response response (request, rpc, system.service);
while (response.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response.status);
auto iterations (0);
while (system.nodes[1]->balance (rai::test_genesis_key.pub) == rai::genesis_amount)
{
system.poll ();
++iterations;
ASSERT_GT (200, iterations);
}
}
36 changes: 36 additions & 0 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,38 @@ void rai::rpc_handler::representatives ()
response (response_l);
}

void rai::rpc_handler::republish ()
{
std::string hash_text (request.get <std::string> ("hash"));
rai::uint256_union hash;
auto error (hash.decode_hex (hash_text));
if (!error)
{
rai::transaction transaction (node.store.environment, nullptr, false);
auto block (node.store.block_get (transaction, hash));
if (block != nullptr)
{
while (!hash.is_zero ())
{
block = node.store.block_get (transaction, hash);
node.network.republish_block (*block);
hash = node.store.block_successor (transaction, hash);
}
boost::property_tree::ptree response_l;
response_l.put ("success", "");
response (response_l);
}
else
{
error_response (response, "Block not found");
}
}
else
{
error_response (response, "Bad hash number");
}
}

void rai::rpc_handler::search_pending ()
{
if (rpc.config.enable_control)
Expand Down Expand Up @@ -2224,6 +2256,10 @@ void rai::rpc_handler::process_request ()
{
representatives ();
}
else if (action == "republish")
{
republish ();
}
else if (action == "search_pending")
{
search_pending ();
Expand Down
1 change: 1 addition & 0 deletions rai/node/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class rpc_handler : public std::enable_shared_from_this <rai::rpc_handler>
void rai_to_raw ();
void rai_from_raw ();
void representatives ();
void republish ();
void search_pending ();
void send ();
void stop ();
Expand Down

0 comments on commit 094708d

Please sign in to comment.