BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
vesting_balance_evaluator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
29 #include <graphene/chain/hardfork.hpp>
31 
32 namespace graphene { namespace chain {
33 
35 { try {
36  const database& d = db();
37 
38  const account_object& creator_account = *fee_paying_account;
39  const account_object& owner_account = op.owner( d );
40 
41  const asset_object& asset_obj = op.amount.asset_id( d );
42 
43  FC_ASSERT( !asset_obj.is_transfer_restricted(), "Asset has transfer_restricted flag enabled" );
44 
45  // Since hard fork core-973, check asset authorization limitations
46  if( HARDFORK_CORE_973_PASSED(d.head_block_time()) )
47  {
48  FC_ASSERT( is_authorized_asset( d, creator_account, asset_obj ),
49  "The creator account is not allowed to transact the asset" );
50  FC_ASSERT( is_authorized_asset( d, owner_account, asset_obj ),
51  "The owner account is not allowed to transact the asset" );
52  }
53 
54  return void_result();
55 } FC_CAPTURE_AND_RETHROW( (op) ) }
56 
58 {
59  typedef void result_type;
60 
62  const share_type& begin_balance,
63  const fc::time_point_sec& n ):p(po),init_balance(begin_balance),now(n){}
64 
68 
70  {
71  linear_vesting_policy policy;
75  policy.begin_balance = init_balance;
76  p = policy;
77  }
78 
80  {
81  cdd_vesting_policy policy;
83  policy.start_claim = i.start_claim;
84  policy.coin_seconds_earned = 0;
86  p = policy;
87  }
88 
90  {
92  }
93 
94 };
95 
97 { try {
98  database& d = db();
99  const time_point_sec now = d.head_block_time();
100 
101  d.adjust_balance( op.creator, -op.amount );
102 
104  {
105  //WARNING: The logic to create a vesting balance object is replicated in vesting_balance_worker_type::initializer::init.
106  // If making changes to this logic, check if those changes should also be made there as well.
107  obj.owner = op.owner;
108  obj.balance = op.amount;
109  op.policy.visit( init_policy_visitor( obj.policy, op.amount.amount, now ) );
110  } );
111 
112 
113  return vbo.id;
114 } FC_CAPTURE_AND_RETHROW( (op) ) }
115 
117 { try {
118  const database& d = db();
119  const time_point_sec now = d.head_block_time();
120 
121  const vesting_balance_object& vbo = op.vesting_balance( d );
122  FC_ASSERT( op.owner == vbo.owner, "", ("op.owner", op.owner)("vbo.owner", vbo.owner) );
123  FC_ASSERT( vbo.is_withdraw_allowed( now, op.amount ), "", ("now", now)("op", op)("vbo", vbo) );
124  assert( op.amount <= vbo.balance ); // is_withdraw_allowed should fail before this check is reached
125 
126  // Note: asset authorization check is skipped here,
127  // because it is fine to allow the funds to be moved to account balance
128  return void_result();
129 } FC_CAPTURE_AND_RETHROW( (op) ) }
130 
132 { try {
133  database& d = db();
134  const time_point_sec now = d.head_block_time();
135 
136  const vesting_balance_object& vbo = op.vesting_balance( d );
137 
138  // Allow zero balance objects to stick around, (1) to comply
139  // with the chain's "objects live forever" design principle, (2)
140  // if it's cashback or worker, it'll be filled up again.
141 
142  d.modify( vbo, [&]( vesting_balance_object& vbo )
143  {
144  vbo.withdraw( now, op.amount );
145  } );
146 
147  d.adjust_balance( op.owner, op.amount );
148 
149  return void_result();
150 } FC_CAPTURE_AND_RETHROW( (op) ) }
151 
152 } } // graphene::chain
graphene::chain::cdd_vesting_policy
defines vesting in terms of coin-days accrued which allows for dynamic deposit/withdraw
Definition: vesting_balance_object.hpp:89
FC_CAPTURE_AND_RETHROW
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:479
graphene::chain::linear_vesting_policy::begin_balance
share_type begin_balance
The total amount of asset to vest.
Definition: vesting_balance_object.hpp:70
graphene::chain::cdd_vesting_policy::coin_seconds_earned
fc::uint128_t coin_seconds_earned
Definition: vesting_balance_object.hpp:92
graphene::db::object::id
object_id_type id
Definition: object.hpp:69
is_authorized_asset.hpp
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
graphene::protocol::vesting_balance_withdraw_operation
Withdraw from a vesting balance.
Definition: vesting.hpp:101
graphene::chain::vesting_balance_withdraw_evaluator::do_apply
void_result do_apply(const vesting_balance_withdraw_operation &op)
Definition: vesting_balance_evaluator.cpp:131
graphene::chain::linear_vesting_policy::vesting_duration_seconds
uint32_t vesting_duration_seconds
Duration of the vesting period, in seconds. Must be greater than 0 and greater than vesting_cliff_sec...
Definition: vesting_balance_object.hpp:68
graphene::chain::database::head_block_time
time_point_sec head_block_time() const
Definition: db_getter.cpp:67
graphene::protocol::vesting_balance_withdraw_operation::vesting_balance
vesting_balance_id_type vesting_balance
Definition: vesting.hpp:106
database.hpp
graphene::protocol::vesting_balance_create_operation
Create a vesting balance.
Definition: vesting.hpp:74
fc::static_variant< linear_vesting_policy, cdd_vesting_policy, instant_vesting_policy >
graphene::protocol::vesting_balance_withdraw_operation::amount
asset amount
Definition: vesting.hpp:108
graphene::chain::instant_vesting_policy
instant vesting policy
Definition: vesting_balance_object.hpp:126
graphene::protocol::vesting_balance_create_operation::owner
account_id_type owner
Who is able to withdraw the balance.
Definition: vesting.hpp:80
graphene::chain::asset_object
tracks the parameters of an asset
Definition: asset_object.hpp:75
graphene::db::object_database::create
const T & create(F &&constructor)
Definition: object_database.hpp:63
graphene::chain::database::adjust_balance
void adjust_balance(account_id_type account, asset delta)
Adjust a particular account's balance in a given asset by a delta.
Definition: db_balance.cpp:54
graphene::protocol::vesting_balance_create_operation::policy
vesting_policy_initializer policy
Definition: vesting.hpp:82
graphene::chain::asset_object::is_transfer_restricted
bool is_transfer_restricted() const
Definition: asset_object.hpp:94
graphene::chain::init_policy_visitor::operator()
void operator()(const instant_vesting_policy_initializer &i) const
Definition: vesting_balance_evaluator.cpp:89
graphene::protocol::cdd_vesting_policy_initializer::vesting_seconds
uint32_t vesting_seconds
Definition: vesting.hpp:42
graphene::protocol::linear_vesting_policy_initializer::vesting_cliff_seconds
uint32_t vesting_cliff_seconds
Definition: vesting.hpp:34
graphene::protocol::linear_vesting_policy_initializer::begin_timestamp
fc::time_point_sec begin_timestamp
Definition: vesting.hpp:33
graphene::chain::init_policy_visitor
Definition: vesting_balance_evaluator.cpp:57
graphene::chain::vesting_balance_object
Definition: vesting_balance_object.hpp:151
graphene::chain::init_policy_visitor::init_policy_visitor
init_policy_visitor(vesting_policy &po, const share_type &begin_balance, const fc::time_point_sec &n)
Definition: vesting_balance_evaluator.cpp:61
graphene::protocol::vesting_balance_withdraw_operation::owner
account_id_type owner
Must be vesting_balance.owner.
Definition: vesting.hpp:107
graphene::chain::cdd_vesting_policy::coin_seconds_earned_last_update
fc::time_point_sec coin_seconds_earned_last_update
Definition: vesting_balance_object.hpp:95
graphene::chain::init_policy_visitor::p
vesting_policy & p
Definition: vesting_balance_evaluator.cpp:65
graphene::chain::vesting_balance_create_evaluator::do_evaluate
void_result do_evaluate(const vesting_balance_create_operation &op)
Definition: vesting_balance_evaluator.cpp:34
graphene::chain::account_object
This class represents an account on the object graph.
Definition: account_object.hpp:180
graphene::chain::init_policy_visitor::operator()
void operator()(const cdd_vesting_policy_initializer &i) const
Definition: vesting_balance_evaluator.cpp:79
fc::time_point_sec
Definition: time.hpp:74
graphene::protocol::asset::asset_id
asset_id_type asset_id
Definition: asset.hpp:37
account_object.hpp
graphene::chain::cdd_vesting_policy::start_claim
fc::time_point_sec start_claim
Definition: vesting_balance_object.hpp:94
graphene::chain::init_policy_visitor::now
fc::time_point_sec now
Definition: vesting_balance_evaluator.cpp:67
vesting_balance_object.hpp
graphene::protocol::vesting_balance_create_operation::amount
asset amount
Definition: vesting.hpp:81
graphene::chain::is_authorized_asset
bool is_authorized_asset(const database &d, const account_object &acct, const asset_object &asset_obj)
Definition: is_authorized_asset.hpp:43
graphene::chain::vesting_balance_object::balance
asset balance
Definition: vesting_balance_object.hpp:159
graphene::protocol::cdd_vesting_policy_initializer
Definition: vesting.hpp:38
graphene::chain::generic_evaluator::db
database & db() const
Definition: evaluator.cpp:39
graphene::chain::init_policy_visitor::result_type
void result_type
Definition: vesting_balance_evaluator.cpp:59
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::chain::vesting_balance_object::withdraw
void withdraw(const fc::time_point_sec &now, const asset &amount)
Definition: vesting_balance_object.cpp:259
graphene::chain::init_policy_visitor::init_balance
share_type init_balance
Definition: vesting_balance_evaluator.cpp:66
graphene::protocol::cdd_vesting_policy_initializer::start_claim
fc::time_point_sec start_claim
Definition: vesting.hpp:41
graphene::db::object_id_type
Definition: object_id.hpp:30
graphene::protocol::asset::amount
share_type amount
Definition: asset.hpp:36
vesting_balance_evaluator.hpp
graphene::protocol::linear_vesting_policy_initializer
Definition: vesting.hpp:30
graphene::chain::linear_vesting_policy::vesting_cliff_seconds
uint32_t vesting_cliff_seconds
No amount may be withdrawn before this many seconds of the vesting period have elapsed.
Definition: vesting_balance_object.hpp:66
fc::static_variant::visit
visitor::result_type visit(visitor &v)
Definition: static_variant.hpp:256
graphene::chain::init_policy_visitor::operator()
void operator()(const linear_vesting_policy_initializer &i) const
Definition: vesting_balance_evaluator.cpp:69
graphene::chain::vesting_balance_object::owner
account_id_type owner
Account which owns and may withdraw from this vesting balance.
Definition: vesting_balance_object.hpp:156
graphene::chain::generic_evaluator::fee_paying_account
const account_object * fee_paying_account
Definition: evaluator.hpp:116
graphene::protocol::vesting_balance_create_operation::creator
account_id_type creator
Who provides funds initially.
Definition: vesting.hpp:79
graphene::chain::linear_vesting_policy::begin_timestamp
fc::time_point_sec begin_timestamp
This is the time at which funds begin vesting.
Definition: vesting_balance_object.hpp:64
graphene::chain::vesting_balance_withdraw_evaluator::do_evaluate
void_result do_evaluate(const vesting_balance_withdraw_operation &op)
Definition: vesting_balance_evaluator.cpp:116
graphene::protocol::void_result
Definition: base.hpp:86
graphene::chain::cdd_vesting_policy::vesting_seconds
uint32_t vesting_seconds
Definition: vesting_balance_object.hpp:91
graphene::protocol::instant_vesting_policy_initializer
Definition: vesting.hpp:46
graphene
Definition: api.cpp:48
graphene::db::object_database::modify
void modify(const T &obj, const Lambda &m)
Definition: object_database.hpp:99
graphene::chain::linear_vesting_policy
Linear vesting balance with cliff.
Definition: vesting_balance_object.hpp:61
graphene::chain::vesting_balance_object::is_withdraw_allowed
bool is_withdraw_allowed(const fc::time_point_sec &now, const asset &amount) const
Definition: vesting_balance_object.cpp:231
graphene::protocol::linear_vesting_policy_initializer::vesting_duration_seconds
uint32_t vesting_duration_seconds
Definition: vesting.hpp:35
graphene::chain::vesting_balance_create_evaluator::do_apply
object_id_type do_apply(const vesting_balance_create_operation &op)
Definition: vesting_balance_evaluator.cpp:96
fc::safe
Definition: safe.hpp:26