Merge branch 'bug/548-balance-discrepancies' into 'develop'

Fix balance discrepancies

See merge request PBSA/peerplays!231
This commit is contained in:
Vlad Dobromyslov 2023-06-19 09:58:13 +00:00
commit 2c9fd332d4
5 changed files with 158 additions and 80 deletions

View file

@ -185,13 +185,109 @@ void database::update_worker_votes()
} }
} }
void database::pay_sons() void database::pay_sons_before_hf_ethereum()
{
const auto 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())))
{
const sidechain_type st = sidechain_type::bitcoin;
const auto sons = sort_votable_objects<son_index>(st, get_global_properties().parameters.maximum_son_count());
// After SON2 HF
uint64_t total_votes = 0;
for( const son_object& son : sons )
{
FC_ASSERT(son.get_sidechain_vote_id(st).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", st)("son", son));
total_votes += _vote_tally_buffer[*son.get_sidechain_vote_id(st)];
}
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 ) {
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 ) {
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;
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, &st](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>();
const auto son_obj = idx.find( s.owner );
uint16_t son_weight = 0;
FC_ASSERT(son_obj->get_sidechain_vote_id(st).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", st)("son", *son_obj));
if( now >= HARDFORK_SON2_TIME ) {
son_weight += get_weight(_vote_tally_buffer[*son_obj->get_sidechain_vote_id(st)]);
}
else {
son_weight += get_weight_before_son2_hf(_vote_tally_buffer[*son_obj->get_sidechain_vote_id(st)]);
}
const uint64_t txs_signed_bitcoin = s.txs_signed.contains(sidechain_type::bitcoin) ? s.txs_signed.at(sidechain_type::bitcoin) : 0;
const uint64_t txs_signed_hive = s.txs_signed.contains(sidechain_type::hive) ? s.txs_signed.at(sidechain_type::hive) : 0;
weighted_total_txs_signed += ((txs_signed_bitcoin + txs_signed_hive) * 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, &st](const object& o) {
const son_statistics_object& s = static_cast<const son_statistics_object&>(o);
const uint64_t txs_signed_bitcoin = s.txs_signed.contains(sidechain_type::bitcoin) ? s.txs_signed.at(sidechain_type::bitcoin) : 0;
const uint64_t txs_signed_hive = s.txs_signed.contains(sidechain_type::hive) ? s.txs_signed.at(sidechain_type::hive) : 0;
if(txs_signed_bitcoin > 0 || txs_signed_hive > 0) {
const auto& idx = get_index_type<son_index>().indices().get<by_id>();
auto son_obj = idx.find( s.owner );
uint16_t son_weight = 0;
FC_ASSERT(son_obj->get_sidechain_vote_id(st).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", st)("son", *son_obj));
if( now >= HARDFORK_SON2_TIME ) {
son_weight += get_weight(_vote_tally_buffer[*son_obj->get_sidechain_vote_id(st)]);
}
else {
son_weight += get_weight_before_son2_hf(_vote_tally_buffer[*son_obj->get_sidechain_vote_id(st)]);
}
const share_type pay = ((txs_signed_bitcoin + txs_signed_hive) * son_weight * son_budget.value)/weighted_total_txs_signed;
modify( *son_obj, [&]( son_object& _son_obj)
{
_son_obj.pay_son_fee(pay, *this);
});
//Remove the amount paid out to SON from global SON Budget
modify( dpo, [&]( dynamic_global_property_object& _dpo )
{
_dpo.son_budget -= pay;
} );
//Reset the tx counter in each son statistics object
modify( s, [&]( son_statistics_object& _s)
{
if(_s.txs_signed.contains(sidechain_type::bitcoin))
_s.txs_signed.at(sidechain_type::bitcoin) = 0;
if(_s.txs_signed.contains(sidechain_type::hive))
_s.txs_signed.at(sidechain_type::hive) = 0;
});
}
});
//Note the last son pay out time
modify( dpo, [&]( dynamic_global_property_object& _dpo )
{
_dpo.last_son_payout_time = now;
});
}
}
void database::pay_sons_after_hf_ethereum()
{ {
const 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(); 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())))
{ {
flat_map<sidechain_type, int8_t> bits_to_drop;
for(const auto& active_sidechain_type : active_sidechain_types(now)) for(const auto& active_sidechain_type : active_sidechain_types(now))
{ {
assert( _son_count_histogram_buffer.at(active_sidechain_type).size() > 0 ); assert( _son_count_histogram_buffer.at(active_sidechain_type).size() > 0 );
@ -209,93 +305,72 @@ void database::pay_sons()
} }
} }
const sidechain_type st = [&now, &active_sidechain_type]{ const auto sons = sort_votable_objects<son_index>(active_sidechain_type, (std::max(son_count*2+1, (size_t)get_chain_properties().immutable_parameters.min_son_count)));
if( now < HARDFORK_SON_FOR_ETHEREUM_TIME )
return sidechain_type::bitcoin;
else
return active_sidechain_type;
}();
const auto sons = sort_votable_objects<son_index>(st,
(std::max(son_count*2+1, (size_t)get_chain_properties().immutable_parameters.min_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 )
{ {
FC_ASSERT(son.get_sidechain_vote_id(st).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", st)("son", son)); FC_ASSERT(son.get_sidechain_vote_id(active_sidechain_type).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", active_sidechain_type)("son", son));
total_votes += _vote_tally_buffer[*son.get_sidechain_vote_id(st)]; total_votes += _vote_tally_buffer[*son.get_sidechain_vote_id(active_sidechain_type)];
} }
const int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0); bits_to_drop[active_sidechain_type] = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0);
auto get_weight = [&bits_to_drop]( uint64_t son_votes ) { }
const uint16_t weight = std::max((son_votes >> bits_to_drop), uint64_t(1) );
return weight; auto get_weight = [&bits_to_drop]( sidechain_type sidechain, uint64_t son_votes ) {
}; const uint16_t weight = std::max((son_votes >> bits_to_drop.at(sidechain)), uint64_t(1) );
// Before SON2 HF return weight;
auto get_weight_before_son2_hf = []( uint64_t son_votes ) { };
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) ); // Calculate weighted_total_txs_signed
return weight; uint64_t weighted_total_txs_signed = 0;
}; get_index_type<son_stats_index>().inspect_all_objects([this, &weighted_total_txs_signed, &get_weight, &now](const object& o) {
uint64_t weighted_total_txs_signed = 0; for(const auto& active_sidechain_type : active_sidechain_types(now)) {
const share_type son_budget = dpo.son_budget; const son_statistics_object &s = static_cast<const son_statistics_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, &st](const object& o) { const auto &idx = get_index_type<son_index>().indices().get<by_id>();
const son_statistics_object& s = static_cast<const son_statistics_object&>(o); const auto son_obj = idx.find(s.owner);
const auto& idx = get_index_type<son_index>().indices().get<by_id>(); FC_ASSERT(son_obj->get_sidechain_vote_id(active_sidechain_type).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", active_sidechain_type)("son", *son_obj));
const auto son_obj = idx.find( s.owner ); const uint16_t son_weight = get_weight(active_sidechain_type, _vote_tally_buffer[*son_obj->get_sidechain_vote_id(active_sidechain_type)]);
uint16_t son_weight = 0;
FC_ASSERT(son_obj->get_sidechain_vote_id(st).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", st)("son", *son_obj));
if( now >= HARDFORK_SON2_TIME ) {
son_weight += get_weight(_vote_tally_buffer[*son_obj->get_sidechain_vote_id(st)]);
}
else {
son_weight += get_weight_before_son2_hf(_vote_tally_buffer[*son_obj->get_sidechain_vote_id(st)]);
}
const uint64_t txs_signed = s.txs_signed.contains(active_sidechain_type) ? s.txs_signed.at(active_sidechain_type) : 0; const uint64_t txs_signed = s.txs_signed.contains(active_sidechain_type) ? s.txs_signed.at(active_sidechain_type) : 0;
weighted_total_txs_signed += (txs_signed * son_weight); weighted_total_txs_signed += (txs_signed * son_weight);
}); }
});
// Now pay off each SON proportional to the number of transactions signed
const share_type son_budget = dpo.son_budget;
get_index_type<son_stats_index>().inspect_all_objects([this, &now, &get_weight, &weighted_total_txs_signed, &dpo, &son_budget](const object& o) {
for(const auto& active_sidechain_type : active_sidechain_types(now)) {
const son_statistics_object &s = static_cast<const son_statistics_object &>(o);
// 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, &st](const object& o) {
const son_statistics_object& s = static_cast<const son_statistics_object&>(o);
const uint64_t txs_signed = s.txs_signed.contains(active_sidechain_type) ? s.txs_signed.at(active_sidechain_type) : 0; const uint64_t txs_signed = s.txs_signed.contains(active_sidechain_type) ? s.txs_signed.at(active_sidechain_type) : 0;
if (txs_signed > 0) {
if(txs_signed > 0) { 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;
FC_ASSERT(son_obj->get_sidechain_vote_id(st).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", st)("son", *son_obj)); FC_ASSERT(son_obj->get_sidechain_vote_id(active_sidechain_type).valid(), "Invalid vote id, sidechain: ${sidechain}, son: ${son}", ("sidechain", active_sidechain_type)("son", *son_obj));
if( now >= HARDFORK_SON2_TIME ) { son_weight += get_weight(active_sidechain_type, _vote_tally_buffer[*son_obj->get_sidechain_vote_id(active_sidechain_type)]);
son_weight += get_weight(_vote_tally_buffer[*son_obj->get_sidechain_vote_id(st)]); const share_type pay = (txs_signed * son_weight * son_budget.value) / weighted_total_txs_signed;
} modify(*son_obj, [&](son_object &_son_obj) {
else { _son_obj.pay_son_fee(pay, *this);
son_weight += get_weight_before_son2_hf(_vote_tally_buffer[*son_obj->get_sidechain_vote_id(st)]);
}
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);
}); });
//Remove the amount paid out to SON from global SON Budget // Remove the amount paid out to SON from global SON Budget
modify( dpo, [&]( dynamic_global_property_object& _dpo ) modify(dpo, [&](dynamic_global_property_object &_dpo) {
{ _dpo.son_budget -= pay;
_dpo.son_budget -= pay; });
} ); // Reset the tx counter in each son statistics object
//Reset the tx counter in each son statistics object modify(s, [&](son_statistics_object &_s) {
modify( s, [&]( son_statistics_object& _s) if (_s.txs_signed.contains(active_sidechain_type))
{
if(_s.txs_signed.contains(active_sidechain_type))
_s.txs_signed.at(active_sidechain_type) = 0; _s.txs_signed.at(active_sidechain_type) = 0;
}); });
} }
}); }
//Note the last son pay out time });
modify( dpo, [&]( dynamic_global_property_object& _dpo )
{ //Note the last son pay out time
_dpo.last_son_payout_time = now; modify( dpo, [&]( dynamic_global_property_object& _dpo )
}); {
} _dpo.last_son_payout_time = now;
});
} }
} }
@ -2152,7 +2227,7 @@ void database::perform_son_tasks()
}); });
} }
// create HIVE asset here because son_account is the issuer of the HIVE // create HIVE asset here because son_account is the issuer of the HIVE
if (gpo.parameters.hive_asset() == asset_id_type() && head_block_time() >= HARDFORK_SON_FOR_HIVE_TIME) if (gpo.parameters.hive_asset() == asset_id_type() && head_block_time() >= HARDFORK_SON_FOR_HIVE_TIME)
{ {
const asset_dynamic_data_object& dyn_asset = const asset_dynamic_data_object& dyn_asset =
create<asset_dynamic_data_object>([](asset_dynamic_data_object& a) { create<asset_dynamic_data_object>([](asset_dynamic_data_object& a) {
@ -2192,7 +2267,10 @@ void database::perform_son_tasks()
// Before making a budget we should pay out SONs // Before making a budget we should pay out SONs
// This function should check if its time to pay sons // This function should check if its time to pay sons
// and modify the global son funds accordingly, whatever is left is passed on to next budget // and modify the global son funds accordingly, whatever is left is passed on to next budget
pay_sons(); if(head_block_time() < HARDFORK_SON_FOR_ETHEREUM_TIME)
pay_sons_before_hf_ethereum();
else
pay_sons_after_hf_ethereum();
} }
// Split vote_ids // Split vote_ids

View file

@ -579,7 +579,8 @@ namespace graphene { namespace chain {
void initialize_budget_record( fc::time_point_sec now, budget_record& rec )const; void initialize_budget_record( fc::time_point_sec now, budget_record& rec )const;
void process_budget(); void process_budget();
void pay_workers( share_type& budget ); void pay_workers( share_type& budget );
void pay_sons(); void pay_sons_before_hf_ethereum();
void pay_sons_after_hf_ethereum();
void perform_son_tasks(); void perform_son_tasks();
void perform_chain_maintenance(const signed_block& next_block, const global_property_object& global_props); void perform_chain_maintenance(const signed_block& next_block, const global_property_object& global_props);
void update_active_witnesses(); void update_active_witnesses();

View file

@ -680,8 +680,7 @@ void sidechain_net_handler::on_applied_block(const signed_block &b) {
const bool is_tracked_asset = const bool is_tracked_asset =
((sidechain == sidechain_type::bitcoin) && (transfer_op.amount.asset_id == gpo.parameters.btc_asset())) || ((sidechain == sidechain_type::bitcoin) && (transfer_op.amount.asset_id == gpo.parameters.btc_asset())) ||
((sidechain == sidechain_type::ethereum) && (transfer_op.amount.asset_id == gpo.parameters.eth_asset())) || ((sidechain == sidechain_type::ethereum) && (transfer_op.amount.asset_id == gpo.parameters.eth_asset())) ||
((sidechain == sidechain_type::ethereum) && (transfer_op.amount.asset_id != gpo.parameters.btc_asset()) ((sidechain == sidechain_type::ethereum) && (transfer_op.amount.asset_id != gpo.parameters.btc_asset()) && (transfer_op.amount.asset_id != gpo.parameters.hbd_asset()) && (transfer_op.amount.asset_id != gpo.parameters.hive_asset())) ||
&& (transfer_op.amount.asset_id != gpo.parameters.hbd_asset()) && (transfer_op.amount.asset_id != gpo.parameters.hive_asset())) ||
((sidechain == sidechain_type::hive) && (transfer_op.amount.asset_id == gpo.parameters.hbd_asset())) || ((sidechain == sidechain_type::hive) && (transfer_op.amount.asset_id == gpo.parameters.hbd_asset())) ||
((sidechain == sidechain_type::hive) && (transfer_op.amount.asset_id == gpo.parameters.hive_asset())); ((sidechain == sidechain_type::hive) && (transfer_op.amount.asset_id == gpo.parameters.hive_asset()));

View file

@ -674,7 +674,7 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
uint32_t rpc_port = options.at("bitcoin-node-rpc-port").as<uint32_t>(); uint32_t rpc_port = options.at("bitcoin-node-rpc-port").as<uint32_t>();
std::string rpc_user = options.at("bitcoin-node-rpc-user").as<std::string>(); std::string rpc_user = options.at("bitcoin-node-rpc-user").as<std::string>();
std::string rpc_password = options.at("bitcoin-node-rpc-password").as<std::string>(); std::string rpc_password = options.at("bitcoin-node-rpc-password").as<std::string>();
if (options.count("use-bitcoind-client")) { if (options.count("use-bitcoind-client")) {
use_bitcoind_client = options.at("use-bitcoind-client").as<bool>(); use_bitcoind_client = options.at("use-bitcoind-client").as<bool>();
} }

View file

@ -574,7 +574,7 @@ BOOST_AUTO_TEST_CASE( son_pay_test )
BOOST_REQUIRE_EQUAL(son_stats_obj2->total_sidechain_txs_reported.at(sidechain_type::hive), 12); BOOST_REQUIRE_EQUAL(son_stats_obj2->total_sidechain_txs_reported.at(sidechain_type::hive), 12);
BOOST_REQUIRE_EQUAL(son_stats_obj2->total_sidechain_txs_reported.at(sidechain_type::ethereum), 18); BOOST_REQUIRE_EQUAL(son_stats_obj2->total_sidechain_txs_reported.at(sidechain_type::ethereum), 18);
// Check that Alice and Bob are paid for signing the transactions in the previous day/cycle // Check that Alice and Bob are paid for signing the transactions in the previous day/cycle
BOOST_REQUIRE_EQUAL(db.get_balance(obj1->son_account, asset_id_type()).amount.value, 80+obj1_balance); BOOST_REQUIRE_EQUAL(db.get_balance(obj1->son_account, asset_id_type()).amount.value, 79+obj1_balance);
BOOST_REQUIRE_EQUAL(db.get_balance(obj2->son_account, asset_id_type()).amount.value, 120+obj2_balance); BOOST_REQUIRE_EQUAL(db.get_balance(obj2->son_account, asset_id_type()).amount.value, 120+obj2_balance);
// Check the SON Budget is again allocated after maintenance // Check the SON Budget is again allocated after maintenance
BOOST_CHECK( dpo.son_budget.value == 200); BOOST_CHECK( dpo.son_budget.value == 200);