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

database api changes in witnesses, committee members and workers calls. #352

Merged
merged 3 commits into from
Aug 20, 2017
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
91 changes: 73 additions & 18 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
* Copyright (c) 2017 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
Expand Down Expand Up @@ -54,6 +54,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
database_api_impl( graphene::chain::database& db );
~database_api_impl();


// Objects
fc::variants get_objects(const vector<object_id_type>& ids)const;

Expand All @@ -78,7 +79,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>

// Keys
vector<vector<account_id_type>> get_key_references( vector<public_key_type> key )const;
bool is_public_key_registered(string public_key) const;
bool is_public_key_registered(string public_key) const;

// Accounts
vector<optional<account_object>> get_accounts(const vector<account_id_type>& account_ids)const;
Expand Down Expand Up @@ -123,6 +124,12 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
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(account_id_type account)const;
map<string, committee_member_id_type> lookup_committee_member_accounts(const string& lower_bound_name, uint32_t limit)const;
uint64_t get_committee_count()const;

// Workers
vector<worker_object> get_all_workers()const;
vector<optional<worker_object>> get_workers_by_account(account_id_type account)const;
uint64_t get_worker_count()const;

// Votes
vector<variant> lookup_vote_ids( const vector<vote_id_type>& votes )const;
Expand All @@ -143,6 +150,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
// Blinded balances
vector<blinded_balance_object> get_blinded_balances( const flat_set<commitment_type>& commitments )const;


//private:
template<typename T>
void subscribe_to_item( const T& i )const
Expand Down Expand Up @@ -1337,22 +1345,6 @@ vector<optional<witness_object>> database_api::get_witnesses(const vector<witnes
return my->get_witnesses( witness_ids );
}

vector<worker_object> database_api::get_workers_by_account(account_id_type account)const
{
const auto& idx = my->_db.get_index_type<worker_index>().indices().get<by_account>();
auto itr = idx.find(account);
vector<worker_object> result;

if( itr != idx.end() && itr->worker_account == account )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a bug here, if should be while. Fixed in this PR.

{
result.emplace_back( *itr );
++itr;
}

return result;
}


vector<optional<witness_object>> database_api_impl::get_witnesses(const vector<witness_id_type>& witness_ids)const
{
vector<optional<witness_object>> result; result.reserve(witness_ids.size());
Expand Down Expand Up @@ -1482,6 +1474,69 @@ map<string, committee_member_id_type> database_api_impl::lookup_committee_member
return committee_members_by_account_name;
}

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

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


//////////////////////////////////////////////////////////////////////
// //
// Workers //
// //
//////////////////////////////////////////////////////////////////////

vector<worker_object> database_api::get_all_workers()const
{
return my->get_all_workers();
}

vector<worker_object> database_api_impl::get_all_workers()const
{
vector<worker_object> result;
const auto& workers_idx = _db.get_index_type<worker_index>().indices().get<by_id>();
for( const auto& w : workers_idx )
{
result.push_back( w );
}
return result;
}

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

vector<optional<worker_object>> database_api_impl::get_workers_by_account(account_id_type account)const
{
vector<optional<worker_object>> result;
const auto& workers_idx = _db.get_index_type<worker_index>().indices().get<by_account>();

for( const auto& w : workers_idx )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid iterating through the whole index. Use lower_bound and check for end while iterating.

{
if( w.worker_account == account )
result.push_back( w );
}
return result;
}

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

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



//////////////////////////////////////////////////////////////////////
// //
// Votes //
Expand Down
32 changes: 29 additions & 3 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,35 @@ 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 registered with the blockchain
*/
uint64_t get_committee_count()const;

/// WORKERS

///////////////////////
// Worker proposals //
///////////////////////

/**
* @brief Get all workers
* @return All the workers
*
*/
vector<worker_object> get_all_workers()const;

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

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



///////////
Expand Down Expand Up @@ -657,9 +679,13 @@ FC_API(graphene::app::database_api,
(get_committee_members)
(get_committee_member_by_account)
(lookup_committee_member_accounts)
(get_committee_count)

// workers
(get_all_workers)
(get_workers_by_account)
(get_worker_count)

// Votes
(lookup_vote_ids)

Expand Down