Skip to content

Commit

Permalink
Fixed removing votes (#139)
Browse files Browse the repository at this point in the history
* Fixed removing votes

After removing votes `last_vote_weight` was equal `inf` due to division
by zero producers size.

* Update voting.cpp

* Update voting.cpp

Co-authored-by: Roman Cherednik <[email protected]>
  • Loading branch information
Alladin9393 and roman-tik authored Feb 13, 2020
1 parent 1a2ee09 commit 4d8ba5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
14 changes: 12 additions & 2 deletions contracts/contracts/rem.system/src/voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,12 @@ namespace eosiosystem {
}
}

auto new_vote_weight = stake2vote( voter->staked, voter->stake_lock_time ) / producers.size();
double new_vote_weight = 0;

if(producers.size()) {
new_vote_weight = stake2vote( voter->staked, voter->stake_lock_time ) / producers.size();
}

if( voter->is_proxy ) {
new_vote_weight += voter->proxied_vote_weight;
}
Expand Down Expand Up @@ -277,7 +282,12 @@ namespace eosiosystem {

void system_contract::propagate_weight_change( const voter_info& voter ) {
check( !voter.proxy || !voter.is_proxy, "account registered as a proxy is not allowed to use a proxy" );
double new_weight = stake2vote( voter.staked, voter.stake_lock_time ) / voter.producers.size();

double new_weight = 0;

if(voter.producers.size()) {
new_weight = stake2vote( voter.staked, voter.stake_lock_time ) / voter.producers.size();
}
if ( voter.is_proxy ) {
new_weight += voter.proxied_vote_weight;
}
Expand Down
11 changes: 11 additions & 0 deletions unittests/rem_voting_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,17 @@ BOOST_FIXTURE_TEST_CASE( rem_vote_weight_test, voting_tester ) {
BOOST_TEST_REQUIRE( 1.4880535147525217e+18 == prod["total_votes"].as_double() );

}

// Test removing votes
{
const auto voter_before = get_voter_info( name("whale1") );
BOOST_TEST_REQUIRE( 0.0 < voter_before["last_vote_weight"].as_double() );
votepro( N(whale1), {} );

// last_vote_weight should be 0
const auto voter_after = get_voter_info( name("whale1") );
BOOST_TEST_REQUIRE( 0.0 == voter_after["last_vote_weight"].as_double() );
}
} FC_LOG_AND_RETHROW()
}

Expand Down

0 comments on commit 4d8ba5a

Please sign in to comment.