From c288bb132743e31f282996bb87ee554f27a7ed0a Mon Sep 17 00:00:00 2001 From: clemahieu Date: Wed, 22 Mar 2023 02:17:27 +0000 Subject: [PATCH] RPC test cleanup rpc.accounts_receivable (#4196) * Move rpc tests related to receivables in to their own file. * Removing unneeded line. * Splitting rpc.accounts_receivable in to multiple tests. --- nano/rpc_test/receivable.cpp | 173 ++++++++++++++++++++++++++++++++++- nano/rpc_test/rpc.cpp | 106 --------------------- 2 files changed, 172 insertions(+), 107 deletions(-) diff --git a/nano/rpc_test/receivable.cpp b/nano/rpc_test/receivable.cpp index b94ae990e8..2a95715c60 100644 --- a/nano/rpc_test/receivable.cpp +++ b/nano/rpc_test/receivable.cpp @@ -130,7 +130,6 @@ TEST (rpc, receivable_unconfirmed) { nano::test::system system; auto node = add_ipc_enabled_node (system); - nano::thread_role::set (nano::thread_role::name::unknown); // thread_role::name::io is disallowed for performance reasons by write transactions. Set our thread to ::unknown. auto chain = nano::test::setup_chain (system, *node, 1, nano::dev::genesis_key, false); auto block1 = chain[0]; auto const rpc_ctx = add_rpc (system, node); @@ -376,3 +375,175 @@ TEST (rpc, search_receivable) auto response (wait_response (system, rpc_ctx, request)); ASSERT_TIMELY (10s, node->balance (nano::dev::genesis_key.pub) == nano::dev::constants.genesis_amount); } + +TEST (rpc, accounts_pending_deprecated) +{ + nano::test::system system; + auto node = add_ipc_enabled_node (system); + auto const rpc_ctx = add_rpc (system, node); + boost::property_tree::ptree request; + boost::property_tree::ptree child; + boost::property_tree::ptree accounts; + child.put ("", nano::dev::genesis_key.pub.to_account ()); + accounts.push_back (std::make_pair ("", child)); + request.add_child ("accounts", accounts); + request.put ("action", "accounts_pending"); + auto response (wait_response (system, rpc_ctx, request)); + ASSERT_EQ ("1", response.get ("deprecated")); +} + +TEST (rpc, accounts_receivable_blocks) +{ + nano::test::system system; + auto node = add_ipc_enabled_node (system); + auto chain = nano::test::setup_chain (system, *node, 1); + auto block1 = chain[0]; + ASSERT_TIMELY (5s, node->block_confirmed (block1->hash ())); + + auto const rpc_ctx = add_rpc (system, node); + boost::property_tree::ptree request; + request.put ("action", "accounts_receivable"); + boost::property_tree::ptree entry; + boost::property_tree::ptree peers_l; + entry.put ("", block1->link ().to_account ()); + peers_l.push_back (std::make_pair ("", entry)); + request.add_child ("accounts", peers_l); + auto response = wait_response (system, rpc_ctx, request); + for (auto & blocks : response.get_child ("blocks")) + { + std::string account_text{ blocks.first }; + ASSERT_EQ (block1->link ().to_account (), account_text); + nano::block_hash hash1{ blocks.second.begin ()->second.get ("") }; + ASSERT_EQ (block1->hash (), hash1); + } +} + +TEST (rpc, accounts_receivable_sorting) +{ + nano::test::system system; + auto node = add_ipc_enabled_node (system); + auto chain = nano::test::setup_chain (system, *node, 1); + auto block1 = chain[0]; + ASSERT_TIMELY (5s, node->block_confirmed (block1->hash ())); + + auto const rpc_ctx = add_rpc (system, node); + boost::property_tree::ptree request; + request.put ("action", "accounts_receivable"); + boost::property_tree::ptree entry; + boost::property_tree::ptree peers_l; + entry.put ("", block1->link ().to_account ()); + peers_l.push_back (std::make_pair ("", entry)); + request.add_child ("accounts", peers_l); + request.put ("sorting", "true"); // Sorting test + auto response = wait_response (system, rpc_ctx, request); + for (auto & blocks : response.get_child ("blocks")) + { + std::string account_text{ blocks.first }; + ASSERT_EQ (block1->link ().to_account (), account_text); + nano::block_hash hash1{ blocks.second.begin ()->first }; + ASSERT_EQ (block1->hash (), hash1); + std::string amount{ blocks.second.begin ()->second.get ("") }; + ASSERT_EQ ("1", amount); + } +} + +TEST (rpc, accounts_receivable_threshold) +{ + nano::test::system system; + auto node = add_ipc_enabled_node (system); + auto chain = nano::test::setup_chain (system, *node, 1); + auto block1 = chain[0]; + ASSERT_TIMELY (5s, node->block_confirmed (block1->hash ())); + + auto const rpc_ctx = add_rpc (system, node); + boost::property_tree::ptree request; + request.put ("action", "accounts_receivable"); + boost::property_tree::ptree entry; + boost::property_tree::ptree peers_l; + entry.put ("", block1->link ().to_account ()); + peers_l.push_back (std::make_pair ("", entry)); + request.add_child ("accounts", peers_l); + request.put ("threshold", "1"); // Threshold test + auto response = wait_response (system, rpc_ctx, request); + std::unordered_map blocks; + for (auto & pending : response.get_child ("blocks")) + { + std::string account_text{ pending.first }; + ASSERT_EQ (block1->link ().to_account (), account_text); + for (auto i (pending.second.begin ()), j (pending.second.end ()); i != j; ++i) + { + nano::block_hash hash; + hash.decode_hex (i->first); + nano::uint128_union amount; + amount.decode_dec (i->second.get ("")); + blocks[hash] = amount; + boost::optional source{ i->second.get_optional ("source") }; + ASSERT_FALSE (source.is_initialized ()); + } + } + ASSERT_EQ (blocks[block1->hash ()], 1); +} + +TEST (rpc, accounts_receivable_source) +{ + nano::test::system system; + auto node = add_ipc_enabled_node (system); + auto chain = nano::test::setup_chain (system, *node, 1, nano::dev::genesis_key); + auto block1 = chain[0]; + + auto const rpc_ctx = add_rpc (system, node); + boost::property_tree::ptree request; + request.put ("action", "accounts_receivable"); + boost::property_tree::ptree entry; + boost::property_tree::ptree peers_l; + entry.put ("", block1->link ().to_account ()); + peers_l.push_back (std::make_pair ("", entry)); + request.add_child ("accounts", peers_l); + request.put ("source", "true"); + { + auto response (wait_response (system, rpc_ctx, request)); + std::unordered_map amounts; + std::unordered_map sources; + for (auto & pending : response.get_child ("blocks")) + { + std::string account_text (pending.first); + ASSERT_EQ (block1->link ().to_account (), account_text); + for (auto i (pending.second.begin ()), j (pending.second.end ()); i != j; ++i) + { + nano::block_hash hash; + hash.decode_hex (i->first); + amounts[hash].decode_dec (i->second.get ("amount")); + sources[hash].decode_account (i->second.get ("source")); + } + } + ASSERT_EQ (amounts[block1->hash ()], 1); + ASSERT_EQ (sources[block1->hash ()], nano::dev::genesis_key.pub); + } +} + +TEST (rpc, accounts_receivable_confirmed) +{ + nano::test::system system; + auto node = add_ipc_enabled_node (system); + auto chain = nano::test::setup_chain (system, *node, 1, nano::dev::genesis_key, false); + auto block1 = chain[0]; + + auto const rpc_ctx = add_rpc (system, node); + boost::property_tree::ptree request; + request.put ("action", "accounts_receivable"); + boost::property_tree::ptree entry; + boost::property_tree::ptree peers_l; + entry.put ("", block1->link ().to_account ()); + peers_l.push_back (std::make_pair ("", entry)); + request.add_child ("accounts", peers_l); + + ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 0)); + request.put ("include_only_confirmed", "true"); + ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 0)); + request.put ("include_only_confirmed", "false"); + ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); + { + node->store.confirmation_height.put (node->store.tx_begin_write (), nano::dev::genesis_key.pub, { 2, block1->hash () }); + } + ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); +} diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index d6007c7a43..5009a10b35 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -3124,112 +3124,6 @@ TEST (rpc, accounts_frontiers) ASSERT_EQ (reply_map.size (), 0); } -TEST (rpc, accounts_pending) -{ - nano::test::system system; - auto node = add_ipc_enabled_node (system); - auto const rpc_ctx = add_rpc (system, node); - boost::property_tree::ptree request; - boost::property_tree::ptree child; - boost::property_tree::ptree accounts; - child.put ("", nano::dev::genesis_key.pub.to_account ()); - accounts.push_back (std::make_pair ("", child)); - request.add_child ("accounts", accounts); - request.put ("action", "accounts_pending"); - auto response (wait_response (system, rpc_ctx, request)); - ASSERT_EQ ("1", response.get ("deprecated")); -} - -TEST (rpc, accounts_receivable) -{ - nano::test::system system; - auto node = add_ipc_enabled_node (system); - nano::keypair key1; - system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv); - auto block1 (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key1.pub, 100)); - ASSERT_TIMELY (5s, node->block_confirmed (block1->hash ())); - - auto const rpc_ctx = add_rpc (system, node); - boost::property_tree::ptree request; - request.put ("action", "accounts_receivable"); - boost::property_tree::ptree entry; - boost::property_tree::ptree peers_l; - entry.put ("", key1.pub.to_account ()); - peers_l.push_back (std::make_pair ("", entry)); - request.add_child ("accounts", peers_l); - request.put ("count", "100"); - { - auto response (wait_response (system, rpc_ctx, request)); - for (auto & blocks : response.get_child ("blocks")) - { - std::string account_text (blocks.first); - ASSERT_EQ (key1.pub.to_account (), account_text); - nano::block_hash hash1 (blocks.second.begin ()->second.get ("")); - ASSERT_EQ (block1->hash (), hash1); - } - } - request.put ("sorting", "true"); // Sorting test - { - auto response (wait_response (system, rpc_ctx, request)); - for (auto & blocks : response.get_child ("blocks")) - { - std::string account_text (blocks.first); - ASSERT_EQ (key1.pub.to_account (), account_text); - nano::block_hash hash1 (blocks.second.begin ()->first); - ASSERT_EQ (block1->hash (), hash1); - std::string amount (blocks.second.begin ()->second.get ("")); - ASSERT_EQ ("100", amount); - } - } - request.put ("threshold", "100"); // Threshold test - { - auto response (wait_response (system, rpc_ctx, request)); - std::unordered_map blocks; - for (auto & pending : response.get_child ("blocks")) - { - std::string account_text (pending.first); - ASSERT_EQ (key1.pub.to_account (), account_text); - for (auto i (pending.second.begin ()), j (pending.second.end ()); i != j; ++i) - { - nano::block_hash hash; - hash.decode_hex (i->first); - nano::uint128_union amount; - amount.decode_dec (i->second.get ("")); - blocks[hash] = amount; - boost::optional source (i->second.get_optional ("source")); - ASSERT_FALSE (source.is_initialized ()); - } - } - ASSERT_EQ (blocks[block1->hash ()], 100); - } - request.put ("source", "true"); - { - auto response (wait_response (system, rpc_ctx, request)); - std::unordered_map amounts; - std::unordered_map sources; - for (auto & pending : response.get_child ("blocks")) - { - std::string account_text (pending.first); - ASSERT_EQ (key1.pub.to_account (), account_text); - for (auto i (pending.second.begin ()), j (pending.second.end ()); i != j; ++i) - { - nano::block_hash hash; - hash.decode_hex (i->first); - amounts[hash].decode_dec (i->second.get ("amount")); - sources[hash].decode_account (i->second.get ("source")); - } - } - ASSERT_EQ (amounts[block1->hash ()], 100); - ASSERT_EQ (sources[block1->hash ()], nano::dev::genesis_key.pub); - } - - ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); - reset_confirmation_height (system.nodes.front ()->store, block1->account ()); - ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 0)); - request.put ("include_only_confirmed", "false"); - ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); -} - TEST (rpc, blocks) { nano::test::system system;