From 80602a1f7b9c05cd642b1f49a893ef825757abe2 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 13 Aug 2015 09:22:05 -0400 Subject: [PATCH] adding api to query vote objects by id --- libraries/app/api.cpp | 38 +++++++++++++++++++ libraries/app/include/graphene/app/api.hpp | 11 ++++++ .../chain/committee_member_object.hpp | 4 ++ .../include/graphene/chain/protocol/vote.hpp | 1 + .../include/graphene/chain/witness_object.hpp | 4 ++ 5 files changed, 58 insertions(+) diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index fbe0cea0..8ca34c06 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -417,6 +417,44 @@ namespace graphene { namespace app { return *itr; return {}; } + vector database_api::lookup_vote_ids( const vector& votes )const + { + FC_ASSERT( votes.size() < 100, "Only 100 votes can be queried at a time" ); + + const auto& witness_idx = _db.get_index_type().indices().get(); + const auto& committee_idx = _db.get_index_type().indices().get(); + + vector result; + result.reserve( votes.size() ); + for( auto id : votes ) + { + switch( id.type() ) + { + case vote_id_type::committee: + { + auto itr = committee_idx.find( id ); + if( itr != committee_idx.end() ) + result.emplace_back( variant( *itr ) ); + else + result.emplace_back( variant() ); + break; + } + case vote_id_type::witness: + { + auto itr = witness_idx.find( id ); + if( itr != witness_idx.end() ) + result.emplace_back( variant( *itr ) ); + else + result.emplace_back( variant() ); + break; + } + case vote_id_type::worker: + break; + case vote_id_type::VOTE_TYPE_COUNT: break; // supress unused enum value warnings + } + } + return result; + } uint64_t database_api::get_witness_count()const { diff --git a/libraries/app/include/graphene/app/api.hpp b/libraries/app/include/graphene/app/api.hpp index 321fcc81..129da370 100644 --- a/libraries/app/include/graphene/app/api.hpp +++ b/libraries/app/include/graphene/app/api.hpp @@ -222,6 +222,16 @@ namespace graphene { namespace app { */ fc::optional get_witness_by_account(account_id_type account)const; + /** + * @brief Given a set of votes, return the objects they are voting 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. + */ + vector lookup_vote_ids( const vector& votes )const; + /** * @brief Get the total number of witnesses registered with the blockchain */ @@ -558,6 +568,7 @@ FC_API(graphene::app::database_api, (get_witnesses) (get_committee_members) (get_witness_by_account) + (lookup_vote_ids) (get_witness_count) (lookup_witness_accounts) (lookup_committee_member_accounts) diff --git a/libraries/chain/include/graphene/chain/committee_member_object.hpp b/libraries/chain/include/graphene/chain/committee_member_object.hpp index 447e51bf..f767cf79 100644 --- a/libraries/chain/include/graphene/chain/committee_member_object.hpp +++ b/libraries/chain/include/graphene/chain/committee_member_object.hpp @@ -48,6 +48,7 @@ namespace graphene { namespace chain { }; struct by_account; + struct by_vote_id; using committee_member_multi_index_type = multi_index_container< committee_member_object, indexed_by< @@ -56,6 +57,9 @@ namespace graphene { namespace chain { >, hashed_unique< tag, member + >, + hashed_unique< tag, + member > > >; diff --git a/libraries/chain/include/graphene/chain/protocol/vote.hpp b/libraries/chain/include/graphene/chain/protocol/vote.hpp index 1599c112..824ab0e3 100644 --- a/libraries/chain/include/graphene/chain/protocol/vote.hpp +++ b/libraries/chain/include/graphene/chain/protocol/vote.hpp @@ -52,6 +52,7 @@ struct vote_id_type /// Lower 8 bits are type; upper 24 bits are instance uint32_t content; + friend size_t hash_value( vote_id_type v ) { return std::hash()(v.content); } enum vote_type { committee, diff --git a/libraries/chain/include/graphene/chain/witness_object.hpp b/libraries/chain/include/graphene/chain/witness_object.hpp index 4c83ba55..54216dfd 100644 --- a/libraries/chain/include/graphene/chain/witness_object.hpp +++ b/libraries/chain/include/graphene/chain/witness_object.hpp @@ -43,6 +43,7 @@ namespace graphene { namespace chain { }; struct by_account; + struct by_vote_id; using witness_multi_index_type = multi_index_container< witness_object, indexed_by< @@ -51,6 +52,9 @@ namespace graphene { namespace chain { >, hashed_unique< tag, member + >, + hashed_unique< tag, + member > > >;