BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
database.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
25 
27 
36 
38 #include <graphene/db/object.hpp>
40 #include <fc/signals.hpp>
41 
42 #include <fc/log/logger.hpp>
43 
44 #include <map>
45 
46 namespace graphene { namespace protocol { struct predicate_result; } }
47 
48 namespace graphene { namespace chain {
51  class op_evaluator;
52  class transaction_evaluation_state;
53  class proposal_object;
54  class operation_history_object;
55  class chain_property_object;
56  class witness_schedule_object;
57  class witness_object;
58  class force_settlement_object;
59  class limit_order_object;
60  class collateral_bid_object;
61  class call_order_object;
62 
63  struct budget_record;
64  enum class vesting_balance_type;
65 
71  {
73  public:
74  database();
75  ~database() override;
76 
78  {
79  skip_nothing = 0,
80  skip_witness_signature = 1 << 0,
81  skip_transaction_signatures = 1 << 1,
82  skip_transaction_dupe_check = 1 << 2,
83  skip_block_size_check = 1 << 4,
84  skip_tapos_check = 1 << 5,
85  // skip_authority_check = 1 << 6, // removed, effectively identical to skip_transaction_signatures
86  skip_merkle_check = 1 << 7,
87  skip_assert_evaluation = 1 << 8,
88  skip_undo_history_check = 1 << 9,
89  skip_witness_schedule_check = 1 << 10
90  };
91 
104  void open(
105  const fc::path& data_dir,
106  std::function<genesis_state_type()> genesis_loader,
107  const std::string& db_version );
108 
116  void reindex(fc::path data_dir);
117 
125  void wipe(const fc::path& data_dir, bool include_blocks);
126  void close(bool rewind = true);
127 
129 
144  witness_id_type get_scheduled_witness(uint32_t slot_num)const;
145 
154  fc::time_point_sec get_slot_time(uint32_t slot_num)const;
155 
164  uint32_t get_slot_at_time(fc::time_point_sec when)const;
165 
170  uint32_t witness_participation_rate()const;
171  private:
172  uint32_t update_witness_missed_blocks( const signed_block& b );
173 
174  void update_witness_schedule();
175 
177 
178  public:
179  const chain_id_type& get_chain_id()const;
180  const asset_object& get_core_asset()const;
181  const asset_dynamic_data_object& get_core_dynamic_data()const;
182  const chain_property_object& get_chain_properties()const;
183  const global_property_object& get_global_properties()const;
184  const dynamic_global_property_object& get_dynamic_global_properties()const;
185  const node_property_object& get_node_properties()const;
186  const fee_schedule& current_fee_schedule()const;
187  const account_statistics_object& get_account_stats_by_owner( account_id_type owner )const;
188  const witness_schedule_object& get_witness_schedule_object()const;
189 
190  time_point_sec head_block_time()const;
191  uint32_t head_block_num()const;
192  block_id_type head_block_id()const;
193  witness_id_type head_block_witness()const;
194 
195  decltype( chain_parameters::block_interval ) block_interval( )const;
196 
197  node_property_object& node_properties();
198 
207  vector<authority> get_viable_custom_authorities(
208  account_id_type account, const operation& op,
209  rejected_predicate_map* rejected_authorities = nullptr )const;
210 
211  uint32_t last_non_undoable_block_num() const;
212 
216  const limit_order_object* find_settled_debt_order( const asset_id_type& a )const;
217 
222  const call_order_object* find_least_collateralized_short( const asset_bitasset_data_object& bitasset,
223  bool force_by_collateral_index )const;
224 
227 
229  void initialize_indexes(); // Mark as public since it is used in tests
230  private:
231  void initialize_evaluators();
232  void init_genesis(const genesis_state_type& genesis_state = genesis_state_type());
233 
234  template<typename EvaluatorType>
235  void register_evaluator()
236  {
237  const auto op_type = operation::tag<typename EvaluatorType::operation_type>::value;
238  FC_ASSERT( op_type >= 0, "Negative operation type" );
239  FC_ASSERT( op_type < _operation_evaluators.size(),
240  "The operation type (${a}) must be smaller than the size of _operation_evaluators (${b})",
241  ("a", op_type)("b", _operation_evaluators.size()) );
242  _operation_evaluators[op_type] = std::make_unique<op_evaluator_impl<EvaluatorType>>();
243  }
245 
247 
248  public:
255  asset get_balance(account_id_type owner, asset_id_type asset_id)const;
257  asset get_balance(const account_object& owner, const asset_object& asset_obj)const;
258 
264  void adjust_balance(account_id_type account, asset delta);
265 
266  void deposit_market_fee_vesting_balance(const account_id_type &account_id, const asset &delta);
273  asset get_market_fee_vesting_balance(const account_id_type &account_id, const asset_id_type &asset_id);
274 
288  optional< vesting_balance_id_type > deposit_lazy_vesting(
290  share_type amount,
291  uint32_t req_vesting_seconds,
292  vesting_balance_type balance_type,
293  account_id_type req_owner,
294  bool require_vesting );
295 
297  void deposit_cashback(const account_object& acct, share_type amount, bool require_vesting = true);
299  void deposit_witness_pay(const witness_object& wit, share_type amount);
300 
301  string to_pretty_string( const asset& a )const;
302 
304 
305  void debug_dump();
306  void apply_debug_updates();
307  void debug_update( const fc::variant_object& update );
308 
310 
313 
317  void globally_settle_asset( const asset_object& bitasset, const price& settle_price,
318  bool check_margin_calls = false );
319  void cancel_settle_order( const force_settlement_object& order );
320  void cancel_limit_order( const limit_order_object& order,
321  bool create_virtual_op = true,
322  bool skip_cancel_fee = false );
323  void revive_bitasset( const asset_object& bitasset, const asset_bitasset_data_object& bad );
324  void cancel_bid(const collateral_bid_object& bid, bool create_virtual_op = true);
325  void execute_bid( const collateral_bid_object& bid, share_type debt_covered,
326  share_type collateral_from_fund, const price_feed& current_feed );
327 
328  private:
329  void _cancel_bids_and_revive_mpa( const asset_object& bitasset, const asset_bitasset_data_object& bad );
330  bool check_for_blackswan( const asset_object& mia, bool enable_black_swan = true,
331  const asset_bitasset_data_object* bitasset_ptr = nullptr );
332  template<typename IndexType>
333  void globally_settle_asset_impl( const asset_object& bitasset,
334  const price& settle_price,
335  const IndexType& call_index,
336  bool check_margin_calls = false );
341  void individually_settle( const asset_bitasset_data_object& bitasset, const call_order_object& call_order );
345  bool match_force_settlements( const asset_bitasset_data_object& bitasset );
357  asset match_impl( const force_settlement_object& settle,
358  const call_order_object& call,
359  const price& match_price,
360  const asset_bitasset_data_object& bitasset,
361  const asset& max_settlement,
362  const price& fill_price,
363  bool is_margin_call = false,
364  bool settle_order_is_taker = true );
365 
366  public:
375  bool apply_order_before_hardfork_625( const limit_order_object& new_order_object );
377  bool apply_order( const limit_order_object& new_order_object );
379 
389  void apply_force_settlement( const force_settlement_object& new_settlement,
390  const asset_bitasset_data_object& bitasset,
391  const asset_object& asset_obj );
392 
393  bool check_call_orders( const asset_object& mia, bool enable_black_swan = true,
394  bool for_new_limit_order = false,
395  const asset_bitasset_data_object* bitasset_ptr = nullptr,
396  bool mute_exceptions = false,
397  bool skip_matching_settle_orders = false );
398 
399  // Note: Ideally this should be private.
400  // Now it is public because we use it in a non-member function in db_market.cpp .
401  enum class match_result_type
402  {
403  none_filled = 0,
404  only_taker_filled = 1,
405  only_maker_filled = 2,
406  both_filled = 3
407  };
408 
409  private:
415  match_result_type match( const limit_order_object& taker, const limit_order_object& maker,
417  const price& trade_price );
418  match_result_type match_limit_normal_limit( const limit_order_object& taker, const limit_order_object& maker,
419  const price& trade_price );
420  match_result_type match_limit_settled_debt( const limit_order_object& taker, const limit_order_object& maker,
421  const price& trade_price );
422  /***
423  * @brief Match limit order as taker to a call order as maker
424  * @param taker the order that is removing liquidity from the book
425  * @param maker the order that put liquidity on the book
426  * @param trade_price the price the trade should execute at
427  * @param bitasset the bitasset object corresponding to debt asset of the call order
428  * @param call_pays_price price call order pays. Call order may pay more collateral
429  * than limit order takes if call order subject to a margin call fee.
430  * @returns 0 if no orders were matched, 1 if taker was filled, 2 if maker was filled, 3 if both were filled
431  */
432  match_result_type match( const limit_order_object& taker, const call_order_object& maker,
433  const price& trade_price,
434  const asset_bitasset_data_object& bitasset,
435  const price& call_pays_price);
437  match_result_type match( const limit_order_object& taker, const call_order_object& maker,
438  const price& trade_price,
439  const asset_bitasset_data_object& bitasset) {
440  return match(taker, maker, trade_price, bitasset, trade_price);
441  }
442 
444 
455  asset match( const force_settlement_object& settle,
456  const call_order_object& call,
457  const price& match_price,
458  const asset_bitasset_data_object& bitasset,
459  const asset& max_settlement,
460  const price& fill_price,
461  bool is_margin_call = false );
462 
472  asset match( const call_order_object& call,
473  const force_settlement_object& settle,
474  const price& match_price,
475  const asset_bitasset_data_object& bitasset,
476  const asset& max_settlement,
477  const price& fill_price );
478 
489  bool fill_limit_order( const limit_order_object& order, const asset& pays, const asset& receives,
490  bool cull_if_small, const price& fill_price, const bool is_maker );
491  /***
492  * @brief attempt to fill a call order
493  * @param order the order
494  * @param pays what the buyer pays for the collateral
495  * @param receives the collateral received by the buyer
496  * @param fill_price the price the transaction executed at
497  * @param is_maker TRUE if the buyer is the maker, FALSE if the buyer is the taker
498  * @param margin_fee Margin call fees paid in collateral asset
499  * @param reduce_current_supply Whether to reduce current supply of the asset. Usually it is true.
500  * When globally settleing or individually settling it is false.
501  * @returns TRUE if the order was completely filled
502  */
503  bool fill_call_order( const call_order_object& order, const asset& pays, const asset& receives,
504  const price& fill_price, const bool is_maker, const asset& margin_fee,
505  bool reduce_current_supply = true );
506 
508  bool fill_call_order( const call_order_object& order, const asset& pays, const asset& receives,
509  const price& fill_price, const bool is_maker,
510  bool reduce_current_supply = true )
511  {
512  return fill_call_order( order, pays, receives, fill_price, is_maker, asset(0, pays.asset_id),
513  reduce_current_supply );
514  }
515 
516  bool fill_settle_order( const force_settlement_object& settle, const asset& pays, const asset& receives,
517  const price& fill_price, bool is_maker, bool pay_force_settle_fee = true );
518 
521  void pay_order( const account_object& receiver, const asset& receives, const asset& pays );
522 
523  public:
530  asset calculate_market_fee( const asset_object& trade_asset, const asset& trade_amount,
531  const bool& is_maker )const;
532  asset pay_market_fees(const account_object* seller, const asset_object& recv_asset, const asset& receives,
533  const bool& is_maker, const optional<asset>& calculated_market_fees = {});
534  asset pay_force_settle_fees(const asset_object& collecting_asset, const asset& collat_receives);
537 
539 
544  bool is_known_block( const block_id_type& id )const;
545  bool is_known_transaction( const transaction_id_type& id )const;
546  block_id_type get_block_id_for_num( uint32_t block_num )const;
547  optional<signed_block> fetch_block_by_id( const block_id_type& id )const;
548  optional<signed_block> fetch_block_by_number( uint32_t num )const;
549  const signed_transaction& get_recent_transaction( const transaction_id_type& trx_id )const;
550  std::vector<block_id_type> get_block_ids_on_fork(block_id_type head_of_fork) const;
551 
552  void add_checkpoints( const flat_map<uint32_t,block_id_type>& checkpts );
553  const flat_map<uint32_t,block_id_type> get_checkpoints()const { return _checkpoints; }
554  bool before_last_checkpoint()const;
555 
556  bool push_block( const signed_block& b, uint32_t skip = skip_nothing );
557  processed_transaction push_transaction( const precomputable_transaction& trx, uint32_t skip = skip_nothing );
558  private:
559  bool _push_block( const signed_block& b );
560  public:
561  // It is public because it is used in pending_transactions_restorer in db_with.hpp
562  processed_transaction _push_transaction( const precomputable_transaction& trx );
564  processed_transaction push_proposal( const proposal_object& proposal );
565 
566  signed_block generate_block(
567  const fc::time_point_sec when,
568  witness_id_type witness_id,
569  const fc::ecc::private_key& block_signing_private_key,
570  uint32_t skip
571  );
572  private:
573  signed_block _generate_block(
574  const fc::time_point_sec when,
575  witness_id_type witness_id,
576  const fc::ecc::private_key& block_signing_private_key
577  );
578 
579  public:
580  void pop_block();
581  void clear_pending();
582 
594  uint32_t push_applied_operation( const operation& op, bool is_virtual = true );
595  void set_applied_operation_result( uint32_t op_id, const operation_result& r );
596  const vector<optional< operation_history_object > >& get_applied_operations()const;
597 
607 
613 
618  fc::signal<void(const vector<object_id_type>&, const flat_set<account_id_type>&)> new_objects;
619 
624  fc::signal<void(const vector<object_id_type>&, const flat_set<account_id_type>&)> changed_objects;
625 
630  const vector<const object*>&, const flat_set<account_id_type>&)> removed_objects;
631 
633 
637  processed_transaction validate_transaction( const signed_transaction& trx );
638 
639 
642  std::deque< precomputable_transaction > _popped_tx;
643 
657  fc::future<void> precompute_parallel( const signed_block& block, const uint32_t skip = skip_nothing )const;
658 
666  fc::future<void> precompute_parallel( const precomputable_transaction& trx )const;
667  private:
668  template<typename Trx>
669  void _precompute_parallel( const Trx* trx, const size_t count, const uint32_t skip )const;
670 
671  protected:
672  // Mark pop_undo() as protected -- we do not want outside calling pop_undo(),
673  // it should call pop_block() instead
675 
676  private:
677  optional<undo_database::session> _pending_tx_session;
678  vector< unique_ptr<op_evaluator> > _operation_evaluators;
679 
680  template<class Index>
681  vector<std::reference_wrapper<const typename Index::object_type>> sort_votable_objects(size_t count)const;
682 
683  public:
684  // these were formerly private, but they have a fairly well-defined API, so let's make them public
685  void apply_block( const signed_block& next_block, uint32_t skip = skip_nothing );
686  processed_transaction apply_transaction( const signed_transaction& trx, uint32_t skip = skip_nothing );
687  operation_result apply_operation( transaction_evaluation_state& eval_state, const operation& op,
688  bool is_virtual = true );
689 
690  private:
691  void _apply_block( const signed_block& next_block );
692  processed_transaction _apply_transaction( const signed_transaction& trx );
693 
696 
697  const witness_object& validate_block_header( uint32_t skip, const signed_block& next_block )const;
698  const witness_object& _validate_block_header( const signed_block& next_block )const;
699  void verify_signing_witness( const signed_block& new_block, const fork_item& fork_entry )const;
700  void update_witnesses( fork_item& fork_entry )const;
701  void create_block_summary(const signed_block& next_block);
702 
704 
705  protected:
706  void notify_applied_block( const signed_block& block );
707  void notify_on_pending_transaction( const signed_transaction& tx );
708  void notify_changed_objects();
709 
711  public:
712  generic_operation_result process_tickets();
716  void update_bitasset_current_feed( const asset_bitasset_data_object& bitasset,
717  bool skip_median_update = false );
718  private:
719  void update_global_dynamic_data( const signed_block& b, const uint32_t missed_blocks );
720  void update_signing_witness(const witness_object& signing_witness, const signed_block& new_block);
721  void update_last_irreversible_block();
722  void clear_expired_transactions();
723  void clear_expired_proposals();
724  void clear_expired_orders();
725  void clear_expired_force_settlements();
726  void update_expired_feeds();
727  void update_core_exchange_rates();
728  void update_maintenance_flag( bool new_maintenance_flag );
729  void update_withdraw_permissions();
730  void update_credit_offers_and_deals();
731  void clear_expired_htlcs();
732 
735 
737 
738  void initialize_budget_record( fc::time_point_sec now, budget_record& rec )const;
739  void process_budget();
740  void pay_workers( share_type& budget );
741  void perform_chain_maintenance( const signed_block& next_block );
742  void update_active_witnesses();
743  void update_active_committee_members();
744  void update_worker_votes();
745  void process_bids( const asset_bitasset_data_object& bad );
746  void process_bitassets();
747 
748  template<class Type>
749  void perform_account_maintenance( Type tally_helper );
752 
753  vector< processed_transaction > _pending_tx;
754  fork_database _fork_db;
755 
765  block_database _block_id_to_block;
766 
773  vector<optional<operation_history_object> > _applied_ops;
774 
775  public:
777  uint32_t _current_block_num = 0;
778  private:
779  uint16_t _current_trx_in_block = 0;
780  uint16_t _current_op_in_trx = 0;
781  uint32_t _current_virtual_op = 0;
782 
783  vector<uint64_t> _vote_tally_buffer;
784  vector<uint64_t> _witness_count_histogram_buffer;
785  vector<uint64_t> _committee_count_histogram_buffer;
786  std::array<uint64_t,2> _total_voting_stake; // 0=committee, 1=witness,
787  // as in vote_id_type::vote_type
788 
789  flat_map<uint32_t,block_id_type> _checkpoints;
790 
791  node_property_object _node_property_object;
792 
795  bool _track_standby_votes = true;
796 
804  bool _opened = false;
805 
806  // Counts nested proposal updates
807  uint32_t _push_proposal_nesting_depth = 0;
808 
810  flat_set<asset_id_type> _issue_453_affected_assets;
811 
814  const asset_object* _p_core_asset_obj = nullptr;
815  const asset_dynamic_data_object* _p_core_dynamic_data_obj = nullptr;
816  const global_property_object* _p_global_prop_obj = nullptr;
817  const dynamic_global_property_object* _p_dyn_global_prop_obj = nullptr;
818  const chain_property_object* _p_chain_property_obj = nullptr;
819  const witness_schedule_object* _p_witness_schedule_obj = nullptr;
821  public:
823  inline void enable_standby_votes_tracking(bool enable) { _track_standby_votes = enable; }
824  };
825 
826 } }
map< custom_authority_id_type, rejected_predicate > rejected_predicate_map
Definition: transaction.hpp:31
contains properties that only apply to bitassets (market issued assets)
fc::signal< void(const signed_block &)> applied_block
Definition: database.hpp:606
contains all of the parameters necessary to calculate the fee for any operation
This class represents an account on the object graphAccounts are the primary unit of authority on the...
Maintains global state information (committee_member list, current fees)This is an implementation det...
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
An order-perserving dictionary of variant&#39;s.
Definition: api.cpp:48
tracks debt and call price information
fc::static_variant< transfer_operation, limit_order_create_operation, limit_order_cancel_operation, call_order_update_operation, fill_order_operation, account_create_operation, account_update_operation, account_whitelist_operation, account_upgrade_operation, account_transfer_operation, asset_create_operation, asset_update_operation, asset_update_bitasset_operation, asset_update_feed_producers_operation, asset_issue_operation, asset_reserve_operation, asset_fund_fee_pool_operation, asset_settle_operation, asset_global_settle_operation, asset_publish_feed_operation, witness_create_operation, witness_update_operation, proposal_create_operation, proposal_update_operation, proposal_delete_operation, withdraw_permission_create_operation, withdraw_permission_update_operation, withdraw_permission_claim_operation, withdraw_permission_delete_operation, committee_member_create_operation, committee_member_update_operation, committee_member_update_global_parameters_operation, vesting_balance_create_operation, vesting_balance_withdraw_operation, worker_create_operation, custom_operation, assert_operation, balance_claim_operation, override_transfer_operation, transfer_to_blind_operation, blind_transfer_operation, transfer_from_blind_operation, asset_settle_cancel_operation, asset_claim_fees_operation, fba_distribute_operation, bid_collateral_operation, execute_bid_operation, asset_claim_pool_operation, asset_update_issuer_operation, htlc_create_operation, htlc_redeem_operation, htlc_redeemed_operation, htlc_extend_operation, htlc_refund_operation, custom_authority_create_operation, custom_authority_update_operation, custom_authority_delete_operation, ticket_create_operation, ticket_update_operation, liquidity_pool_create_operation, liquidity_pool_delete_operation, liquidity_pool_deposit_operation, liquidity_pool_withdraw_operation, liquidity_pool_exchange_operation, samet_fund_create_operation, samet_fund_delete_operation, samet_fund_update_operation, samet_fund_borrow_operation, samet_fund_repay_operation, credit_offer_create_operation, credit_offer_delete_operation, credit_offer_update_operation, credit_offer_accept_operation, credit_deal_repay_operation, credit_deal_expired_operation >
const flat_map< uint32_t, block_id_type > get_checkpoints() const
Definition: database.hpp:553
fc::signal< void(const signed_transaction &)> on_pending_transaction
Definition: database.hpp:612
Contains per-node database configuration.
std::string to_pretty_string(int64_t)
Definition: string.cpp:26
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:108
maintains a set of indexed objects that can be modified with multi-level rollback support ...
tracks bitassets scheduled for force settlement at some point in the future.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
adds a signature to a transaction
tracks the asset information that changes frequentlyBecause the asset_object is very large it doesn&#39;t...
bids of collateral for debt after a black swan
tracks the parameters of an assetAll assets have a globally unique symbol name that controls how they...
std::deque< precomputable_transaction > _popped_tx
Definition: database.hpp:642
defines market parameters for margin positions
Definition: asset.hpp:160
asset_id_type asset_id
Definition: asset.hpp:37
void enable_standby_votes_tracking(bool enable)
Enable or disable tracking of votes of standby witnesses and committee members.
Definition: database.hpp:823
Maintains global state information (committee_member list, current fees)This is an implementation det...
boost::signals2::signal< T > signal
Definition: signals.hpp:20
base for all database objects
Definition: object.hpp:61
an elliptic curve private key.
Definition: elliptic.hpp:89
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
captures the result of evaluating the operations contained in the transaction
uint8_t block_interval
interval in seconds between blocks
fc::time_point_sec _current_block_time
Definition: database.hpp:776
an offer to sell an amount of an asset at a specified exchange rate by a certain timeThe objects are ...
tracks the approval of a partially approved transaction