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

Add database APIs to query tickets #2289

Merged
merged 1 commit into from
Oct 17, 2020
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
5 changes: 5 additions & 0 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>();
}
if(_options->count("api-limit-get-tickets")) {
_app_options.api_limit_get_tickets = _options->at("api-limit-get-tickets").as<uint64_t>();
}
}

void application_impl::startup()
Expand Down Expand Up @@ -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<uint64_t>()->default_value(101),
"For database_api_impl::get_liquidity_pools_* to set max limit value")
("api-limit-get-tickets",boost::program_options::value<uint64_t>()->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()
Expand Down
89 changes: 89 additions & 0 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,95 @@ vector<htlc_object> database_api_impl::list_htlcs(const htlc_id_type start, uint
return result;
}

//////////////////////////////////////////////////////////////////////
// //
// Tickets //
// //
//////////////////////////////////////////////////////////////////////

vector<ticket_object> database_api::list_tickets(
optional<uint32_t> limit,
optional<ticket_id_type> start_id )const
{
return my->list_tickets(
limit,
start_id );
}

vector<ticket_object> database_api_impl::list_tickets(
optional<uint32_t> olimit,
optional<ticket_id_type> 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<ticket_object> results;

ticket_id_type start_id = ostart_id.valid() ? *ostart_id : ticket_id_type();

const auto& idx = _db.get_index_type<ticket_index>().indices().get<by_id>();
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<ticket_object> database_api::get_tickets_by_account(
std::string account_name_or_id,
optional<uint32_t> limit,
optional<ticket_id_type> start_id )const
{
return my->get_tickets_by_account(
account_name_or_id,
limit,
start_id );
}

vector<ticket_object> database_api_impl::get_tickets_by_account(
std::string account_name_or_id,
optional<uint32_t> olimit,
optional<ticket_id_type> 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<ticket_object> 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<ticket_index>().indices().get<by_account>();
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 //
Expand Down
9 changes: 9 additions & 0 deletions libraries/app/database_api_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
htlc_id_type start, uint32_t limit) const;
vector<htlc_object> list_htlcs(const htlc_id_type lower_bound_id, uint32_t limit) const;

// Tickets
vector<ticket_object> list_tickets(
optional<uint32_t> limit = 101,
optional<ticket_id_type> start_id = optional<ticket_id_type>() )const;
vector<ticket_object> get_tickets_by_account(
std::string account_name_or_id,
optional<uint32_t> limit = 101,
optional<ticket_id_type> start_id = optional<ticket_id_type>() )const;

//private:

////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions libraries/app/include/graphene/app/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <graphene/chain/confidential_object.hpp>
#include <graphene/chain/liquidity_pool_object.hpp>
#include <graphene/chain/operation_history_object.hpp>
#include <graphene/chain/ticket_object.hpp>
#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/witness_object.hpp>

Expand Down Expand Up @@ -1024,6 +1025,43 @@ class database_api
vector<htlc_object> 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<ticket_object> list_tickets(
optional<uint32_t> limit = 101,
optional<ticket_id_type> start_id = optional<ticket_id_type>() )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<ticket_object> get_tickets_by_account(
std::string account_name_or_id,
optional<uint32_t> limit = 101,
optional<ticket_id_type> start_id = optional<ticket_id_type>() )const;

private:
std::shared_ptr< database_api_impl > my;
};
Expand Down Expand Up @@ -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)
)
5 changes: 2 additions & 3 deletions libraries/chain/include/graphene/chain/ticket_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ticket_object : public abstract_object<ticket_object>
};

struct by_next_update;
struct by_account_type;
struct by_account;

/**
* @ingroup object_index
Expand All @@ -132,10 +132,9 @@ typedef multi_index_container<
member< object, object_id_type, &object::id>
>
>,
ordered_unique< tag<by_account_type>,
ordered_unique< tag<by_account>,
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>
>
>
Expand Down