BitShares-Core  5.0.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  {
72  public:
74 
75  database();
76  ~database();
77 
79  {
80  skip_nothing = 0,
81  skip_witness_signature = 1 << 0,
82  skip_transaction_signatures = 1 << 1,
83  skip_transaction_dupe_check = 1 << 2,
84  skip_block_size_check = 1 << 4,
85  skip_tapos_check = 1 << 5,
86  // skip_authority_check = 1 << 6, ///< removed because effectively identical to skip_transaction_signatures
87  skip_merkle_check = 1 << 7,
88  skip_assert_evaluation = 1 << 8,
89  skip_undo_history_check = 1 << 9,
90  skip_witness_schedule_check = 1 << 10
91  };
92 
105  void open(
106  const fc::path& data_dir,
107  std::function<genesis_state_type()> genesis_loader,
108  const std::string& db_version );
109 
116  void reindex(fc::path data_dir);
117 
124  void wipe(const fc::path& data_dir, bool include_blocks);
125  void close(bool rewind = true);
126 
128 
133  bool is_known_block( const block_id_type& id )const;
134  bool is_known_transaction( const transaction_id_type& id )const;
135  block_id_type get_block_id_for_num( uint32_t block_num )const;
136  optional<signed_block> fetch_block_by_id( const block_id_type& id )const;
137  optional<signed_block> fetch_block_by_number( uint32_t num )const;
138  const signed_transaction& get_recent_transaction( const transaction_id_type& trx_id )const;
139  std::vector<block_id_type> get_block_ids_on_fork(block_id_type head_of_fork) const;
140 
145  uint32_t witness_participation_rate()const;
146 
147  void add_checkpoints( const flat_map<uint32_t,block_id_type>& checkpts );
148  const flat_map<uint32_t,block_id_type> get_checkpoints()const { return _checkpoints; }
149  bool before_last_checkpoint()const;
150 
151  bool push_block( const signed_block& b, uint32_t skip = skip_nothing );
152  processed_transaction push_transaction( const precomputable_transaction& trx, uint32_t skip = skip_nothing );
153  bool _push_block( const signed_block& b );
154  processed_transaction _push_transaction( const precomputable_transaction& trx );
155 
157  processed_transaction push_proposal( const proposal_object& proposal );
158 
159  signed_block generate_block(
160  const fc::time_point_sec when,
161  witness_id_type witness_id,
162  const fc::ecc::private_key& block_signing_private_key,
163  uint32_t skip
164  );
165  signed_block _generate_block(
166  const fc::time_point_sec when,
167  witness_id_type witness_id,
168  const fc::ecc::private_key& block_signing_private_key
169  );
170 
171  void pop_block();
172  void clear_pending();
173 
183  uint32_t push_applied_operation( const operation& op );
184  void set_applied_operation_result( uint32_t op_id, const operation_result& r );
185  const vector<optional< operation_history_object > >& get_applied_operations()const;
186 
187  string to_pretty_string( const asset& a )const;
188 
198 
204 
209  fc::signal<void(const vector<object_id_type>&, const flat_set<account_id_type>&)> new_objects;
210 
215  fc::signal<void(const vector<object_id_type>&, const flat_set<account_id_type>&)> changed_objects;
216 
220  fc::signal<void(const vector<object_id_type>&, const vector<const object*>&, const flat_set<account_id_type>&)> removed_objects;
221 
223 
238  witness_id_type get_scheduled_witness(uint32_t slot_num)const;
239 
248  fc::time_point_sec get_slot_time(uint32_t slot_num)const;
249 
258  uint32_t get_slot_at_time(fc::time_point_sec when)const;
259 
260  void update_witness_schedule();
261 
263 
264  const chain_id_type& get_chain_id()const;
265  const asset_object& get_core_asset()const;
266  const asset_dynamic_data_object& get_core_dynamic_data()const;
267  const chain_property_object& get_chain_properties()const;
268  const global_property_object& get_global_properties()const;
269  const dynamic_global_property_object& get_dynamic_global_properties()const;
270  const node_property_object& get_node_properties()const;
271  const fee_schedule& current_fee_schedule()const;
272  const account_statistics_object& get_account_stats_by_owner( account_id_type owner )const;
273  const witness_schedule_object& get_witness_schedule_object()const;
274 
275  time_point_sec head_block_time()const;
276  uint32_t head_block_num()const;
277  block_id_type head_block_id()const;
278  witness_id_type head_block_witness()const;
279 
280  decltype( chain_parameters::block_interval ) block_interval( )const;
281 
282  node_property_object& node_properties();
283 
292  vector<authority> get_viable_custom_authorities(
293  account_id_type account, const operation& op,
294  rejected_predicate_map* rejected_authorities = nullptr )const;
295 
296  uint32_t last_non_undoable_block_num() const;
298 
299  void initialize_evaluators();
301  void initialize_indexes();
302  void init_genesis(const genesis_state_type& genesis_state = genesis_state_type());
303 
304  template<typename EvaluatorType>
305  void register_evaluator()
306  {
307  _operation_evaluators[
308  operation::tag<typename EvaluatorType::operation_type>::value].reset( new op_evaluator_impl<EvaluatorType>() );
309  }
310 
312 
319  asset get_balance(account_id_type owner, asset_id_type asset_id)const;
321  asset get_balance(const account_object& owner, const asset_object& asset_obj)const;
322 
328  void adjust_balance(account_id_type account, asset delta);
329 
330  void deposit_market_fee_vesting_balance(const account_id_type &account_id, const asset &delta);
337  asset get_market_fee_vesting_balance(const account_id_type &account_id, const asset_id_type &asset_id);
338 
352  optional< vesting_balance_id_type > deposit_lazy_vesting(
354  share_type amount,
355  uint32_t req_vesting_seconds,
356  vesting_balance_type balance_type,
357  account_id_type req_owner,
358  bool require_vesting );
359 
360  // helper to handle cashback rewards
361  void deposit_cashback(const account_object& acct, share_type amount, bool require_vesting = true);
362  // helper to handle witness pay
363  void deposit_witness_pay(const witness_object& wit, share_type amount);
364 
366 
367  void debug_dump();
368  void apply_debug_updates();
369  void debug_update( const fc::variant_object& update );
370 
372 
374  void globally_settle_asset( const asset_object& bitasset, const price& settle_price );
375  void cancel_settle_order(const force_settlement_object& order, bool create_virtual_op = true);
376  void cancel_limit_order(const limit_order_object& order, bool create_virtual_op = true, bool skip_cancel_fee = false);
377  void revive_bitasset( const asset_object& bitasset );
378  void cancel_bid(const collateral_bid_object& bid, bool create_virtual_op = true);
379  void execute_bid( const collateral_bid_object& bid, share_type debt_covered, share_type collateral_from_fund, const price_feed& current_feed );
380 
381  private:
382  template<typename IndexType>
383  void globally_settle_asset_impl( const asset_object& bitasset,
384  const price& settle_price,
385  const IndexType& call_index );
386 
387  public:
396  bool apply_order_before_hardfork_625(const limit_order_object& new_order_object, bool allow_black_swan = true);
398  bool apply_order(const limit_order_object& new_order_object, bool allow_black_swan = true);
400 
411  int match( const limit_order_object& taker, const limit_order_object& maker, const price& trade_price );
413  /***
414  * @brief Match limit order as taker to a call order as maker
415  * @param taker the order that is removing liquidity from the book
416  * @param maker the order that put liquidity on the book
417  * @param trade_price the price the trade should execute at
418  * @param feed_price the price of the current feed
419  * @param maintenance_collateral_ratio the maintenance collateral ratio
420  * @param maintenance_collateralization the maintenance collateralization
421  * @param call_pays_price price call order pays. Call order may pay more collateral
422  * than limit order takes if call order subject to a margin call fee.
423  * @returns 0 if no orders were matched, 1 if taker was filled, 2 if maker was filled, 3 if both were filled
424  */
425  int match( const limit_order_object& taker, const call_order_object& maker, const price& trade_price,
426  const price& feed_price, const uint16_t maintenance_collateral_ratio,
427  const optional<price>& maintenance_collateralization,
428  const price& call_pays_price);
429  // If separate call_pays_price not provided, assume call pays at trade_price:
430  int match( const limit_order_object& taker, const call_order_object& maker, const price& trade_price,
431  const price& feed_price, const uint16_t maintenance_collateral_ratio,
432  const optional<price>& maintenance_collateralization) {
433  return match(taker, maker, trade_price, feed_price, maintenance_collateral_ratio,
434  maintenance_collateralization, trade_price);
435  }
436 
438 
441  asset match(const call_order_object& call,
442  const force_settlement_object& settle,
443  const price& match_price,
444  asset max_settlement,
445  const price& fill_price);
446 
457  bool fill_limit_order( const limit_order_object& order, const asset& pays, const asset& receives,
458  bool cull_if_small, const price& fill_price, const bool is_maker );
459  /***
460  * @brief attempt to fill a call order
461  * @param order the order
462  * @param pays what the buyer pays for the collateral
463  * @param receives the collateral received by the buyer
464  * @param fill_price the price the transaction executed at
465  * @param is_maker TRUE if the buyer is the maker, FALSE if the buyer is the taker
466  * @param margin_fee Margin call fees paid in collateral asset
467  * @returns TRUE if the order was completely filled
468  */
469  bool fill_call_order( const call_order_object& order, const asset& pays, const asset& receives,
470  const price& fill_price, const bool is_maker, const asset& margin_fee );
471 
472  // Overload provides compatible default value for margin_fee: (margin_fee.asset_id == pays.asset_id)
473  bool fill_call_order( const call_order_object& order, const asset& pays, const asset& receives,
474  const price& fill_price, const bool is_maker )
475  {
476  return fill_call_order( order, pays, receives, fill_price, is_maker, asset(0, pays.asset_id) );
477  }
478 
479  bool fill_settle_order( const force_settlement_object& settle, const asset& pays, const asset& receives,
480  const price& fill_price, const bool is_maker );
481 
482  bool check_call_orders( const asset_object& mia, bool enable_black_swan = true, bool for_new_limit_order = false,
483  const asset_bitasset_data_object* bitasset_ptr = nullptr );
484 
485  // helpers to fill_order
486  void pay_order( const account_object& receiver, const asset& receives, const asset& pays );
487 
494  asset calculate_market_fee( const asset_object& trade_asset, const asset& trade_amount,
495  const bool& is_maker )const;
496  asset pay_market_fees(const account_object* seller, const asset_object& recv_asset, const asset& receives,
497  const bool& is_maker, const optional<asset>& calculated_market_fees = {});
498  asset pay_force_settle_fees(const asset_object& collecting_asset, const asset& collat_receives);
500 
501 
503 
507  processed_transaction validate_transaction( const signed_transaction& trx );
508 
509 
512  std::deque< precomputable_transaction > _popped_tx;
513 
518  inline void enable_standby_votes_tracking(bool enable) { _track_standby_votes = enable; }
520 
530  fc::future<void> precompute_parallel( const signed_block& block, const uint32_t skip = skip_nothing )const;
531 
539  fc::future<void> precompute_parallel( const precomputable_transaction& trx )const;
540  private:
541  template<typename Trx>
542  void _precompute_parallel( const Trx* trx, const size_t count, const uint32_t skip )const;
543 
544  protected:
545  //Mark pop_undo() as protected -- we do not want outside calling pop_undo(); it should call pop_block() instead
547  void notify_applied_block( const signed_block& block );
548  void notify_on_pending_transaction( const signed_transaction& tx );
549  void notify_changed_objects();
550 
551  private:
552  optional<undo_database::session> _pending_tx_session;
553  vector< unique_ptr<op_evaluator> > _operation_evaluators;
554 
555  template<class Index>
556  vector<std::reference_wrapper<const typename Index::object_type>> sort_votable_objects(size_t count)const;
557 
559 
560  public:
561  // these were formerly private, but they have a fairly well-defined API, so let's make them public
562  void apply_block( const signed_block& next_block, uint32_t skip = skip_nothing );
563  processed_transaction apply_transaction( const signed_transaction& trx, uint32_t skip = skip_nothing );
564  operation_result apply_operation( transaction_evaluation_state& eval_state, const operation& op );
565 
566  private:
567  void _apply_block( const signed_block& next_block );
568  processed_transaction _apply_transaction( const signed_transaction& trx );
569  void _cancel_bids_and_revive_mpa( const asset_object& bitasset, const asset_bitasset_data_object& bad );
570 
573 
574  const witness_object& validate_block_header( uint32_t skip, const signed_block& next_block )const;
575  const witness_object& _validate_block_header( const signed_block& next_block )const;
576  void verify_signing_witness( const signed_block& new_block, const fork_item& fork_entry )const;
577  void update_witnesses( fork_item& fork_entry )const;
578  void create_block_summary(const signed_block& next_block);
579 
581 
582  uint32_t update_witness_missed_blocks( const signed_block& b );
583 
585  public:
586  generic_operation_result process_tickets();
587  private:
588  void update_global_dynamic_data( const signed_block& b, const uint32_t missed_blocks );
589  void update_signing_witness(const witness_object& signing_witness, const signed_block& new_block);
590  void update_last_irreversible_block();
591  void clear_expired_transactions();
592  void clear_expired_proposals();
593  void clear_expired_orders();
594  void update_expired_feeds();
595  void update_core_exchange_rates();
596  void update_maintenance_flag( bool new_maintenance_flag );
597  void update_withdraw_permissions();
598  bool check_for_blackswan( const asset_object& mia, bool enable_black_swan = true,
599  const asset_bitasset_data_object* bitasset_ptr = nullptr );
600  void clear_expired_htlcs();
601 
604 
606 
607  void initialize_budget_record( fc::time_point_sec now, budget_record& rec )const;
608  void process_budget();
609  void pay_workers( share_type& budget );
610  void perform_chain_maintenance(const signed_block& next_block, const global_property_object& global_props);
611  void update_active_witnesses();
612  void update_active_committee_members();
613  void update_worker_votes();
614  void process_bids( const asset_bitasset_data_object& bad );
615  void process_bitassets();
616 
617  template<class Type>
618  void perform_account_maintenance( Type tally_helper );
621 
622  vector< processed_transaction > _pending_tx;
623  fork_database _fork_db;
624 
634  block_database _block_id_to_block;
635 
642  vector<optional<operation_history_object> > _applied_ops;
643 
644  uint32_t _current_block_num = 0;
645  uint16_t _current_trx_in_block = 0;
646  uint16_t _current_op_in_trx = 0;
647  uint32_t _current_virtual_op = 0;
648 
649  vector<uint64_t> _vote_tally_buffer;
650  vector<uint64_t> _witness_count_histogram_buffer;
651  vector<uint64_t> _committee_count_histogram_buffer;
652  uint64_t _total_voting_stake[2]; // 0=committee, 1=witness,
653  // as in vote_id_type::vote_type
654 
655  flat_map<uint32_t,block_id_type> _checkpoints;
656 
657  node_property_object _node_property_object;
658 
661  bool _track_standby_votes = true;
662 
670  bool _opened = false;
671 
672  // Counts nested proposal updates
673  uint32_t _push_proposal_nesting_depth = 0;
674 
676  flat_set<asset_id_type> _issue_453_affected_assets;
677 
680  const asset_object* _p_core_asset_obj = nullptr;
681  const asset_dynamic_data_object* _p_core_dynamic_data_obj = nullptr;
682  const global_property_object* _p_global_prop_obj = nullptr;
683  const dynamic_global_property_object* _p_dyn_global_prop_obj = nullptr;
684  const chain_property_object* _p_chain_property_obj = nullptr;
685  const witness_schedule_object* _p_witness_schedule_obj = nullptr;
687  };
688 
689  namespace detail
690  {
691  template<int... Is>
692  struct seq { };
693 
694  template<int N, int... Is>
695  struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
696 
697  template<int... Is>
698  struct gen_seq<0, Is...> : seq<Is...> { };
699 
700  template<typename T, int... Is>
701  void for_each(T&& t, const account_object& a, seq<Is...>)
702  {
703  auto l = { (std::get<Is>(t)(a), 0)... };
704  (void)l;
705  }
706  }
707 
708 } }
Identifies a weighted set of keys and accounts that must approve operations.
Definition: authority.hpp:34
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:197
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:56
tracks debt and call price information
bool fill_call_order(const call_order_object &order, const asset &pays, const asset &receives, const price &fill_price, const bool is_maker)
Definition: database.hpp:473
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 >
fc::signal< void(const signed_transaction &)> on_pending_transaction
Definition: database.hpp:203
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:114
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.
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
Use the Curiously Recurring Template Pattern to automatically add the ability to clone, serialize, and move objects polymorphically.
Definition: object.hpp:86
const flat_map< uint32_t, block_id_type > get_checkpoints() const
Definition: database.hpp:148
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:512
defines market parameters for margin positions
Definition: asset.hpp:164
asset_id_type asset_id
Definition: asset.hpp:39
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:62
an elliptic curve private key.
Definition: elliptic.hpp:89
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
void for_each(T &&t, const account_object &a, seq< Is... >)
Definition: database.hpp:701
captures the result of evaluating the operations contained in the transaction
int match(const limit_order_object &taker, const call_order_object &maker, const price &trade_price, const price &feed_price, const uint16_t maintenance_collateral_ratio, const optional< price > &maintenance_collateralization)
Definition: database.hpp:430
uint8_t block_interval
interval in seconds between blocks
an offer to sell a amount of a asset at a specified exchange rate by a certain timeThis limit_order_o...
tracks the approval of a partially approved transaction