diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index 10a83060..2d0412b0 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -173,6 +173,10 @@ public: fc::optional get_sidechain_address_by_account_and_sidechain(account_id_type account, sidechain_type sidechain) const; uint64_t get_sidechain_addresses_count() const; + // Workers + vector> get_workers(const vector &witness_ids) const; + fc::optional get_worker_by_account(const std::string account_id_or_name) const; + // Votes vector lookup_vote_ids(const vector &votes) const; vector get_votes_ids(const string &account_name_or_id) const; @@ -1569,20 +1573,6 @@ vector> database_api::get_witnesses(const vectorget_witnesses(witness_ids); } -vector database_api::get_workers_by_account(const std::string account_id_or_name) const { - const auto &idx = my->_db.get_index_type().indices().get(); - const account_id_type account = my->get_account_from_string(account_id_or_name)->id; - auto itr = idx.find(account); - vector result; - - if (itr != idx.end() && itr->worker_account == account) { - result.emplace_back(*itr); - ++itr; - } - - return result; -} - vector> database_api_impl::get_witnesses(const vector &witness_ids) const { vector> result; result.reserve(witness_ids.size()); @@ -1892,6 +1882,41 @@ uint64_t database_api_impl::get_sidechain_addresses_count() const { return _db.get_index_type().indices().size(); } +////////////////////////////////////////////////////////////////////// +// // +// Workers // +// // +////////////////////////////////////////////////////////////////////// + +vector> database_api::get_workers(const vector &worker_ids) const { + return my->get_workers(worker_ids); +} + +fc::optional database_api::get_worker_by_account(const std::string account_id_or_name) const { + return my->get_worker_by_account(account_id_or_name); +} + +vector> database_api_impl::get_workers(const vector &worker_ids) const { + vector> result; + result.reserve(worker_ids.size()); + std::transform(worker_ids.begin(), worker_ids.end(), std::back_inserter(result), + [this](worker_id_type id) -> optional { + if (auto o = _db.find(id)) + return *o; + return {}; + }); + return result; +} + +fc::optional database_api_impl::get_worker_by_account(const std::string account_id_or_name) const { + const auto &idx = _db.get_index_type().indices().get(); + 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 {}; +} + ////////////////////////////////////////////////////////////////////// // // // Votes // diff --git a/libraries/app/include/graphene/app/database_api.hpp b/libraries/app/include/graphene/app/database_api.hpp index 5b507f8e..acbe2c8b 100644 --- a/libraries/app/include/graphene/app/database_api.hpp +++ b/libraries/app/include/graphene/app/database_api.hpp @@ -699,14 +699,25 @@ public: */ uint64_t get_sidechain_addresses_count() const; - /// WORKERS + ///////////// + // Workers // + ///////////// + + /** + * @brief Get a list of workers by ID + * @param worker_ids IDs of the workers to retrieve + * @return The workers corresponding to the provided IDs + * + * This function has semantics identical to @ref get_objects + */ + vector> get_workers(const vector &worker_ids) const; /** * @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 * @return The worker object or null if the account does not have a worker */ - vector get_workers_by_account(const std::string account_id_or_name) const; + fc::optional get_worker_by_account(const std::string account_id_or_name) const; /////////// // Votes // @@ -1091,8 +1102,9 @@ FC_API(graphene::app::database_api, (get_sidechain_address_by_account_and_sidechain) (get_sidechain_addresses_count) - // workers - (get_workers_by_account) + // Workers + (get_workers) + (get_worker_by_account) // Votes (lookup_vote_ids)