Skip to content

Commit

Permalink
Add include_only_confirmed option to rpc accounts_balances (#4074)
Browse files Browse the repository at this point in the history
The RPC accounts_balances returned possibly unconfirmed data.
Now it returns confirmed data only, by default.
Unconfirmed data can still be got by setting include_only_confirmed=false.

resolves #3797
  • Loading branch information
dsiganos authored Jan 25, 2023
1 parent 0812626 commit d98f2bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,8 @@ void nano::json_handler::accounts_balances ()
auto account = account_impl (account_from_request.second.data ());
if (!ec)
{
auto balance = node.balance_pending (account, false);
bool const include_only_confirmed = request.get<bool> ("include_only_confirmed", true);
auto balance = node.balance_pending (account, include_only_confirmed);
entry.put ("balance", balance.first.convert_to<std::string> ());
entry.put ("pending", balance.second.convert_to<std::string> ());
entry.put ("receivable", balance.second.convert_to<std::string> ());
Expand Down
27 changes: 21 additions & 6 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3498,6 +3498,7 @@ TEST (rpc, accounts_balances)
/**
* Test the case where an account has no blocks at all (unopened) but has receivables
* In other words, sending to an a unopened account without receiving the funds
* It also checks the operation of the include_only_confirmed flag
*/
TEST (rpc, accounts_balances_unopened_account_with_receivables)
{
Expand All @@ -3516,7 +3517,7 @@ TEST (rpc, accounts_balances_unopened_account_with_receivables)
.work (*node->work_generate_blocking (nano::dev::genesis->hash ()))
.build_shared ();
{
auto transaction (node->store.tx_begin_write ());
auto transaction = node->store.tx_begin_write ();
ASSERT_EQ (nano::process_result::progress, node->ledger.process (transaction, *send).code);
}
ASSERT_TIMELY (5s, node->block (send->hash ()));
Expand All @@ -3530,12 +3531,26 @@ TEST (rpc, accounts_balances_unopened_account_with_receivables)
accounts_l.push_back (std::make_pair ("", entry));
request.add_child ("accounts", accounts_l);
request.put ("action", "accounts_balances");
auto response (wait_response (system, rpc_ctx, request));

// Check receivable amount
auto genesis_entry = response.get_child ("balances." + unopened_account.pub.to_account ());
ASSERT_EQ ("0", genesis_entry.get<std::string> ("balance"));
ASSERT_EQ ("1", genesis_entry.get<std::string> ("receivable"));
// Check confirmed receivable amount
auto response = wait_response (system, rpc_ctx, request);
auto response_entry = response.get_child ("balances." + unopened_account.pub.to_account ());
ASSERT_EQ ("0", response_entry.get<std::string> ("balance"));
ASSERT_EQ ("0", response_entry.get<std::string> ("receivable"));

// check unconfirmed receivable amount
request.put ("include_only_confirmed", "false");
response = wait_response (system, rpc_ctx, request);
response_entry = response.get_child ("balances." + unopened_account.pub.to_account ());
ASSERT_EQ ("0", response_entry.get<std::string> ("balance"));
ASSERT_EQ ("1", response_entry.get<std::string> ("receivable"));

// check confirmed receivable amount by explicitly setting include_only_confirmed
request.put ("include_only_confirmed", "true");
response = wait_response (system, rpc_ctx, request);
response_entry = response.get_child ("balances." + unopened_account.pub.to_account ());
ASSERT_EQ ("0", response_entry.get<std::string> ("balance"));
ASSERT_EQ ("0", response_entry.get<std::string> ("receivable"));
}

// Tests the happy path of retrieving an account's representative
Expand Down

0 comments on commit d98f2bb

Please sign in to comment.