Skip to content

Commit

Permalink
Merge pull request #3482 from steemit/downvote-overflow-fix
Browse files Browse the repository at this point in the history
Downvote overflow fix
  • Loading branch information
Michael Vandeberg authored Aug 27, 2019
2 parents 1e59c96 + ec6eda5 commit 51ec865
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
5 changes: 4 additions & 1 deletion libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ asset create_vesting2( database& db, const account_object& to_account, asset liq
cprops,
a,
db.has_hardfork( STEEM_HARDFORK_0_21__3336 ),
db.head_block_num() > STEEM_HF_21_STALL_BLOCK,
new_vesting.amount.value );
});
}
Expand Down Expand Up @@ -4294,6 +4295,7 @@ void database::clear_expired_delegations()
while( itr != delegations_by_exp.end() && itr->expiration < now )
{
operation vop = return_vesting_delegation_operation( itr->delegator, itr->vesting_shares );
try{
pre_push_virtual_operation( vop );

modify( get_account( itr->delegator ), [&]( account_object& a )
Expand All @@ -4304,6 +4306,7 @@ void database::clear_expired_delegations()
gpo,
a,
has_hardfork( STEEM_HARDFORK_0_21__3336 ),
head_block_num() > STEEM_HF_21_STALL_BLOCK,
itr->vesting_shares.amount.value );
}

Expand All @@ -4314,7 +4317,7 @@ void database::clear_expired_delegations()

remove( *itr );
itr = delegations_by_exp.begin();
}
} FC_CAPTURE_AND_RETHROW( (vop) ) }
}
#ifdef STEEM_ENABLE_SMT
template< typename smt_balance_object_type, class balance_operator_type >
Expand Down
28 changes: 24 additions & 4 deletions libraries/chain/include/steem/chain/util/manabar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ struct manabar_params
: max_mana(mm), regen_time(rt) {}

void validate()const
{
{ try{
FC_ASSERT( max_mana >= 0 );
}
} FC_CAPTURE_AND_RETHROW( (max_mana) ) }
};

struct manabar
Expand Down Expand Up @@ -117,21 +117,41 @@ int64_t get_effective_vesting_shares( const T& account )
}

template< typename PropType, typename AccountType >
void update_manabar( const PropType& gpo, AccountType& account, bool downvote_mana = false, int64_t new_mana = 0 )
void update_manabar( const PropType& gpo, AccountType& account, bool downvote_mana = false, bool check_overflow = true, int64_t new_mana = 0 )
{
auto effective_vests = util::get_effective_vesting_shares( account );
try {
manabar_params params( effective_vests, STEEM_VOTING_MANA_REGENERATION_SECONDS );
account.voting_manabar.regenerate_mana( params, gpo.time );
account.voting_manabar.use_mana( -new_mana );
} FC_CAPTURE_LOG_AND_RETHROW( (account)(effective_vests) )

FC_TODO( "This hardfork check should not be needed. Remove after HF21 if that is the case." );
// This is used as a hardfork check. Can be replaced with if( gpo.downvote_pool_percent ). Leaving as a hard check to be safe until after HF 21
try{
if( downvote_mana )
{
util::manabar_params params( ( effective_vests * gpo.downvote_pool_percent ) / STEEM_100_PERCENT, STEEM_VOTING_MANA_REGENERATION_SECONDS );
manabar_params params;
params.regen_time = STEEM_VOTING_MANA_REGENERATION_SECONDS;

if( check_overflow )
{
params.max_mana = ( ( uint128_t( effective_vests ) * gpo.downvote_pool_percent ) / STEEM_100_PERCENT ).to_int64();
}
else
{
FC_TODO( "Cleanup once we have verified the overflow has not permanently made it in to the chain" );
uint128_t numerator = effective_vests * gpo.downvote_pool_percent;
if( numerator.hi != 0 )
elog( "NOTIFYALERT! max mana overflow made it in to the chain" );

params.max_mana = ( effective_vests * gpo.downvote_pool_percent ) / STEEM_100_PERCENT;
}

account.downvote_manabar.regenerate_mana( params, gpo.time );
account.downvote_manabar.use_mana( ( -new_mana * gpo.downvote_pool_percent ) / STEEM_100_PERCENT );
}
} FC_CAPTURE_LOG_AND_RETHROW( (account)(effective_vests) )
}

} } } // steem::chain::util
Expand Down
10 changes: 5 additions & 5 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ void hf20_vote_evaluator( const vote_operation& o, database& _db )

_db.modify( voter, [&]( account_object& a )
{
util::update_manabar( _db.get_dynamic_global_properties(), a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ) );
util::update_manabar( _db.get_dynamic_global_properties(), a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ), _db.head_block_num() > STEEM_HF_21_STALL_BLOCK );
});

if ( _db.has_hardfork( STEEM_HARDFORK_0_21__3004 ) )
Expand Down Expand Up @@ -2975,7 +2975,7 @@ void claim_reward_balance_evaluator::do_apply( const claim_reward_balance_operat
{
if( _db.has_hardfork( STEEM_HARDFORK_0_20__2539 ) )
{
util::update_manabar( _db.get_dynamic_global_properties(), a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ), op.reward_vests.amount.value );
util::update_manabar( _db.get_dynamic_global_properties(), a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ), _db.head_block_num() > STEEM_HF_21_STALL_BLOCK, op.reward_vests.amount.value );
}

a.vesting_shares += op.reward_vests;
Expand Down Expand Up @@ -3084,7 +3084,7 @@ void delegate_vesting_shares_evaluator::do_apply( const delegate_vesting_shares_

_db.modify( delegator, [&]( account_object& a )
{
util::update_manabar( gpo, a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ) );
util::update_manabar( gpo, a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ), _db.head_block_num() > STEEM_HF_21_STALL_BLOCK );
});

available_shares = asset( delegator.voting_manabar.current_mana, VESTS_SYMBOL );
Expand Down Expand Up @@ -3180,7 +3180,7 @@ void delegate_vesting_shares_evaluator::do_apply( const delegate_vesting_shares_
{
if( _db.has_hardfork( STEEM_HARDFORK_0_20__2539 ) )
{
util::update_manabar( gpo, a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ), op.vesting_shares.amount.value );
util::update_manabar( gpo, a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ), _db.head_block_num() > STEEM_HF_21_STALL_BLOCK, op.vesting_shares.amount.value );
}

a.received_vesting_shares += op.vesting_shares;
Expand Down Expand Up @@ -3218,7 +3218,7 @@ void delegate_vesting_shares_evaluator::do_apply( const delegate_vesting_shares_
{
if( _db.has_hardfork( STEEM_HARDFORK_0_20__2539 ) )
{
util::update_manabar( gpo, a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ), delta.amount.value );
util::update_manabar( gpo, a, _db.has_hardfork( STEEM_HARDFORK_0_21__3336 ), _db.head_block_num() > STEEM_HF_21_STALL_BLOCK, delta.amount.value );
}

a.received_vesting_shares += delta;
Expand Down
25 changes: 17 additions & 8 deletions libraries/plugins/rc/rc_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ void use_account_rcs(
#endif
)
{

if( account_name == account_name_type() )
{
if( db.is_producing() )
Expand All @@ -262,6 +263,8 @@ void use_account_rcs(
mbparams.max_mana = get_maximum_rc( account, rc_account );
mbparams.regen_time = STEEM_RC_REGEN_TIME;

try{

db.modify( rc_account, [&]( rc_account_object& rca )
{
rca.rc_manabar.regenerate_mana< true >( mbparams, gpo.time.sec_since_epoch() );
Expand Down Expand Up @@ -305,10 +308,11 @@ void use_account_rcs(

rca.rc_manabar.use_mana( rc, min_mana );
} );
}FC_CAPTURE_AND_RETHROW( (account)(rc_account)(mbparams.max_mana) )
}

void rc_plugin_impl::on_post_apply_transaction( const transaction_notification& note )
{
{ try {
const dynamic_global_property_object& gpo = _db.get_dynamic_global_properties();
if( before_first_block() )
return;
Expand Down Expand Up @@ -357,7 +361,7 @@ void rc_plugin_impl::on_post_apply_transaction( const transaction_notification&
}
if( export_data )
export_data->tx_info.push_back( tx_info );
}
} FC_CAPTURE_AND_RETHROW( (note.transaction) ) }

struct block_extensions_count_resources_visitor
{
Expand All @@ -383,7 +387,7 @@ struct block_extensions_count_resources_visitor
};

void rc_plugin_impl::on_post_apply_block( const block_notification& note )
{
{ try{
const dynamic_global_property_object& gpo = _db.get_dynamic_global_properties();
if( before_first_block() )
{
Expand Down Expand Up @@ -512,7 +516,7 @@ void rc_plugin_impl::on_post_apply_block( const block_notification& note )
steem::plugins::block_data_export::find_export_data< exp_rc_data >( STEEM_RC_PLUGIN_NAME );
if( export_data )
export_data->block_info = block_info;
}
} FC_CAPTURE_AND_RETHROW( (note.block) ) }

void rc_plugin_impl::on_first_block()
{
Expand Down Expand Up @@ -617,6 +621,8 @@ struct pre_apply_operation_visitor
mbparams.max_mana = get_maximum_rc( account, rc_account );
mbparams.regen_time = STEEM_RC_REGEN_TIME;

try {

if( mbparams.max_mana != rc_account.last_max_rc )
{
if( !_skip.skip_reject_unknown_delta_vests )
Expand All @@ -636,6 +642,7 @@ struct pre_apply_operation_visitor
{
rca.rc_manabar.regenerate_mana< true >( mbparams, _current_time );
} );
} FC_CAPTURE_AND_RETHROW( (account)(rc_account)(mbparams.max_mana) )
}

template< bool account_may_not_exist = false >
Expand All @@ -654,8 +661,9 @@ struct pre_apply_operation_visitor

const rc_account_object* rc_account = _db.find< rc_account_object, by_name >( name );
FC_ASSERT( rc_account != nullptr, "Unexpectedly, rc_account ${a} does not exist", ("a", name) );

try{
regenerate( *account, *rc_account );
} FC_CAPTURE_AND_RETHROW( (*account)(*rc_account) )
}

void operator()( const account_create_with_delegation_operation& op )const
Expand Down Expand Up @@ -971,7 +979,7 @@ typedef post_apply_operation_visitor post_apply_optional_action_visitor;


void rc_plugin_impl::on_pre_apply_operation( const operation_notification& note )
{
{ try {
if( before_first_block() )
return;

Expand All @@ -987,6 +995,7 @@ void rc_plugin_impl::on_pre_apply_operation( const operation_notification& note

// ilog( "Calling pre-vtor on ${op}", ("op", note.op) );
note.op.visit( vtor );
} FC_CAPTURE_AND_RETHROW( (note.op) )
}

void update_modified_accounts( database& db, const std::vector< account_regen_info >& modified_accounts )
Expand All @@ -1009,7 +1018,7 @@ void update_modified_accounts( database& db, const std::vector< account_regen_in
}

void rc_plugin_impl::on_post_apply_operation( const operation_notification& note )
{
{ try {
if( before_first_block() )
return;

Expand All @@ -1023,7 +1032,7 @@ void rc_plugin_impl::on_post_apply_operation( const operation_notification& note
note.op.visit( vtor );

update_modified_accounts( _db, modified_accounts );
}
} FC_CAPTURE_AND_RETHROW( (note.op) ) }

void rc_plugin_impl::on_pre_apply_optional_action( const optional_action_notification& note )
{
Expand Down
2 changes: 2 additions & 0 deletions libraries/protocol/hardfork.d/0_21.hf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#define STEEM_HARDFORK_0_21__3336 (STEEM_HARDFORK_0_21)
#define STEEM_HARDFORK_0_21__3343 (STEEM_HARDFORK_0_21)

#define STEEM_HF_21_STALL_BLOCK 35922615

#define STEEM_HARDFORK_0_21_VERSION hardfork_version( 0, 21 )

#endif

0 comments on commit 51ec865

Please sign in to comment.