Skip to content

Commit

Permalink
Splitting rpc.pending in to several independent tests. The testing se…
Browse files Browse the repository at this point in the history
…veral separate things and was difficult to debug because of its size.
  • Loading branch information
clemahieu committed Mar 21, 2023
1 parent 2a23efc commit 0469bfa
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 110 deletions.
26 changes: 2 additions & 24 deletions nano/rpc_test/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nano::rpc> & rpc_a, std::unique_ptr<nano::ipc::ipc_server> & ipc_server_a, std::unique_ptr<nano::ipc_rpc_processor> & ipc_rpc_processor_a, std::unique_ptr<nano::node_rpc_config> & node_rpc_config_a, std::unique_ptr<scoped_io_thread_name_change> & io_scope_a)
nano::test::rpc_context::rpc_context (std::shared_ptr<nano::rpc> & rpc_a, std::unique_ptr<nano::ipc::ipc_server> & ipc_server_a, std::unique_ptr<nano::ipc_rpc_processor> & ipc_rpc_processor_a, std::unique_ptr<nano::node_rpc_config> & 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::node> nano::test::add_ipc_enabled_node (nano::test::system & system, nano::node_config & node_config, nano::node_flags const & node_flags)
Expand Down Expand Up @@ -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<nano::node> const & node_a)
{
auto scoped_thread_name_io (std::make_unique<scoped_io_thread_name_change> ());
auto node_rpc_config (std::make_unique<nano::node_rpc_config> ());
auto ipc_server (std::make_unique<nano::ipc::ipc_server> (*node_a, *node_rpc_config));
nano::rpc_config rpc_config (node_a->network_params.network, nano::test::get_available_port (), true);
Expand All @@ -167,6 +145,6 @@ nano::test::rpc_context nano::test::add_rpc (nano::test::system & system, std::s
auto rpc (std::make_shared<nano::rpc> (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 };
}
}
11 changes: 1 addition & 10 deletions nano/rpc_test/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -54,13 +46,12 @@ namespace test
class rpc_context
{
public:
rpc_context (std::shared_ptr<nano::rpc> & rpc_a, std::unique_ptr<nano::ipc::ipc_server> & ipc_server_a, std::unique_ptr<nano::ipc_rpc_processor> & ipc_rpc_processor_a, std::unique_ptr<nano::node_rpc_config> & node_rpc_config_a, std::unique_ptr<scoped_io_thread_name_change> & io_scope_a);
rpc_context (std::shared_ptr<nano::rpc> & rpc_a, std::unique_ptr<nano::ipc::ipc_server> & ipc_server_a, std::unique_ptr<nano::ipc_rpc_processor> & ipc_rpc_processor_a, std::unique_ptr<nano::node_rpc_config> & node_rpc_config_a);

std::shared_ptr<nano::rpc> rpc;
std::unique_ptr<nano::ipc::ipc_server> ipc_server;
std::unique_ptr<nano::ipc_rpc_processor> ipc_rpc_processor;
std::unique_ptr<nano::node_rpc_config> node_rpc_config;
std::unique_ptr<scoped_io_thread_name_change> io_scope;
};

std::shared_ptr<nano::node> add_ipc_enabled_node (nano::test::system & system, nano::node_config & node_config, nano::node_flags const & node_flags);
Expand Down
200 changes: 124 additions & 76 deletions nano/rpc_test/receivable.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <nano/rpc_test/common.hpp>
#include <nano/node/ipc/ipc_server.hpp>
#include <nano/rpc/rpc_request_processor.hpp>
#include <nano/rpc_test/common.hpp>
#include <nano/test_common/chains.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

Expand All @@ -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<std::string> (""));
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<std::string> ("") };
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<std::string> ("") };
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<nano::block_hash, nano::uint128_union> 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<std::string> (""));
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<nano::block_hash, nano::uint128_union> 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<std::string> (""));
blocks[hash] = amount;
boost::optional<std::string> source (i->second.get_optional<std::string> ("source"));
ASSERT_FALSE (source.is_initialized ());
boost::optional<uint8_t> min_version (i->second.get_optional<uint8_t> ("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<std::string> (""));
blocks[hash] = amount;
auto source = i->second.get_optional<std::string> ("source");
ASSERT_FALSE (source.is_initialized ());
auto min_version = i->second.get_optional<uint8_t> ("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<nano::block_hash, nano::uint128_union> amounts;
std::unordered_map<nano::block_hash, nano::account> 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<nano::block_hash, nano::uint128_union> amounts;
std::unordered_map<nano::block_hash, nano::account> 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<std::string> ("amount"));
sources[hash].decode_account (i->second.get<std::string> ("source"));
ASSERT_EQ (i->second.get<uint8_t> ("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<std::string> ("amount"));
sources[hash].decode_account (i->second.get<std::string> ("source"));
ASSERT_EQ (i->second.get<uint8_t> ("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));
Expand All @@ -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.
Expand Down

0 comments on commit 0469bfa

Please # to comment.