From ec76179b0be06b67947ad718c0da7c8c566ce47e Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Thu, 18 Jun 2015 15:51:11 -0400 Subject: [PATCH] Resolve #40 --- libraries/chain/db_block.cpp | 6 +++++ libraries/chain/db_init.cpp | 7 +++--- libraries/chain/db_maint.cpp | 48 ++++++++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/libraries/chain/db_block.cpp b/libraries/chain/db_block.cpp index 99ee8802..dc4476d2 100644 --- a/libraries/chain/db_block.cpp +++ b/libraries/chain/db_block.cpp @@ -515,6 +515,7 @@ processed_transaction database::_apply_transaction( const signed_transaction& tr eval_state.operation_results.reserve( trx.operations.size() ); + //Finally process the operations processed_transaction ptrx(trx); _current_op_in_trx = 0; for( const auto& op : ptrx.operations ) @@ -524,6 +525,11 @@ processed_transaction database::_apply_transaction( const signed_transaction& tr } ptrx.operation_results = std::move( eval_state.operation_results ); + //Make sure the temp account has no non-zero balances + const auto& index = get_index_type().indices().get(); + auto range = index.equal_range(GRAPHENE_TEMP_ACCOUNT); + std::for_each(range.first, range.second, [](const account_balance_object& b) { FC_ASSERT(b.balance == 0); }); + return ptrx; } FC_CAPTURE_AND_RETHROW( (trx) ) } diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index 204f6020..1d75f601 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -155,8 +155,9 @@ void database::init_genesis(const genesis_state_type& genesis_state) n.membership_expiration_date = time_point_sec::maximum(); n.network_fee_percentage = GRAPHENE_DEFAULT_NETWORK_PERCENT_OF_FEE; n.lifetime_referrer_fee_percentage = GRAPHENE_100_PERCENT - GRAPHENE_DEFAULT_NETWORK_PERCENT_OF_FEE; + n.owner.weight_threshold = 1; + n.active.weight_threshold = 1; n.name = "committee-account"; - n.active = n.owner; n.statistics = create( [&](account_statistics_object& b){}).id; }); FC_ASSERT(committee_account.get_id() == GRAPHENE_COMMITTEE_ACCOUNT); @@ -183,8 +184,8 @@ void database::init_genesis(const genesis_state_type& genesis_state) FC_ASSERT(create([this](account_object& a) { a.name = "null-account"; a.statistics = create([](account_statistics_object&){}).id; - a.owner.weight_threshold = 0; - a.active.weight_threshold = 0; + a.owner.weight_threshold = 1; + a.active.weight_threshold = 1; a.registrar = a.lifetime_referrer = a.referrer = GRAPHENE_NULL_ACCOUNT; a.membership_expiration_date = time_point_sec::maximum(); a.network_fee_percentage = GRAPHENE_DEFAULT_NETWORK_PERCENT_OF_FEE; diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index 906d106e..89d8b1b9 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -100,6 +100,34 @@ void database::update_active_witnesses() auto wits = sort_votable_objects(std::max(witness_count*2+1, GRAPHENE_MIN_WITNESS_COUNT)); const global_property_object& gpo = get_global_properties(); + // Update witness authority + modify( get(GRAPHENE_WITNESS_ACCOUNT), [&]( account_object& a ) { + uint64_t total_votes = 0; + map weights; + a.active.weight_threshold = 0; + a.active.auths.clear(); + + for( const witness_object& wit : wits ) + { + weights.emplace(wit.witness_account, _vote_tally_buffer[wit.vote_id]); + total_votes += _vote_tally_buffer[wit.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 auto& weight : weights ) + { + // Ensure that everyone has at least one vote. Zero weights aren't allowed. + uint16_t votes = std::max((weight.second >> bits_to_drop), uint64_t(1) ); + a.active.auths[weight.first] += votes; + a.active.weight_threshold += votes; + } + + a.active.weight_threshold /= 2; + a.active.weight_threshold += 1; + }); + modify( gpo, [&]( global_property_object& gp ){ gp.active_witnesses.clear(); gp.active_witnesses.reserve( wits.size() ); @@ -139,11 +167,12 @@ void database::update_active_delegates() // Update genesis authorities if( !delegates.empty() ) - modify( get(account_id_type()), [&]( account_object& a ) { + { + modify( get(GRAPHENE_COMMITTEE_ACCOUNT), [&]( account_object& a ) { uint64_t total_votes = 0; map weights; - a.owner.weight_threshold = 0; - a.owner.auths.clear(); + a.active.weight_threshold = 0; + a.active.auths.clear(); for( const delegate_object& del : delegates ) { @@ -158,14 +187,17 @@ void database::update_active_delegates() { // Ensure that everyone has at least one vote. Zero weights aren't allowed. uint16_t votes = std::max((weight.second >> bits_to_drop), uint64_t(1) ); - a.owner.auths[weight.first] += votes; - a.owner.weight_threshold += votes; + a.active.auths[weight.first] += votes; + a.active.weight_threshold += votes; } - a.owner.weight_threshold /= 2; - a.owner.weight_threshold += 1; - a.active = a.owner; + a.active.weight_threshold /= 2; + a.active.weight_threshold += 1; }); + modify( get(GRAPHENE_RELAXED_COMMITTEE_ACCOUNT), [&](account_object& a) { + a.active = get(GRAPHENE_COMMITTEE_ACCOUNT).active; + }); + } modify( get_global_properties(), [&]( global_property_object& gp ) { gp.active_delegates.clear(); std::transform(delegates.begin(), delegates.end(),