BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
db_debug.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 
34 
35 namespace graphene { namespace chain {
36 
42 {
43  const auto& db = *this;
44  const asset_dynamic_data_object& core_asset_data = db.get_core_asset().dynamic_asset_data_id(db);
45 
46  const auto& balance_index = db.get_index_type<account_balance_index>().indices();
47  const auto& statistics_index = db.get_index_type<account_stats_index>().indices();
48  const auto& bids = db.get_index_type<collateral_bid_index>().indices();
49  const auto& settle_index = db.get_index_type<force_settlement_index>().indices();
50  const auto& htlcs = db.get_index_type<htlc_index>().indices();
51  map<asset_id_type,share_type> total_balances;
52  map<asset_id_type,share_type> total_debts;
53  share_type core_in_orders;
54  share_type reported_core_in_orders;
55 
56  for( const account_balance_object& a : balance_index )
57  {
58  // idump(("balance")(a));
59  total_balances[a.asset_type] += a.balance;
60  }
61  for( const force_settlement_object& s : settle_index )
62  {
63  total_balances[s.balance.asset_id] += s.balance.amount;
64  }
65  for( const vesting_balance_object& vbo : db.get_index_type< vesting_balance_index >().indices() )
66  total_balances[ vbo.balance.asset_id ] += vbo.balance.amount;
67  for( const fba_accumulator_object& fba : db.get_index_type< simple_index< fba_accumulator_object > >() )
68  total_balances[ asset_id_type() ] += fba.accumulated_fba_fees;
69  for( const account_statistics_object& s : statistics_index )
70  {
71  // idump(("statistics")(s));
72  reported_core_in_orders += s.total_core_in_orders;
73  }
74  for( const collateral_bid_object& b : bids )
75  total_balances[b.inv_swan_price.base.asset_id] += b.inv_swan_price.base.amount;
76  for( const limit_order_object& o : db.get_index_type<limit_order_index>().indices() )
77  {
78  // idump(("limit_order")(o));
79  auto for_sale = o.amount_for_sale();
80  if( for_sale.asset_id == asset_id_type() ) core_in_orders += for_sale.amount;
81  total_balances[for_sale.asset_id] += for_sale.amount;
82  }
83  for( const call_order_object& o : db.get_index_type<call_order_index>().indices() )
84  {
85 // idump(("call_order")(o));
86  auto col = o.get_collateral();
87  if( col.asset_id == asset_id_type() ) core_in_orders += col.amount;
88  total_balances[col.asset_id] += col.amount;
89  total_debts[o.get_debt().asset_id] += o.get_debt().amount;
90  }
91  for( const asset_object& asset_obj : db.get_index_type<asset_index>().indices() )
92  {
93  total_balances[asset_obj.id] += asset_obj.dynamic_asset_data_id(db).accumulated_fees;
94  total_balances[asset_id_type()] += asset_obj.dynamic_asset_data_id(db).fee_pool;
95 // edump((total_balances[asset_obj.id])(asset_obj.dynamic_asset_data_id(db).current_supply ) );
96  }
97  for( const auto& htlc : htlcs )
98  total_balances[htlc.transfer.asset_id] += htlc.transfer.amount;
99 
100  if( total_balances[asset_id_type()].value != core_asset_data.current_supply.value )
101  {
102  FC_THROW( "computed balance of CORE mismatch",
103  ("computed value",total_balances[asset_id_type()].value)
104  ("current supply",core_asset_data.current_supply.value) );
105  }
106 
107 
108  /*
109  const auto& vbidx = db.get_index_type<simple_index<vesting_balance_object>>();
110  for( const auto& s : vbidx )
111  {
112 // idump(("vesting_balance")(s));
113  }
114  */
115 }
116 
118 {
119  static const uint8_t
120  db_action_nil = 0,
121  db_action_create = 1,
122  db_action_write = 2,
123  db_action_update = 3,
124  db_action_delete = 4;
125 
126  // "_action" : "create" object must not exist, unspecified fields take defaults
127  // "_action" : "write" object may exist, is replaced entirely, unspecified fields take defaults
128  // "_action" : "update" object must exist, unspecified fields don't change
129  // "_action" : "delete" object must exist, will be deleted
130 
131  // if _action is unspecified:
132  // - delete if object contains only ID field
133  // - otherwise, write
134 
135  object_id_type oid;
136  uint8_t action = db_action_nil;
137  auto it_id = vo.find("id");
138  FC_ASSERT( it_id != vo.end() );
139 
140  from_variant( it_id->value(), oid );
141  action = ( vo.size() == 1 ) ? db_action_delete : db_action_write;
142 
143  from_variant( vo["id"], oid );
144  if( vo.size() == 1 )
145  action = db_action_delete;
146  auto it_action = vo.find("_action" );
147  if( it_action != vo.end() )
148  {
149  const std::string& str_action = it_action->value().get_string();
150  if( str_action == "create" )
151  action = db_action_create;
152  else if( str_action == "write" )
153  action = db_action_write;
154  else if( str_action == "update" )
155  action = db_action_update;
156  else if( str_action == "delete" )
157  action = db_action_delete;
158  }
159 
160  auto& idx = db.get_index( oid );
161 
162  switch( action )
163  {
164  case db_action_create:
165  FC_ASSERT( false );
166  break;
167  case db_action_write:
168  db.modify( db.get_object( oid ), [&]( object& obj )
169  {
170  idx.object_default( obj );
171  idx.object_from_variant( vo, obj, GRAPHENE_MAX_NESTED_OBJECTS );
172  } );
173  break;
174  case db_action_update:
175  db.modify( db.get_object( oid ), [&]( object& obj )
176  {
177  idx.object_from_variant( vo, obj, GRAPHENE_MAX_NESTED_OBJECTS );
178  } );
179  break;
180  case db_action_delete:
181  db.remove( db.get_object( oid ) );
182  break;
183  default:
184  FC_ASSERT( false );
185  }
186 }
187 
189 {
190  block_id_type head_id = head_block_id();
191  auto it = _node_property_object.debug_updates.find( head_id );
192  if( it == _node_property_object.debug_updates.end() )
193  return;
194  for( const fc::variant_object& update : it->second )
195  debug_apply_update( *this, update );
196 }
197 
199 {
200  block_id_type head_id = head_block_id();
201  auto it = _node_property_object.debug_updates.find( head_id );
202  if( it == _node_property_object.debug_updates.end() )
203  it = _node_property_object.debug_updates.emplace( head_id, std::vector< fc::variant_object >() ).first;
204  it->second.emplace_back( update );
205 
206  optional<signed_block> head_block = fetch_block_by_id( head_id );
207  FC_ASSERT( head_block.valid() );
208 
209  // What the last block does has been changed by adding to node_property_object, so we have to re-apply it
210  pop_block();
211  push_block( *head_block );
212 }
213 
214 } }
Tracks the balance of a single account/asset pairThis object is indexed on owner and asset_type so th...
void modify(const T &obj, const Lambda &m)
iterator find(const string &key) const
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:31
A simple index uses a vector<unique_ptr<T>> to store data.
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
An order-perserving dictionary of variant&#39;s.
Definition: api.cpp:56
void debug_apply_update(database &db, const fc::variant_object &vo)
Definition: db_debug.cpp:117
tracks debt and call price information
bool valid() const
Definition: optional.hpp:186
#define FC_THROW(...)
Definition: exception.hpp:366
bool push_block(const signed_block &b, uint32_t skip=skip_nothing)
Definition: db_block.cpp:116
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
std::map< block_id_type, std::vector< fc::variant_object > > debug_updates
const index & get_index() const
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
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...
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
iterator end() const
void remove(const object &obj)
share_type current_supply
The number of shares currently in existence.
block_id_type head_block_id() const
Definition: db_getter.cpp:74
void debug_update(const fc::variant_object &update)
Definition: db_debug.cpp:198
const object & get_object(object_id_type id) const
T value
Definition: safe.hpp:22
size_t size() const
an offer to sell a amount of a asset at a specified exchange rate by a certain timeThis limit_order_o...
optional< signed_block > fetch_block_by_id(const block_id_type &id) const
Definition: db_block.cpp:67
const index_type & indices() const