Skip to content

Commit

Permalink
#270 functions to unified form
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Dobromyslov authored and serkixenos committed Feb 11, 2022
1 parent 8b611c3 commit 339adbb
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 13 deletions.
102 changes: 91 additions & 11 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,17 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>

// Witnesses
vector<optional<witness_object>> get_witnesses(const vector<witness_id_type> &witness_ids) const;
fc::optional<witness_object> get_witness_by_account_id(account_id_type account) const;
fc::optional<witness_object> get_witness_by_account(const std::string account_id_or_name) const;
map<string, witness_id_type> lookup_witness_accounts(const string &lower_bound_name, uint32_t limit) const;
uint64_t get_witness_count() const;

// Committee members
vector<optional<committee_member_object>> get_committee_members(const vector<committee_member_id_type> &committee_member_ids) const;
fc::optional<committee_member_object> get_committee_member_by_account_id(account_id_type account) const;
fc::optional<committee_member_object> get_committee_member_by_account(const std::string account_id_or_name) const;
map<string, committee_member_id_type> lookup_committee_member_accounts(const string &lower_bound_name, uint32_t limit) const;
uint64_t get_committee_member_count() const;

// SON members
vector<optional<son_object>> get_sons(const vector<son_id_type> &son_ids) const;
Expand All @@ -200,7 +203,10 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>

// Workers
vector<optional<worker_object>> get_workers(const vector<worker_id_type> &witness_ids) const;
vector<worker_object> get_workers_by_account_id(account_id_type account) const;
vector<worker_object> get_workers_by_account(const std::string account_id_or_name) const;
map<string, worker_id_type> lookup_worker_accounts(const string &lower_bound_name, uint32_t limit) const;
uint64_t get_worker_count() const;

// Votes
vector<variant> lookup_vote_ids(const vector<vote_id_type> &votes) const;
Expand Down Expand Up @@ -292,6 +298,8 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
uint32_t api_limit_lookup_accounts = 1000;
uint32_t api_limit_lookup_witness_accounts = 1000;
uint32_t api_limit_lookup_committee_member_accounts = 1000;
uint32_t api_limit_lookup_son_accounts = 1000;
uint32_t api_limit_lookup_worker_accounts = 1000;
uint32_t api_limit_get_trade_history = 100;
uint32_t api_limit_get_trade_history_by_sequence = 100;

Expand Down Expand Up @@ -1611,19 +1619,27 @@ vector<optional<witness_object>> database_api_impl::get_witnesses(const vector<w
return result;
}

fc::optional<witness_object> database_api::get_witness_by_account(const std::string account_id_or_name) const {
return my->get_witness_by_account(account_id_or_name);
fc::optional<witness_object> database_api::get_witness_by_account_id(account_id_type account) const {
return my->get_witness_by_account_id(account);
}

fc::optional<witness_object> database_api_impl::get_witness_by_account(const std::string account_id_or_name) const {
fc::optional<witness_object> database_api_impl::get_witness_by_account_id(account_id_type account) const {
const auto &idx = _db.get_index_type<witness_index>().indices().get<by_account>();
const account_id_type account = get_account_from_string(account_id_or_name)->id;
auto itr = idx.find(account);
if (itr != idx.end())
return *itr;
return {};
}

fc::optional<witness_object> database_api::get_witness_by_account(const std::string account_id_or_name) const {
return my->get_witness_by_account(account_id_or_name);
}

fc::optional<witness_object> database_api_impl::get_witness_by_account(const std::string account_id_or_name) const {
const account_id_type account = get_account_from_string(account_id_or_name)->id;
return get_witness_by_account_id(account);
}

map<string, witness_id_type> database_api::lookup_witness_accounts(const string &lower_bound_name, uint32_t limit) const {
return my->lookup_witness_accounts(lower_bound_name, limit);
}
Expand Down Expand Up @@ -1682,19 +1698,27 @@ vector<optional<committee_member_object>> database_api_impl::get_committee_membe
return result;
}

fc::optional<committee_member_object> database_api::get_committee_member_by_account(const std::string account_id_or_name) const {
return my->get_committee_member_by_account(account_id_or_name);
fc::optional<committee_member_object> database_api::get_committee_member_by_account_id(account_id_type account) const {
return my->get_committee_member_by_account_id(account);
}

fc::optional<committee_member_object> database_api_impl::get_committee_member_by_account(const std::string account_id_or_name) const {
fc::optional<committee_member_object> database_api_impl::get_committee_member_by_account_id(account_id_type account) const {
const auto &idx = _db.get_index_type<committee_member_index>().indices().get<by_account>();
const account_id_type account = get_account_from_string(account_id_or_name)->id;
auto itr = idx.find(account);
if (itr != idx.end())
return *itr;
return {};
}

fc::optional<committee_member_object> database_api::get_committee_member_by_account(const std::string account_id_or_name) const {
return my->get_committee_member_by_account(account_id_or_name);
}

fc::optional<committee_member_object> database_api_impl::get_committee_member_by_account(const std::string account_id_or_name) const {
const account_id_type account = get_account_from_string(account_id_or_name)->id;
return get_committee_member_by_account_id(account);
}

map<string, committee_member_id_type> database_api::lookup_committee_member_accounts(const string &lower_bound_name, uint32_t limit) const {
return my->lookup_committee_member_accounts(lower_bound_name, limit);
}
Expand Down Expand Up @@ -1723,6 +1747,14 @@ map<string, committee_member_id_type> database_api_impl::lookup_committee_member
return committee_members_by_account_name;
}

uint64_t database_api::get_committee_member_count() const {
return my->get_committee_member_count();
}

uint64_t database_api_impl::get_committee_member_count() const {
return _db.get_index_type<committee_member_index>().indices().size();
}

//////////////////////////////////////////////////////////////////////
// //
// SON members //
Expand Down Expand Up @@ -1771,7 +1803,10 @@ map<string, son_id_type> database_api::lookup_son_accounts(const string &lower_b
}

map<string, son_id_type> database_api_impl::lookup_son_accounts(const string &lower_bound_name, uint32_t limit) const {
FC_ASSERT(limit <= 1000);
FC_ASSERT(limit <= api_limit_lookup_son_accounts,
"Number of querying accounts can not be greater than ${configured_limit}",
("configured_limit", api_limit_lookup_son_accounts));

const auto &sons_by_id = _db.get_index_type<son_index>().indices().get<by_id>();

// we want to order sons by account name, but that name is in the account object
Expand Down Expand Up @@ -1927,10 +1962,22 @@ vector<optional<worker_object>> database_api::get_workers(const vector<worker_id
return my->get_workers(worker_ids);
}

vector<worker_object> database_api::get_workers_by_account_id(account_id_type account) const {
return my->get_workers_by_account_id(account);
}

vector<worker_object> database_api::get_workers_by_account(const std::string account_id_or_name) const {
return my->get_workers_by_account(account_id_or_name);
}

map<string, worker_id_type> database_api::lookup_worker_accounts(const string &lower_bound_name, uint32_t limit) const {
return my->lookup_worker_accounts(lower_bound_name, limit);
}

uint64_t database_api::get_worker_count() const {
return my->get_worker_count();
}

vector<optional<worker_object>> database_api_impl::get_workers(const vector<worker_id_type> &worker_ids) const {
vector<optional<worker_object>> result;
result.reserve(worker_ids.size());
Expand All @@ -1943,9 +1990,8 @@ vector<optional<worker_object>> database_api_impl::get_workers(const vector<work
return result;
}

vector<worker_object> database_api_impl::get_workers_by_account(const std::string account_id_or_name) const {
vector<worker_object> database_api_impl::get_workers_by_account_id(account_id_type account) const {
const auto &idx = _db.get_index_type<worker_index>().indices().get<by_account>();
const account_id_type account = get_account_from_string(account_id_or_name)->id;
auto itr = idx.find(account);
vector<worker_object> result;

Expand All @@ -1957,6 +2003,40 @@ vector<worker_object> database_api_impl::get_workers_by_account(const std::strin
return result;
}

vector<worker_object> database_api_impl::get_workers_by_account(const std::string account_id_or_name) const {
const account_id_type account = get_account_from_string(account_id_or_name)->id;
return get_workers_by_account_id(account);
}

map<string, worker_id_type> database_api_impl::lookup_worker_accounts(const string &lower_bound_name, uint32_t limit) const {
FC_ASSERT(limit <= api_limit_lookup_worker_accounts,
"Number of querying accounts can not be greater than ${configured_limit}",
("configured_limit", api_limit_lookup_worker_accounts));

const auto &workers_by_id = _db.get_index_type<worker_index>().indices().get<by_id>();

// we want to order workers by account name, but that name is in the account object
// so the worker_index doesn't have a quick way to access it.
// get all the names and look them all up, sort them, then figure out what
// records to return. This could be optimized, but we expect the
// number of witnesses to be few and the frequency of calls to be rare
std::map<std::string, worker_id_type> workers_by_account_name;
for (const worker_object &worker : workers_by_id)
if (auto account_iter = _db.find(worker.worker_account))
if (account_iter->name >= lower_bound_name) // we can ignore anything below lower_bound_name
workers_by_account_name.insert(std::make_pair(account_iter->name, worker.id));

auto end_iter = workers_by_account_name.begin();
while (end_iter != workers_by_account_name.end() && limit--)
++end_iter;
workers_by_account_name.erase(end_iter, workers_by_account_name.end());
return workers_by_account_name;
}

uint64_t database_api_impl::get_worker_count() const {
return _db.get_index_type<worker_index>().indices().size();
}

//////////////////////////////////////////////////////////////////////
// //
// Votes //
Expand Down
49 changes: 47 additions & 2 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ class database_api {
* @param account The ID of the account whose witness should be retrieved
* @return The witness object, or null if the account does not have a witness
*/
fc::optional<witness_object> get_witness_by_account_id(account_id_type account) const;

/**
* @brief Get the witness owned by a given account
* @param account_id_or_name The ID or name of the account whose witness should be retrieved
* @return The witness object, or null if the account does not have a witness
*/
fc::optional<witness_object> get_witness_by_account(const std::string account_name_or_id) const;

/**
Expand Down Expand Up @@ -588,6 +595,13 @@ class database_api {
*/
vector<optional<committee_member_object>> get_committee_members(const vector<committee_member_id_type> &committee_member_ids) const;

/**
* @brief Get the committee_member owned by a given account
* @param account The ID of the account whose committee_member should be retrieved
* @return The committee_member object, or null if the account does not have a committee_member
*/
fc::optional<committee_member_object> get_committee_member_by_account_id(account_id_type account) const;

/**
* @brief Get the committee_member owned by a given account
* @param account_id_or_name The ID or name of the account whose committee_member should be retrieved
Expand All @@ -603,6 +617,11 @@ class database_api {
*/
map<string, committee_member_id_type> lookup_committee_member_accounts(const string &lower_bound_name, uint32_t limit) const;

/**
* @brief Get the total number of committee_members registered with the blockchain
*/
uint64_t get_committee_member_count() const;

/////////////////
// SON members //
/////////////////
Expand All @@ -625,7 +644,7 @@ class database_api {

/**
* @brief Get the SON owned by a given account
* @param account The ID of the account whose SON should be retrieved
* @param account_id_or_name The ID of the account whose SON should be retrieved
* @return The SON object, or null if the account does not have a SON
*/
fc::optional<son_object> get_son_by_account(const std::string account_id_or_name) const;
Expand Down Expand Up @@ -722,11 +741,31 @@ class database_api {

/**
* @brief Return the worker objects associated with this account.
* @param account_id_or_name The ID or name of the account whose worker should be retrieved
* @param account The ID of the account whose workers should be retrieved
* @return The worker object or null if the account does not have a worker
*/
vector<worker_object> get_workers_by_account_id(account_id_type account) const;

/**
* @brief Return the worker objects associated with this account.
* @param account_id_or_name The ID or name of the account whose workers should be retrieved
* @return The worker object or null if the account does not have a worker
*/
vector<worker_object> get_workers_by_account(const std::string account_id_or_name) const;

/**
* @brief Get names and IDs for registered workers
* @param lower_bound_name Lower bound of the first name to return
* @param limit Maximum number of results to return -- must not exceed 1000
* @return Map of worker names to corresponding IDs
*/
map<string, worker_id_type> lookup_worker_accounts(const string &lower_bound_name, uint32_t limit) const;

/**
* @brief Get the total number of workers registered with the blockchain
*/
uint64_t get_worker_count() const;

///////////
// Votes //
///////////
Expand Down Expand Up @@ -1086,14 +1125,17 @@ FC_API(graphene::app::database_api,

// Witnesses
(get_witnesses)
(get_witness_by_account_id)
(get_witness_by_account)
(lookup_witness_accounts)
(get_witness_count)

// Committee members
(get_committee_members)
(get_committee_member_by_account_id)
(get_committee_member_by_account)
(lookup_committee_member_accounts)
(get_committee_member_count)

// SON members
(get_sons)
Expand All @@ -1116,7 +1158,10 @@ FC_API(graphene::app::database_api,

// Workers
(get_workers)
(get_workers_by_account_id)
(get_workers_by_account)
(lookup_worker_accounts)
(get_worker_count)

// Votes
(lookup_vote_ids)
Expand Down
23 changes: 23 additions & 0 deletions libraries/wallet/include/graphene/wallet/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,21 @@ class wallet_api
*/
map<string, committee_member_id_type> list_committee_members(const string& lowerbound, uint32_t limit);

/** Lists all workers in the blockchain.
* This returns a list of all account names that own worker, and the associated worker id,
* sorted by name. This lists workers whether they are currently voted in or not.
*
* Use the \c lowerbound and limit parameters to page through the list. To retrieve all workers,
* start by setting \c lowerbound to the empty string \c "", and then each iteration, pass
* the last worker name returned as the \c lowerbound for the next \c list_workers() call.
*
* @param lowerbound the name of the first worker to return. If the named worker does not exist,
* the list will start at the worker that comes after \c lowerbound
* @param limit the maximum number of worker to return (max: 1000)
* @returns a list of worker mapping worker names to worker ids
*/
map<string, worker_id_type> list_workers(const string& lowerbound, uint32_t limit);

/** Returns information about the given SON.
* @param owner_account the name or id of the SON account owner, or the id of the SON
* @returns the information about the SON stored in the block chain
Expand All @@ -1370,6 +1385,12 @@ class wallet_api
*/
committee_member_object get_committee_member(string owner_account);

/** Returns information about the given worker.
* @param owner_account the name or id of the worker account owner, or the id of the worker
* @returns the information about the workers stored in the block chain
*/
vector<worker_object> get_workers(string owner_account);


/** Creates a SON object owned by the given account.
*
Expand Down Expand Up @@ -2658,8 +2679,10 @@ FC_API( graphene::wallet::wallet_api,
(get_witness)
(is_witness)
(get_committee_member)
(get_workers)
(list_witnesses)
(list_committee_members)
(list_workers)
(create_son)
(try_create_son)
(update_son)
Expand Down
Loading

0 comments on commit 339adbb

Please sign in to comment.