From 0469bfa124752510efc8989b373f26c222228402 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 20 Mar 2023 21:37:21 +0000 Subject: [PATCH] Splitting rpc.pending in to several independent tests. The testing several separate things and was difficult to debug because of its size. --- nano/rpc_test/common.cpp | 26 +---- nano/rpc_test/common.hpp | 11 +- nano/rpc_test/receivable.cpp | 200 ++++++++++++++++++++++------------- 3 files changed, 127 insertions(+), 110 deletions(-) diff --git a/nano/rpc_test/common.cpp b/nano/rpc_test/common.cpp index 2606e12e26..6f7ed1c7cd 100644 --- a/nano/rpc_test/common.cpp +++ b/nano/rpc_test/common.cpp @@ -76,33 +76,12 @@ void nano::test::test_response::run (uint16_t port_a) }); } -nano::test::scoped_io_thread_name_change::scoped_io_thread_name_change () -{ - renew (); -} - -nano::test::scoped_io_thread_name_change::~scoped_io_thread_name_change () -{ - reset (); -} - -void nano::test::scoped_io_thread_name_change::reset () -{ - nano::thread_role::set (nano::thread_role::name::unknown); -} - -void nano::test::scoped_io_thread_name_change::renew () -{ - nano::thread_role::set (nano::thread_role::name::io); -} - -nano::test::rpc_context::rpc_context (std::shared_ptr & rpc_a, std::unique_ptr & ipc_server_a, std::unique_ptr & ipc_rpc_processor_a, std::unique_ptr & node_rpc_config_a, std::unique_ptr & io_scope_a) +nano::test::rpc_context::rpc_context (std::shared_ptr & rpc_a, std::unique_ptr & ipc_server_a, std::unique_ptr & ipc_rpc_processor_a, std::unique_ptr & node_rpc_config_a) { rpc = std::move (rpc_a); ipc_server = std::move (ipc_server_a); ipc_rpc_processor = std::move (ipc_rpc_processor_a); node_rpc_config = std::move (node_rpc_config_a); - io_scope = std::move (io_scope_a); } std::shared_ptr nano::test::add_ipc_enabled_node (nano::test::system & system, nano::node_config & node_config, nano::node_flags const & node_flags) @@ -157,7 +136,6 @@ bool nano::test::check_block_response_count (nano::test::system & system, rpc_co nano::test::rpc_context nano::test::add_rpc (nano::test::system & system, std::shared_ptr const & node_a) { - auto scoped_thread_name_io (std::make_unique ()); auto node_rpc_config (std::make_unique ()); auto ipc_server (std::make_unique (*node_a, *node_rpc_config)); nano::rpc_config rpc_config (node_a->network_params.network, nano::test::get_available_port (), true); @@ -167,6 +145,6 @@ nano::test::rpc_context nano::test::add_rpc (nano::test::system & system, std::s auto rpc (std::make_shared (system.io_ctx, rpc_config, *ipc_rpc_processor)); rpc->start (); - return rpc_context{ rpc, ipc_server, ipc_rpc_processor, node_rpc_config, scoped_thread_name_io }; + return rpc_context{ rpc, ipc_server, ipc_rpc_processor, node_rpc_config }; } } diff --git a/nano/rpc_test/common.hpp b/nano/rpc_test/common.hpp index 31b33e79ec..bffc8ac8ea 100644 --- a/nano/rpc_test/common.hpp +++ b/nano/rpc_test/common.hpp @@ -29,14 +29,6 @@ namespace ipc namespace test { class system; - class scoped_io_thread_name_change - { - public: - scoped_io_thread_name_change (); - ~scoped_io_thread_name_change (); - void reset (); - void renew (); - }; class test_response { public: @@ -54,13 +46,12 @@ namespace test class rpc_context { public: - rpc_context (std::shared_ptr & rpc_a, std::unique_ptr & ipc_server_a, std::unique_ptr & ipc_rpc_processor_a, std::unique_ptr & node_rpc_config_a, std::unique_ptr & io_scope_a); + rpc_context (std::shared_ptr & rpc_a, std::unique_ptr & ipc_server_a, std::unique_ptr & ipc_rpc_processor_a, std::unique_ptr & node_rpc_config_a); std::shared_ptr rpc; std::unique_ptr ipc_server; std::unique_ptr ipc_rpc_processor; std::unique_ptr node_rpc_config; - std::unique_ptr io_scope; }; std::shared_ptr add_ipc_enabled_node (nano::test::system & system, nano::node_config & node_config, nano::node_flags const & node_flags); diff --git a/nano/rpc_test/receivable.cpp b/nano/rpc_test/receivable.cpp index b146d81543..b94ae990e8 100644 --- a/nano/rpc_test/receivable.cpp +++ b/nano/rpc_test/receivable.cpp @@ -1,6 +1,7 @@ -#include #include #include +#include +#include #include #include @@ -12,96 +13,143 @@ TEST (rpc, 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)); + 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", "receivable"); - request.put ("account", key1.pub.to_account ()); - request.put ("count", "100"); - { - auto response (wait_response (system, rpc_ctx, request)); - auto & blocks_node (response.get_child ("blocks")); - ASSERT_EQ (1, blocks_node.size ()); - nano::block_hash hash (blocks_node.begin ()->second.get ("")); - ASSERT_EQ (block1->hash (), hash); - } + request.put ("account", block1->link ().to_account ()); + auto response = wait_response (system, rpc_ctx, request); + auto & blocks_node = response.get_child ("blocks"); + ASSERT_EQ (1, blocks_node.size ()); + nano::block_hash hash{ blocks_node.begin ()->second.get ("") }; + ASSERT_EQ (block1->hash (), hash); +} + +TEST (rpc, 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", "receivable"); + request.put ("account", block1->link ().to_account ()); request.put ("sorting", "true"); // Sorting test + auto response = wait_response (system, rpc_ctx, request); + auto & blocks_node = response.get_child ("blocks"); + ASSERT_EQ (1, blocks_node.size ()); + nano::block_hash hash{ blocks_node.begin ()->first }; + ASSERT_EQ (block1->hash (), hash); + std::string amount{ blocks_node.begin ()->second.get ("") }; + ASSERT_EQ ("1", amount); +} + +TEST (rpc, receivable_threshold_sufficient) +{ + 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", "receivable"); + request.put ("account", block1->link ().to_account ()); + request.put ("threshold", "1"); // Threshold test + auto response = wait_response (system, rpc_ctx, request); + auto & blocks_node = response.get_child ("blocks"); + ASSERT_EQ (1, blocks_node.size ()); + std::unordered_map blocks; + for (auto i (blocks_node.begin ()), j (blocks_node.end ()); i != j; ++i) { - auto response (wait_response (system, rpc_ctx, request)); - auto & blocks_node (response.get_child ("blocks")); - ASSERT_EQ (1, blocks_node.size ()); - nano::block_hash hash (blocks_node.begin ()->first); - ASSERT_EQ (block1->hash (), hash); - std::string amount (blocks_node.begin ()->second.get ("")); - ASSERT_EQ ("100", amount); - } - request.put ("threshold", "100"); // Threshold test - { - auto response (wait_response (system, rpc_ctx, request)); - auto & blocks_node (response.get_child ("blocks")); - ASSERT_EQ (1, blocks_node.size ()); - std::unordered_map blocks; - for (auto i (blocks_node.begin ()), j (blocks_node.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 ()); - boost::optional min_version (i->second.get_optional ("min_version")); - ASSERT_FALSE (min_version.is_initialized ()); - } - ASSERT_EQ (blocks[block1->hash ()], 100); - } - request.put ("threshold", "101"); - { - auto response (wait_response (system, rpc_ctx, request, 10s)); - auto & blocks_node (response.get_child ("blocks")); - ASSERT_EQ (0, blocks_node.size ()); + nano::block_hash hash; + hash.decode_hex (i->first); + nano::uint128_union amount; + amount.decode_dec (i->second.get ("")); + blocks[hash] = amount; + auto source = i->second.get_optional ("source"); + ASSERT_FALSE (source.is_initialized ()); + auto min_version = i->second.get_optional ("min_version"); + ASSERT_FALSE (min_version.is_initialized ()); } - request.put ("threshold", "0"); + ASSERT_EQ (blocks[block1->hash ()], 1); +} + +TEST (rpc, receivable_threshold_insufficient) +{ + 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", "receivable"); + request.put ("account", block1->link ().to_account ()); + request.put ("threshold", "2"); // Chains are set up with 1 raw transfers therefore all blocks are less than 2 raw. + auto response = wait_response (system, rpc_ctx, request, 10s); + auto & blocks_node = response.get_child ("blocks"); + ASSERT_EQ (0, blocks_node.size ()); +} + +TEST (rpc, receivable_source_min_version) +{ + 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", "receivable"); + request.put ("account", block1->link ().to_account ()); request.put ("source", "true"); request.put ("min_version", "true"); + auto response (wait_response (system, rpc_ctx, request)); + auto & blocks_node (response.get_child ("blocks")); + ASSERT_EQ (1, blocks_node.size ()); + std::unordered_map amounts; + std::unordered_map sources; + for (auto i (blocks_node.begin ()), j (blocks_node.end ()); i != j; ++i) { - auto response (wait_response (system, rpc_ctx, request)); - auto & blocks_node (response.get_child ("blocks")); - ASSERT_EQ (1, blocks_node.size ()); - std::unordered_map amounts; - std::unordered_map sources; - for (auto i (blocks_node.begin ()), j (blocks_node.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 (i->second.get ("min_version"), 0); - } - ASSERT_EQ (amounts[block1->hash ()], 100); - ASSERT_EQ (sources[block1->hash ()], nano::dev::genesis_key.pub); + 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 (i->second.get ("min_version"), 0); } + ASSERT_EQ (amounts[block1->hash ()], 1); + ASSERT_EQ (sources[block1->hash ()], nano::dev::genesis_key.pub); +} - request.put ("account", key1.pub.to_account ()); - request.put ("source", "false"); - request.put ("min_version", "false"); - - ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); - rpc_ctx.io_scope->reset (); - reset_confirmation_height (system.nodes.front ()->store, block1->account ()); - rpc_ctx.io_scope->renew (); +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); + boost::property_tree::ptree request; + request.put ("action", "receivable"); + request.put ("account", block1->link ().to_account ()); + 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"); - rpc_ctx.io_scope->renew (); ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1)); - request.put ("include_only_confirmed", "true"); + { + 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)); +} - // Sorting with a smaller count than total should give absolute sorted amounts - rpc_ctx.io_scope->reset (); - node->store.confirmation_height.put (node->store.tx_begin_write (), nano::dev::genesis_key.pub, { 2, block1->hash () }); +/*TEST (rpc, amounts) +{ auto block2 (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key1.pub, 200)); auto block3 (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key1.pub, 300)); auto block4 (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key1.pub, 400)); @@ -121,7 +169,7 @@ TEST (rpc, receivable) ASSERT_EQ (block4->hash (), hash); ASSERT_EQ (block3->hash (), hash1); } -} +}*/ /** * This test case tests the receivable RPC command when used with offsets and sorting.