#339 - fix database::pay_sons()

This commit is contained in:
Vlad Dobromyslov 2022-04-07 14:29:57 +03:00
parent 8f2c1b06b1
commit e58b7d6916

View file

@ -185,17 +185,16 @@ void database::pay_sons()
time_point_sec now = head_block_time(); time_point_sec now = head_block_time();
const dynamic_global_property_object& dpo = get_dynamic_global_properties(); 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 // 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()))) { if( dpo.son_budget.value > 0 && ((now - dpo.last_son_payout_time) >= fc::seconds(get_global_properties().parameters.son_pay_time())))
//! Fixme - sort_votable_objects only for bitcoin {
auto sons = sort_votable_objects<son_index>(sidechain_type::bitcoin, get_global_properties().parameters.maximum_son_count()); 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());
// After SON2 HF // After SON2 HF
uint64_t total_votes = 0; uint64_t total_votes = 0;
for( const son_object& son : sons ) for( const son_object& son : sons )
{ {
for(const auto& vote_id : son.sidechain_vote_ids) total_votes += _vote_tally_buffer[son.sidechain_vote_ids.at(active_sidechain_type)];
{
total_votes += _vote_tally_buffer[vote_id.second];
}
} }
int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0); 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 ) { auto get_weight = [&bits_to_drop]( uint64_t son_votes ) {
@ -210,20 +209,16 @@ void database::pay_sons()
}; };
uint64_t weighted_total_txs_signed = 0; uint64_t weighted_total_txs_signed = 0;
share_type son_budget = dpo.son_budget; 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](const object& o) { 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 son_statistics_object& s = static_cast<const son_statistics_object&>(o);
const auto& idx = get_index_type<son_index>().indices().get<by_id>(); const auto& idx = get_index_type<son_index>().indices().get<by_id>();
auto son_obj = idx.find( s.owner ); auto son_obj = idx.find( s.owner );
uint16_t son_weight = 0; uint16_t son_weight = 0;
if( now >= HARDFORK_SON2_TIME ) { if( now >= HARDFORK_SON2_TIME ) {
for (const auto& vote_id : son_obj->sidechain_vote_ids) { son_weight += get_weight(_vote_tally_buffer[son_obj->sidechain_vote_ids.at(active_sidechain_type)]);
son_weight += get_weight(_vote_tally_buffer[vote_id.second]);
}
} }
else { else {
for (const auto& vote_id : son_obj->sidechain_vote_ids) { son_weight += get_weight_before_son2_hf(_vote_tally_buffer[son_obj->sidechain_vote_ids.at(active_sidechain_type)]);
son_weight += get_weight_before_son2_hf(_vote_tally_buffer[vote_id.second]);
}
} }
uint64_t txs_signed = 0; uint64_t txs_signed = 0;
for (const auto &ts : s.txs_signed) { for (const auto &ts : s.txs_signed) {
@ -233,7 +228,7 @@ void database::pay_sons()
}); });
// Now pay off each SON proportional to the number of transactions signed. // 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](const object& o) { 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); const son_statistics_object& s = static_cast<const son_statistics_object&>(o);
uint64_t txs_signed = 0; uint64_t txs_signed = 0;
for (const auto &ts : s.txs_signed) { for (const auto &ts : s.txs_signed) {
@ -245,14 +240,10 @@ void database::pay_sons()
auto son_obj = idx.find( s.owner ); auto son_obj = idx.find( s.owner );
uint16_t son_weight = 0; uint16_t son_weight = 0;
if( now >= HARDFORK_SON2_TIME ) { if( now >= HARDFORK_SON2_TIME ) {
for (const auto& vote_id : son_obj->sidechain_vote_ids) { son_weight += get_weight(_vote_tally_buffer[son_obj->sidechain_vote_ids.at(active_sidechain_type)]);
son_weight += get_weight(_vote_tally_buffer[vote_id.second]);
}
} }
else { else {
for (const auto& vote_id : son_obj->sidechain_vote_ids) { son_weight += get_weight_before_son2_hf(_vote_tally_buffer[son_obj->sidechain_vote_ids.at(active_sidechain_type)]);
son_weight += get_weight_before_son2_hf(_vote_tally_buffer[vote_id.second]);
}
} }
share_type pay = (txs_signed * son_weight * son_budget.value)/weighted_total_txs_signed; share_type pay = (txs_signed * son_weight * son_budget.value)/weighted_total_txs_signed;
modify( *son_obj, [&]( son_object& _son_obj) modify( *son_obj, [&]( son_object& _son_obj)
@ -280,6 +271,7 @@ void database::pay_sons()
}); });
} }
} }
}
void database::update_son_metrics(const flat_map<sidechain_type, vector<son_info> >& curr_active_sons) void database::update_son_metrics(const flat_map<sidechain_type, vector<son_info> >& curr_active_sons)
{ {