BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
vesting_balance_object.hpp
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 #pragma once
25 
28 
29 #include <boost/multi_index/composite_key.hpp>
30 #include <boost/multi_index/hashed_index.hpp>
31 #include <boost/multi_index/identity.hpp>
32 
33 #include <fc/static_variant.hpp>
34 #include <fc/uint128.hpp>
35 
36 namespace graphene { namespace chain {
37  using namespace graphene::db;
38  using namespace graphene::protocol;
39 
41  {
43  asset _balance,
44  fc::time_point_sec _now,
45  asset _amount)
46  : balance(_balance), now(_now), amount(_amount) {}
47 
51  };
52 
62  {
66  uint32_t vesting_cliff_seconds = 0;
68  uint32_t vesting_duration_seconds = 0;
71 
72  asset get_allowed_withdraw(const vesting_policy_context& ctx)const;
73  bool is_deposit_allowed(const vesting_policy_context& ctx)const;
74  bool is_deposit_vested_allowed(const vesting_policy_context&)const { return false; }
75  bool is_withdraw_allowed(const vesting_policy_context& ctx)const;
76  void on_deposit(const vesting_policy_context& ctx);
78  { FC_THROW( "May not deposit vested into a linear vesting balance." ); }
79  void on_withdraw(const vesting_policy_context& ctx);
80  };
81 
90  {
91  uint32_t vesting_seconds = 0;
92  fc::uint128_t coin_seconds_earned;
96 
102  fc::uint128_t compute_coin_seconds_earned(const vesting_policy_context& ctx)const;
103 
109  void update_coin_seconds_earned(const vesting_policy_context& ctx);
110 
111  asset get_allowed_withdraw(const vesting_policy_context& ctx)const;
112  bool is_deposit_allowed(const vesting_policy_context& ctx)const;
113  bool is_deposit_vested_allowed(const vesting_policy_context& ctx)const;
114  bool is_withdraw_allowed(const vesting_policy_context& ctx)const;
115  void on_deposit(const vesting_policy_context& ctx);
116  void on_deposit_vested(const vesting_policy_context& ctx);
117  void on_withdraw(const vesting_policy_context& ctx);
118  };
119 
127  {
128  asset get_allowed_withdraw(const vesting_policy_context& ctx)const;
129  bool is_deposit_allowed(const vesting_policy_context& ctx)const;
130  bool is_deposit_vested_allowed(const vesting_policy_context&)const { return false; }
131  bool is_withdraw_allowed(const vesting_policy_context& ctx)const;
132  void on_deposit(const vesting_policy_context& ctx);
133  void on_deposit_vested(const vesting_policy_context&);
134  void on_withdraw(const vesting_policy_context& ctx);
135  };
136 
137  typedef fc::static_variant<
138  linear_vesting_policy,
139  cdd_vesting_policy,
140  instant_vesting_policy
142 
144  cashback,
145  worker,
146  witness,
151  class vesting_balance_object : public abstract_object<vesting_balance_object,
152  protocol_ids, vesting_balance_object_type>
153  {
154  public:
156  account_id_type owner;
164 
166 
168  void deposit(const fc::time_point_sec& now, const asset& amount);
169  bool is_deposit_allowed(const fc::time_point_sec& now, const asset& amount)const;
170 
172  void deposit_vested(const fc::time_point_sec& now, const asset& amount);
173  bool is_deposit_vested_allowed(const fc::time_point_sec& now, const asset& amount)const;
174 
183  void withdraw(const fc::time_point_sec& now, const asset& amount);
184  bool is_withdraw_allowed(const fc::time_point_sec& now, const asset& amount)const;
185 
189  asset get_allowed_withdraw(const time_point_sec& now)const;
190  };
194  struct by_account;
195  // by_vesting_type index MUST NOT be used for iterating because order is not well-defined.
196  struct by_vesting_type;
197 
198 namespace detail {
199 
204  inline uint64_t vbo_mfs_hash(const account_id_type& account_id, const asset_id_type& asset_id)
205  {
206  return (asset_id.instance.value << 40) ^ account_id.instance.value;
207  }
208 
217  {
218  uint64_t operator()(const vesting_balance_object& vbo) const
219  {
221  {
222  return vbo_mfs_hash(vbo.owner, vbo.balance.asset_id);
223  }
224  return hash_value(vbo.id);
225  }
226  };
227 
236  {
237  bool operator() (const vesting_balance_object& lhs, const vesting_balance_object& rhs) const
238  {
240  ( lhs.balance_type == rhs.balance_type ) &&
241  ( lhs.owner == rhs.owner ) &&
242  ( lhs.balance.asset_id == rhs.balance.asset_id)
243  )
244  {
245  return true;
246  }
247  return ( lhs.id == rhs.id );
248  }
249  };
250 } // detail
251 
252  typedef multi_index_container<
253  vesting_balance_object,
254  indexed_by<
255  ordered_unique< tag<by_id>, member< object, object_id_type, &object::id >
256  >,
257  ordered_non_unique< tag<by_account>,
258  member<vesting_balance_object, account_id_type, &vesting_balance_object::owner>
259  >,
260  hashed_unique< tag<by_vesting_type>,
261  identity<vesting_balance_object>,
262  detail::vesting_balance_object_hash,
263  detail::vesting_balance_object_equal
264  >
265  >
271 
272 } } // graphene::chain
273 
275 
277  (begin_timestamp)
278  (vesting_cliff_seconds)
279  (vesting_duration_seconds)
280  (begin_balance)
281  )
282 
284  (vesting_seconds)
285  (start_claim)
286  (coin_seconds_earned)
287  (coin_seconds_earned_last_update)
288  )
289 
291 
293 
295  (owner)
296  (balance)
297  (policy)
298  (balance_type)
299  )
300 
301 FC_REFLECT_ENUM( graphene::chain::vesting_balance_type, (unspecified)(cashback)(worker)(witness)(market_fee_sharing) )
302 
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
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
uint128.hpp
graphene::db::object::id
object_id_type id
Definition: object.hpp:69
FC_REFLECT_EMPTY
#define FC_REFLECT_EMPTY(TYPE)
Definition: reflect.hpp:395
graphene::chain::vesting_balance_object::vesting_balance_object
vesting_balance_object()
Definition: vesting_balance_object.hpp:165
fc::static_variant
Definition: raw_fwd.hpp:27
FC_REFLECT_TYPENAME
FC_REFLECT_TYPENAME(fc::log_message)
graphene::chain::vesting_policy_context::balance
asset balance
Definition: vesting_balance_object.hpp:48
graphene::chain::instant_vesting_policy
instant vesting policy
Definition: vesting_balance_object.hpp:126
FC_THROW
#define FC_THROW( ...)
Definition: exception.hpp:366
static_variant.hpp
graphene::chain::vesting_balance_type::unspecified
@ unspecified
graphene::chain::vesting_balance_type
vesting_balance_type
Definition: vesting_balance_object.hpp:143
generic_index.hpp
graphene::chain::vesting_policy_context
Definition: vesting_balance_object.hpp:40
MAP_OBJECT_ID_TO_TYPE
#define MAP_OBJECT_ID_TO_TYPE(OBJECT)
Definition: object_id.hpp:93
graphene::db::abstract_object
Definition: object.hpp:107
graphene::chain::vesting_policy_context::amount
asset amount
Definition: vesting_balance_object.hpp:50
graphene::chain::vesting_balance_object::policy
vesting_policy policy
The vesting policy stores details on when funds vest, and controls when they may be withdrawn.
Definition: vesting_balance_object.hpp:161
graphene::chain::vesting_balance_object
Definition: vesting_balance_object.hpp:151
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
FC_REFLECT_DERIVED
#define FC_REFLECT_DERIVED(TYPE, INHERITS, MEMBERS)
Specializes fc::reflector for TYPE where type inherits other reflected classes.
Definition: reflect.hpp:305
graphene::protocol
Definition: util.hpp:29
graphene::chain::instant_vesting_policy::is_deposit_vested_allowed
bool is_deposit_vested_allowed(const vesting_policy_context &) const
Definition: vesting_balance_object.hpp:130
graphene::chain::linear_vesting_policy::is_deposit_vested_allowed
bool is_deposit_vested_allowed(const vesting_policy_context &) const
Definition: vesting_balance_object.hpp:74
fc::time_point_sec
Definition: time.hpp:74
graphene::protocol::asset::asset_id
asset_id_type asset_id
Definition: asset.hpp:37
graphene::chain::vesting_policy
fc::static_variant< linear_vesting_policy, cdd_vesting_policy, instant_vesting_policy > vesting_policy
Definition: vesting_balance_object.hpp:141
graphene::chain::cdd_vesting_policy::start_claim
fc::time_point_sec start_claim
Definition: vesting_balance_object.hpp:94
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
graphene::chain::vesting_balance_object::balance
asset balance
Definition: vesting_balance_object.hpp:159
graphene::chain::vesting_balance_object::balance_type
vesting_balance_type balance_type
type of the vesting balance
Definition: vesting_balance_object.hpp:163
graphene::chain::vesting_balance_type::market_fee_sharing
@ market_fee_sharing
graphene::chain::detail::vesting_balance_object_hash
Definition: vesting_balance_object.hpp:216
FC_REFLECT_ENUM
FC_REFLECT_ENUM(graphene::net::core_message_type_enum,(trx_message_type)(block_message_type)(core_message_type_first)(item_ids_inventory_message_type)(blockchain_item_ids_inventory_message_type)(fetch_blockchain_item_ids_message_type)(fetch_items_message_type)(item_not_available_message_type)(hello_message_type)(connection_accepted_message_type)(connection_rejected_message_type)(address_request_message_type)(address_message_type)(closing_connection_message_type)(current_time_request_message_type)(current_time_reply_message_type)(check_firewall_message_type)(check_firewall_reply_message_type)(get_current_connections_request_message_type)(get_current_connections_reply_message_type)(core_message_type_last))(different_chain)(already_connected)(connected_to_self)(not_accepting_connections)(blocked)(invalid_hello_message)(client_too_old))(inbound)(outbound))(firewalled)(not_firewalled))(unable_to_connect)(connection_successful)) namespace std
Definition: core_messages.hpp:404
graphene::chain::vesting_balance_multi_index_type
multi_index_container< vesting_balance_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type, &object::id > >, ordered_non_unique< tag< by_account >, member< vesting_balance_object, account_id_type, &vesting_balance_object::owner > >, hashed_unique< tag< by_vesting_type >, identity< vesting_balance_object >, detail::vesting_balance_object_hash, detail::vesting_balance_object_equal > > > vesting_balance_multi_index_type
Definition: vesting_balance_object.hpp:266
graphene::chain::vesting_policy_context::vesting_policy_context
vesting_policy_context(asset _balance, fc::time_point_sec _now, asset _amount)
Definition: vesting_balance_object.hpp:42
graphene::chain::detail::vesting_balance_object_equal
Definition: vesting_balance_object.hpp:235
graphene::chain::vesting_balance_type::cashback
@ cashback
FC_REFLECT
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
graphene::chain::detail::vesting_balance_object_hash::operator()
uint64_t operator()(const vesting_balance_object &vbo) const
Definition: vesting_balance_object.hpp:218
graphene::db::generic_index
Definition: generic_index.hpp:43
graphene::chain::detail::vbo_mfs_hash
uint64_t vbo_mfs_hash(const account_id_type &account_id, const asset_id_type &asset_id)
Definition: vesting_balance_object.hpp:204
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::vesting_balance_type::worker
@ worker
graphene::chain::vesting_balance_index
generic_index< vesting_balance_object, vesting_balance_multi_index_type > vesting_balance_index
Definition: vesting_balance_object.hpp:270
graphene::protocol::asset
Definition: asset.hpp:31
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::db
Definition: generic_index.hpp:31
graphene::chain::linear_vesting_policy::on_deposit_vested
void on_deposit_vested(const vesting_policy_context &)
Definition: vesting_balance_object.hpp:77
asset.hpp
graphene::chain::vesting_balance_type::witness
@ witness
graphene
Definition: api.cpp:48
graphene::chain::vesting_policy_context::now
fc::time_point_sec now
Definition: vesting_balance_object.hpp:49
graphene::chain::linear_vesting_policy
Linear vesting balance with cliff.
Definition: vesting_balance_object.hpp:61
graphene::db::object
base for all database objects
Definition: object.hpp:61
fc::safe
Definition: safe.hpp:26