Skip to content

Commit

Permalink
A simpler test case for accounts_frontiers
Browse files Browse the repository at this point in the history
  • Loading branch information
dsiganos committed Apr 14, 2022
1 parent 6e40140 commit 2497b27
Showing 1 changed file with 22 additions and 70 deletions.
92 changes: 22 additions & 70 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3175,97 +3175,49 @@ TEST (rpc, accounts_representatives)
ASSERT_EQ (response_representative, nano::dev::genesis->account ().to_account ());
}

/**
* 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_frontiers)
{
nano::system system;
auto node = add_ipc_enabled_node (system);
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
request.put ("action", "accounts_frontiers");
boost::property_tree::ptree entry;
boost::property_tree::ptree accounts_l;
entry.put ("", nano::dev::genesis_key.pub.to_account ());
accounts_l.push_back (std::make_pair ("", entry));
request.add_child ("accounts", accounts_l);
auto response (wait_response (system, rpc_ctx, request));
for (auto & frontiers : response.get_child ("frontiers"))
{
std::string account_text (frontiers.first);
ASSERT_EQ (nano::dev::genesis_key.pub.to_account (), account_text);
std::string frontier_text (frontiers.second.get<std::string> (""));
ASSERT_EQ (node->latest (nano::dev::genesis->account ()), nano::block_hash{ frontier_text });
}
}

// Tests the accounts_frontiers RPC, where its response has two errors from different types.
// response
// {
// "frontiers": {
// "nano_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo": "04270D7F11C4B2B472F2854C5A59F2A7E84226CE9ED799DE75744BD7D85FC9D9",
// "nano_36uccgpjzhjsdbj44wm1y5hyz8gefx3wjpp1jircxt84nopxkxti5bzq1rnz": "error: Bad account number",
// "nano_1hrts7hcoozxccnffoq9hqhngnn9jz783usapejm57ejtqcyz9dpso1bibuy": "error: Account not found"
// }
// }
TEST (rpc, accounts_frontiers_per_account_result_with_errors)
{
nano::system system;
auto node = add_ipc_enabled_node (system);
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
request.put ("action", "accounts_frontiers");
boost::property_tree::ptree entry1, entry2, entry3;
boost::property_tree::ptree accounts_l;

// Adding a key that will be found.
boost::property_tree::ptree entry1;
entry1.put ("", nano::dev::genesis_key.pub.to_account ());
accounts_l.push_back (std::make_pair ("", entry1));

// Adding a bad account that will trigger an error.
auto weird_account = [] () {
// Creates a weird key with two different key parts, so it is invalid to be used.
nano::keypair key1, key2;
return key1.pub.to_account ().substr (0, 40) + key2.pub.to_account ().substr (40, key2.pub.to_account ().size ());
};
auto const bad_account = weird_account ();
entry2.put ("", bad_account);
boost::property_tree::ptree entry2;
entry2.put ("", "nano_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtd1");
accounts_l.push_back (std::make_pair ("", entry2));

// Adding a random new key that won't be found as it isn't on the ledger.
nano::keypair random_key;
entry3.put ("", random_key.pub.to_account ());
boost::property_tree::ptree entry3;
entry3.put ("", "nano_1os6txqxyuesnxrtshnfb5or1hesc1647wpk9rsr84pmki6eairwha79hk3j");
accounts_l.push_back (std::make_pair ("", entry3));

// Pack the account entries.
request.add_child ("accounts", accounts_l);
auto response (wait_response (system, rpc_ctx, request));

auto & frontiers = response.get_child ("frontiers");
auto genesis_entry = frontiers.get_value<std::string> (nano::dev::genesis_key.pub.to_account ());
// create a map of expected replies, everytime we receive and echeck a reply, we remove it from this map
// in the end, this container should be empty, which would signify that all 3 replies were received correctly
std::map<std::string, std::string> reply_map{
{ nano::dev::genesis_key.pub.to_account (), nano::dev::genesis->hash ().to_string () },
{ "nano_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtd1", "error: Bad account number" },
{ "nano_1os6txqxyuesnxrtshnfb5or1hesc1647wpk9rsr84pmki6eairwha79hk3j", "error: Account not found" },
};

std::unordered_map<std::string, std::string> frontiers_read;
for (auto & frontiers : response.get_child ("frontiers"))
for (auto & frontier : response.get_child ("frontiers"))
{
frontiers_read.emplace (frontiers.first, frontiers.second.get<std::string> (""));
std::string account_text = frontier.first;
std::string frontier_text = frontier.second.get<std::string> ("");
ASSERT_EQ (frontier_text, reply_map[account_text]);
reply_map.erase (account_text);
}

// Checks the response for the valid account present on the ledger is correct.
auto genesis_it = frontiers_read.find (nano::dev::genesis_key.pub.to_account ());
ASSERT_TRUE (genesis_it != frontiers_read.end ());
ASSERT_EQ (node->latest (nano::dev::genesis->account ()), nano::block_hash{ genesis_it->second });

// Checks the response for the bad account has the proper error.
auto bad_account_it = frontiers_read.find (bad_account);
ASSERT_TRUE (bad_account_it != frontiers_read.end ());
std::error_code ec_bad_account = nano::error_common::bad_account_number;
ASSERT_EQ (boost::str (boost::format ("error: %1%") % ec_bad_account.message ()), bad_account_it->second);

// Checks the response for the not found account has the proper error.
auto account_not_found_it = frontiers_read.find (random_key.pub.to_account ());
ASSERT_TRUE (account_not_found_it != frontiers_read.end ());
std::error_code ec_account_not_found = nano::error_common::account_not_found;
ASSERT_EQ (boost::str (boost::format ("error: %1%") % ec_account_not_found.message ()), account_not_found_it->second);
// we expect all replies to have been received and this container to be empty
ASSERT_EQ (reply_map.size (), 0);
}

TEST (rpc, accounts_pending)
Expand Down

0 comments on commit 2497b27

Please # to comment.