Improve accounts_representatives RPC
This commit is contained in:
parent
9d9cee2b3d
commit
e98d99ea1c
2 changed files with 27 additions and 26 deletions
|
|
@ -924,6 +924,7 @@ void nano::json_handler::accounts_balances ()
|
||||||
void nano::json_handler::accounts_representatives ()
|
void nano::json_handler::accounts_representatives ()
|
||||||
{
|
{
|
||||||
boost::property_tree::ptree representatives;
|
boost::property_tree::ptree representatives;
|
||||||
|
boost::property_tree::ptree errors;
|
||||||
auto transaction = node.store.tx_begin_read ();
|
auto transaction = node.store.tx_begin_read ();
|
||||||
for (auto & account_from_request : request.get_child ("accounts"))
|
for (auto & account_from_request : request.get_child ("accounts"))
|
||||||
{
|
{
|
||||||
|
|
@ -937,10 +938,15 @@ void nano::json_handler::accounts_representatives ()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
representatives.put (account_from_request.second.data (), boost::format ("error: %1%") % ec.message ());
|
debug_assert (ec);
|
||||||
|
errors.put (account_from_request.second.data (), ec.message ());
|
||||||
ec = {};
|
ec = {};
|
||||||
}
|
}
|
||||||
response_l.add_child ("representatives", representatives);
|
response_l.add_child ("representatives", representatives);
|
||||||
|
if (!errors.empty ())
|
||||||
|
{
|
||||||
|
response_l.add_child ("errors", errors);
|
||||||
|
}
|
||||||
response_errors ();
|
response_errors ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3017,12 +3017,14 @@ TEST (rpc, accounts_representatives)
|
||||||
// Ensures the response is correct.
|
// Ensures the response is correct.
|
||||||
auto response_representative (response.get_child ("representatives").get<std::string> (nano::dev::genesis->account ().to_account ()));
|
auto response_representative (response.get_child ("representatives").get<std::string> (nano::dev::genesis->account ().to_account ()));
|
||||||
ASSERT_EQ (response_representative, nano::dev::genesis->account ().to_account ());
|
ASSERT_EQ (response_representative, nano::dev::genesis->account ().to_account ());
|
||||||
|
|
||||||
|
ASSERT_EQ (response.count ("errors"), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the RPC accounts_frontiers with 3 accounts, one good one, one with an invalid account ID and one with an account that does not exist.
|
* Test the RPC accounts_frontiers with 3 accounts, one good one, one with an invalid account ID and one with an account that does not exist.
|
||||||
*/
|
*/
|
||||||
TEST (rpc, accounts_representatives_per_account_result_with_errors)
|
TEST (rpc, accounts_representatives_with_errors)
|
||||||
{
|
{
|
||||||
nano::test::system system;
|
nano::test::system system;
|
||||||
auto node = add_ipc_enabled_node (system);
|
auto node = add_ipc_enabled_node (system);
|
||||||
|
|
@ -3038,38 +3040,31 @@ TEST (rpc, accounts_representatives_per_account_result_with_errors)
|
||||||
|
|
||||||
// Adds an invalid account, malformed number with a wrong checksum.
|
// Adds an invalid account, malformed number with a wrong checksum.
|
||||||
// Got with this formula: key1.substr(0, 40) + key2.substr(40, key2.size()).
|
// Got with this formula: key1.substr(0, 40) + key2.substr(40, key2.size()).
|
||||||
auto const invalid_key = "nano_36uccgpjzhjsdbj44wm1y5hyz8gefx3wjpp1jircxt84nopxkxti5bzq1rnz";
|
auto const bad_account_number = "nano_36uccgpjzhjsdbj44wm1y5hyz8gefx3wjpp1jircxt84nopxkxti5bzq1rnz";
|
||||||
entry2.put ("", invalid_key);
|
entry2.put ("", bad_account_number);
|
||||||
accounts_l.push_back (std::make_pair ("", entry2));
|
accounts_l.push_back (std::make_pair ("", entry2));
|
||||||
|
|
||||||
// Adds a valid key but that isn't on the ledger. It wont'be found.
|
// Adds a valid key but that isn't on the ledger. It won't be found.
|
||||||
auto const valid_key = "nano_1hrts7hcoozxccnffoq9hqhngnn9jz783usapejm57ejtqcyz9dpso1bibuy";
|
auto const account_not_found = "nano_1hrts7hcoozxccnffoq9hqhngnn9jz783usapejm57ejtqcyz9dpso1bibuy";
|
||||||
entry3.put ("", valid_key);
|
entry3.put ("", account_not_found);
|
||||||
accounts_l.push_back (std::make_pair ("", entry3));
|
accounts_l.push_back (std::make_pair ("", entry3));
|
||||||
|
|
||||||
// Packs all the account entries.
|
// Packs all the account entries.
|
||||||
request.add_child ("accounts", accounts_l);
|
request.add_child ("accounts", accounts_l);
|
||||||
auto response (wait_response (system, rpc_ctx, request));
|
auto response (wait_response (system, rpc_ctx, request));
|
||||||
|
|
||||||
auto get_error_message = [] (nano::error_common error_common) -> std::string {
|
ASSERT_EQ (response.count ("representatives"), 1);
|
||||||
std::error_code ec = error_common;
|
ASSERT_EQ (response.get_child ("representatives").size (), 1);
|
||||||
return boost::str (boost::format ("error: %1%") % ec.message ());
|
ASSERT_EQ (response.get_child ("representatives").count (nano::dev::genesis_key.pub.to_account ()), 1);
|
||||||
};
|
auto rep_text = response.get_child ("representatives").get<std::string> (nano::dev::genesis_key.pub.to_account ());
|
||||||
|
ASSERT_EQ (rep_text, nano::dev::genesis->account ().to_account ());
|
||||||
|
|
||||||
std::map<std::string, std::string> reply_map{
|
ASSERT_EQ (response.count ("errors"), 1);
|
||||||
{ nano::dev::genesis_key.pub.to_account (), nano::dev::genesis->account ().to_account () },
|
ASSERT_EQ (response.get_child ("errors").size (), 2);
|
||||||
{ invalid_key, get_error_message (nano::error_common::bad_account_number) },
|
ASSERT_EQ (response.get_child ("errors").count (bad_account_number), 1);
|
||||||
{ valid_key, get_error_message (nano::error_common::account_not_found) }
|
ASSERT_EQ (response.get_child ("errors").count (account_not_found), 1);
|
||||||
};
|
ASSERT_EQ (response.get_child ("errors").get<std::string> (bad_account_number), make_error_code (nano::error_common::bad_account_number).message ());
|
||||||
|
ASSERT_EQ (response.get_child ("errors").get<std::string> (account_not_found), make_error_code (nano::error_common::account_not_found).message ());
|
||||||
for (auto & representative : response.get_child ("representatives"))
|
|
||||||
{
|
|
||||||
std::string account_text = representative.first;
|
|
||||||
std::string frontier_text = representative.second.get<std::string> ("");
|
|
||||||
ASSERT_EQ (frontier_text, reply_map[account_text]);
|
|
||||||
reply_map.erase (account_text);
|
|
||||||
}
|
|
||||||
ASSERT_EQ (reply_map.size (), 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST (rpc, accounts_frontiers)
|
TEST (rpc, accounts_frontiers)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue