BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
evaluator.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
28 
29 namespace graphene { namespace chain {
30 
31  class database;
32  class generic_evaluator;
33  class transaction_evaluation_state;
34  class account_object;
35  class account_statistics_object;
36  class asset_object;
37  class asset_dynamic_data_object;
38 
40  {
41  public:
42  virtual ~generic_evaluator(){}
43 
44  virtual int get_type()const = 0;
45  virtual operation_result start_evaluate(transaction_evaluation_state& eval_state, const operation& op, bool apply);
46 
52  virtual operation_result evaluate(const operation& op) = 0;
53  virtual operation_result apply(const operation& op) = 0;
54 
69  virtual void pay_fee();
70 
71  database& db()const;
72 
73  //void check_required_authorities(const operation& op);
74  protected:
85  void prepare_fee(account_id_type account_id, asset fee);
86 
99  virtual void convert_fee();
100 
102 
106  void pay_fba_fee( uint64_t fba_id );
107 
108  // the next two functions are helpers that allow template functions declared in this
109  // header to call db() without including database.hpp, which would
110  // cause a circular dependency
112  void db_adjust_balance(const account_id_type& fee_payer, asset fee_from_account);
113 
118  const asset_object* fee_asset = nullptr;
121  };
122 
124  {
125  public:
126  virtual ~op_evaluator(){}
127  virtual operation_result evaluate(transaction_evaluation_state& eval_state, const operation& op, bool apply) = 0;
128  };
129 
130  template<typename T>
132  {
133  public:
134  virtual operation_result evaluate(transaction_evaluation_state& eval_state, const operation& op, bool apply = true) override
135  {
136  T eval;
137  return eval.start_evaluate(eval_state, op, apply);
138  }
139  };
140 
141  template<typename DerivedEvaluator>
143  {
144  public:
145  virtual int get_type()const override { return operation::tag<typename DerivedEvaluator::operation_type>::value; }
146 
147  virtual operation_result evaluate(const operation& o) final override
148  {
149  auto* eval = static_cast<DerivedEvaluator*>(this);
150  const auto& op = o.get<typename DerivedEvaluator::operation_type>();
151 
152  prepare_fee(op.fee_payer(), op.fee);
154  {
155  share_type required_fee = calculate_fee_for_operation(op);
156  GRAPHENE_ASSERT( core_fee_paid >= required_fee,
157  insufficient_fee,
158  "Insufficient Fee Paid",
159  ("core_fee_paid",core_fee_paid)("required", required_fee) );
160  }
161 
162  return eval->do_evaluate(op);
163  }
164 
165  virtual operation_result apply(const operation& o) final override
166  {
167  auto* eval = static_cast<DerivedEvaluator*>(this);
168  const auto& op = o.get<typename DerivedEvaluator::operation_type>();
169 
170  convert_fee();
171  pay_fee();
172 
173  auto result = eval->do_apply(op);
174 
175  db_adjust_balance(op.fee_payer(), -fee_from_account);
176 
177  return result;
178  }
179  };
180 } }
graphene::chain::evaluator::get_type
virtual int get_type() const override
Definition: evaluator.hpp:145
fc::typelist::apply
typename impl::apply< List, Delegate >::type apply
Apply a list of types as arguments to another template.
Definition: typelist.hpp:142
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
graphene::chain::op_evaluator
Definition: evaluator.hpp:123
graphene::chain::generic_evaluator::fee_paying_account_statistics
const account_statistics_object * fee_paying_account_statistics
Definition: evaluator.hpp:117
graphene::chain::transaction_evaluation_state::skip_fee_schedule_check
bool skip_fee_schedule_check
Definition: transaction_evaluation_state.hpp:50
graphene::chain::op_evaluator::evaluate
virtual operation_result evaluate(transaction_evaluation_state &eval_state, const operation &op, bool apply)=0
fc::static_variant< void_result, object_id_type, asset, generic_operation_result, generic_exchange_operation_result, extendable_operation_result >
graphene::chain::generic_evaluator::get_relative_id
object_id_type get_relative_id(object_id_type rel_id) const
graphene::chain::generic_evaluator::fee_asset
const asset_object * fee_asset
Definition: evaluator.hpp:118
graphene::chain::transaction_evaluation_state
Definition: transaction_evaluation_state.hpp:37
graphene::chain::generic_evaluator::apply
virtual operation_result apply(const operation &op)=0
graphene::chain::asset_object
tracks the parameters of an asset
Definition: asset_object.hpp:75
graphene::chain::generic_evaluator::pay_fee
virtual void pay_fee()
Definition: evaluator.cpp:91
graphene::chain::generic_evaluator::fee_from_account
asset fee_from_account
Definition: evaluator.hpp:114
graphene::chain::generic_evaluator
Definition: evaluator.hpp:39
graphene::chain::op_evaluator_impl::evaluate
virtual operation_result evaluate(transaction_evaluation_state &eval_state, const operation &op, bool apply=true) override
Definition: evaluator.hpp:134
graphene::chain::generic_evaluator::pay_fba_fee
void pay_fba_fee(uint64_t fba_id)
Definition: evaluator.cpp:100
graphene::chain::account_statistics_object
Definition: account_object.hpp:46
operations.hpp
graphene::chain::generic_evaluator::convert_fee
virtual void convert_fee()
Definition: evaluator.cpp:80
graphene::chain::asset_dynamic_data_object
tracks the asset information that changes frequently
Definition: asset_object.hpp:56
graphene::chain::op_evaluator_impl
Definition: evaluator.hpp:131
graphene::chain::account_object
This class represents an account on the object graph.
Definition: account_object.hpp:180
graphene::chain::generic_evaluator::db_adjust_balance
void db_adjust_balance(const account_id_type &fee_payer, asset fee_from_account)
Definition: evaluator.cpp:119
graphene::chain::evaluator
Definition: evaluator.hpp:142
graphene::chain::generic_evaluator::fee_asset_dyn_data
const asset_dynamic_data_object * fee_asset_dyn_data
Definition: evaluator.hpp:119
graphene::chain::evaluator::apply
virtual operation_result apply(const operation &o) final override
Definition: evaluator.hpp:165
graphene::chain::op_evaluator::~op_evaluator
virtual ~op_evaluator()
Definition: evaluator.hpp:126
graphene::chain::generic_evaluator::db
database & db() const
Definition: evaluator.cpp:39
graphene::chain::generic_evaluator::get_type
virtual int get_type() const =0
graphene::db::object_id_type
Definition: object_id.hpp:30
fc::static_variant::get
X & get()
Definition: static_variant.hpp:236
graphene::chain::generic_evaluator::calculate_fee_for_operation
share_type calculate_fee_for_operation(const operation &op) const
Definition: evaluator.cpp:115
graphene::chain::generic_evaluator::evaluate
virtual operation_result evaluate(const operation &op)=0
transaction_evaluation_state.hpp
graphene::chain::evaluator::evaluate
virtual operation_result evaluate(const operation &o) final override
Definition: evaluator.hpp:147
graphene::chain::generic_evaluator::prepare_fee
void prepare_fee(account_id_type account_id, asset fee)
Fetch objects relevant to fee payer and set pointer members.
Definition: evaluator.cpp:51
graphene::chain::generic_evaluator::fee_paying_account
const account_object * fee_paying_account
Definition: evaluator.hpp:116
graphene::protocol::asset
Definition: asset.hpp:31
graphene::chain::generic_evaluator::start_evaluate
virtual operation_result start_evaluate(transaction_evaluation_state &eval_state, const operation &op, bool apply)
Definition: evaluator.cpp:41
graphene::chain::generic_evaluator::core_fee_paid
share_type core_fee_paid
Definition: evaluator.hpp:115
GRAPHENE_ASSERT
#define GRAPHENE_ASSERT(expr, exc_type, FORMAT,...)
Definition: exceptions.hpp:28
graphene
Definition: api.cpp:48
exceptions.hpp
graphene::chain::generic_evaluator::trx_state
transaction_evaluation_state * trx_state
Definition: evaluator.hpp:120
graphene::chain::generic_evaluator::~generic_evaluator
virtual ~generic_evaluator()
Definition: evaluator.hpp:42
fc::safe
Definition: safe.hpp:26