Add hive votes to votes_info struct + fix get_votes function

This commit is contained in:
Vlad Dobromyslov 2022-03-10 08:15:25 +03:00
parent e23da4ecdb
commit 49e07d4f32
3 changed files with 79 additions and 66 deletions

View file

@ -2130,63 +2130,74 @@ vector<vote_id_type> database_api_impl::get_votes_ids(const string &account_name
votes_info database_api_impl::get_votes(const string &account_name_or_id) const {
votes_info result;
//const auto &votes_ids = get_votes_ids(account_name_or_id);
//const auto &committee_ids = get_votes_objects<committee_member_index, by_vote_id>(votes_ids);
//const auto &witness_ids = get_votes_objects<witness_index, by_vote_id>(votes_ids);
//const auto &for_worker_ids = get_votes_objects<worker_index, by_vote_for>(votes_ids);
//const auto &against_worker_ids = get_votes_objects<worker_index, by_vote_against>(votes_ids);
//const auto &son_ids = get_votes_objects<son_index, by_vote_id>(votes_ids, 5);
const auto &votes_ids = get_votes_ids(account_name_or_id);
const auto &committee_ids = get_votes_objects<committee_member_index, by_vote_id>(votes_ids);
const auto &witness_ids = get_votes_objects<witness_index, by_vote_id>(votes_ids);
const auto &for_worker_ids = get_votes_objects<worker_index, by_vote_for>(votes_ids);
const auto &against_worker_ids = get_votes_objects<worker_index, by_vote_against>(votes_ids);
const auto &son_bitcoin_ids = get_votes_objects<son_index, by_vote_id_bitcoin>(votes_ids, 5);
const auto &son_hive_ids = get_votes_objects<son_index, by_vote_id_hive>(votes_ids, 5);
////! Fill votes info
//if (!committee_ids.empty()) {
// vector<votes_info_object> votes_for_committee_members;
// votes_for_committee_members.reserve(committee_ids.size());
// for (const auto &committee : committee_ids) {
// const auto &committee_obj = committee.as<committee_member_object>(2);
// votes_for_committee_members.emplace_back(votes_info_object{committee_obj.vote_id, committee_obj.id});
// }
// result.votes_for_committee_members = std::move(votes_for_committee_members);
//}
//
//if (!witness_ids.empty()) {
// vector<votes_info_object> votes_for_witnesses;
// votes_for_witnesses.reserve(witness_ids.size());
// for (const auto &witness : witness_ids) {
// const auto &witness_obj = witness.as<witness_object>(2);
// votes_for_witnesses.emplace_back(votes_info_object{witness_obj.vote_id, witness_obj.id});
// }
// result.votes_for_witnesses = std::move(votes_for_witnesses);
//}
//
//if (!for_worker_ids.empty()) {
// vector<votes_info_object> votes_for_workers;
// votes_for_workers.reserve(for_worker_ids.size());
// for (const auto &for_worker : for_worker_ids) {
// const auto &for_worker_obj = for_worker.as<worker_object>(2);
// votes_for_workers.emplace_back(votes_info_object{for_worker_obj.vote_for, for_worker_obj.id});
// }
// result.votes_for_workers = std::move(votes_for_workers);
//}
//
//if (!against_worker_ids.empty()) {
// vector<votes_info_object> votes_against_workers;
// votes_against_workers.reserve(against_worker_ids.size());
// for (const auto &against_worker : against_worker_ids) {
// const auto &against_worker_obj = against_worker.as<worker_object>(2);
// votes_against_workers.emplace_back(votes_info_object{against_worker_obj.vote_against, against_worker_obj.id});
// }
// result.votes_against_workers = std::move(votes_against_workers);
//}
//
//if (!son_ids.empty()) {
// vector<votes_info_object> votes_for_sons;
// votes_for_sons.reserve(son_ids.size());
// for (const auto &son : son_ids) {
// const auto &son_obj = son.as<son_object>(6);
// votes_for_sons.emplace_back(votes_info_object{son_obj.vote_id, son_obj.id});
// }
// result.votes_for_sons = std::move(votes_for_sons);
//}
//! Fill votes info
if (!committee_ids.empty()) {
vector<votes_info_object> votes_for_committee_members;
votes_for_committee_members.reserve(committee_ids.size());
for (const auto &committee : committee_ids) {
const auto &committee_obj = committee.as<committee_member_object>(2);
votes_for_committee_members.emplace_back(votes_info_object{committee_obj.vote_id, committee_obj.id});
}
result.votes_for_committee_members = std::move(votes_for_committee_members);
}
if (!witness_ids.empty()) {
vector<votes_info_object> votes_for_witnesses;
votes_for_witnesses.reserve(witness_ids.size());
for (const auto &witness : witness_ids) {
const auto &witness_obj = witness.as<witness_object>(2);
votes_for_witnesses.emplace_back(votes_info_object{witness_obj.vote_id, witness_obj.id});
}
result.votes_for_witnesses = std::move(votes_for_witnesses);
}
if (!for_worker_ids.empty()) {
vector<votes_info_object> votes_for_workers;
votes_for_workers.reserve(for_worker_ids.size());
for (const auto &for_worker : for_worker_ids) {
const auto &for_worker_obj = for_worker.as<worker_object>(2);
votes_for_workers.emplace_back(votes_info_object{for_worker_obj.vote_for, for_worker_obj.id});
}
result.votes_for_workers = std::move(votes_for_workers);
}
if (!against_worker_ids.empty()) {
vector<votes_info_object> votes_against_workers;
votes_against_workers.reserve(against_worker_ids.size());
for (const auto &against_worker : against_worker_ids) {
const auto &against_worker_obj = against_worker.as<worker_object>(2);
votes_against_workers.emplace_back(votes_info_object{against_worker_obj.vote_against, against_worker_obj.id});
}
result.votes_against_workers = std::move(votes_against_workers);
}
if (!son_bitcoin_ids.empty()) {
vector<votes_info_object> votes_for_sons;
votes_for_sons.reserve(son_bitcoin_ids.size());
for (const auto &son : son_bitcoin_ids) {
const auto &son_obj = son.as<son_object>(6);
votes_for_sons.emplace_back(votes_info_object{son_obj.vote_id_bitcoin, son_obj.id});
}
result.votes_for_bitcoin_sons = std::move(votes_for_sons);
}
if (!son_hive_ids.empty()) {
vector<votes_info_object> votes_for_sons;
votes_for_sons.reserve(son_hive_ids.size());
for (const auto &son : son_hive_ids) {
const auto &son_obj = son.as<son_object>(6);
votes_for_sons.emplace_back(votes_info_object{son_obj.vote_id_hive, son_obj.id});
}
result.votes_for_hive_sons = std::move(votes_for_sons);
}
return result;
}

View file

@ -23,7 +23,8 @@ namespace graphene { namespace chain {
optional< vector< votes_info_object > > votes_for_witnesses;
optional< vector< votes_info_object > > votes_for_workers;
optional< vector< votes_info_object > > votes_against_workers;
optional< vector< votes_info_object > > votes_for_sons;
optional< vector< votes_info_object > > votes_for_bitcoin_sons;
optional< vector< votes_info_object > > votes_for_hive_sons;
};
} } // graphene::chain
@ -37,4 +38,5 @@ FC_REFLECT( graphene::chain::votes_info,
(votes_for_witnesses)
(votes_for_workers)
(votes_against_workers)
(votes_for_sons) )
(votes_for_bitcoin_sons)
(votes_for_hive_sons))

View file

@ -268,10 +268,10 @@ BOOST_AUTO_TEST_CASE( son_voting )
//! Check votes of nathan
auto nathan_votes = con.wallet_api_ptr->get_votes("nathan");
BOOST_REQUIRE(nathan_votes.votes_for_sons);
BOOST_CHECK_EQUAL(nathan_votes.votes_for_sons->size(), 2);
BOOST_CHECK_EQUAL(nathan_votes.votes_for_sons->at(0).id.instance(), son1_obj.id.instance());
BOOST_CHECK_EQUAL(nathan_votes.votes_for_sons->at(1).id.instance(), son2_obj.id.instance());
BOOST_REQUIRE(nathan_votes.votes_for_bitcoin_sons);
BOOST_CHECK_EQUAL(nathan_votes.votes_for_bitcoin_sons->size(), 2);
BOOST_CHECK_EQUAL(nathan_votes.votes_for_bitcoin_sons->at(0).id.instance(), son1_obj.id.instance());
BOOST_CHECK_EQUAL(nathan_votes.votes_for_bitcoin_sons->at(1).id.instance(), son2_obj.id.instance());
// Withdraw vote for a son1account
BOOST_TEST_MESSAGE("Withdraw vote for a son1account");
@ -290,9 +290,9 @@ BOOST_AUTO_TEST_CASE( son_voting )
//! Check votes of nathan
nathan_votes = con.wallet_api_ptr->get_votes("nathan");
BOOST_REQUIRE(nathan_votes.votes_for_sons);
BOOST_CHECK_EQUAL(nathan_votes.votes_for_sons->size(), 1);
BOOST_CHECK_EQUAL(nathan_votes.votes_for_sons->at(0).id.instance(), son2_obj.id.instance());
BOOST_REQUIRE(nathan_votes.votes_for_bitcoin_sons);
BOOST_CHECK_EQUAL(nathan_votes.votes_for_bitcoin_sons->size(), 1);
BOOST_CHECK_EQUAL(nathan_votes.votes_for_bitcoin_sons->at(0).id.instance(), son2_obj.id.instance());
// Withdraw vote for a son2account
BOOST_TEST_MESSAGE("Withdraw vote for a son2account");
@ -311,7 +311,7 @@ BOOST_AUTO_TEST_CASE( son_voting )
//! Check votes of nathan
nathan_votes = con.wallet_api_ptr->get_votes("nathan");
BOOST_CHECK(!nathan_votes.votes_for_sons.valid());
BOOST_CHECK(!nathan_votes.votes_for_bitcoin_sons.valid());
} catch( fc::exception& e ) {
BOOST_TEST_MESSAGE("SON cli wallet tests exception");