BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
transfer_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  */
27 #include <graphene/chain/hardfork.hpp>
29 
30 namespace graphene { namespace chain {
32 { try {
33 
34  const database& d = db();
35 
36  const account_object& from_account = op.from(d);
37  const account_object& to_account = op.to(d);
38  const asset_object& asset_type = op.amount.asset_id(d);
39 
40  try {
41 
43  is_authorized_asset( d, from_account, asset_type ),
44  transfer_from_account_not_whitelisted,
45  "'from' account ${from} is not whitelisted for asset ${asset}",
46  ("from",op.from)
47  ("asset",op.amount.asset_id)
48  );
50  is_authorized_asset( d, to_account, asset_type ),
51  transfer_to_account_not_whitelisted,
52  "'to' account ${to} is not whitelisted for asset ${asset}",
53  ("to",op.to)
54  ("asset",op.amount.asset_id)
55  );
56 
57  if( asset_type.is_transfer_restricted() )
58  {
60  from_account.id == asset_type.issuer || to_account.id == asset_type.issuer,
61  transfer_restricted_transfer_asset,
62  "Asset ${asset} has transfer_restricted flag enabled",
63  ("asset", op.amount.asset_id)
64  );
65  }
66 
67  bool insufficient_balance = d.get_balance( from_account, asset_type ).amount >= op.amount.amount;
68  FC_ASSERT( insufficient_balance,
69  "Insufficient Balance: ${balance}, unable to transfer '${total_transfer}' from account '${a}' to '${t}'",
70  ("a",from_account.name)("t",to_account.name)("total_transfer",d.to_pretty_string(op.amount))("balance",d.to_pretty_string(d.get_balance(from_account, asset_type))) );
71 
72  return void_result();
73  } FC_RETHROW_EXCEPTIONS( error, "Unable to transfer ${a} from ${f} to ${t}", ("a",d.to_pretty_string(op.amount))("f",op.from(d).name)("t",op.to(d).name) );
74 
75 } FC_CAPTURE_AND_RETHROW( (op) ) }
76 
78 { try {
79  db().adjust_balance( o.from, -o.amount );
80  db().adjust_balance( o.to, o.amount );
81  return void_result();
82 } FC_CAPTURE_AND_RETHROW( (o) ) }
83 
84 
85 
87 { try {
88  const database& d = db();
89 
90  const asset_object& asset_type = op.amount.asset_id(d);
92  asset_type.can_override(),
93  override_transfer_not_permitted,
94  "override_transfer not permitted for asset ${asset}",
95  ("asset", op.amount.asset_id)
96  );
97  FC_ASSERT( asset_type.issuer == op.issuer );
98 
99  const account_object& from_account = op.from(d);
100  const account_object& to_account = op.to(d);
101 
102  FC_ASSERT( is_authorized_asset( d, to_account, asset_type ),
103  "The to_account is not allowed to transact the asset" );
104  // Since hard fork core-2295, do not check asset authorization limitations on from_account for override_transfer
105  // TODO code cleanup: if applicable (could be false due to proposals),
106  // remove the check and the assertion below after the hard fork time, keep the comment about reasoning above
107  if( !HARDFORK_CORE_2295_PASSED(d.head_block_time()) )
108  {
109  FC_ASSERT( is_authorized_asset( d, from_account, asset_type ),
110  "The from_account is not allowed to transact the asset" );
111  }
112 
113  FC_ASSERT( d.get_balance( from_account, asset_type ).amount >= op.amount.amount,
114  "", ("total_transfer",op.amount)("balance",d.get_balance(from_account, asset_type).amount) );
115 
116  return void_result();
117 } FC_CAPTURE_AND_RETHROW( (op) ) }
118 
120 { try {
121  db().adjust_balance( o.from, -o.amount );
122  db().adjust_balance( o.to, o.amount );
123  return void_result();
124 } FC_CAPTURE_AND_RETHROW( (o) ) }
125 
126 } } // graphene::chain
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::transfer_operation::amount
asset amount
The amount of asset to transfer from from to to.
Definition: transfer.hpp:58
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
graphene::chain::asset_object::can_override
bool can_override() const
Definition: asset_object.hpp:95
graphene::chain::database::head_block_time
time_point_sec head_block_time() const
Definition: db_getter.cpp:67
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::asset_object
tracks the parameters of an asset
Definition: asset_object.hpp:75
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::chain::transfer_evaluator::do_apply
void_result do_apply(const transfer_operation &o)
Definition: transfer_evaluator.cpp:77
graphene::protocol::override_transfer_operation::issuer
account_id_type issuer
Definition: transfer.hpp:85
graphene::chain::account_object
This class represents an account on the object graph.
Definition: account_object.hpp:180
graphene::protocol::override_transfer_operation::from
account_id_type from
Account to transfer asset from.
Definition: transfer.hpp:87
graphene::protocol::asset::asset_id
asset_id_type asset_id
Definition: asset.hpp:37
graphene::protocol::transfer_operation
Transfers an amount of one asset from one account to another.
Definition: transfer.hpp:45
account_object.hpp
graphene::protocol::override_transfer_operation::to
account_id_type to
Account to transfer asset to.
Definition: transfer.hpp:89
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::database::to_pretty_string
string to_pretty_string(const asset &a) const
Definition: db_balance.cpp:49
graphene::protocol::override_transfer_operation::amount
asset amount
The amount of asset to transfer from from to to.
Definition: transfer.hpp:91
graphene::protocol::transfer_operation::to
account_id_type to
Account to transfer asset to.
Definition: transfer.hpp:56
graphene::chain::override_transfer_evaluator::do_apply
void_result do_apply(const override_transfer_operation &o)
Definition: transfer_evaluator.cpp:119
graphene::protocol::transfer_operation::from
account_id_type from
Account to transfer asset from.
Definition: transfer.hpp:54
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
transfer_evaluator.hpp
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::asset::amount
share_type amount
Definition: asset.hpp:36
FC_RETHROW_EXCEPTIONS
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
Definition: exception.hpp:464
graphene::chain::override_transfer_evaluator::do_evaluate
void_result do_evaluate(const override_transfer_operation &o)
Definition: transfer_evaluator.cpp:86
graphene::protocol::override_transfer_operation
Allows the issuer of an asset to transfer an asset from any account to any account if they have overr...
Definition: transfer.hpp:77
GRAPHENE_ASSERT
#define GRAPHENE_ASSERT(expr, exc_type, FORMAT,...)
Definition: exceptions.hpp:28
graphene::protocol::void_result
Definition: base.hpp:86
graphene
Definition: api.cpp:48
exceptions.hpp
graphene::chain::transfer_evaluator::do_evaluate
void_result do_evaluate(const transfer_operation &o)
Definition: transfer_evaluator.cpp:31