Adding block_confirm RPC.

This commit is contained in:
clemahieu 2018-04-25 20:27:04 +02:00
commit ded0ca58a4
3 changed files with 74 additions and 0 deletions

View file

@ -3509,3 +3509,46 @@ TEST (rpc, confirmation_history)
ASSERT_EQ ((rai::genesis_amount - rai::Gxrb_ratio).convert_to<std::string> (), tally);
system.stop ();
}
TEST (rpc, block_confirm)
{
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::genesis genesis;
system.nodes [0]->ledger.state_block_parse_canary = genesis.hash ();
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
auto send1 (std::make_shared <rai::state_block> (rai::test_genesis_key.pub, genesis.hash (), rai::test_genesis_key.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.nodes [0]->generate_work (genesis.hash ())));
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->ledger.process (transaction, *send1).code);
}
rai::rpc rpc (system.service, *system.nodes[0], rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "block_confirm");
request.put ("hash", send1->hash ().to_string ());
test_response response (request, rpc, system.service);
while (response.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response.status);
ASSERT_EQ ("1", response.json.get <std::string> ("started"));
}
TEST (rpc, block_confirm_absent)
{
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv); rai::rpc rpc (system.service, *system.nodes[0], rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "block_confirm");
request.put ("hash", "0");
test_response response (request, rpc, system.service);
while (response.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response.status);
ASSERT_EQ ("Block not found", response.json.get <std::string> ("error"));
}

View file

@ -899,6 +899,32 @@ void rai::rpc_handler::block ()
}
}
void rai::rpc_handler::block_confirm ()
{
std::string hash_text (request.get<std::string> ("hash"));
rai::block_hash hash_l;
if (!hash_l.decode_hex (hash_text))
{
rai::transaction transaction (node.store.environment, nullptr, false);
auto block_l (node.store.block_get (transaction, hash_l));
if (block_l != nullptr)
{
node.block_confirm (std::move (block_l));
boost::property_tree::ptree response_l;
response_l.put ("started", "1");
response (response_l);
}
else
{
error_response (response, "Block not found");
}
}
else
{
error_response (response, "Invalid block hash");
}
}
void rai::rpc_handler::blocks ()
{
std::vector<std::string> hashes;
@ -4597,6 +4623,10 @@ void rai::rpc_handler::process_request ()
{
block ();
}
else if (action == "block_confirm")
{
block_confirm ();
}
else if (action == "blocks")
{
blocks ();

View file

@ -133,6 +133,7 @@ public:
void accounts_pending ();
void available_supply ();
void block ();
void block_confirm ();
void blocks ();
void blocks_info ();
void block_account ();