BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
withdraw_permission_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  */
29 #include <graphene/chain/hardfork.hpp>
31 
32 namespace graphene { namespace chain {
33 
35 { try {
36  database& d = db();
43 
44  return void_result();
45 } FC_CAPTURE_AND_RETHROW( (op) ) }
46 
48 { try {
50  p.withdraw_from_account = op.withdraw_from_account;
51  p.authorized_account = op.authorized_account;
52  p.withdrawal_limit = op.withdrawal_limit;
53  p.withdrawal_period_sec = op.withdrawal_period_sec;
55  p.period_start_time = op.period_start_time;
56  }).id;
57 } FC_CAPTURE_AND_RETHROW( (op) ) }
58 
60 { try {
61  const database& d = db();
62  time_point_sec head_block_time = d.head_block_time();
63 
64  const withdraw_permission_object& permit = op.withdraw_permission(d);
65  FC_ASSERT(permit.expiration > head_block_time);
68  FC_ASSERT(permit.period_start_time <= head_block_time);
69  FC_ASSERT(op.amount_to_withdraw <= permit.available_this_period( head_block_time ) );
71 
72  const asset_object& _asset = op.amount_to_withdraw.asset_id(d);
73  if( _asset.is_transfer_restricted() )
74  {
75  FC_ASSERT( _asset.issuer == permit.authorized_account || _asset.issuer == permit.withdraw_from_account,
76  "Asset ${a} '${sym}' has transfer_restricted flag enabled",
77  ("a", _asset.id)("sym", _asset.symbol) );
78  }
79 
80  const account_object& to = permit.authorized_account(d);
81  FC_ASSERT( is_authorized_asset( d, to, _asset ),
82  "Account ${acct} '${name}' is unauthorized to transact asset ${a} '${sym}' due to whitelist / blacklist",
83  ("acct", to.id)("name", to.name)("a", _asset.id)("sym", _asset.symbol) );
84 
85  const account_object& from = op.withdraw_from_account(d);
86  bool from_is_authorized = ( is_authorized_asset( d, from, _asset ) );
87  FC_ASSERT( from_is_authorized,
88  "Account ${acct} '${name}' is unauthorized to withdraw asset ${a} '${sym}' due to whitelist / blacklist",
89  ("acct", from.id)("name", from.name)("a", _asset.id)("sym", _asset.symbol) );
90 
91  return void_result();
92 } FC_CAPTURE_AND_RETHROW( (op) ) }
93 
95 { try {
96  database& d = db();
97 
99  d.modify(permit, [&](withdraw_permission_object& p) {
100  auto periods = (d.head_block_time() - p.period_start_time).to_seconds() / p.withdrawal_period_sec;
101  p.period_start_time += periods * p.withdrawal_period_sec;
102  if( periods == 0 )
104  else
106  });
107 
110 
111  return void_result();
112 } FC_CAPTURE_AND_RETHROW( (op) ) }
113 
115 { try {
116  database& d = db();
117 
118  const withdraw_permission_object& permit = op.permission_to_update(d);
125 
126  return void_result();
127 } FC_CAPTURE_AND_RETHROW( (op) ) }
128 
130 { try {
131  database& d = db();
132 
134  p.period_start_time = op.period_start_time;
136  p.withdrawal_limit = op.withdrawal_limit;
137  p.withdrawal_period_sec = op.withdrawal_period_sec;
138  });
139 
140  return void_result();
141 } FC_CAPTURE_AND_RETHROW( (op) ) }
142 
144 { try {
145  database& d = db();
146 
150 
151  return void_result();
152 } FC_CAPTURE_AND_RETHROW( (op) ) }
153 
155 { try {
156  db().remove(db().get(op.withdrawal_permission));
157  return void_result();
158 } FC_CAPTURE_AND_RETHROW( (op) ) }
159 
160 } } // graphene::chain
uint32_t withdrawal_period_sec
The duration of a withdrawal period in seconds.
account_id_type authorized_account
The account authorized to make withdrawals from withdraw_from_account.
void modify(const T &obj, const Lambda &m)
account_id_type withdraw_from_account
Must match withdraw_permission->withdraw_from_account.
uint32_t periods_until_expiration
The new number of withdrawal periods for which this permission will be valid.
void adjust_balance(account_id_type account, asset delta)
Adjust a particular account&#39;s balance in a given asset by a delta.
Definition: db_balance.cpp:54
account_id_type withdraw_to_account
Must match withdraw_permision->authorized_account.
account_id_type authorized_account
The account authorized to make withdrawals from withdraw_from_account.
bool is_authorized_asset(const database &d, const account_object &acct, const asset_object &asset_obj)
const object * find_object(object_id_type id) const
asset withdrawal_limit
New maximum amount the withdrawer is allowed to charge per withdrawal period.
This class represents an account on the object graphAccounts are the primary unit of authority on the...
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
asset withdrawal_limit
The maximum amount authorized_account is allowed to withdraw in a given withdrawal period...
Definition: api.cpp:56
Delete an existing withdrawal permissionThis operation cancels a withdrawal permission, thus preventing any future withdrawals using that permission.
asset get_balance(account_id_type owner, asset_id_type asset_id) const
Retrieve a particular account&#39;s balance in a given asset.
Definition: db_balance.cpp:35
account_id_type withdraw_from_account
The account authorizing withdrawals from its balances.
asset amount_to_withdraw
Amount to withdraw. Must not exceed withdraw_permission->withdrawal_limit.
uint32_t withdrawal_period_sec
Length of the withdrawal period in seconds.
time_point_sec period_start_time
Time at which the first withdrawal period begins; must be in the future.
uint32_t periods_until_expiration
The number of withdrawal periods this permission is valid for.
Withdraw from an account which has published a withdrawal permissionThis operation is used to withdra...
const global_property_object & get_global_properties() const
Definition: db_getter.cpp:44
const T & get(object_id_type id) const
account_id_type authorized_account
The account authorized to make withdrawals. Must match permission_to_update->authorized_account.
Grants another account authority to withdraw a limited amount of funds per interval.
object_id_type id
Definition: object.hpp:69
account_id_type withdraw_from_account
Must match withdrawal_permission->withdraw_from_account. This account pays the fee.
time_point_sec expiration
The time at which this withdraw permission expires.
time_point_sec head_block_time() const
Definition: db_getter.cpp:64
account_id_type withdraw_from_account
This account pays the fee. Must match permission_to_update->withdraw_from_account.
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:478
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
string name
The account&#39;s name. This name must be unique among all account names on the graph. May not be empty.
account_id_type withdraw_from_account
The account authorizing authorized_account to withdraw from it.
withdraw_permission_id_type withdraw_permission
ID of the permission authorizing this withdrawal.
withdraw_permission_id_type withdrawal_permission
ID of the permission to be revoked.
withdraw_permission_id_type permission_to_update
ID of the permission which is being updated.
asset available_this_period(fc::time_point_sec current_time) const
Update an existing withdraw permissionThis oeration is used to update the settings for an existing wi...
tracks the parameters of an assetAll assets have a globally unique symbol name that controls how they...
uint32_t withdrawal_period_sec
New length of the period between withdrawals.
asset_id_type asset_id
Definition: asset.hpp:39
void remove(const object &obj)
account_id_type authorized_account
The account previously authorized to make withdrawals. Must match withdrawal_permission->authorized_a...
const T & create(F &&constructor)
Create a new withdrawal permissionThis operation creates a withdrawal permission, which allows some a...
uint8_t block_interval
interval in seconds between blocks
time_point_sec period_start_time
New beginning of the next withdrawal period; must be in the future.