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

Extend 'wallet_info' RPC command #3277

Merged
merged 4 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
28 changes: 25 additions & 3 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4208,15 +4208,32 @@ void nano::json_handler::wallet_info ()
nano::uint128_t balance (0);
nano::uint128_t pending (0);
uint64_t count (0);
uint64_t block_count (0);
uint64_t cemented_block_count (0);
uint64_t deterministic_count (0);
uint64_t adhoc_count (0);
auto transaction (node.wallets.tx_begin_read ());
auto block_transaction (node.store.tx_begin_read ());

for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i)
{
nano::account const & account (i->first);
balance = balance + node.ledger.account_balance (block_transaction, account);
pending = pending + node.ledger.account_pending (block_transaction, account);

nano::account_info account_info{};
if (!node.store.account_get (block_transaction, account, account_info))
{
block_count += account_info.block_count;
}

nano::confirmation_height_info confirmation_info{};
if (!node.store.confirmation_height_get (block_transaction, account, confirmation_info))
{
cemented_block_count += confirmation_info.height;
}

balance += node.ledger.account_balance (block_transaction, account);
theohax marked this conversation as resolved.
Show resolved Hide resolved
pending += node.ledger.account_pending (block_transaction, account);

nano::key_type key_type (wallet->store.key_type (i->second));
if (key_type == nano::key_type::deterministic)
{
Expand All @@ -4226,16 +4243,21 @@ void nano::json_handler::wallet_info ()
{
adhoc_count++;
}
count++;

++count;
}

uint32_t deterministic_index (wallet->store.deterministic_index_get (transaction));
response_l.put ("balance", balance.convert_to<std::string> ());
response_l.put ("pending", pending.convert_to<std::string> ());
response_l.put ("accounts_count", std::to_string (count));
response_l.put ("accounts_block_count", std::to_string (block_count));
response_l.put ("accounts_cemented_block_count", std::to_string (cemented_block_count));
response_l.put ("deterministic_count", std::to_string (deterministic_count));
response_l.put ("adhoc_count", std::to_string (adhoc_count));
response_l.put ("deterministic_index", std::to_string (deterministic_index));
}

response_errors ();
}

Expand Down
25 changes: 23 additions & 2 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4097,11 +4097,28 @@ TEST (rpc, blocks)
TEST (rpc, wallet_info)
{
nano::system system;
auto node = add_ipc_enabled_node (system);
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.enable_voting = true;
auto node = add_ipc_enabled_node (system, node_config);
system.wallet (0)->insert_adhoc (nano::dev_genesis_key.prv);
nano::keypair key;
system.wallet (0)->insert_adhoc (key.prv);
auto send (system.wallet (0)->send_action (nano::dev_genesis_key.pub, key.pub, 1));

// at first, 1 block and 1 confirmed -- the genesis
ASSERT_EQ (1, node->ledger.cache.block_count);
ASSERT_EQ (1, node->ledger.cache.cemented_count);

auto send (system.wallet (0)->send_action (nano::dev_genesis_key.pub, key.pub, nano::Gxrb_ratio));
// after the send, expect 2 blocks immediately, then 2 confirmed in a timely manner,
// and finally 3 blocks and 3 confirmed after the wallet generates the receive block for this send
ASSERT_EQ (2, node->ledger.cache.block_count);
ASSERT_TIMELY (5s, 2 == node->ledger.cache.cemented_count);
ASSERT_TIMELY (5s, 3 == node->ledger.cache.block_count && 3 == node->ledger.cache.cemented_count);

// do another send to be able to expect some "pending" down below
auto send2 (system.wallet (0)->send_action (nano::dev_genesis_key.pub, key.pub, 1));
ASSERT_EQ (4, node->ledger.cache.block_count);

nano::account account (system.wallet (0)->deterministic_insert ());
{
auto transaction (node->wallets.tx_begin_write ());
Expand All @@ -4128,6 +4145,10 @@ TEST (rpc, wallet_info)
ASSERT_EQ ("1", pending_text);
std::string count_text (response.json.get<std::string> ("accounts_count"));
ASSERT_EQ ("3", count_text);
std::string block_count_text (response.json.get<std::string> ("accounts_block_count"));
ASSERT_EQ ("4", block_count_text);
std::string cemented_block_count_text (response.json.get<std::string> ("accounts_cemented_block_count"));
ASSERT_EQ ("3", cemented_block_count_text);
std::string adhoc_count (response.json.get<std::string> ("adhoc_count"));
ASSERT_EQ ("2", adhoc_count);
std::string deterministic_count (response.json.get<std::string> ("deterministic_count"));
Expand Down