BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
db_getter.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  */
24 
26 
27 #include <graphene/chain/hardfork.hpp>
28 
34 
35 namespace graphene { namespace chain {
36 
38 {
39  return *_p_core_asset_obj;
40 }
41 
43 {
44  return *_p_core_dynamic_data_obj;
45 }
46 
48 {
49  return *_p_global_prop_obj;
50 }
51 
53 {
54  return *_p_chain_property_obj;
55 }
56 
58 {
59  return *_p_dyn_global_prop_obj;
60 }
61 
63 {
65 }
66 
68 {
70 }
71 
72 uint32_t database::head_block_num()const
73 {
75 }
76 
78 {
80 }
81 
82 decltype( chain_parameters::block_interval ) database::block_interval( )const
83 {
85 }
86 
88 {
90 }
91 
93 {
94  return _node_property_object;
95 }
96 
98 {
99  return _node_property_object;
100 }
101 
103  account_id_type account, const operation &op,
104  rejected_predicate_map* rejected_authorities) const
105 {
106  const auto& index = get_index_type<custom_authority_index>().indices().get<by_account_custom>();
107  auto range = index.equal_range(boost::make_tuple(account, unsigned_int(op.which()), true));
108 
109  auto is_valid = [now=head_block_time()](const custom_authority_object& auth) { return auth.is_valid(now); };
110  vector<std::reference_wrapper<const custom_authority_object>> valid_auths;
111  std::copy_if(range.first, range.second, std::back_inserter(valid_auths), is_valid);
112 
113  vector<authority> results;
114  for (const auto& cust_auth : valid_auths) {
115  try {
116  auto result = cust_auth.get().get_predicate()(op);
117  if (result.success)
118  results.emplace_back(cust_auth.get().auth);
119  else if (rejected_authorities != nullptr)
120  rejected_authorities->insert(std::make_pair(cust_auth.get().get_id(), std::move(result)));
121  } catch (fc::exception& e) {
122  if (rejected_authorities != nullptr)
123  rejected_authorities->insert(std::make_pair(cust_auth.get().get_id(), std::move(e)));
124  }
125  }
126 
127  return results;
128 }
129 
131 {
132  //see https://github.com/bitshares/bitshares-core/issues/377
133  /*
134  There is a case when a value of undo_db.size() is greater then head_block_num(),
135  and as result we get a wrong value for last_non_undoable_block_num.
136  To resolve it we should take into account a number of active_sessions in calculations of
137  last_non_undoable_block_num (active sessions are related to a new block which is under generation).
138  */
139  return head_block_num() - ( _undo_db.size() - _undo_db.active_sessions() );
140 }
141 
143 {
144  return account_statistics_id_type(owner.instance)(*this);
145 }
146 
148 {
149  return *_p_witness_schedule_obj;
150 }
151 
152 const limit_order_object* database::find_settled_debt_order( const asset_id_type& a )const
153 {
154  const auto& limit_index = get_index_type<limit_order_index>().indices().get<by_is_settled_debt>();
155  auto itr = limit_index.lower_bound( std::make_tuple( true, a ) );
156  if( itr != limit_index.end() && itr->receive_asset_id() == a )
157  return &(*itr);
158  return nullptr;
159 }
160 
162  bool force_by_collateral_index )const
163 {
164  bool find_by_collateral = true;
165  if( !force_by_collateral_index )
166  // core-1270 hard fork : call price caching issue
167  find_by_collateral = ( get_dynamic_global_properties().next_maintenance_time > HARDFORK_CORE_1270_TIME );
168 
169  const call_order_object* call_ptr = nullptr; // place holder
170 
171  auto call_min = price::min( bitasset.options.short_backing_asset, bitasset.asset_id );
172 
173  if( !find_by_collateral ) // before core-1270 hard fork, check with call_price
174  {
175  const auto& call_price_index = get_index_type<call_order_index>().indices().get<by_price>();
176  auto call_itr = call_price_index.lower_bound( call_min );
177  if( call_itr != call_price_index.end() ) // found a call order
178  call_ptr = &(*call_itr);
179  }
180  else // after core-1270 hard fork, check with collateralization
181  {
182  // Note: it is safe to check here even if there is no call order due to individual settlements
183  const auto& call_collateral_index = get_index_type<call_order_index>().indices().get<by_collateral>();
184  auto call_itr = call_collateral_index.lower_bound( call_min );
185  if( call_itr != call_collateral_index.end() ) // found a call order
186  call_ptr = &(*call_itr);
187  }
188  if( !call_ptr ) // not found
189  return nullptr;
190  if( call_ptr->debt_type() != bitasset.asset_id ) // call order is of another asset
191  return nullptr;
192  return call_ptr;
193 }
194 
195 } }
graphene::db::undo_database::size
std::size_t size() const
Definition: undo_database.hpp:120
graphene::chain::dynamic_global_property_object
Maintains global state information (committee_member list, current fees)
Definition: global_property_object.hpp:62
graphene::chain::database::find_settled_debt_order
const limit_order_object * find_settled_debt_order(const asset_id_type &a) const
Definition: db_getter.cpp:152
graphene::chain::database::get_chain_id
const chain_id_type & get_chain_id() const
Definition: db_getter.cpp:87
graphene::protocol::rejected_predicate_map
map< custom_authority_id_type, rejected_predicate > rejected_predicate_map
Definition: transaction.hpp:31
fc::exception
Used to generate a useful error report when an exception is thrown.
Definition: exception.hpp:56
graphene::chain::dynamic_global_property_object::time
time_point_sec time
Definition: global_property_object.hpp:68
graphene::chain::database::head_block_time
time_point_sec head_block_time() const
Definition: db_getter.cpp:67
asset_object.hpp
graphene::protocol::fee_schedule
contains all of the parameters necessary to calculate the fee for any operation
Definition: fee_schedule.hpp:173
database.hpp
graphene::chain::dynamic_global_property_object::next_maintenance_time
time_point_sec next_maintenance_time
Definition: global_property_object.hpp:70
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, liquidity_pool_update_operation, credit_deal_update_operation, limit_order_update_operation >
graphene::chain::global_property_object::parameters
chain_parameters parameters
Definition: global_property_object.hpp:44
custom_authority_object.hpp
chain_property_object.hpp
graphene::chain::database::get_viable_custom_authorities
vector< authority > get_viable_custom_authorities(account_id_type account, const operation &op, rejected_predicate_map *rejected_authorities=nullptr) const
Get a list of custom authorities which can validate the provided operation for the provided account.
Definition: db_getter.cpp:102
graphene::chain::dynamic_global_property_object::head_block_id
block_id_type head_block_id
Definition: global_property_object.hpp:67
fc::sha256
Definition: sha256.hpp:10
graphene::chain::asset_object
tracks the parameters of an asset
Definition: asset_object.hpp:75
graphene::chain::custom_authority_object
Tracks account custom authorities.
Definition: custom_authority_object.hpp:39
graphene::chain::database::get_core_asset
const asset_object & get_core_asset() const
Definition: db_getter.cpp:37
graphene::protocol::chain_parameters::get_current_fees
const fee_schedule & get_current_fees() const
Definition: chain_parameters.hpp:50
graphene::chain::call_order_object::debt_type
asset_id_type debt_type() const
Definition: market_object.hpp:146
graphene::chain::database::head_block_id
block_id_type head_block_id() const
Definition: db_getter.cpp:77
graphene::chain::account_statistics_object
Definition: account_object.hpp:46
graphene::chain::database::get_node_properties
const node_property_object & get_node_properties() const
Definition: db_getter.cpp:92
graphene::chain::dynamic_global_property_object::head_block_number
uint32_t head_block_number
Definition: global_property_object.hpp:66
graphene::chain::chain_property_object::chain_id
chain_id_type chain_id
Definition: chain_property_object.hpp:37
graphene::db::index::get
const object & get(object_id_type id) const
Definition: index.hpp:110
graphene::chain::database::current_fee_schedule
const fee_schedule & current_fee_schedule() const
Definition: db_getter.cpp:62
graphene::chain::asset_dynamic_data_object
tracks the asset information that changes frequently
Definition: asset_object.hpp:56
graphene::chain::database::get_core_dynamic_data
const asset_dynamic_data_object & get_core_dynamic_data() const
Definition: db_getter.cpp:42
graphene::db::undo_database::active_sessions
uint32_t active_sessions() const
Definition: undo_database.hpp:123
fc::unsigned_int
Definition: varint.hpp:6
fc::time_point_sec
Definition: time.hpp:74
fc::ripemd160
Definition: ripemd160.hpp:11
graphene::chain::node_property_object
Contains per-node database configuration.
Definition: node_property_object.hpp:39
graphene::chain::asset_bitasset_data_object
contains properties that only apply to bitassets (market issued assets)
Definition: asset_object.hpp:255
graphene::chain::database::get_chain_properties
const chain_property_object & get_chain_properties() const
Definition: db_getter.cpp:52
graphene::chain::database::head_block_num
uint32_t head_block_num() const
Definition: db_getter.cpp:72
graphene::chain::database::last_non_undoable_block_num
uint32_t last_non_undoable_block_num() const
Definition: db_getter.cpp:130
graphene::chain::database::node_properties
node_property_object & node_properties()
Definition: db_getter.cpp:97
fc::static_variant::which
tag_type which() const
Definition: static_variant.hpp:329
graphene::chain::global_property_object
Maintains global state information (committee_member list, current fees)
Definition: global_property_object.hpp:40
graphene::chain::call_order_object
tracks debt and call price information
Definition: market_object.hpp:140
graphene::chain::witness_schedule_object
Definition: witness_schedule_object.hpp:30
graphene::chain::database::get_witness_schedule_object
const witness_schedule_object & get_witness_schedule_object() const
Definition: db_getter.cpp:147
graphene::chain::database::get_account_stats_by_owner
const account_statistics_object & get_account_stats_by_owner(account_id_type owner) const
Definition: db_getter.cpp:142
graphene::protocol::price::min
price min() const
Definition: asset.hpp:125
graphene::chain::database::get_global_properties
const global_property_object & get_global_properties() const
Definition: db_getter.cpp:47
graphene::chain::chain_property_object
Definition: chain_property_object.hpp:33
market_object.hpp
graphene::chain::database::get_dynamic_global_properties
const dynamic_global_property_object & get_dynamic_global_properties() const
Definition: db_getter.cpp:57
graphene::chain::database::find_least_collateralized_short
const call_order_object * find_least_collateralized_short(const asset_bitasset_data_object &bitasset, bool force_by_collateral_index) const
Definition: db_getter.cpp:161
graphene::db::index
abstract base class for accessing objects indexed in various ways.
Definition: index.hpp:70
global_property_object.hpp
graphene::chain::limit_order_object
an offer to sell an amount of an asset at a specified exchange rate by a certain time
Definition: market_object.hpp:45
graphene::protocol::bitasset_options::short_backing_asset
asset_id_type short_backing_asset
Definition: asset_ops.hpp:171
graphene
Definition: api.cpp:48
graphene::chain::asset_bitasset_data_object::options
bitasset_options options
The tunable options for BitAssets are stored in this field.
Definition: asset_object.hpp:263
graphene::protocol::chain_parameters::block_interval
uint8_t block_interval
interval in seconds between blocks
Definition: chain_parameters.hpp:53
graphene::db::object_database::_undo_db
undo_database _undo_db
Definition: object_database.hpp:170
graphene::chain::asset_bitasset_data_object::asset_id
asset_id_type asset_id
The asset this object belong to.
Definition: asset_object.hpp:260