From 7ea3bc159fad9c3f0ba62237c6482969ad2e2803 Mon Sep 17 00:00:00 2001 From: Koneru Satyanarayana Date: Fri, 13 Dec 2019 01:48:01 +1100 Subject: [PATCH] SON194-SON195 - Addition of SON BTC Account and report son down changes --- libraries/app/impacted.cpp | 3 + libraries/chain/db_init.cpp | 1 + libraries/chain/db_maint.cpp | 61 +++++++++++++++++-- libraries/chain/db_notify.cpp | 3 + .../chain/protocol/chain_parameters.hpp | 5 ++ .../graphene/chain/protocol/operations.hpp | 3 +- .../include/graphene/chain/protocol/son.hpp | 18 +++++- .../include/graphene/chain/son_evaluator.hpp | 9 +++ libraries/chain/proposal_evaluator.cpp | 4 ++ libraries/chain/son_evaluator.cpp | 32 ++++++++++ 10 files changed, 132 insertions(+), 7 deletions(-) diff --git a/libraries/app/impacted.cpp b/libraries/app/impacted.cpp index 66dc2939..50a937ba 100644 --- a/libraries/app/impacted.cpp +++ b/libraries/app/impacted.cpp @@ -310,6 +310,9 @@ struct get_impacted_account_visitor void operator()( const son_heartbeat_operation& op ){ _impacted.insert( op.owner_account ); } + void operator()( const son_report_down_operation& op ){ + _impacted.insert( op.payer ); + } }; void operation_get_impacted_accounts( const operation& op, flat_set& result ) diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index 956e34a7..52416050 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -248,6 +248,7 @@ void database::initialize_evaluators() register_evaluator(); register_evaluator(); register_evaluator(); + register_evaluator(); } void database::initialize_indexes() diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index cae17eda..1e3310ae 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -455,11 +455,62 @@ void database::update_active_sons() }); }); - //const witness_schedule_object& wso = witness_schedule_id_type()(*this); - //modify(wso, [&](witness_schedule_object& _wso) - //{ - // _wso.scheduler.update(gpo.active_witnesses); - //}); + if(gpo.parameters.get_son_btc_account_id() == GRAPHENE_NULL_ACCOUNT) { + const auto& son_btc_account = create( [&]( account_object& obj ) { + uint64_t total_votes = 0; + obj.name = "son_btc_account"; + obj.statistics = create([&]( account_statistics_object& acc_stat ){ acc_stat.owner = obj.id; }).id; + obj.membership_expiration_date = time_point_sec::maximum(); + obj.network_fee_percentage = GRAPHENE_DEFAULT_NETWORK_PERCENT_OF_FEE; + obj.lifetime_referrer_fee_percentage = GRAPHENE_100_PERCENT - GRAPHENE_DEFAULT_NETWORK_PERCENT_OF_FEE; + + for( const son_object& son : gpo.active_sons ) + { + total_votes += _vote_tally_buffer[son.vote_id]; + } + // total_votes is 64 bits. Subtract the number of leading low bits from 64 to get the number of useful bits, + // then I want to keep the most significant 16 bits of what's left. + int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0); + for( const son_object& son : gpo.active_sons ) + { + // Ensure that everyone has at least one vote. Zero weights aren't allowed. + uint16_t votes = std::max((_vote_tally_buffer[son.vote_id] >> bits_to_drop), uint64_t(1) ); + obj.active.account_auths[son.son_account] += votes; + obj.active.weight_threshold += votes; + } + obj.active.weight_threshold *= 2; + obj.active.weight_threshold /= 3; + obj.active.weight_threshold += 1; + }); + + modify( gpo, [&]( global_property_object& gpo ) { + gpo.parameters.extensions.value.son_btc_account = son_btc_account; + if( gpo.pending_parameters ) + gpo.pending_parameters->extensions.value.son_btc_account = son_btc_account; + }); + } else { + modify( get(gpo.parameters.get_son_btc_account_id()), [&]( account_object& obj ) + { + uint64_t total_votes = 0; + for( const son_object& son : gpo.active_sons ) + { + total_votes += _vote_tally_buffer[son.vote_id]; + } + // total_votes is 64 bits. Subtract the number of leading low bits from 64 to get the number of useful bits, + // then I want to keep the most significant 16 bits of what's left. + int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0); + for( const son_object& son : gpo.active_sons ) + { + // Ensure that everyone has at least one vote. Zero weights aren't allowed. + uint16_t votes = std::max((_vote_tally_buffer[son.vote_id] >> bits_to_drop), uint64_t(1) ); + obj.active.account_auths[son.son_account] += votes; + obj.active.weight_threshold += votes; + } + obj.active.weight_threshold *= 2; + obj.active.weight_threshold /= 3; + obj.active.weight_threshold += 1; + }); + } } FC_CAPTURE_AND_RETHROW() } void database::initialize_budget_record( fc::time_point_sec now, budget_record& rec )const diff --git a/libraries/chain/db_notify.cpp b/libraries/chain/db_notify.cpp index c71b2930..0a4f8efc 100644 --- a/libraries/chain/db_notify.cpp +++ b/libraries/chain/db_notify.cpp @@ -297,6 +297,9 @@ struct get_impacted_account_visitor void operator()( const son_heartbeat_operation& op ) { _impacted.insert( op.owner_account ); } + void operator()( const son_report_down_operation& op ) { + _impacted.insert( op.payer ); + } }; void operation_get_impacted_accounts( const operation& op, flat_set& result ) diff --git a/libraries/chain/include/graphene/chain/protocol/chain_parameters.hpp b/libraries/chain/include/graphene/chain/protocol/chain_parameters.hpp index c62274ba..9dcd5aad 100644 --- a/libraries/chain/include/graphene/chain/protocol/chain_parameters.hpp +++ b/libraries/chain/include/graphene/chain/protocol/chain_parameters.hpp @@ -46,6 +46,7 @@ namespace graphene { namespace chain { optional < uint32_t > son_vesting_amount; optional < uint32_t > son_vesting_period; optional < uint32_t > son_pay_daily_max; + optional < graphene::chain::account_id_type > son_btc_account; }; struct chain_parameters @@ -138,6 +139,9 @@ namespace graphene { namespace chain { inline uint16_t son_pay_daily_max()const { return extensions.value.son_pay_daily_max.valid() ? *extensions.value.son_pay_daily_max : MIN_SON_PAY_DAILY_MAX; } + inline const account_id_type& get_son_btc_account_id() const { + return extensions.value.son_btc_account.valid() ? *extensions.value.son_btc_account : GRAPHENE_NULL_ACCOUNT; + } }; } } // graphene::chain @@ -155,6 +159,7 @@ FC_REFLECT( graphene::chain::parameter_extension, (son_vesting_amount) (son_vesting_period) (son_pay_daily_max) + (son_btc_account) ) FC_REFLECT( graphene::chain::chain_parameters, diff --git a/libraries/chain/include/graphene/chain/protocol/operations.hpp b/libraries/chain/include/graphene/chain/protocol/operations.hpp index a0e72094..ce98ffca 100644 --- a/libraries/chain/include/graphene/chain/protocol/operations.hpp +++ b/libraries/chain/include/graphene/chain/protocol/operations.hpp @@ -140,7 +140,8 @@ namespace graphene { namespace chain { son_create_operation, son_update_operation, son_delete_operation, - son_heartbeat_operation + son_heartbeat_operation, + son_report_down_operation > operation; /// @} // operations group diff --git a/libraries/chain/include/graphene/chain/protocol/son.hpp b/libraries/chain/include/graphene/chain/protocol/son.hpp index 08e74a2d..e907c6d6 100644 --- a/libraries/chain/include/graphene/chain/protocol/son.hpp +++ b/libraries/chain/include/graphene/chain/protocol/son.hpp @@ -60,6 +60,19 @@ namespace graphene { namespace chain { share_type calculate_fee(const fee_parameters_type& k)const { return 0; } }; + struct son_report_down_operation : public base_operation + { + struct fee_parameters_type { uint64_t fee = 0; }; + + asset fee; + son_id_type son_id; + account_id_type payer; + time_point_sec down_ts; + + account_id_type fee_payer()const { return payer; } + share_type calculate_fee(const fee_parameters_type& k)const { return 0; } + }; + } } // namespace graphene::chain FC_REFLECT(graphene::chain::son_create_operation::fee_parameters_type, (fee) ) @@ -74,4 +87,7 @@ FC_REFLECT(graphene::chain::son_delete_operation::fee_parameters_type, (fee) ) FC_REFLECT(graphene::chain::son_delete_operation, (fee)(son_id)(payer)(owner_account) ) FC_REFLECT(graphene::chain::son_heartbeat_operation::fee_parameters_type, (fee) ) -FC_REFLECT(graphene::chain::son_heartbeat_operation, (fee)(son_id)(owner_account)(ts) ) \ No newline at end of file +FC_REFLECT(graphene::chain::son_heartbeat_operation, (fee)(son_id)(owner_account)(ts) ) + +FC_REFLECT(graphene::chain::son_report_down_operation::fee_parameters_type, (fee) ) +FC_REFLECT(graphene::chain::son_report_down_operation, (fee)(son_id)(payer)(down_ts) ) \ No newline at end of file diff --git a/libraries/chain/include/graphene/chain/son_evaluator.hpp b/libraries/chain/include/graphene/chain/son_evaluator.hpp index 6b82f5e5..4615c95b 100644 --- a/libraries/chain/include/graphene/chain/son_evaluator.hpp +++ b/libraries/chain/include/graphene/chain/son_evaluator.hpp @@ -40,4 +40,13 @@ public: object_id_type do_apply(const son_heartbeat_operation& o); }; +class son_report_down_evaluator : public evaluator +{ +public: + typedef son_report_down_operation operation_type; + + void_result do_evaluate(const son_report_down_operation& o); + object_id_type do_apply(const son_report_down_operation& o); +}; + } } // namespace graphene::chain diff --git a/libraries/chain/proposal_evaluator.cpp b/libraries/chain/proposal_evaluator.cpp index b50d4b82..dc1aba3e 100644 --- a/libraries/chain/proposal_evaluator.cpp +++ b/libraries/chain/proposal_evaluator.cpp @@ -152,6 +152,10 @@ struct proposal_operation_hardfork_visitor FC_ASSERT( block_time >= HARDFORK_SON_TIME, "son_heartbeat_operation not allowed yet!" ); } + void operator()(const son_report_down_operation &v) const { + FC_ASSERT( block_time >= HARDFORK_SON_TIME, "son_report_down_operation not allowed yet!" ); + } + // loop and self visit in proposals void operator()(const proposal_create_operation &v) const { for (const op_wrapper &op : v.proposed_ops) diff --git a/libraries/chain/son_evaluator.cpp b/libraries/chain/son_evaluator.cpp index 4300bdbb..face1319 100644 --- a/libraries/chain/son_evaluator.cpp +++ b/libraries/chain/son_evaluator.cpp @@ -141,4 +141,36 @@ object_id_type son_heartbeat_evaluator::do_apply(const son_heartbeat_operation& return op.son_id; } FC_CAPTURE_AND_RETHROW( (op) ) } +void_result son_report_down_evaluator::do_evaluate(const son_report_down_operation& op) +{ try { + FC_ASSERT(op.payer == db().get_global_properties().parameters.get_son_btc_account_id(), "Payer should be the son btc account"); + const auto& idx = db().get_index_type().indices().get(); + FC_ASSERT( idx.find(op.son_id) != idx.end() ); + auto itr = idx.find(op.son_id); + auto stats = itr->statistics( db() ); + FC_ASSERT(itr->status == son_status::active, "Inactive/Deregistered/in_maintenance SONs cannot be reported on as down"); + FC_ASSERT(op.down_ts >= stats.last_active_timestamp, "down_ts should be greater than last_active_timestamp"); + return void_result(); +} FC_CAPTURE_AND_RETHROW( (op) ) } + +object_id_type son_report_down_evaluator::do_apply(const son_report_down_operation& op) +{ try { + const auto& idx = db().get_index_type().indices().get(); + auto itr = idx.find(op.son_id); + if(itr != idx.end()) + { + if (itr->status == son_status::active) { + db().modify( itr->statistics( db() ), [&]( son_statistics_object& sso ) + { + sso.last_down_timestamp = op.down_ts; + }); + + db().modify(*itr, [&op](son_object &so) { + so.status = son_status::in_maintenance; + }); + } + } + return op.son_id; +} FC_CAPTURE_AND_RETHROW( (op) ) } + } } // namespace graphene::chain