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

RPC unopened #1727

Merged
merged 12 commits into from Feb 22, 2019
66 changes: 66 additions & 0 deletions nano/core_test/rpc.cpp
Expand Up @@ -4357,6 +4357,72 @@ TEST (rpc, stats_clear)
ASSERT_LE (system.nodes[0]->stats.last_reset ().count (), 5);
}

TEST (rpc, unopened)
{
nano::system system (24000, 1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
nano::keypair key;
auto genesis (system.nodes[0]->latest (nano::test_genesis_key.pub));
ASSERT_FALSE (genesis.is_zero ());
auto send (system.wallet (0)->send_action (nano::test_genesis_key.pub, key.pub, 1));
ASSERT_NE (nullptr, send);
nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "unopened");
test_response response (request, rpc, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
auto & accounts (response.json.get_child ("accounts"));
ASSERT_EQ (1, accounts.size ());
ASSERT_EQ ("1", accounts.get<std::string> (key.pub.to_account ()));
}

TEST (rpc, unopened_burn)
{
nano::system system (24000, 1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
auto genesis (system.nodes[0]->latest (nano::test_genesis_key.pub));
ASSERT_FALSE (genesis.is_zero ());
auto send (system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::burn_account, 1));
ASSERT_NE (nullptr, send);
nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "unopened");
test_response response (request, rpc, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
auto & accounts (response.json.get_child ("accounts"));
ASSERT_EQ (0, accounts.size ());
}

TEST (rpc, unopened_no_accounts)
{
nano::system system (24000, 1);
nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "unopened");
test_response response (request, rpc, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
auto & accounts (response.json.get_child ("accounts"));
ASSERT_EQ (0, accounts.size ());
}

TEST (rpc, uptime)
{
nano::system system (24000, 1);
Expand Down
51 changes: 51 additions & 0 deletions nano/node/rpc.cpp
Expand Up @@ -3314,6 +3314,53 @@ void nano::rpc_handler::unchecked_keys ()
response_errors ();
}

void nano::rpc_handler::unopened ()
{
rpc_control_impl ();
guilhermelawless marked this conversation as resolved.
Show resolved Hide resolved
auto transaction (node.store.tx_begin_read ());
auto iterator (node.store.pending_begin (transaction, nano::pending_key (1, 0))); // exclude burn account
auto end (node.store.pending_end ());
nano::account current_account;
nano::uint128_t current_account_sum{ 0 };
boost::property_tree::ptree accounts;
while (iterator != end)
{
nano::pending_key key (iterator->first);
nano::account account (key.account);
nano::pending_info info (iterator->second);
if (node.store.account_exists (transaction, account))
{
if (account.number () == std::numeric_limits<nano::uint256_t>::max ())
{
break;
}
// Skip existing accounts
iterator = node.store.pending_begin (transaction, nano::pending_key (account.number () + 1, 0));
guilhermelawless marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
if (account != current_account)
{
if (current_account_sum > 0)
{
accounts.put (current_account.to_account (), current_account_sum.convert_to<std::string> ());
current_account_sum = 0;
}
current_account = account;
}
current_account_sum += info.amount.number ();
++iterator;
}
}
// last one after iterator reaches end
if (current_account_sum > 0)
{
accounts.put (current_account.to_account (), current_account_sum.convert_to<std::string> ());
}
response_l.add_child ("accounts", accounts);
response_errors ();
}

void nano::rpc_handler::uptime ()
{
response_l.put ("seconds", std::chrono::duration_cast<std::chrono::seconds> (std::chrono::steady_clock::now () - node.startup_time).count ());
Expand Down Expand Up @@ -4607,6 +4654,10 @@ void nano::rpc_handler::process_request ()
{
unchecked_keys ();
}
else if (action == "unopened")
{
unopened ();
}
else if (action == "uptime")
{
uptime ();
Expand Down
1 change: 1 addition & 0 deletions nano/node/rpc.hpp
Expand Up @@ -206,6 +206,7 @@ class rpc_handler : public std::enable_shared_from_this<nano::rpc_handler>
void unchecked_clear ();
void unchecked_get ();
void unchecked_keys ();
void unopened ();
void uptime ();
void validate_account_number ();
void version ();
Expand Down