Improve vote counting implementation #533

This commit is contained in:
theoreticalbts 2016-01-22 15:57:53 -05:00
parent 447018b319
commit a1ee326b55
3 changed files with 102 additions and 4 deletions

View file

@ -28,6 +28,7 @@
#include <fc/uint128.hpp>
#include <graphene/chain/database.hpp>
#include <graphene/chain/hardfork.hpp>
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/asset_object.hpp>
@ -36,6 +37,7 @@
#include <graphene/chain/committee_member_object.hpp>
#include <graphene/chain/global_property_object.hpp>
#include <graphene/chain/vesting_balance_object.hpp>
#include <graphene/chain/vote_count.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/chain/worker_object.hpp>
@ -183,7 +185,10 @@ void database::update_active_witnesses()
}
// Update witness authority
modify( get(GRAPHENE_WITNESS_ACCOUNT), [&]( account_object& a ) {
modify( get(GRAPHENE_WITNESS_ACCOUNT), [&]( account_object& a )
{
if( head_block_time() < HARDFORK_533_TIME )
{
uint64_t total_votes = 0;
map<account_id_type, uint64_t> weights;
a.active.weight_threshold = 0;
@ -208,7 +213,15 @@ void database::update_active_witnesses()
a.active.weight_threshold /= 2;
a.active.weight_threshold += 1;
});
}
else
{
vote_counter vc;
for( const witness_object& wit : wits )
vc.add( wit.witness_account, _vote_tally_buffer[wit.vote_id] );
vc.finish( a.active );
}
} );
modify(gpo, [&]( global_property_object& gp ){
gp.active_witnesses.clear();
@ -256,7 +269,10 @@ void database::update_active_committee_members()
// Update committee authorities
if( !committee_members.empty() )
{
modify(get(GRAPHENE_COMMITTEE_ACCOUNT), [&](account_object& a) {
modify(get(GRAPHENE_COMMITTEE_ACCOUNT), [&](account_object& a)
{
if( head_block_time() < HARDFORK_533_TIME )
{
uint64_t total_votes = 0;
map<account_id_type, uint64_t> weights;
a.active.weight_threshold = 0;
@ -281,7 +297,15 @@ void database::update_active_committee_members()
a.active.weight_threshold /= 2;
a.active.weight_threshold += 1;
});
}
else
{
vote_counter vc;
for( const committee_member_object& cm : committee_members )
vc.add( cm.committee_member_account, _vote_tally_buffer[cm.vote_id] );
vc.finish( a.active );
}
} );
modify(get(GRAPHENE_RELAXED_COMMITTEE_ACCOUNT), [&](account_object& a) {
a.active = get(GRAPHENE_COMMITTEE_ACCOUNT).active;
});

View file

@ -0,0 +1,4 @@
// #533 Improve vote counting implementation
#ifndef HARDFORK_533_TIME
#define HARDFORK_533_TIME (fc::time_point_sec( 1455127200 ))
#endif

View file

@ -0,0 +1,70 @@
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
#include <graphene/chain/protocol/authority.hpp>
namespace graphene { namespace chain {
/**
* Keep track of vote totals in internal authority object. See #533.
*/
struct vote_counter
{
template< typename Component >
void add( Component who, uint64_t votes )
{
assert( votes <= last_votes );
last_votes = votes;
if( bitshift == -1 )
bitshift = std::max(int(boost::multiprecision::detail::find_msb( votes )) - 15, 0);
uint64_t scaled_votes = std::max( votes >> bitshift, uint64_t(1) );
assert( scaled_votes <= std::numeric_limits<weight_type>::max() );
total_votes += scaled_votes;
assert( total_votes <= std::numeric_limits<uint32_t>::max() );
auth.add_authority( who, weight_type( scaled_votes ) );
}
/**
* Write into out_auth, but only if we have at least one member.
*/
void finish( authority& out_auth )
{
if( total_votes == 0 )
return;
assert( total_votes <= std::numeric_limits<uint32_t>::max() );
uint32_t weight = uint32_t( total_votes );
weight = (weight >> 1)+1;
auth.weight_threshold = weight;
out_auth = auth;
}
uint64_t last_votes = std::numeric_limits<uint64_t>::max();
uint64_t total_votes = 0;
int8_t bitshift = -1;
authority auth;
};
} } // graphene::chain