From b228e5bea5a3262b05ca8b245bbf82ad87ecfddc Mon Sep 17 00:00:00 2001 From: timur <12267899-timur.5@users.noreply.gitlab.com> Date: Wed, 14 Dec 2022 08:33:11 -0400 Subject: [PATCH] Limit NFT object count returned --- libraries/app/database_api.cpp | 19 ++++++++++++------- .../app/include/graphene/app/database_api.hpp | 2 +- .../wallet/include/graphene/wallet/wallet.hpp | 4 +++- libraries/wallet/wallet.cpp | 7 +++++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index 64fb82c2..2087d3c5 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -269,7 +269,7 @@ public: uint64_t nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const; nft_object nft_token_by_index(const nft_metadata_id_type nft_metadata_id, const uint64_t token_idx) const; nft_object nft_token_of_owner_by_index(const nft_metadata_id_type nft_metadata_id, const account_id_type owner, const uint64_t token_idx) const; - vector nft_get_all_tokens() const; + vector nft_get_all_tokens(const nft_id_type lower_id, uint32_t limit) const; vector nft_get_tokens_by_owner(const account_id_type owner) const; // Marketplace @@ -291,6 +291,7 @@ public: uint32_t api_limit_get_limit_orders_by_account = 101; uint32_t api_limit_get_order_book = 50; uint32_t api_limit_all_offers_count = 100; + uint32_t api_limit_nft_tokens = 100; uint32_t api_limit_lookup_accounts = 1000; uint32_t api_limit_lookup_witness_accounts = 1000; uint32_t api_limit_lookup_committee_member_accounts = 1000; @@ -3102,16 +3103,20 @@ nft_object database_api_impl::nft_token_of_owner_by_index(const nft_metadata_id_ return {}; } -vector database_api::nft_get_all_tokens() const { - return my->nft_get_all_tokens(); +vector database_api::nft_get_all_tokens(const nft_id_type lower_id, uint32_t limit) const { + return my->nft_get_all_tokens(lower_id, limit); } -vector database_api_impl::nft_get_all_tokens() const { +vector database_api_impl::nft_get_all_tokens(const nft_id_type lower_id, uint32_t limit) const { + FC_ASSERT(limit <= api_limit_nft_tokens, + "Number of querying nft tokens can not be greater than ${configured_limit}", + ("configured_limit", api_limit_nft_tokens)); const auto &idx_nft = _db.get_index_type().indices().get(); vector result; - for (auto itr = idx_nft.begin(); itr != idx_nft.end(); ++itr) { - result.push_back(*itr); - } + result.reserve(limit); + auto itr = idx_nft.lower_bound(lower_id); + while (limit-- && itr != idx_nft.end()) + result.emplace_back(*itr++); return result; } diff --git a/libraries/app/include/graphene/app/database_api.hpp b/libraries/app/include/graphene/app/database_api.hpp index b53c8eeb..425c2bae 100644 --- a/libraries/app/include/graphene/app/database_api.hpp +++ b/libraries/app/include/graphene/app/database_api.hpp @@ -1027,7 +1027,7 @@ public: * @brief Returns list of all available NTF's * @return List of all available NFT's */ - vector nft_get_all_tokens() const; + vector nft_get_all_tokens(const nft_id_type lower_id, uint32_t limit) const; /** * @brief Returns NFT's owned by owner diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 6a0b5ee5..28cdc42a 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -2571,9 +2571,11 @@ class wallet_api /** * @brief Returns all tokens + * @param limit the maximum number of NFT objects to return (max: 100) + * @param lowerbound the symbol of the first NFT object to include in the list. * @return Returns vector of NFT objects, empty vector if none */ - vector nft_get_all_tokens() const; + vector nft_get_all_tokens(uint32_t limit, optional lower_id) const; signed_transaction nft_lottery_buy_ticket( nft_metadata_id_type lottery, account_id_type buyer, uint64_t tickets_to_buy, bool broadcast ); signed_transaction create_offer(set item_ids, diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 7eefae27..77760c38 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -7027,9 +7027,12 @@ bool wallet_api::nft_is_approved_for_all(string owner_account_id_or_name, string return my->_remote_db->nft_is_approved_for_all(owner_account.id, operator_account.id); } -vector wallet_api::nft_get_all_tokens() const +vector wallet_api::nft_get_all_tokens(uint32_t limit, optional lower_id) const { - return my->_remote_db->nft_get_all_tokens(); + nft_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + return my->_remote_db->nft_get_all_tokens(lb_id, limit); } signed_transaction wallet_api::nft_lottery_buy_ticket( nft_metadata_id_type lottery, account_id_type buyer, uint64_t tickets_to_buy, bool broadcast )