Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pending RPC to sort by absolute amounts when returning a subset #3060

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2858,11 +2858,15 @@ void nano::json_handler::pending ()
const bool include_only_confirmed = request.get<bool> ("include_only_confirmed", false);
const bool sorting = request.get<bool> ("sorting", false);
auto simple (threshold.is_zero () && !source && !min_version && !sorting); // if simple, response is a list of hashes
const bool should_sort = sorting && !simple;
if (!ec)
{
boost::property_tree::ptree peers_l;
auto transaction (node.store.tx_begin_read ());
for (auto i (node.store.pending_begin (transaction, nano::pending_key (account, 0))), n (node.store.pending_end ()); i != n && nano::pending_key (i->first).account == account && peers_l.size () < count; ++i)
// The ptree container is used if there are any children nodes (e.g source/min_version) otherwise the amount container is used.
std::vector<std::pair<std::string, boost::property_tree::ptree>> hash_ptree_pairs;
std::vector<std::pair<std::string, nano::uint128_t>> hash_amount_pairs;
for (auto i (node.store.pending_begin (transaction, nano::pending_key (account, 0))), n (node.store.pending_end ()); i != n && nano::pending_key (i->first).account == account && (should_sort || peers_l.size () < count); ++i)
{
nano::pending_key const & key (i->first);
if (block_confirmed (node, transaction, key.hash, include_active, include_only_confirmed))
Expand Down Expand Up @@ -2890,29 +2894,55 @@ void nano::json_handler::pending ()
{
pending_tree.put ("min_version", epoch_as_string (info.epoch));
}
peers_l.add_child (key.hash.to_string (), pending_tree);

if (should_sort)
{
hash_ptree_pairs.emplace_back (key.hash.to_string (), pending_tree);
}
else
{
peers_l.add_child (key.hash.to_string (), pending_tree);
}
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
if (should_sort)
{
hash_amount_pairs.emplace_back (key.hash.to_string (), info.amount.number ());
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
}
}
}
if (sorting && !simple)
if (should_sort)
{
if (source || min_version)
{
peers_l.sort ([](const auto & child1, const auto & child2) -> bool {
return child1.second.template get<nano::uint128_t> ("amount") > child2.second.template get<nano::uint128_t> ("amount");
auto mid = hash_ptree_pairs.size () <= count ? hash_ptree_pairs.end () : hash_ptree_pairs.begin () + count;
std::partial_sort (hash_ptree_pairs.begin (), mid, hash_ptree_pairs.end (), [](const auto & lhs, const auto & rhs) {
return lhs.second.template get<nano::uint128_t> ("amount") > rhs.second.template get<nano::uint128_t> ("amount");
});
for (auto i = 0; i < hash_ptree_pairs.size () && i < count; ++i)
{
peers_l.add_child (hash_ptree_pairs[i].first, hash_ptree_pairs[i].second);
}
}
else
{
peers_l.sort ([](const auto & child1, const auto & child2) -> bool {
return child1.second.template get<nano::uint128_t> ("") > child2.second.template get<nano::uint128_t> ("");
auto mid = hash_amount_pairs.size () <= count ? hash_amount_pairs.end () : hash_amount_pairs.begin () + count;
std::partial_sort (hash_amount_pairs.begin (), mid, hash_amount_pairs.end (), [](const auto & lhs, const auto & rhs) {
return lhs.second > rhs.second;
});

for (auto i = 0; i < hash_amount_pairs.size () && i < count; ++i)
{
peers_l.put (hash_amount_pairs[i].first, hash_amount_pairs[i].second.convert_to<std::string> ());
}
}
}
response_l.add_child ("blocks", peers_l);
Expand Down
26 changes: 26 additions & 0 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,32 @@ TEST (rpc, pending)
reset_confirmation_height (system.nodes.front ()->store, block1->account ());
scoped_thread_name_io.renew ();
check_block_response_count (0);

// Sorting with a smaller count than total should give absolute sorted amounts
scoped_thread_name_io.reset ();
node->store.confirmation_height_put (node->store.tx_begin_write (), nano::dev_genesis_key.pub, { 2, block1->hash () });
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));
scoped_thread_name_io.renew ();

ASSERT_TIMELY (10s, node->ledger.account_pending (node->store.tx_begin_read (), key1.pub) == 1000);
ASSERT_TIMELY (5s, !node->active.active (*block4));
ASSERT_TIMELY (5s, node->block_confirmed (block4->hash ()));

request.put ("count", "2");

{
test_response response (request, rpc.config.port, system.io_ctx);
ASSERT_TIMELY (5s, response.status != 0);
ASSERT_EQ (200, response.status);
auto & blocks_node (response.json.get_child ("blocks"));
ASSERT_EQ (2, blocks_node.size ());
nano::block_hash hash (blocks_node.begin ()->first);
nano::block_hash hash1 ((++blocks_node.begin ())->first);
ASSERT_EQ (block4->hash (), hash);
ASSERT_EQ (block3->hash (), hash1);
}
}

TEST (rpc, pending_burn)
Expand Down