diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index d7ed1a84..a89119ec 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -2947,6 +2947,7 @@ void nano::json_handler::pending () { auto account (account_impl ()); auto count (count_optional_impl ()); + auto offset (offset_optional_impl (0)); auto threshold (threshold_optional_impl ()); bool const source = request.get ("source", false); bool const min_version = request.get ("min_version", false); @@ -2957,6 +2958,7 @@ void nano::json_handler::pending () bool const should_sort = sorting && !simple; if (!ec) { + auto offset_counter = offset; boost::property_tree::ptree peers_l; auto transaction (node.store.tx_begin_read ()); // The ptree container is used if there are any children nodes (e.g source/min_version) otherwise the amount container is used. @@ -2967,6 +2969,12 @@ void nano::json_handler::pending () nano::pending_key const & key (i->first); if (block_confirmed (node, transaction, key.hash, include_active, include_only_confirmed)) { + if (!should_sort && offset_counter > 0) + { + --offset_counter; + continue; + } + if (simple) { boost::property_tree::ptree entry; @@ -3019,23 +3027,23 @@ void nano::json_handler::pending () { if (source || min_version) { - auto mid = hash_ptree_pairs.size () <= count ? hash_ptree_pairs.end () : hash_ptree_pairs.begin () + count; + auto mid = hash_ptree_pairs.size () <= (offset + count) ? hash_ptree_pairs.end () : hash_ptree_pairs.begin () + offset + count; std::partial_sort (hash_ptree_pairs.begin (), mid, hash_ptree_pairs.end (), [] (auto const & lhs, auto const & rhs) { return lhs.second.template get ("amount") > rhs.second.template get ("amount"); }); - for (auto i = 0; i < hash_ptree_pairs.size () && i < count; ++i) + for (auto i = offset, j = offset + count; i < hash_ptree_pairs.size () && i < j; ++i) { peers_l.add_child (hash_ptree_pairs[i].first, hash_ptree_pairs[i].second); } } else { - auto mid = hash_amount_pairs.size () <= count ? hash_amount_pairs.end () : hash_amount_pairs.begin () + count; + auto mid = hash_amount_pairs.size () <= (offset + count) ? hash_amount_pairs.end () : hash_amount_pairs.begin () + offset + count; std::partial_sort (hash_amount_pairs.begin (), mid, hash_amount_pairs.end (), [] (auto const & lhs, auto const & rhs) { return lhs.second > rhs.second; }); - for (auto i = 0; i < hash_amount_pairs.size () && i < count; ++i) + for (auto i = offset, j = offset + count; i < hash_amount_pairs.size () && i < j; ++i) { peers_l.put (hash_amount_pairs[i].first, hash_amount_pairs[i].second.convert_to ()); } diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 14b93e10..a920346e 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -1849,7 +1849,6 @@ TEST (rpc, pending) ASSERT_TIMELY (5s, node->block_confirmed (block4->hash ())); request.put ("count", "2"); - { auto response (wait_response (system, rpc_ctx, request)); auto & blocks_node (response.get_child ("blocks")); @@ -1859,6 +1858,17 @@ TEST (rpc, pending) ASSERT_EQ (block4->hash (), hash); ASSERT_EQ (block3->hash (), hash1); } + + request.put ("offset", "2"); // Offset test + { + auto response (wait_response (system, rpc_ctx, request)); + auto & blocks_node (response.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 (block2->hash (), hash); + ASSERT_EQ (block1->hash (), hash1); + } } TEST (rpc, pending_burn)