From 32a22c7190f39b05561282f4ebe608d3e51071eb Mon Sep 17 00:00:00 2001 From: abitmore Date: Tue, 13 Oct 2020 16:44:45 -0400 Subject: [PATCH] Add database APIs to query tickets --- libraries/app/application.cpp | 5 ++ libraries/app/database_api.cpp | 89 +++++++++++++++++++ libraries/app/database_api_impl.hxx | 9 ++ .../app/include/graphene/app/application.hpp | 1 + .../app/include/graphene/app/database_api.hpp | 42 +++++++++ .../include/graphene/chain/ticket_object.hpp | 5 +- 6 files changed, 148 insertions(+), 3 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 48984faa74..cf0d81c807 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -329,6 +329,9 @@ void application_impl::set_api_limit() { if(_options->count("api-limit-get-liquidity-pools")) { _app_options.api_limit_get_liquidity_pools = _options->at("api-limit-get-liquidity-pools").as(); } + if(_options->count("api-limit-get-tickets")) { + _app_options.api_limit_get_tickets = _options->at("api-limit-get-tickets").as(); + } } void application_impl::startup() @@ -1059,6 +1062,8 @@ void application::set_program_options(boost::program_options::options_descriptio "For database_api_impl::get_withdraw_permissions_by_recipient to set max limit value") ("api-limit-get-liquidity-pools",boost::program_options::value()->default_value(101), "For database_api_impl::get_liquidity_pools_* to set max limit value") + ("api-limit-get-tickets",boost::program_options::value()->default_value(101), + "For database_api_impl::get_tickets_* to set max limit value") ; command_line_options.add(configuration_file_options); command_line_options.add_options() diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index 813f379088..7fd01626ad 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -2789,6 +2789,95 @@ vector database_api_impl::list_htlcs(const htlc_id_type start, uint return result; } +////////////////////////////////////////////////////////////////////// +// // +// Tickets // +// // +////////////////////////////////////////////////////////////////////// + +vector database_api::list_tickets( + optional limit, + optional start_id )const +{ + return my->list_tickets( + limit, + start_id ); +} + +vector database_api_impl::list_tickets( + optional olimit, + optional ostart_id )const +{ + uint32_t limit = olimit.valid() ? *olimit : 101; + + FC_ASSERT( _app_options, "Internal error" ); + const auto configured_limit = _app_options->api_limit_get_tickets; + FC_ASSERT( limit <= configured_limit, + "limit can not be greater than ${configured_limit}", + ("configured_limit", configured_limit) ); + + vector results; + + ticket_id_type start_id = ostart_id.valid() ? *ostart_id : ticket_id_type(); + + const auto& idx = _db.get_index_type().indices().get(); + auto lower_itr = idx.lower_bound( start_id ); + auto upper_itr = idx.end(); + + results.reserve( limit ); + uint32_t count = 0; + for ( ; lower_itr != upper_itr && count < limit; ++lower_itr, ++count) + { + results.emplace_back( *lower_itr ); + } + + return results; +} + +vector database_api::get_tickets_by_account( + std::string account_name_or_id, + optional limit, + optional start_id )const +{ + return my->get_tickets_by_account( + account_name_or_id, + limit, + start_id ); +} + +vector database_api_impl::get_tickets_by_account( + std::string account_name_or_id, + optional olimit, + optional ostart_id )const +{ + uint32_t limit = olimit.valid() ? *olimit : 101; + + FC_ASSERT( _app_options, "Internal error" ); + const auto configured_limit = _app_options->api_limit_get_tickets; + FC_ASSERT( limit <= configured_limit, + "limit can not be greater than ${configured_limit}", + ("configured_limit", configured_limit) ); + + vector results; + + account_id_type account = get_account_from_string(account_name_or_id)->id; + + ticket_id_type start_id = ostart_id.valid() ? *ostart_id : ticket_id_type(); + + const auto& idx = _db.get_index_type().indices().get(); + auto lower_itr = idx.lower_bound( std::make_tuple( account, start_id ) ); + auto upper_itr = idx.upper_bound( account ); + + results.reserve( limit ); + uint32_t count = 0; + for ( ; lower_itr != upper_itr && count < limit; ++lower_itr, ++count) + { + results.emplace_back( *lower_itr ); + } + + return results; +} + ////////////////////////////////////////////////////////////////////// // // // Private methods // diff --git a/libraries/app/database_api_impl.hxx b/libraries/app/database_api_impl.hxx index 5a70bfe284..61626120fe 100644 --- a/libraries/app/database_api_impl.hxx +++ b/libraries/app/database_api_impl.hxx @@ -223,6 +223,15 @@ class database_api_impl : public std::enable_shared_from_this htlc_id_type start, uint32_t limit) const; vector list_htlcs(const htlc_id_type lower_bound_id, uint32_t limit) const; + // Tickets + vector list_tickets( + optional limit = 101, + optional start_id = optional() )const; + vector get_tickets_by_account( + std::string account_name_or_id, + optional limit = 101, + optional start_id = optional() )const; + //private: //////////////////////////////////////////////// diff --git a/libraries/app/include/graphene/app/application.hpp b/libraries/app/include/graphene/app/application.hpp index 5d9be78b92..7c331cb5b0 100644 --- a/libraries/app/include/graphene/app/application.hpp +++ b/libraries/app/include/graphene/app/application.hpp @@ -73,6 +73,7 @@ namespace graphene { namespace app { uint64_t api_limit_get_withdraw_permissions_by_giver = 101; uint64_t api_limit_get_withdraw_permissions_by_recipient = 101; uint64_t api_limit_get_liquidity_pools = 101; + uint64_t api_limit_get_tickets = 101; }; class application diff --git a/libraries/app/include/graphene/app/database_api.hpp b/libraries/app/include/graphene/app/database_api.hpp index 7197116428..8fc420bf15 100644 --- a/libraries/app/include/graphene/app/database_api.hpp +++ b/libraries/app/include/graphene/app/database_api.hpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -1024,6 +1025,43 @@ class database_api vector list_htlcs(const htlc_id_type start, uint32_t limit) const; + ///////////// + // Tickets // + ///////////// + + /** + * @brief Get a list of tickets + * @param limit The limitation of items each query can fetch, not greater than a configured value + * @param start_id Start ticket id, fetch tickets whose IDs are greater than or equal to this ID + * @return The tickets + * + * @note + * 1. @p limit can be omitted or be null, if so the default value 101 will be used + * 2. @p start_id can be omitted or be null, if so the api will return the "first page" of tickets + * 3. can only omit one or more arguments in the end of the list, but not one or more in the middle + */ + vector list_tickets( + optional limit = 101, + optional start_id = optional() )const; + + /** + * @brief Get a list of tickets by the name or ID of the owner account + * @param account_name_or_id name or ID of the owner account + * @param limit The limitation of items each query can fetch, not greater than a configured value + * @param start_id Start ticket id, fetch tickets whose IDs are greater than or equal to this ID + * @return The tickets + * + * @note + * 1. if @p account_name_or_id cannot be tied to an account, an error will be returned + * 2. @p limit can be omitted or be null, if so the default value 101 will be used + * 3. @p start_id can be omitted or be null, if so the api will return the "first page" of tickets + * 4. can only omit one or more arguments in the end of the list, but not one or more in the middle + */ + vector get_tickets_by_account( + std::string account_name_or_id, + optional limit = 101, + optional start_id = optional() )const; + private: std::shared_ptr< database_api_impl > my; }; @@ -1160,4 +1198,8 @@ FC_API(graphene::app::database_api, (get_htlc_by_from) (get_htlc_by_to) (list_htlcs) + + // Tickets + (list_tickets) + (get_tickets_by_account) ) diff --git a/libraries/chain/include/graphene/chain/ticket_object.hpp b/libraries/chain/include/graphene/chain/ticket_object.hpp index d6c18e93e1..16610dccf7 100644 --- a/libraries/chain/include/graphene/chain/ticket_object.hpp +++ b/libraries/chain/include/graphene/chain/ticket_object.hpp @@ -117,7 +117,7 @@ class ticket_object : public abstract_object }; struct by_next_update; -struct by_account_type; +struct by_account; /** * @ingroup object_index @@ -132,10 +132,9 @@ typedef multi_index_container< member< object, object_id_type, &object::id> > >, - ordered_unique< tag, + ordered_unique< tag, composite_key< ticket_object, member< ticket_object, account_id_type, &ticket_object::account>, - member< ticket_object, ticket_type, &ticket_object::current_type>, member< object, object_id_type, &object::id> > >