Skip to content

Commit

Permalink
Fix some issues with the delegation logic for removing delegations. M…
Browse files Browse the repository at this point in the history
…akes removing delegation from account creation instant, but with a longer expiration time. steemit#818
  • Loading branch information
Michael Vandeberg committed Feb 20, 2017
1 parent b169ad4 commit ffbd635
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 5 additions & 6 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2262,8 +2262,9 @@ void delegate_vesting_shares_evaluator::do_apply( const delegate_vesting_shares_
});
}
// Else if the delegation is increasing
else if( op.vesting_shares - delegation->vesting_shares >= min_update )
else if( op.vesting_shares > delegation->vesting_shares )
{
FC_ASSERT( op.vesting_shares - delegation->vesting_shares >= min_update, "Steem Power increase is not enough of a different. min_update: ${min}", ("min", min_update) );
FC_ASSERT( available_shares >= op.vesting_shares - delegation->vesting_shares, "Account does not have enough vesting shares to delegate." );

auto delta = op.vesting_shares - delegation->vesting_shares;
Expand All @@ -2284,19 +2285,17 @@ void delegate_vesting_shares_evaluator::do_apply( const delegate_vesting_shares_
});
}
// Else the delegation is decreasing
else if( delegation->vesting_shares - op.vesting_shares >= min_update || delegation->vesting_shares == op.vesting_shares )
else if( delegation->vesting_shares > op.vesting_shares )
{
FC_ASSERT( delegation->min_delegation_time <= _db.head_block_time(), "Delegation cannot be removed yet." );
if( delegation->vesting_shares != op.vesting_shares )
FC_ASSERT( delegation->vesting_shares - op.vesting_shares >= min_delegation, "Delegation must be removed or leave minimum delegation amount of ${v}", ("v", min_delegation) );
FC_ASSERT( delegation->vesting_shares - op.vesting_shares >= min_delegation || op.vesting_shares.amount == 0, "Delegation must be removed or leave minimum delegation amount of ${v}", ("v", min_delegation) );

auto delta = delegation->vesting_shares - op.vesting_shares;

_db.create< vesting_delegation_expiration_object >( [&]( vesting_delegation_expiration_object& obj )
{
obj.delegator = op.delegator;
obj.vesting_shares = delta;
obj.expiration = _db.head_block_time() + STEEMIT_CASHOUT_WINDOW_SECONDS; // TODO: Replace with config constant with payout change branch
obj.expiration = std::max( _db.head_block_time() + STEEMIT_CASHOUT_WINDOW_SECONDS, delegation->min_delegation_time );
});

_db.modify( delegatee, [&]( account_object& a )
Expand Down
21 changes: 21 additions & 0 deletions tests/tests/operation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5999,8 +5999,11 @@ BOOST_AUTO_TEST_CASE( account_create_with_delegation_apply )

BOOST_REQUIRE( delegation != nullptr);
BOOST_REQUIRE( delegation->delegator == op.creator);
BOOST_REQUIRE( delegation->delegatee == op.new_account_name );
BOOST_REQUIRE( delegation->vesting_shares == ASSET( "10000.000000 VESTS" ) );
BOOST_REQUIRE( delegation->min_delegation_time == db.head_block_time() + STEEMIT_CREATE_ACCOUNT_DELEGATION_TIME );
auto del_amt = delegation->vesting_shares;
auto exp_time = delegation->min_delegation_time;

generate_block();

Expand Down Expand Up @@ -6041,6 +6044,24 @@ BOOST_AUTO_TEST_CASE( account_create_with_delegation_apply )
STEEMIT_REQUIRE_THROW( db.push_transaction( tx, 0 ), fc::exception );

validate_database();

tx.clear();
delegate_vesting_shares_operation delegate;
delegate.delegator = "alice";
delegate.delegatee = "bob";
delegate.vesting_shares = ASSET( "0.000000 VESTS" );
tx.operations.push_back( delegate );
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

auto itr = db.get_index< vesting_delegation_expiration_index, by_id >().begin();
auto end = db.get_index< vesting_delegation_expiration_index, by_id >().end();

BOOST_REQUIRE( itr != end );
BOOST_REQUIRE( itr->delegator == "alice" );
BOOST_REQUIRE( itr->vesting_shares == del_amt );
BOOST_REQUIRE( itr->expiration == exp_time );
validate_database();
}
FC_LOG_AND_RETHROW()
}
Expand Down

0 comments on commit ffbd635

Please sign in to comment.