Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
Refactored pay_curators method. Fixed comment options extention. #898
Browse files Browse the repository at this point in the history
  • Loading branch information
AKorpusenko committed Sep 28, 2018
1 parent 495abf9 commit 088fca0
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 41 deletions.
3 changes: 2 additions & 1 deletion libraries/api/include/golos/api/comment_api_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace golos { namespace api {

using namespace golos::chain;
using namespace golos::protocol;
using auc_win_destination = protocol::auction_window_reward_destination_type;

struct comment_api_object {
comment_object::id_type id;
Expand Down Expand Up @@ -59,7 +60,7 @@ namespace golos { namespace api {
int32_t net_votes = 0;

comment_mode mode = not_set;
protocol::auction_window_reward_destination_type auction_window_reward_destination = protocol::destination_not_set;
auc_win_destination auction_window_reward_destination;
uint32_t auction_window_size;

comment_object::id_type root_comment;
Expand Down
87 changes: 60 additions & 27 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2252,59 +2252,92 @@ namespace golos { namespace chain {
try {
uint128_t total_weight(c.total_vote_weight);
//edump( (total_weight)(max_rewards) );
uint128_t votes_in_auction_window_weight;
uint128_t votes_after_auction_window_weight;

share_type unclaimed_rewards = max_rewards;

if (c.total_vote_weight > 0 && c.allow_curation_rewards) {
share_type votes_in_auction_window_reward;
share_type votes_after_auction_window_reward;
share_type auction_window_reward;

auto auw_time = c.created + c.auction_window_size;

uint32_t votes_after_auction_window_count = 0;
uint32_t total_votes_count = 0;
if (c.total_vote_weight > 0 && c.allow_curation_rewards) {
uint128_t additional_claim; //< Needed when auction window reward goes to curators

// If auction window reward is chosen to go to curators,
// then total_votes_count and votes_after_auction_window_count
// should be calculated before calculating curation rewards
// If auction window reward is chosen to go to curators, we need to
// separated votes for 2 sets:
// c.created auction_window cashout
// |_______________|____________________________|
// 1 2
// 1st set contains votes with last_update from [c.created, c.created + auction_window_size)
// 2st set contains votes with last_update from [c.created + auction_window_size, cashout]

// Also we need to distibute auction_window reward to voters from second set.
// So we need to calculate parts of reward for 3 sets:
// - votes_in_auction_window_reward
// - votes_after_auction_window_reward
// - auction_window_reward

if (has_hardfork(STEEMIT_HARDFORK_0_19__898) &&
c.auction_window_reward_destination == protocol::to_curators
) {
// separate votes
const auto &cvlupdidx = get_index<comment_vote_index>().indices().get<by_vote_last_update>();


auto itr = cvlupdidx.lower_bound(boost::make_tuple(0, c.created));
// auto itr = cvlupdidx.begin();
auto itr_after_auw = cvlupdidx.lower_bound( // auw -- auctcion window
boost::make_tuple(0, c.created + c.auction_window_size)
);

while (itr != itr_after_auw && itr->comment == c.id) {
++total_votes_count;
uint128_t weight(itr->weight);
if (itr->last_update >= c.created &&
itr->last_update < auw_time &&
weight > 0
) {
votes_in_auction_window_weight += weight;
}
}

itr = itr_after_auw;

// calculate count of votes after auction window,
while (itr != cvlupdidx.end() && itr->comment == c.id) {
++total_votes_count;
++votes_after_auction_window_count;
}
votes_after_auction_window_weight = total_weight -
votes_in_auction_window_weight - c.auction_window_weight;

votes_in_auction_window_reward = ((max_rewards.value * votes_in_auction_window_weight) /
total_weight).to_uint64();

additional_claim = (max_rewards.value * c.auction_window_weight) / total_weight;
unclaimed_rewards -= additional_claim.to_uint64();
auction_window_reward = ((max_rewards.value * c.auction_window_weight) /
total_weight).to_uint64();

votes_after_auction_window_reward = ((max_rewards.value * votes_after_auction_window_weight) /
total_weight).to_uint64();
}

const auto &cvidx = get_index<comment_vote_index>().indices().get<by_comment_weight_voter>();
auto itr = cvidx.lower_bound(c.id);
while (itr != cvidx.end() && itr->comment == c.id) {
uint128_t weight(itr->weight);

auto claim = ((max_rewards.value * weight) /
total_weight).to_uint64();

uint64_t claim;
// to_curators case
if (has_hardfork(STEEMIT_HARDFORK_0_19__898) &&
c.auction_window_reward_destination == protocol::to_curators
) {
claim += ((additional_claim * weight) /
c.auction_window_reward_destination == protocol::to_curators) {
if (itr->last_update >= c.created && itr->last_update < auw_time) {
claim = ((votes_in_auction_window_reward.value * weight) /
votes_in_auction_window_weight).to_uint64();
}

if (itr->last_update >= auw_time) {
claim = ((votes_after_auction_window_reward.value * weight) /
votes_after_auction_window_weight).to_uint64();

claim += ((auction_window_reward.value * weight) /
c.auction_window_weight).to_uint64();
}
}
// to_reward_fund case
else {
claim = ((max_rewards.value * weight) /
total_weight).to_uint64();
}

Expand Down Expand Up @@ -2341,7 +2374,7 @@ namespace golos { namespace chain {

auto reward_fund_claim = (max_rewards.value * c.auction_window_weight) / total_weight;
unclaimed_rewards -= reward_fund_claim.to_uint64();
// Add virtual operation

push_virtual_operation(auction_window_reward_operation(asset(reward_fund_claim.to_uint64(), STEEM_SYMBOL), c.author, to_string(c.permlink)));
modify(get_dynamic_global_properties(), [&](dynamic_global_property_object &props) {
props.total_reward_fund_steem += asset(reward_fund_claim.to_uint64(), STEEM_SYMBOL);
Expand Down
4 changes: 3 additions & 1 deletion libraries/chain/include/golos/chain/comment_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ namespace golos {
archived
};

using auc_win_destination = protocol::auction_window_reward_destination_type;


class comment_object
: public object<comment_object_type, comment_object> {
public:
Expand Down Expand Up @@ -93,7 +95,7 @@ namespace golos {

comment_mode mode = first_payout;

protocol::auction_window_reward_destination_type auction_window_reward_destination = protocol::destination_not_set;
auc_win_destination auction_window_reward_destination = protocol::to_reward_fund;
uint32_t auction_window_size;

asset max_accepted_payout = asset(1000000000, SBD_SYMBOL); /// SBD value of the maximum payout this post will receive
Expand Down
20 changes: 17 additions & 3 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,23 @@ namespace golos { namespace chain {
}

void operator()( const comment_auction_window_reward_destination& cawrd ) const {
_db.modify(_c, [&](comment_object& c) {
c.auction_window_reward_destination = cawrd.destination;
});
if (_db.has_hardfork(STEEMIT_HARDFORK_0_19__898)) {
GOLOS_CHECK_PARAM(cawrd.destination, {
GOLOS_CHECK_VALUE(cawrd.destination == to_reward_fund || cawrd.destination == to_curators,
"Auction window reward must go either to reward_fund or to curators."
);
});
_db.modify(_c, [&](comment_object& c) {
c.auction_window_reward_destination = cawrd.destination;
});
}
else {
GOLOS_CHECK_PARAM(cawrd.destination, {
GOLOS_CHECK_VALUE(cawrd.destination == to_author,
"There are no options for auction window reward destination yet."
);
});
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace golos { namespace protocol {
enum auction_window_reward_destination_type {
to_reward_fund,
to_curators,
destination_not_set
to_author
};

struct comment_auction_window_reward_destination {
Expand All @@ -150,7 +150,7 @@ namespace golos { namespace protocol {
: destination(dest) {
}

auction_window_reward_destination_type destination = destination_not_set;
auction_window_reward_destination_type destination;

void validate() const;
};
Expand Down Expand Up @@ -1289,7 +1289,7 @@ FC_REFLECT((golos::protocol::limit_order_cancel_operation), (owner)(orderid))
FC_REFLECT((golos::protocol::delete_comment_operation), (author)(permlink));

FC_REFLECT((golos::protocol::beneficiary_route_type), (account)(weight))
FC_REFLECT_ENUM(golos::protocol::auction_window_reward_destination_type, (to_reward_fund)(to_curators)(destination_not_set))
FC_REFLECT_ENUM(golos::protocol::auction_window_reward_destination_type, (to_reward_fund)(to_curators)(to_author))
FC_REFLECT((golos::protocol::comment_payout_beneficiaries), (beneficiaries));
FC_REFLECT((golos::protocol::comment_auction_window_reward_destination), (destination));
FC_REFLECT_TYPENAME((golos::protocol::comment_options_extension));
Expand Down
11 changes: 5 additions & 6 deletions libraries/protocol/steem_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,11 @@ namespace golos { namespace protocol {
}

void comment_auction_window_reward_destination::validate() const {
// TODO: Figure out how to write it correct
// GOLOS_CHECK_PARAM(destination, {
// GOLOS_CHECK_VALUE(destination == to_reward_fund || destination == to_curators,
// "Auction window reward must go either to reward_fund or to curators"
// );
// });
GOLOS_CHECK_PARAM(destination, {
GOLOS_CHECK_VALUE(destination == to_reward_fund || destination == to_curators,
"Auction window reward must go either to reward_fund or to curators"
);
});
}

void comment_options_operation::validate() const {
Expand Down

0 comments on commit 088fca0

Please sign in to comment.