BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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;
139  p.expiration = op.period_start_time + op.periods_until_expiration * op.withdrawal_period_sec;
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
graphene::protocol::withdraw_permission_create_operation::withdrawal_period_sec
uint32_t withdrawal_period_sec
Length of the withdrawal period in seconds.
Definition: withdraw_permission.hpp:62
FC_CAPTURE_AND_RETHROW
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:479
graphene::db::object::id
object_id_type id
Definition: object.hpp:69
is_authorized_asset.hpp
graphene::protocol::withdraw_permission_update_operation
Update an existing withdraw permission.
Definition: withdraw_permission.hpp:83
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
graphene::protocol::withdraw_permission_create_operation::periods_until_expiration
uint32_t periods_until_expiration
The number of withdrawal periods this permission is valid for.
Definition: withdraw_permission.hpp:64
graphene::protocol::withdraw_permission_claim_operation::withdraw_from_account
account_id_type withdraw_from_account
Must match withdraw_permission->withdraw_from_account.
Definition: withdraw_permission.hpp:132
graphene::chain::withdraw_permission_claim_evaluator::do_apply
void_result do_apply(const operation_type &op) const
Definition: withdraw_permission_evaluator.cpp:95
graphene::chain::withdraw_permission_object::withdrawal_period_sec
uint32_t withdrawal_period_sec
The duration of a withdrawal period in seconds.
Definition: withdraw_permission_object.hpp:56
graphene::chain::database::head_block_time
time_point_sec head_block_time() const
Definition: db_getter.cpp:67
graphene::chain::withdraw_permission_object::claimed_this_period
share_type claimed_this_period
Definition: withdraw_permission_object.hpp:74
graphene::chain::withdraw_permission_delete_evaluator::do_apply
void_result do_apply(const operation_type &op) const
Definition: withdraw_permission_evaluator.cpp:159
graphene::db::object_database::get
const T & get(const object_id_type &id) const
Definition: object_database.hpp:119
graphene::chain::database::get_balance
asset get_balance(account_id_type owner, asset_id_type asset_id) const
Retrieve a particular account's balance in a given asset.
Definition: db_balance.cpp:35
graphene::chain::withdraw_permission_object::available_this_period
asset available_this_period(fc::time_point_sec current_time) const
Definition: withdraw_permission_object.hpp:81
graphene::chain::withdraw_permission_create_evaluator::do_evaluate
void_result do_evaluate(const operation_type &op) const
Definition: withdraw_permission_evaluator.cpp:34
database.hpp
graphene::protocol::withdraw_permission_claim_operation::amount_to_withdraw
asset amount_to_withdraw
Amount to withdraw. Must not exceed withdraw_permission->withdrawal_limit.
Definition: withdraw_permission.hpp:136
graphene::chain::withdraw_permission_create_evaluator::do_apply
object_id_type do_apply(const operation_type &op) const
Definition: withdraw_permission_evaluator.cpp:47
graphene::chain::withdraw_permission_object::period_start_time
time_point_sec period_start_time
Definition: withdraw_permission_object.hpp:63
graphene::chain::withdraw_permission_claim_evaluator::do_evaluate
void_result do_evaluate(const operation_type &op) const
Definition: withdraw_permission_evaluator.cpp:59
graphene::chain::global_property_object::parameters
chain_parameters parameters
Definition: global_property_object.hpp:44
graphene::protocol::withdraw_permission_delete_operation::withdraw_from_account
account_id_type withdraw_from_account
Must match withdrawal_permission->withdraw_from_account. This account pays the fee.
Definition: withdraw_permission.hpp:159
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::chain::asset_object::is_transfer_restricted
bool is_transfer_restricted() const
Definition: asset_object.hpp:94
graphene::protocol::withdraw_permission_claim_operation::withdraw_permission
withdraw_permission_id_type withdraw_permission
ID of the permission authorizing this withdrawal.
Definition: withdraw_permission.hpp:130
graphene::chain::asset_object::symbol
string symbol
Ticker symbol for this asset, i.e. "USD".
Definition: asset_object.hpp:131
graphene::protocol::withdraw_permission_delete_operation::authorized_account
account_id_type authorized_account
The account previously authorized to make withdrawals. Must match withdrawal_permission->authorized_a...
Definition: withdraw_permission.hpp:161
graphene::db::object_database::find
const T * find(const object_id_type &id) const
Definition: object_database.hpp:126
graphene::protocol::withdraw_permission_claim_operation::withdraw_to_account
account_id_type withdraw_to_account
Must match withdraw_permision->authorized_account.
Definition: withdraw_permission.hpp:134
graphene::protocol::withdraw_permission_claim_operation
Withdraw from an account which has published a withdrawal permission.
Definition: withdraw_permission.hpp:120
graphene::protocol::withdraw_permission_update_operation::authorized_account
account_id_type authorized_account
The account authorized to make withdrawals. Must match permission_to_update->authorized_account.
Definition: withdraw_permission.hpp:91
graphene::chain::withdraw_permission_delete_evaluator::do_evaluate
void_result do_evaluate(const operation_type &op) const
Definition: withdraw_permission_evaluator.cpp:147
graphene::protocol::withdraw_permission_create_operation
Create a new withdrawal permission.
Definition: withdraw_permission.hpp:50
graphene::chain::withdraw_permission_object::authorized_account
account_id_type authorized_account
The account authorized to make withdrawals from withdraw_from_account.
Definition: withdraw_permission_object.hpp:52
graphene::chain::account_object
This class represents an account on the object graph.
Definition: account_object.hpp:180
graphene::protocol::withdraw_permission_create_operation::authorized_account
account_id_type authorized_account
The account authorized to make withdrawals from withdraw_from_account.
Definition: withdraw_permission.hpp:58
fc::time_point_sec
Definition: time.hpp:74
graphene::protocol::asset::asset_id
asset_id_type asset_id
Definition: asset.hpp:37
graphene::chain::withdraw_permission_update_evaluator::do_evaluate
void_result do_evaluate(const operation_type &op) const
Definition: withdraw_permission_evaluator.cpp:116
account_object.hpp
graphene::protocol::withdraw_permission_update_operation::withdrawal_limit
asset withdrawal_limit
New maximum amount the withdrawer is allowed to charge per withdrawal period.
Definition: withdraw_permission.hpp:95
withdraw_permission_evaluator.hpp
graphene::protocol::withdraw_permission_delete_operation::withdrawal_permission
withdraw_permission_id_type withdrawal_permission
ID of the permission to be revoked.
Definition: withdraw_permission.hpp:163
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::protocol::withdraw_permission_delete_operation
Delete an existing withdrawal permission.
Definition: withdraw_permission.hpp:153
graphene::chain::withdraw_permission_object::expiration
time_point_sec expiration
The time at which this withdraw permission expires.
Definition: withdraw_permission_object.hpp:65
graphene::chain::generic_evaluator::db
database & db() const
Definition: evaluator.cpp:39
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::account_object::name
string name
The account's name. This name must be unique among all account names on the graph....
Definition: account_object.hpp:209
graphene::chain::asset_object::issuer
account_id_type issuer
ID of the account which issued this asset.
Definition: asset_object.hpp:135
graphene::protocol::withdraw_permission_update_operation::periods_until_expiration
uint32_t periods_until_expiration
The new number of withdrawal periods for which this permission will be valid.
Definition: withdraw_permission.hpp:101
graphene::protocol::withdraw_permission_update_operation::withdrawal_period_sec
uint32_t withdrawal_period_sec
New length of the period between withdrawals.
Definition: withdraw_permission.hpp:97
graphene::db::object_id_type
Definition: object_id.hpp:30
graphene::protocol::asset::amount
share_type amount
Definition: asset.hpp:36
graphene::protocol::withdraw_permission_update_operation::period_start_time
time_point_sec period_start_time
New beginning of the next withdrawal period; must be in the future.
Definition: withdraw_permission.hpp:99
graphene::protocol::withdraw_permission_update_operation::permission_to_update
withdraw_permission_id_type permission_to_update
ID of the permission which is being updated.
Definition: withdraw_permission.hpp:93
graphene::chain::withdraw_permission_object
Grants another account authority to withdraw a limited amount of funds per interval.
Definition: withdraw_permission_object.hpp:45
graphene::chain::withdraw_permission_object::withdraw_from_account
account_id_type withdraw_from_account
The account authorizing authorized_account to withdraw from it.
Definition: withdraw_permission_object.hpp:50
graphene::chain::database::get_global_properties
const global_property_object & get_global_properties() const
Definition: db_getter.cpp:47
withdraw_permission_object.hpp
graphene::protocol::void_result
Definition: base.hpp:86
graphene::db::object_database::remove
void remove(const object &obj)
Definition: object_database.hpp:97
graphene::chain::withdraw_permission_update_evaluator::do_apply
void_result do_apply(const operation_type &op) const
Definition: withdraw_permission_evaluator.cpp:132
graphene::protocol::withdraw_permission_update_operation::withdraw_from_account
account_id_type withdraw_from_account
This account pays the fee. Must match permission_to_update->withdraw_from_account.
Definition: withdraw_permission.hpp:89
graphene::protocol::withdraw_permission_create_operation::period_start_time
time_point_sec period_start_time
Time at which the first withdrawal period begins; must be in the future.
Definition: withdraw_permission.hpp:66
graphene::protocol::withdraw_permission_create_operation::withdrawal_limit
asset withdrawal_limit
The maximum amount authorized_account is allowed to withdraw in a given withdrawal period.
Definition: withdraw_permission.hpp:60
graphene
Definition: api.cpp:48
exceptions.hpp
graphene::db::object_database::modify
void modify(const T &obj, const Lambda &m)
Definition: object_database.hpp:99
graphene::protocol::chain_parameters::block_interval
uint8_t block_interval
interval in seconds between blocks
Definition: chain_parameters.hpp:53
graphene::protocol::withdraw_permission_create_operation::withdraw_from_account
account_id_type withdraw_from_account
The account authorizing withdrawals from its balances.
Definition: withdraw_permission.hpp:56