#310 - refactor pay_sons() function

This commit is contained in:
Vlad Dobromyslov 2022-04-12 08:43:56 +03:00
parent 19b5cb7854
commit b7ac7830ab

View file

@ -182,37 +182,37 @@ void database::update_worker_votes()
void database::pay_sons()
{
time_point_sec now = head_block_time();
const time_point_sec now = head_block_time();
const dynamic_global_property_object& dpo = get_dynamic_global_properties();
// Current requirement is that we have to pay every 24 hours, so the following check
if( dpo.son_budget.value > 0 && ((now - dpo.last_son_payout_time) >= fc::seconds(get_global_properties().parameters.son_pay_time())))
{
for(const auto& active_sidechain_type : active_sidechain_types)
{
auto sons = sort_votable_objects<son_index>(active_sidechain_type, get_global_properties().parameters.maximum_son_count());
const auto sons = sort_votable_objects<son_index>(active_sidechain_type, get_global_properties().parameters.maximum_son_count());
// After SON2 HF
uint64_t total_votes = 0;
for( const son_object& son : sons )
{
total_votes += _vote_tally_buffer[son.sidechain_vote_ids.at(active_sidechain_type)];
}
int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0);
const int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0);
auto get_weight = [&bits_to_drop]( uint64_t son_votes ) {
uint16_t weight = std::max((son_votes >> bits_to_drop), uint64_t(1) );
const uint16_t weight = std::max((son_votes >> bits_to_drop), uint64_t(1) );
return weight;
};
// Before SON2 HF
auto get_weight_before_son2_hf = []( uint64_t son_votes ) {
int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(son_votes)) - 15, 0);
uint16_t weight = std::max((son_votes >> bits_to_drop), uint64_t(1) );
const int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(son_votes)) - 15, 0);
const uint16_t weight = std::max((son_votes >> bits_to_drop), uint64_t(1) );
return weight;
};
uint64_t weighted_total_txs_signed = 0;
share_type son_budget = dpo.son_budget;
const share_type son_budget = dpo.son_budget;
get_index_type<son_stats_index>().inspect_all_objects([this, &weighted_total_txs_signed, &get_weight, &now, &get_weight_before_son2_hf, &active_sidechain_type](const object& o) {
const son_statistics_object& s = static_cast<const son_statistics_object&>(o);
const auto& idx = get_index_type<son_index>().indices().get<by_id>();
auto son_obj = idx.find( s.owner );
const auto son_obj = idx.find( s.owner );
uint16_t son_weight = 0;
if( now >= HARDFORK_SON2_TIME ) {
son_weight += get_weight(_vote_tally_buffer[son_obj->sidechain_vote_ids.at(active_sidechain_type)]);
@ -220,20 +220,14 @@ void database::pay_sons()
else {
son_weight += get_weight_before_son2_hf(_vote_tally_buffer[son_obj->sidechain_vote_ids.at(active_sidechain_type)]);
}
uint64_t txs_signed = 0;
for (const auto &ts : s.txs_signed) {
txs_signed = txs_signed + ts.second;
}
const uint64_t txs_signed = s.txs_signed.at(active_sidechain_type);
weighted_total_txs_signed += (txs_signed * son_weight);
});
// Now pay off each SON proportional to the number of transactions signed.
get_index_type<son_stats_index>().inspect_all_objects([this, &weighted_total_txs_signed, &dpo, &son_budget, &get_weight, &get_weight_before_son2_hf, &now, &active_sidechain_type](const object& o) {
const son_statistics_object& s = static_cast<const son_statistics_object&>(o);
uint64_t txs_signed = 0;
for (const auto &ts : s.txs_signed) {
txs_signed = txs_signed + ts.second;
}
const uint64_t txs_signed = s.txs_signed.at(active_sidechain_type);
if(txs_signed > 0){
const auto& idx = get_index_type<son_index>().indices().get<by_id>();
@ -245,7 +239,7 @@ void database::pay_sons()
else {
son_weight += get_weight_before_son2_hf(_vote_tally_buffer[son_obj->sidechain_vote_ids.at(active_sidechain_type)]);
}
share_type pay = (txs_signed * son_weight * son_budget.value)/weighted_total_txs_signed;
const share_type pay = (txs_signed * son_weight * son_budget.value)/weighted_total_txs_signed;
modify( *son_obj, [&]( son_object& _son_obj)
{
_son_obj.pay_son_fee(pay, *this);
@ -258,9 +252,7 @@ void database::pay_sons()
//Reset the tx counter in each son statistics object
modify( s, [&]( son_statistics_object& _s)
{
for (const auto &ts : s.txs_signed) {
_s.txs_signed.at(ts.first) = 0;
}
_s.txs_signed.at(active_sidechain_type) = 0;
});
}
});