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

Update workers API #2149

Merged
merged 3 commits into from
Apr 20, 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
57 changes: 37 additions & 20 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1765,39 +1765,56 @@ uint64_t database_api_impl::get_committee_count()const
// //
//////////////////////////////////////////////////////////////////////

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

vector<worker_object> database_api_impl::get_all_workers()const
vector<worker_object> database_api_impl::get_all_workers( const optional<bool> is_expired )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<worker_object> result;

if( !is_expired.valid() ) // query for all workers
{
const auto& workers_idx = _db.get_index_type<worker_index>().indices().get<by_id>();
result.reserve( workers_idx.size() );
for( const auto& w : workers_idx )
{
result.push_back( w );
}
}
else // query for workers that are expired only or not expired only
{
const time_point_sec now = _db.head_block_time();
const auto& workers_idx = _db.get_index_type<worker_index>().indices().get<by_end_date>();
auto itr = *is_expired ? workers_idx.begin() : workers_idx.lower_bound( now );
auto end = *is_expired ? workers_idx.upper_bound( now ) : workers_idx.end();
for( ; itr != end; ++itr )
{
result.push_back( *itr );
}
}

return result;
}

vector<optional<worker_object>> database_api::get_workers_by_account(const std::string account_id_or_name)const
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 );
return my->get_workers_by_account( account_id_or_name );
}

vector<optional<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(const std::string account_id_or_name)const
{
vector<optional<worker_object>> result;
vector<worker_object> result;
const auto& workers_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;
for( const auto& w : workers_idx )
{
if( w.worker_account == account )
result.push_back( w );
}
return result;
auto range = workers_idx.equal_range(account);
for(auto itr = range.first; itr != range.second; ++itr)
{
result.push_back( *itr );
}
return result;
}

uint64_t database_api::get_worker_count()const
Expand Down
4 changes: 2 additions & 2 deletions libraries/app/database_api_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
uint64_t get_committee_count()const;

// Workers
vector<worker_object> get_all_workers()const;
vector<optional<worker_object>> get_workers_by_account(const std::string account_id_or_name)const;
vector<worker_object> get_all_workers( const optional<bool> is_expired = optional<bool>() )const;
vector<worker_object> get_workers_by_account(const std::string account_id_or_name)const;
uint64_t get_worker_count()const;

// Votes
Expand Down
9 changes: 5 additions & 4 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,18 +674,19 @@ class database_api
///////////////////////

/**
* @brief Get all workers
* @return All the workers
* @brief Get workers
* @param is_expired null for all workers, true for expired workers only, false for non-expired workers only
* @return A list of worker objects
*
*/
vector<worker_object> get_all_workers()const;
vector<worker_object> get_all_workers( const optional<bool> is_expired = optional<bool>() )const;

/**
* @brief Get the workers owned by a given account
* @param account_name_or_id The name or ID of the account whose worker should be retrieved
* @return A list of worker objects owned by the account
*/
vector<optional<worker_object>> get_workers_by_account(const std::string account_name_or_id)const;
vector<worker_object> get_workers_by_account(const std::string account_name_or_id)const;

/**
* @brief Get the total number of workers registered with the blockchain
Expand Down
4 changes: 3 additions & 1 deletion libraries/chain/include/graphene/chain/worker_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ class worker_object : public abstract_object<worker_object>
struct by_account;
struct by_vote_for;
struct by_vote_against;
struct by_end_date;
typedef multi_index_container<
worker_object,
indexed_by<
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
ordered_non_unique< tag<by_account>, member< worker_object, account_id_type, &worker_object::worker_account > >,
ordered_unique< tag<by_vote_for>, member< worker_object, vote_id_type, &worker_object::vote_for > >,
ordered_unique< tag<by_vote_against>, member< worker_object, vote_id_type, &worker_object::vote_against > >
ordered_unique< tag<by_vote_against>, member< worker_object, vote_id_type, &worker_object::vote_against > >,
ordered_non_unique< tag<by_end_date>, member< worker_object, time_point_sec, &worker_object::work_end_date> >
>
> worker_object_multi_index_type;

Expand Down
116 changes: 116 additions & 0 deletions tests/tests/database_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,122 @@ BOOST_AUTO_TEST_CASE( subscription_notification_test )
} FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_CASE( get_all_workers )
{ try {
graphene::app::database_api db_api( db, &( app.get_options() ));
ACTORS( (connie)(whitney)(wolverine) );

fund(connie);
upgrade_to_lifetime_member(connie);
fund(whitney);
upgrade_to_lifetime_member(whitney);
fund(wolverine);
upgrade_to_lifetime_member(wolverine);

vector<worker_object> results;

const auto& worker1 = create_worker( connie_id, 1000, fc::days(10) );
worker_id_type worker1_id = worker1.id;

BOOST_REQUIRE_EQUAL( db_api.get_all_workers().size(), 1 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(true).size(), 0 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(false).size(), 1 );
BOOST_CHECK( db_api.get_all_workers().front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers(false).front().id == worker1_id );

generate_blocks( db.head_block_time() + fc::days(11) );
set_expiration( db, trx );

BOOST_REQUIRE_EQUAL( db_api.get_all_workers().size(), 1 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(true).size(), 1 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(false).size(), 0 );
BOOST_CHECK( db_api.get_all_workers().front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers(true).front().id == worker1_id );

const auto& worker2 = create_worker( whitney_id, 1000, fc::days(50) );
worker_id_type worker2_id = worker2.id;

BOOST_REQUIRE_EQUAL( db_api.get_all_workers().size(), 2 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(true).size(), 1 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(false).size(), 1 );
BOOST_CHECK( db_api.get_all_workers().front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers().back().id == worker2_id );
BOOST_CHECK( db_api.get_all_workers(true).front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers(false).front().id == worker2_id );

const auto& worker3 = create_worker( wolverine_id, 1000, fc::days(100) );
worker_id_type worker3_id = worker3.id;

BOOST_REQUIRE_EQUAL( db_api.get_all_workers().size(), 3 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(true).size(), 1 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(false).size(), 2 );
BOOST_CHECK( db_api.get_all_workers().front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers().back().id == worker3_id );
BOOST_CHECK( db_api.get_all_workers(true).front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers(false).front().id == worker2_id );
BOOST_CHECK( db_api.get_all_workers(false).back().id == worker3_id );

generate_blocks( db.head_block_time() + fc::days(55) );
set_expiration( db, trx );

BOOST_REQUIRE_EQUAL( db_api.get_all_workers().size(), 3 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(true).size(), 2 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(false).size(), 1 );
BOOST_CHECK( db_api.get_all_workers().front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers().back().id == worker3_id );
BOOST_CHECK( db_api.get_all_workers(true).front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers(true).back().id == worker2_id );
BOOST_CHECK( db_api.get_all_workers(false).front().id == worker3_id );

generate_blocks( db.head_block_time() + fc::days(55) );
set_expiration( db, trx );

BOOST_REQUIRE_EQUAL( db_api.get_all_workers().size(), 3 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(true).size(), 3 );
BOOST_REQUIRE_EQUAL( db_api.get_all_workers(false).size(), 0 );
BOOST_CHECK( db_api.get_all_workers().front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers().back().id == worker3_id );
BOOST_CHECK( db_api.get_all_workers(true).front().id == worker1_id );
BOOST_CHECK( db_api.get_all_workers(true).back().id == worker3_id );

} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_CASE( get_workers_by_account )
{ try {
graphene::app::database_api db_api( db, &( app.get_options() ));
ACTORS( (connie)(whitney)(wolverine) );

fund(connie);
upgrade_to_lifetime_member(connie);
fund(whitney);
upgrade_to_lifetime_member(whitney);
fund(wolverine);
upgrade_to_lifetime_member(wolverine);

vector<worker_object> results;

const auto& worker1 = create_worker( connie_id );
worker_id_type worker1_id = worker1.id;

const auto& worker2 = create_worker( whitney_id, 1000, fc::days(50) );
worker_id_type worker2_id = worker2.id;

const auto& worker3 = create_worker( whitney_id, 1000, fc::days(100) );
worker_id_type worker3_id = worker3.id;

BOOST_REQUIRE_EQUAL( db_api.get_workers_by_account("connie").size(), 1 );
BOOST_CHECK( db_api.get_workers_by_account("connie").front().id == worker1_id );

BOOST_REQUIRE_EQUAL( db_api.get_workers_by_account(string(whitney.id)).size(), 2 );
BOOST_CHECK( db_api.get_workers_by_account(string(whitney.id)).front().id == worker2_id );
BOOST_CHECK( db_api.get_workers_by_account(string(whitney.id)).back().id == worker3_id );

BOOST_REQUIRE_EQUAL( db_api.get_workers_by_account("wolverine").size(), 0 );

BOOST_REQUIRE_THROW( db_api.get_workers_by_account("not-a-user"), fc::exception );

} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_CASE( lookup_vote_ids )
{ try {
graphene::app::database_api db_api( db, &( app.get_options() ));
Expand Down