#260 Add get_voters function
This commit is contained in:
parent
eb43bc3635
commit
cd737b57fe
5 changed files with 261 additions and 9 deletions
|
|
@ -52,6 +52,30 @@ template class fc::api<graphene::app::database_api>;
|
|||
|
||||
namespace graphene { namespace app {
|
||||
|
||||
template<class T>
|
||||
optional<T> maybe_id( const string& name_or_id )
|
||||
{
|
||||
if( std::isdigit( name_or_id.front() ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
return fc::variant(name_or_id, 1).as<T>(1);
|
||||
}
|
||||
catch (const fc::exception&)
|
||||
{ // not an ID
|
||||
}
|
||||
}
|
||||
return optional<T>();
|
||||
}
|
||||
|
||||
std::string object_id_to_string(object_id_type id)
|
||||
{
|
||||
std::string object_id = fc::to_string(id.space())
|
||||
+ "." + fc::to_string(id.type())
|
||||
+ "." + fc::to_string(id.instance());
|
||||
return object_id;
|
||||
}
|
||||
|
||||
class database_api_impl : public std::enable_shared_from_this<database_api_impl> {
|
||||
public:
|
||||
database_api_impl(graphene::chain::database &db);
|
||||
|
|
@ -197,6 +221,7 @@ public:
|
|||
}
|
||||
votes_info get_votes(const string &account_name_or_id) const;
|
||||
vector<account_object> get_voters_by_id(const vote_id_type &vote_id) const;
|
||||
voters_info get_voters(const string &account_name_or_id) const;
|
||||
|
||||
// Authority / validation
|
||||
std::string get_transaction_hex(const signed_transaction &trx) const;
|
||||
|
|
@ -1949,6 +1974,10 @@ vector<account_object> database_api::get_voters_by_id(const vote_id_type &vote_i
|
|||
return my->get_voters_by_id(vote_id);
|
||||
}
|
||||
|
||||
voters_info database_api::get_voters(const string &account_name_or_id) const {
|
||||
return my->get_voters(account_name_or_id);
|
||||
}
|
||||
|
||||
vector<variant> database_api_impl::lookup_vote_ids(const vector<vote_id_type> &votes) const {
|
||||
FC_ASSERT(votes.size() < 1000, "Only 1000 votes can be queried at a time");
|
||||
|
||||
|
|
@ -2082,6 +2111,160 @@ vector<account_object> database_api_impl::get_voters_by_id(const vote_id_type &v
|
|||
return result;
|
||||
}
|
||||
|
||||
voters_info database_api_impl::get_voters(const string &account_name_or_id) const {
|
||||
voters_info result;
|
||||
|
||||
//! Find account name
|
||||
bool owner_account_found = false;
|
||||
std::string owner_account_id;
|
||||
|
||||
//! Check if we have account by name
|
||||
const auto& account_object = get_account_by_name(account_name_or_id);
|
||||
if(account_object) {
|
||||
//! It is account
|
||||
owner_account_id = object_id_to_string( account_object->get_id() );
|
||||
owner_account_found = true;
|
||||
}
|
||||
else {
|
||||
//! Check if we have account id
|
||||
const auto& account_id = maybe_id<account_id_type>(account_name_or_id);
|
||||
if(account_id) {
|
||||
//! It may be account id
|
||||
const auto& account_objects = get_accounts({account_name_or_id});
|
||||
if(!account_objects.empty()){
|
||||
const auto& account_object = account_objects.front();
|
||||
if(account_object) {
|
||||
//! It is account object
|
||||
owner_account_id = object_id_to_string( account_object->get_id() );
|
||||
owner_account_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
//! Check if we have committee member id
|
||||
const auto& committee_member_id = maybe_id<committee_member_id_type>(account_name_or_id);
|
||||
if(committee_member_id) {
|
||||
//! It may be committee member id
|
||||
const auto& committee_member_objects = get_committee_members({*committee_member_id});
|
||||
if(!committee_member_objects.empty()){
|
||||
const auto& committee_member_object = committee_member_objects.front();
|
||||
if(committee_member_object) {
|
||||
//! It is committee member object
|
||||
owner_account_id = object_id_to_string( committee_member_object->committee_member_account );
|
||||
owner_account_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
//! Check if we have witness id
|
||||
const auto& witness_id = maybe_id<witness_id_type>(account_name_or_id);
|
||||
if(witness_id) {
|
||||
//! It may be witness id
|
||||
const auto& witness_objects = get_witnesses({*witness_id});
|
||||
if(!witness_objects.empty()){
|
||||
const auto& witness_object = witness_objects.front();
|
||||
if(witness_object) {
|
||||
//! It is witness object
|
||||
owner_account_id = object_id_to_string( witness_object->witness_account );
|
||||
owner_account_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
//! Check if we have worker id
|
||||
const auto& worker_id = maybe_id<worker_id_type>(account_name_or_id);
|
||||
if(worker_id) {
|
||||
//! It may be worker id
|
||||
const auto& worker_objects = get_workers({*worker_id});
|
||||
if(!worker_objects.empty()){
|
||||
const auto& worker_object = worker_objects.front();
|
||||
if(worker_object) {
|
||||
//! It is worker object
|
||||
owner_account_id = object_id_to_string( worker_object->worker_account );
|
||||
owner_account_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
//! Check if we have son id
|
||||
const auto& son_id = maybe_id<son_id_type>(account_name_or_id);
|
||||
if(son_id) {
|
||||
//! It may be son id
|
||||
const auto& son_objects = get_sons({*son_id});
|
||||
if(!son_objects.empty()){
|
||||
const auto& son_object = son_objects.front();
|
||||
if(son_object) {
|
||||
//! It is son object
|
||||
owner_account_id = object_id_to_string( son_object->son_account );
|
||||
owner_account_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//! We didn't find who it was
|
||||
if(!owner_account_found)
|
||||
FC_THROW_EXCEPTION(database_query_exception, "Wrong account_name_or_id: ${account_name_or_id}", ("account_name_or_id", account_name_or_id));
|
||||
|
||||
//! Fill voters_info
|
||||
const auto& committee_member_object = get_committee_member_by_account(owner_account_id);
|
||||
const auto& witness_object = get_witness_by_account(owner_account_id);
|
||||
const auto& worker_object = get_worker_by_account(owner_account_id);
|
||||
const auto& son_object = get_son_by_account(owner_account_id);
|
||||
|
||||
//! Info for committee member voters
|
||||
if(committee_member_object) {
|
||||
const auto& committee_member_voters = get_voters_by_id(committee_member_object->vote_id);
|
||||
result.voters_for_committee_member.vote_id = committee_member_object->vote_id;
|
||||
result.voters_for_committee_member.voters.reserve(committee_member_voters.size());
|
||||
for(const auto& voter: committee_member_voters) {
|
||||
result.voters_for_committee_member.voters.emplace_back(voter.get_id());
|
||||
}
|
||||
}
|
||||
|
||||
//! Info for witness voters
|
||||
if(witness_object) {
|
||||
const auto& witness_voters = get_voters_by_id(witness_object->vote_id);
|
||||
result.voters_for_witness.vote_id = witness_object->vote_id;
|
||||
result.voters_for_witness.voters.reserve(witness_voters.size());
|
||||
for(const auto& voter: witness_voters) {
|
||||
result.voters_for_witness.voters.emplace_back(voter.get_id());
|
||||
}
|
||||
}
|
||||
|
||||
//! Info for worker voters
|
||||
if(worker_object) {
|
||||
const auto& for_worker_voters = get_voters_by_id(worker_object->vote_for);
|
||||
result.voters_for_worker.vote_id = worker_object->vote_for;
|
||||
result.voters_for_worker.voters.reserve(for_worker_voters.size());
|
||||
for(const auto& voter: for_worker_voters) {
|
||||
result.voters_for_worker.voters.emplace_back(voter.get_id());
|
||||
}
|
||||
const auto& against_worker_voters = get_voters_by_id(worker_object->vote_against);
|
||||
result.voters_against_worker.vote_id = worker_object->vote_against;
|
||||
result.voters_against_worker.voters.reserve(against_worker_voters.size());
|
||||
for(const auto& voter: against_worker_voters) {
|
||||
result.voters_against_worker.voters.emplace_back(voter.get_id());
|
||||
}
|
||||
}
|
||||
|
||||
//! Info for son voters
|
||||
if(son_object) {
|
||||
const auto& son_voters = get_voters_by_id(son_object->vote_id);
|
||||
result.voters_for_son.vote_id = son_object->vote_id;
|
||||
result.voters_for_son.voters.reserve(son_voters.size());
|
||||
for(const auto& voter: son_voters) {
|
||||
result.voters_for_son.voters.emplace_back(voter.get_id());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// Authority / validation //
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@
|
|||
#include <graphene/chain/nft_object.hpp>
|
||||
#include <graphene/chain/offer_object.hpp>
|
||||
#include <graphene/chain/votes_info.hpp>
|
||||
#include <graphene/chain/voters_info.hpp>
|
||||
|
||||
#include <graphene/market_history/market_history_plugin.hpp>
|
||||
|
||||
|
|
@ -741,7 +742,7 @@ public:
|
|||
vector<variant> lookup_vote_ids(const vector<vote_id_type> &votes) const;
|
||||
|
||||
/**
|
||||
* @brief Get a list that ID votes for
|
||||
* @brief Get a list of vote_id_type that ID votes for
|
||||
* @param account_name_or_id ID or name of the account to get votes for
|
||||
* @return The list of vote_id_type ID votes for
|
||||
*
|
||||
|
|
@ -749,20 +750,15 @@ public:
|
|||
vector<vote_id_type> get_votes_ids(const string &account_name_or_id) const;
|
||||
|
||||
/**
|
||||
* @brief Given a set of votes, return the objects ID votes for
|
||||
*
|
||||
* This will be a mixture of committee_member_object, witness_objects, and worker_objects
|
||||
*
|
||||
* The results will be in the same order as the votes. Null will be returned for
|
||||
* any vote ids that are not found.
|
||||
*
|
||||
* @brief Return the objects account_name_or_id votes for
|
||||
* @param account_name_or_id ID or name of the account to get votes for
|
||||
* @return The set of votes ID votes for
|
||||
* @return The votes_info account_name_or_id votes for
|
||||
*
|
||||
*/
|
||||
votes_info get_votes(const string &account_name_or_id) const;
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Get a list of accounts that votes for vote_id
|
||||
* @param vote_id We search accounts that vote for this ID
|
||||
* @return The accounts that votes for provided ID
|
||||
|
|
@ -770,6 +766,14 @@ public:
|
|||
*/
|
||||
vector<account_object> get_voters_by_id(const vote_id_type &vote_id) const;
|
||||
|
||||
/**
|
||||
* @brief Return the accounts that votes for account_name_or_id
|
||||
* @param account_name_or_id ID or name of the account to get voters for
|
||||
* @return The voters_info for account_name_or_id
|
||||
*
|
||||
*/
|
||||
voters_info get_voters(const string &account_name_or_id) const;
|
||||
|
||||
////////////////////////////
|
||||
// Authority / validation //
|
||||
////////////////////////////
|
||||
|
|
@ -1119,6 +1123,7 @@ FC_API(graphene::app::database_api,
|
|||
(get_votes_ids)
|
||||
(get_votes)
|
||||
(get_voters_by_id)
|
||||
(get_voters)
|
||||
|
||||
// Authority / validation
|
||||
(get_transaction_hex)
|
||||
|
|
|
|||
40
libraries/chain/include/graphene/chain/voters_info.hpp
Normal file
40
libraries/chain/include/graphene/chain/voters_info.hpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <graphene/chain/protocol/vote.hpp>
|
||||
|
||||
namespace graphene { namespace chain {
|
||||
|
||||
/**
|
||||
* @class voters_info_object
|
||||
* @ingroup object
|
||||
*/
|
||||
struct voters_info_object {
|
||||
vote_id_type vote_id;
|
||||
vector<account_id_type> voters;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class voters_info
|
||||
* @brief tracks information about a voters info
|
||||
* @ingroup object
|
||||
*/
|
||||
struct voters_info {
|
||||
voters_info_object voters_for_committee_member;
|
||||
voters_info_object voters_for_witness;
|
||||
voters_info_object voters_for_worker;
|
||||
voters_info_object voters_against_worker;
|
||||
voters_info_object voters_for_son;
|
||||
};
|
||||
|
||||
} } // graphene::chain
|
||||
|
||||
FC_REFLECT( graphene::chain::voters_info_object,
|
||||
(vote_id)
|
||||
(voters) )
|
||||
|
||||
FC_REFLECT( graphene::chain::voters_info,
|
||||
(voters_for_committee_member)
|
||||
(voters_for_witness)
|
||||
(voters_for_worker)
|
||||
(voters_against_worker)
|
||||
(voters_for_son) )
|
||||
|
|
@ -2513,6 +2513,15 @@ class wallet_api
|
|||
*/
|
||||
vector<account_object> get_voters_by_id(const vote_id_type &vote_id) const;
|
||||
|
||||
/**
|
||||
* @brief Return the accounts that votes for account_name_or_id
|
||||
* @param account_name_or_id ID or name of the account to get voters for
|
||||
* @return The voters_info for account_name_or_id
|
||||
*
|
||||
*/
|
||||
voters_info get_voters(const string &account_name_or_id) const;
|
||||
|
||||
|
||||
fc::signal<void(bool)> lock_changed;
|
||||
std::shared_ptr<detail::wallet_api_impl> my;
|
||||
void encrypt_keys();
|
||||
|
|
@ -2821,4 +2830,5 @@ FC_API( graphene::wallet::wallet_api,
|
|||
(get_votes_ids)
|
||||
(get_votes)
|
||||
(get_voters_by_id)
|
||||
(get_voters)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -4109,6 +4109,15 @@ public:
|
|||
FC_CAPTURE_AND_RETHROW( (vote_id) )
|
||||
}
|
||||
|
||||
voters_info get_voters(const string &account_name_or_id) const
|
||||
{
|
||||
try
|
||||
{
|
||||
return _remote_db->get_voters(account_name_or_id);
|
||||
}
|
||||
FC_CAPTURE_AND_RETHROW( (account_name_or_id) )
|
||||
}
|
||||
|
||||
string _wallet_filename;
|
||||
wallet_data _wallet;
|
||||
|
||||
|
|
@ -7625,6 +7634,11 @@ vector<account_object> wallet_api::get_voters_by_id(const vote_id_type &vote_id)
|
|||
return my->get_voters_by_id(vote_id);
|
||||
}
|
||||
|
||||
voters_info wallet_api::get_voters(const string &account_name_or_id) const
|
||||
{
|
||||
return my->get_voters(account_name_or_id);
|
||||
}
|
||||
|
||||
// default ctor necessary for FC_REFLECT
|
||||
signed_block_with_info::signed_block_with_info( const signed_block& block )
|
||||
: signed_block( block )
|
||||
|
|
|
|||
Loading…
Reference in a new issue