Skip to content

Commit

Permalink
Added expired action status, added last_id option to list_poposals
Browse files Browse the repository at this point in the history
  • Loading branch information
Dariusz Kedzierski authored and Mariusz-Trela committed Apr 25, 2019
1 parent 3a09faa commit c48335e
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 94 deletions.
6 changes: 5 additions & 1 deletion libraries/plugins/apis/condenser_api/condenser_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ namespace detail
DEFINE_API_IMPL( condenser_api_impl, list_proposals )
{
CHECK_ARG_SIZE( 5 )
FC_ASSERT( args.size() >= 5 && args.size() <= 6, "Expected #s argument(s), was 5 or 6");
FC_ASSERT( _sps_api, "sps_api_plugin not enabled." );

steem::plugins::sps::list_proposals_args list_args;
Expand All @@ -1919,6 +1919,10 @@ namespace detail
list_args.order_direction = args[2].as<steem::plugins::sps::order_direction_type>();
list_args.limit = args[3].as<int>();
list_args.status = args[4].as<steem::plugins::sps::proposal_status>();
if (args.size() == 6 && args[5].as<std::string>().size() > 0)
{
list_args.last_id = args[5].as<uint64_t>();
}

//return _sps_api->list_proposals( {args[0].as<fc::variant>, args[1].as< steem::plugins::sps::order_by_type >, args[2].as<steem::plugins::sps::order_direction_type>, args[3].as<int>, args[4].as<int>} );
return _sps_api->list_proposals( list_args );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,47 @@ namespace steem { namespace plugins { namespace sps {

enum proposal_status : int
{
active = 1,
inactive = 0,
active = 1,
expired = 2,
all = -1,
};

inline order_direction_type to_order_direction(std::string _order_type)
{
std::transform(_order_type.begin(), _order_type.end(), _order_type.begin(), [](unsigned char c) {return std::tolower(c); });
if(_order_type == "desc")
return order_direction_type::direction_descending;
else
return order_direction_type::direction_ascending;
}

inline order_by_type to_order_by(std::string _order_by)
{
std::transform(_order_by.begin(), _order_by.end(), _order_by.begin(), [](unsigned char c) {return std::tolower(c); });
if(_order_by == "start_date")
return order_by_type::by_start_date;
else if(_order_by == "end_date")
return order_by_type::by_end_date;
else if(_order_by == "total_votes")
return order_by_type::by_total_votes;
else
return order_by_type::by_creator; /// Consider exception throw when no constant was matched...
}

inline proposal_status to_proposal_status(std::string _status)
{
std::transform(_status.begin(), _status.end(), _status.begin(), [](unsigned char c) {return std::tolower(c); });

if(_status == "active")
inline order_direction_type to_order_direction(std::string _order_type)
{
std::transform(_order_type.begin(), _order_type.end(), _order_type.begin(), [](unsigned char c) {return std::tolower(c); });
if(_order_type == "desc")
return order_direction_type::direction_descending;
else
return order_direction_type::direction_ascending;
}

inline order_by_type to_order_by(std::string _order_by)
{
std::transform(_order_by.begin(), _order_by.end(), _order_by.begin(), [](unsigned char c) {return std::tolower(c); });
if(_order_by == "start_date")
return order_by_type::by_start_date;
else if(_order_by == "end_date")
return order_by_type::by_end_date;
else if(_order_by == "total_votes")
return order_by_type::by_total_votes;
else
return order_by_type::by_creator; /// Consider exception throw when no constant was matched...
}

inline proposal_status to_proposal_status(std::string _status)
{
std::transform(_status.begin(), _status.end(), _status.begin(), [](unsigned char c) {return std::tolower(c); });

if(_status == "active")
return proposal_status::active;
else if(_status == "inactive")
else if(_status == "inactive")
return proposal_status::inactive;
else
else if(_status == "expired")
return proposal_status::expired;
else
return proposal_status::all; /// Consider exception throw when no constant was matched...
}
}

typedef uint64_t api_id_type;

Expand Down Expand Up @@ -118,12 +121,28 @@ namespace steem { namespace plugins { namespace sps {
uint64_t total_votes = 0;

const bool is_active(const time_point_sec &head_time) const
{
return get_status(head_time) == proposal_status::active;
}

const proposal_status get_status(const time_point_sec &head_time) const
{
if (head_time >= start_date && head_time <= end_date)
{
return true;
return proposal_status::active;
}

if (head_time < start_date)
{
return proposal_status::inactive;
}

if (head_time > end_date)
{
return proposal_status::expired;
}
return false;

FC_THROW("Unexpected status of the proposal");
}
};

Expand All @@ -149,7 +168,9 @@ namespace steem { namespace plugins { namespace sps {
// query limit
uint16_t limit;
// result will contain only data with status flag set to this value
proposal_status status;
proposal_status status = proposal_status::all;
//
fc::optional<api_id_type> last_id;
};

// Return type for list_proposals
Expand Down Expand Up @@ -204,8 +225,9 @@ FC_REFLECT_ENUM(steem::plugins::sps::order_by_type,
);

FC_REFLECT_ENUM(steem::plugins::sps::proposal_status,
(active)
(inactive)
(active)
(expired)
(all)
);

Expand All @@ -231,6 +253,7 @@ FC_REFLECT(steem::plugins::sps::list_proposals_args,
(order_direction)
(limit)
(status)
(last_id)
);

FC_REFLECT(steem::plugins::sps::list_voter_proposals_args,
Expand Down
66 changes: 47 additions & 19 deletions libraries/plugins/apis/sps_api/sps_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,43 @@ CONTAINER filter(const CONTAINER &container, PREDICATE predicate) {
return result;
}

template<typename OrderType, typename ValueType, typename ResultType, typename OnPush>
void iterate_ordered_results(ValueType start, std::vector<ResultType>& result, uint32_t limit, chain::database& db, bool ordered_ascending, bool start_from_end, fc::optional<uint64_t> last_id, OnPush&& on_push)
{
const auto& idx = db.get_index< proposal_index, OrderType >();

if (ordered_ascending)
{
// we are introducing last_id parameter to fix situations where one wants to paginathe trough results with the same values that
// exceed given limit
auto itr = last_id.valid() ?
(std::find_if(idx.begin(), idx.end(), [=](auto &proposal) {return static_cast<uint64_t>(proposal.id) == *last_id;})) :
idx.lower_bound(start);
auto end = idx.end();

while (result.size() < limit && itr != end)
{
result.push_back(on_push(*itr));
++itr;
}
}
else
{
// we are introducing last_id parameter to fix situations where one wants to paginathe trough results with the same values that
// exceed given limit
auto itr = last_id.valid() ?
(std::find_if(idx.rbegin(), idx.rend(), [=](auto &proposal) {return static_cast<uint64_t>(proposal.id) == *last_id;})) :
(start_from_end ? idx.rbegin() : boost::reverse_iterator<decltype(idx.upper_bound(start))>(idx.upper_bound(start)));
auto end = idx.rend();

while (result.size() < limit && itr != end)
{
result.push_back(on_push(*itr));
++itr;
}
}
}

DEFINE_API_IMPL(sps_api_impl, find_proposals) {
ilog("find_proposal called");
// cannot query for more than SPS_API_SINGLE_QUERY_LIMIT ids
Expand Down Expand Up @@ -124,55 +161,59 @@ DEFINE_API_IMPL(sps_api_impl, list_proposals) {
{
case by_creator:
{
steem::utilities::iterate_ordered_results<proposal_index, steem::chain::by_creator>(
iterate_ordered_results<steem::chain::by_creator>(
args.start.as<account_name_type>(),
result,
args.limit,
_db,
args.order_direction == sps::order_direction_type::direction_ascending,
(args.start.as<std::string>()).empty(),
args.last_id,
[&](auto& proposal) { return api_proposal_object(proposal); }
);
}
break;
case by_start_date:
{
const auto start_value = ((args.start.as<std::string>()).empty() && args.order_direction == sps::order_direction_type::direction_descending) ? time_point_sec() : args.start.as<time_point_sec>();
steem::utilities::iterate_ordered_results<proposal_index, steem::chain::by_start_date>(
iterate_ordered_results<steem::chain::by_start_date>(
start_value,
result,
args.limit,
_db,
args.order_direction == sps::order_direction_type::direction_ascending,
(args.start.as<std::string>()).empty(),
args.last_id,
[&](auto& proposal) { return api_proposal_object(proposal); }
);
}
break;
case by_end_date:
{
const auto start_value = ((args.start.as<std::string>()).empty() && args.order_direction == sps::order_direction_type::direction_descending) ? time_point_sec() : args.start.as<time_point_sec>();
steem::utilities::iterate_ordered_results<proposal_index, steem::chain::by_end_date>(
iterate_ordered_results<steem::chain::by_end_date>(
start_value,
result,
args.limit,
_db,
args.order_direction == sps::order_direction_type::direction_ascending,
(args.start.as<std::string>()).empty(),
args.last_id,
[&](auto& proposal) { return api_proposal_object(proposal); }
);
}
break;
case by_total_votes:
{
const auto start_value = ((args.start.as<std::string>()).empty() && args.order_direction == sps::order_direction_type::direction_descending) ? 0 : args.start.as<uint64_t>();
steem::utilities::iterate_ordered_results<proposal_index, steem::chain::by_total_votes>(
iterate_ordered_results<steem::chain::by_total_votes>(
start_value,
result,
args.limit,
_db,
args.order_direction == sps::order_direction_type::direction_ascending,
(args.start.as<std::string>()).empty(),
args.last_id,
[&](auto& proposal) { return api_proposal_object(proposal); }
);
}
Expand All @@ -185,20 +226,7 @@ DEFINE_API_IMPL(sps_api_impl, list_proposals) {
{
// filter with active flag
result = filter(result, [&](const auto& proposal) {
const bool is_active = proposal.is_active(_db.head_block_time());
switch (args.status)
{
case proposal_status::inactive:
return !is_active;
break;

case proposal_status::active:
return is_active;
break;

default:
return true;
}
return args.status == proposal.get_status(_db.head_block_time());
});
}

Expand All @@ -220,7 +248,7 @@ DEFINE_API_IMPL(sps_api_impl, list_voter_proposals) {
auto po = _db.find<steem::chain::proposal_object, steem::chain::by_id>(itr->proposal_id);
FC_ASSERT(po != nullptr, "Proposal with given id does not exist");
auto apo = api_proposal_object(*po);
if (args.status == proposal_status::all || apo.is_active(_db.head_block_time()) == args.status)
if (args.status == proposal_status::all || apo.get_status(_db.head_block_time()) == args.status)
{
result[itr->voter].push_back(apo);
}
Expand Down
29 changes: 0 additions & 29 deletions libraries/utilities/include/steem/utilities/iterate_results.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,4 @@ namespace steem { namespace utilities {
++itr;
}
}

template<typename IndexType, typename OrderType, typename ValueType, typename ResultType, typename OnPush>
void iterate_ordered_results(ValueType start, std::vector<ResultType>& result, uint32_t limit, chain::database& db, bool ordered_ascending, bool start_from_end, OnPush&& on_push)
{
const auto& idx = db.get_index< IndexType, OrderType >();

if (ordered_ascending)
{
auto itr = idx.lower_bound(start);
auto end = idx.end();

while (result.size() < limit && itr != end)
{
result.push_back(on_push(*itr));
++itr;
}
}
else
{
auto itr = start_from_end ? idx.rbegin() : boost::reverse_iterator<decltype(idx.upper_bound(start))>(idx.upper_bound(start));
auto end = idx.rend();

while (result.size() < limit && itr != end)
{
result.push_back(on_push(*itr));
++itr;
}
}
}
}}
2 changes: 1 addition & 1 deletion libraries/wallet/include/steem/wallet/remote_node_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct remote_node_api
vector< market_history::bucket_object > get_market_history( uint32_t, time_point_sec, time_point_sec );
flat_set< uint32_t > get_market_history_buckets();

steem::plugins::sps::list_proposals_return list_proposals(fc::variant _start, steem::plugins::sps::order_by_type _order_by, steem::plugins::sps::order_direction_type _order_type, int _limit, steem::plugins::sps::proposal_status _status);
steem::plugins::sps::list_proposals_return list_proposals(fc::variant _start, steem::plugins::sps::order_by_type _order_by, steem::plugins::sps::order_direction_type _order_type, int _limit, steem::plugins::sps::proposal_status _status, fc::optional<uint64_t> _last_id);
steem::plugins::sps::list_voter_proposals_return list_voter_proposals(fc::variant _start, steem::plugins::sps::order_by_type _order_by, steem::plugins::sps::order_direction_type _order_type, int _limit, steem::plugins::sps::proposal_status _status);
steem::plugins::sps::find_proposals_return find_proposals(flat_set<uint64_t> _ids);
};
Expand Down
8 changes: 5 additions & 3 deletions libraries/wallet/include/steem/wallet/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,21 +1103,23 @@ class wallet_api
* @param _order_by - name a field for sorting operation,
* @param _order_type - set print order asc - ascdending, desc - descending,
* @param _limit - query limit
* @param _status - List only results with given status (inactive, active, all).
* @param _status - List only results with given status (expired, inactive, active, all).
* @param _last_id - (optional) Start search from given id.
*/
list_proposals_return list_proposals(fc::variant _start,
std::string _order_by = "creator",
std::string _order_type = "desc",
int _limit = 10,
std::string _status = "all");
std::string _status = "all",
std::string _last_id = "");

/**
* List proposals of given voter
* @param _start - starting value for querying results,
* @param _order_by - name a field for sorting operation,
* @param _order_type - set print order asc - ascdending, desc - descending,
* @param _limit - query limit
* @param _status - List only results with given status (inactive, active, all).
* @param _status - List only results with given status (expired, inactive, active, all).
*/
list_voter_proposals_return list_voter_proposals(fc::variant _start,
std::string _order_by = "creator",
Expand Down
2 changes: 1 addition & 1 deletion libraries/wallet/remote_node_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ flat_set< uint32_t > remote_node_api::get_market_history_buckets()
FC_ASSERT( false );
}

steem::plugins::sps::list_proposals_return remote_node_api::list_proposals(fc::variant _start, steem::plugins::sps::order_by_type _order_by, steem::plugins::sps::order_direction_type _order_type, int _limit, steem::plugins::sps::proposal_status _status)
steem::plugins::sps::list_proposals_return remote_node_api::list_proposals(fc::variant _start, steem::plugins::sps::order_by_type _order_by, steem::plugins::sps::order_direction_type _order_type, int _limit, steem::plugins::sps::proposal_status _status, fc::optional<uint64_t> _last_id)
{
FC_ASSERT( false );
}
Expand Down
Loading

0 comments on commit c48335e

Please sign in to comment.