BitShares-Core  6.1.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 
61 { try {
62  const database& d = db();
63  time_point_sec head_block_time = d.head_block_time();
64 
65  const withdraw_permission_object& permit = op.withdraw_permission(d);
66  FC_ASSERT(permit.expiration > head_block_time);
69  FC_ASSERT(permit.period_start_time <= head_block_time);
70  FC_ASSERT(op.amount_to_withdraw <= permit.available_this_period( head_block_time ) );
72 
73  const asset_object& _asset = op.amount_to_withdraw.asset_id(d);
74  if( _asset.is_transfer_restricted() )
75  {
76  FC_ASSERT( _asset.issuer == permit.authorized_account || _asset.issuer == permit.withdraw_from_account,
77  "Asset ${a} '${sym}' has transfer_restricted flag enabled",
78  ("a", _asset.id)("sym", _asset.symbol) );
79  }
80 
81  const account_object& to = permit.authorized_account(d);
82  FC_ASSERT( is_authorized_asset( d, to, _asset ),
83  "Account ${acct} '${name}' is unauthorized to transact asset ${a} '${sym}' due to whitelist / blacklist",
84  ("acct", to.id)("name", to.name)("a", _asset.id)("sym", _asset.symbol) );
85 
86  const account_object& from = op.withdraw_from_account(d);
87  bool from_is_authorized = ( is_authorized_asset( d, from, _asset ) );
88  FC_ASSERT( from_is_authorized,
89  "Account ${acct} '${name}' is unauthorized to withdraw asset ${a} '${sym}' due to whitelist / blacklist",
90  ("acct", from.id)("name", from.name)("a", _asset.id)("sym", _asset.symbol) );
91 
92  return void_result();
93 } FC_CAPTURE_AND_RETHROW( (op) ) }
94 
97 { try {
98  database& d = db();
99 
100  const withdraw_permission_object& permit = d.get(op.withdraw_permission);
101  d.modify(permit, [&](withdraw_permission_object& p) {
102  auto periods = (d.head_block_time() - p.period_start_time).to_seconds() / p.withdrawal_period_sec;
103  p.period_start_time += periods * p.withdrawal_period_sec;
104  if( periods == 0 )
106  else
108  });
109 
112 
113  return void_result();
114 } FC_CAPTURE_AND_RETHROW( (op) ) }
115 
118 { try {
119  database& d = db();
120 
121  const withdraw_permission_object& permit = op.permission_to_update(d);
128 
129  return void_result();
130 } FC_CAPTURE_AND_RETHROW( (op) ) }
131 
134 { try {
135  database& d = db();
136 
138  p.period_start_time = op.period_start_time;
140  p.withdrawal_limit = op.withdrawal_limit;
141  p.withdrawal_period_sec = op.withdrawal_period_sec;
142  });
143 
144  return void_result();
145 } FC_CAPTURE_AND_RETHROW( (op) ) }
146 
149 { try {
150  database& d = db();
151 
155 
156  return void_result();
157 } FC_CAPTURE_AND_RETHROW( (op) ) }
158 
161 { try {
162  db().remove(db().get(op.withdrawal_permission));
163  return void_result();
164 } FC_CAPTURE_AND_RETHROW( (op) ) }
165 
166 } } // 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)
time_point_sec head_block_time() const
Definition: db_getter.cpp:67
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:48
Delete an existing withdrawal permissionThis operation cancels a withdrawal permission, thus preventing any future withdrawals using that permission.
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.
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
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...
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 do_apply(const operation_type &op) const
const T & get(const object_id_type &id) const
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.
const T * find(const object_id_type &id) const
time_point_sec expiration
The time at which this withdraw permission expires.
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:479
#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.
asset available_this_period(fc::time_point_sec current_time) const
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.
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:37
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
const global_property_object & get_global_properties() const
Definition: db_getter.cpp:47
time_point_sec period_start_time
New beginning of the next withdrawal period; must be in the future.