From be8dc42d66e7c6772cf0bac8f63177e282ed2a95 Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Tue, 15 Oct 2019 21:55:48 -0300 Subject: [PATCH] add dormant vesting policy for son --- .../graphene/chain/protocol/vesting.hpp | 12 ++++- .../graphene/chain/vesting_balance_object.hpp | 46 +++++++++++++++- libraries/chain/son_evaluator.cpp | 16 +++++- libraries/chain/vesting_balance_evaluator.cpp | 23 ++++++-- libraries/chain/vesting_balance_object.cpp | 53 ++++++++++++++++++ tests/tests/son_operations_tests.cpp | 54 +++++++++++++++---- 6 files changed, 186 insertions(+), 18 deletions(-) diff --git a/libraries/chain/include/graphene/chain/protocol/vesting.hpp b/libraries/chain/include/graphene/chain/protocol/vesting.hpp index 731b8804..57c22be9 100644 --- a/libraries/chain/include/graphene/chain/protocol/vesting.hpp +++ b/libraries/chain/include/graphene/chain/protocol/vesting.hpp @@ -44,9 +44,16 @@ namespace graphene { namespace chain { cdd_vesting_policy_initializer( uint32_t vest_sec = 0, fc::time_point_sec sc = fc::time_point_sec() ):start_claim(sc),vesting_seconds(vest_sec){} }; - typedef fc::static_variant vesting_policy_initializer; - + struct dormant_vesting_policy_initializer + { + /** none may be claimed if dormant is true, otherwise this is a linear policy */ + bool dormant = true; + fc::time_point_sec begin_timestamp; + uint32_t vesting_cliff_seconds = 0; + uint32_t vesting_duration_seconds = 0; + }; + typedef fc::static_variant vesting_policy_initializer; /** * @brief Create a vesting balance. @@ -120,6 +127,7 @@ FC_REFLECT( graphene::chain::vesting_balance_withdraw_operation, (fee)(vesting_b FC_REFLECT(graphene::chain::linear_vesting_policy_initializer, (begin_timestamp)(vesting_cliff_seconds)(vesting_duration_seconds) ) FC_REFLECT(graphene::chain::cdd_vesting_policy_initializer, (start_claim)(vesting_seconds) ) +FC_REFLECT(graphene::chain::dormant_vesting_policy_initializer, (dormant)(begin_timestamp)(vesting_cliff_seconds)(vesting_duration_seconds) ) FC_REFLECT_TYPENAME( graphene::chain::vesting_policy_initializer ) FC_REFLECT_ENUM( graphene::chain::vesting_balance_type, (normal)(gpos)(son) ) diff --git a/libraries/chain/include/graphene/chain/vesting_balance_object.hpp b/libraries/chain/include/graphene/chain/vesting_balance_object.hpp index fc236272..a7a5f222 100644 --- a/libraries/chain/include/graphene/chain/vesting_balance_object.hpp +++ b/libraries/chain/include/graphene/chain/vesting_balance_object.hpp @@ -119,10 +119,44 @@ namespace graphene { namespace chain { void on_withdraw(const vesting_policy_context& ctx); }; + /** + * @brief Cant withdraw anything while dormant mode is true, linear policy after that changes. + * + * This vesting balance type is needed to register SON users where balance may be claimable only after + * the SON object is deleted(plus a linear policy). + * When deleting a SON member the dormant_mode will change and the linear policy will became active. + * + * @note New funds may not be added to a dormant vesting balance. + */ + struct dormant_vesting_policy + { + /// dormant mode flag indicates if we are in dormant mode or linear policy. + bool dormant_mode = true; + + /// This is the time at which funds begin vesting. + fc::time_point_sec begin_timestamp; + /// No amount may be withdrawn before this many seconds of the vesting period have elapsed. + uint32_t vesting_cliff_seconds = 0; + /// Duration of the vesting period, in seconds. Must be greater than 0 and greater than vesting_cliff_seconds. + uint32_t vesting_duration_seconds = 0; + /// The total amount of asset to vest. + share_type begin_balance; + + asset get_allowed_withdraw(const vesting_policy_context& ctx)const; + bool is_deposit_allowed(const vesting_policy_context& ctx)const; + bool is_deposit_vested_allowed(const vesting_policy_context&)const { return false; } + bool is_withdraw_allowed(const vesting_policy_context& ctx)const; + void on_deposit(const vesting_policy_context& ctx); + void on_deposit_vested(const vesting_policy_context&) + { FC_THROW( "May not deposit vested into a linear vesting balance." ); } + void on_withdraw(const vesting_policy_context& ctx); + }; + typedef fc::static_variant< linear_vesting_policy, - cdd_vesting_policy - > vesting_policy; + cdd_vesting_policy, + dormant_vesting_policy + > vesting_policy; /** * Vesting balance object is a balance that is locked by the blockchain for a period of time. @@ -223,6 +257,14 @@ FC_REFLECT(graphene::chain::cdd_vesting_policy, (coin_seconds_earned_last_update) ) +FC_REFLECT(graphene::chain::dormant_vesting_policy, + (dormant_mode) + (begin_timestamp) + (vesting_cliff_seconds) + (vesting_duration_seconds) + (begin_balance) + ) + FC_REFLECT_TYPENAME( graphene::chain::vesting_policy ) FC_REFLECT_DERIVED(graphene::chain::vesting_balance_object, (graphene::db::object), diff --git a/libraries/chain/son_evaluator.cpp b/libraries/chain/son_evaluator.cpp index 6d70dc62..03221984 100644 --- a/libraries/chain/son_evaluator.cpp +++ b/libraries/chain/son_evaluator.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace graphene { namespace chain { @@ -68,7 +69,20 @@ void_result delete_son_evaluator::do_evaluate(const son_delete_operation& op) void_result delete_son_evaluator::do_apply(const son_delete_operation& op) { try { const auto& idx = db().get_index_type().indices().get(); - db().remove(*idx.find(op.son_id)); + auto son = idx.find(op.son_id); + if(son != idx.end()) { + vesting_balance_object deposit = son->deposit(db()); + dormant_vesting_policy new_vesting_policy; + new_vesting_policy.dormant_mode = false; + new_vesting_policy.begin_timestamp = db().head_block_time(); + new_vesting_policy.vesting_cliff_seconds = db().get_global_properties().parameters.son_vesting_period(); + + db().modify(son->deposit(db()), [&new_vesting_policy](vesting_balance_object &vbo) { + vbo.policy = new_vesting_policy; + }); + + db().remove(*son); + } return void_result(); } FC_CAPTURE_AND_RETHROW( (op) ) } diff --git a/libraries/chain/vesting_balance_evaluator.cpp b/libraries/chain/vesting_balance_evaluator.cpp index 3bb798f3..3f5cbca3 100644 --- a/libraries/chain/vesting_balance_evaluator.cpp +++ b/libraries/chain/vesting_balance_evaluator.cpp @@ -82,6 +82,16 @@ struct init_policy_visitor policy.coin_seconds_earned_last_update = now; p = policy; } + void operator()( const dormant_vesting_policy_initializer& i )const + { + dormant_vesting_policy policy; + policy.dormant_mode = i.dormant; + policy.begin_timestamp = i.begin_timestamp; + policy.vesting_cliff_seconds = i.vesting_cliff_seconds; + policy.vesting_duration_seconds = i.vesting_duration_seconds; + policy.begin_balance = init_balance; + p = policy; + } }; object_id_type vesting_balance_create_evaluator::do_apply( const vesting_balance_create_operation& op ) @@ -99,10 +109,17 @@ object_id_type vesting_balance_create_evaluator::do_apply( const vesting_balance obj.owner = op.owner; obj.balance = op.amount; obj.balance_type = op.balance_type; - op.policy.visit( init_policy_visitor( obj.policy, op.amount.amount, now ) ); + if(op.balance_type == vesting_balance_type::son) + { + const auto &gpo = d.get_global_properties(); + // forcing son dormant policy + dormant_vesting_policy p; + p.dormant_mode = true; + obj.policy = p; + } + else + op.policy.visit( init_policy_visitor( obj.policy, op.amount.amount, now ) ); } ); - - return vbo.id; } FC_CAPTURE_AND_RETHROW( (op) ) } diff --git a/libraries/chain/vesting_balance_object.cpp b/libraries/chain/vesting_balance_object.cpp index 73448e04..2463a78a 100644 --- a/libraries/chain/vesting_balance_object.cpp +++ b/libraries/chain/vesting_balance_object.cpp @@ -157,6 +157,59 @@ bool cdd_vesting_policy::is_withdraw_allowed(const vesting_policy_context& ctx)c return (ctx.amount <= get_allowed_withdraw(ctx)); } +asset dormant_vesting_policy::get_allowed_withdraw( const vesting_policy_context& ctx )const +{ + share_type allowed_withdraw = 0; + + if( !dormant_mode && ctx.now > begin_timestamp) + { + const auto elapsed_seconds = (ctx.now - begin_timestamp).to_seconds(); + assert( elapsed_seconds > 0 ); + + if( elapsed_seconds >= vesting_cliff_seconds ) + { + share_type total_vested = 0; + if( elapsed_seconds < vesting_duration_seconds ) + { + total_vested = (fc::uint128_t( begin_balance.value ) * elapsed_seconds / vesting_duration_seconds).to_uint64(); + } + else + { + total_vested = begin_balance; + } + assert( total_vested >= 0 ); + + const share_type withdrawn_already = begin_balance - ctx.balance.amount; + assert( withdrawn_already >= 0 ); + + allowed_withdraw = total_vested - withdrawn_already; + assert( allowed_withdraw >= 0 ); + } + } + + return asset( allowed_withdraw, ctx.balance.asset_id ); +} + +void dormant_vesting_policy::on_deposit(const vesting_policy_context& ctx) +{ +} + +bool dormant_vesting_policy::is_deposit_allowed(const vesting_policy_context& ctx)const +{ + return (ctx.amount.asset_id == ctx.balance.asset_id) + && sum_below_max_shares(ctx.amount, ctx.balance); +} + +void dormant_vesting_policy::on_withdraw(const vesting_policy_context& ctx) +{ +} + +bool dormant_vesting_policy::is_withdraw_allowed(const vesting_policy_context& ctx)const +{ + return (ctx.amount.asset_id == ctx.balance.asset_id) + && (ctx.amount <= get_allowed_withdraw(ctx)); +} + #define VESTING_VISITOR(NAME, MAYBE_CONST) \ struct NAME ## _visitor \ { \ diff --git a/tests/tests/son_operations_tests.cpp b/tests/tests/son_operations_tests.cpp index bd45d648..64ce7bba 100644 --- a/tests/tests/son_operations_tests.cpp +++ b/tests/tests/son_operations_tests.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using namespace graphene::chain; using namespace graphene::chain::test; @@ -13,9 +14,6 @@ BOOST_FIXTURE_TEST_SUITE( son_operation_tests, database_fixture ) BOOST_AUTO_TEST_CASE( create_son_test ) { generate_blocks(HARDFORK_SON_TIME); - while (db.head_block_time() <= HARDFORK_SON_TIME) { - generate_block(); - } generate_block(); set_expiration(db, trx); @@ -36,31 +34,50 @@ BOOST_AUTO_TEST_CASE( create_son_test ) { vesting_balance_create_operation op; op.creator = alice_id; op.owner = alice_id; - op.amount = asset(50); + op.amount = asset(10); op.balance_type = vesting_balance_type::son; + trx.operations.push_back(op); + + // amount in the son balance need to be at least 50 + GRAPHENE_REQUIRE_THROW( PUSH_TX( db, trx ), fc::exception ); + + op.amount = asset(50); + trx.clear(); trx.operations.push_back(op); - set_expiration(db, trx); processed_transaction ptx = PUSH_TX(db, trx, ~0); - trx.clear(); deposit = ptx.operation_results[0].get(); + + auto deposit_vesting = db.get(ptx.operation_results[0].get()); + + BOOST_CHECK_EQUAL(deposit(db).balance.amount.value, 50); + auto now = db.head_block_time(); + BOOST_CHECK_EQUAL(deposit(db).is_withdraw_allowed(now, asset(50)), false); // cant withdraw } - // create payment vesting - vesting_balance_id_type payment; + generate_block(); + set_expiration(db, trx); + + // create payment normal vesting + vesting_balance_id_type payment ; { vesting_balance_create_operation op; op.creator = alice_id; op.owner = alice_id; - op.amount = asset(50); + op.amount = asset(1); op.balance_type = vesting_balance_type::normal; + op.validate(); + trx.operations.push_back(op); - set_expiration(db, trx); + trx.validate(); processed_transaction ptx = PUSH_TX(db, trx, ~0); trx.clear(); payment = ptx.operation_results[0].get(); } + generate_block(); + set_expiration(db, trx); + // alice became son { son_create_operation op; @@ -116,6 +133,9 @@ BOOST_AUTO_TEST_CASE( delete_son_test ) { INVOKE(create_son_test); GET_ACTOR(alice); + auto deposit_vesting = db.get(vesting_balance_id_type(0)); + BOOST_CHECK_EQUAL(deposit_vesting.policy.get().dormant_mode, true); // dormant while active + { son_delete_operation op; op.owner_account = alice_id; @@ -129,6 +149,20 @@ BOOST_AUTO_TEST_CASE( delete_son_test ) { const auto& idx = db.get_index_type().indices().get(); BOOST_REQUIRE( idx.empty() ); + + deposit_vesting = db.get(vesting_balance_id_type(0)); + BOOST_CHECK_EQUAL(deposit_vesting.policy.get().dormant_mode, false); // not sleeping anymore + + auto now = db.head_block_time(); + + BOOST_CHECK_EQUAL(deposit_vesting.is_withdraw_allowed(now, asset(50)), false); // but still cant withdraw + + generate_blocks(now + fc::seconds(db.get_global_properties().parameters.son_vesting_period())); + generate_block(); + + deposit_vesting = db.get(vesting_balance_id_type(0)); + now = db.head_block_time(); + BOOST_CHECK_EQUAL(deposit_vesting.is_withdraw_allowed(now, asset(50)), true); // after 2 days withdraw is allowed } BOOST_AUTO_TEST_CASE( update_delete_not_own ) { // fee payer needs to be the son object owner