From 97423812a1ace4a7e2e2a47549c03d46f88cbd42 Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Thu, 8 Oct 2015 15:49:56 -0400 Subject: [PATCH] db_init.cpp: Check for imbalanced assets --- libraries/chain/db_init.cpp | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index 1bd467f8..60d8acac 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -440,16 +440,20 @@ void database::init_genesis(const genesis_state_type& genesis_state) }; map total_supplies; + map total_debts; // Create initial assets for( const genesis_state_type::initial_asset_type& asset : genesis_state.initial_assets ) { asset_id_type new_asset_id = get_index_type().get_next_id(); + total_supplies[ new_asset_id ] = 0; + asset_dynamic_data_id_type dynamic_data_id; optional bitasset_data_id; if( asset.is_bitasset ) { int collateral_holder_number = 0; + total_debts[ new_asset_id ] = 0; for( const auto& collateral_rec : asset.collateral_records ) { account_create_operation cop; @@ -474,6 +478,7 @@ void database::init_genesis(const genesis_state_type& genesis_state) }); total_supplies[ 0 ] += collateral_rec.collateral; + total_debts[ new_asset_id ] += collateral_rec.debt; ++collateral_holder_number; } @@ -542,6 +547,33 @@ void database::init_genesis(const genesis_state_type& genesis_state) total_supplies[ 0 ] = GRAPHENE_MAX_SHARE_SUPPLY; } + const auto& idx = get_index_type().indices().get(); + auto it = idx.begin(); + bool has_imbalanced_assets = false; + + while( it != idx.end() ) + { + if( it->bitasset_data_id.valid() ) + { + auto supply_itr = total_supplies.find( it->id ); + auto debt_itr = total_debts.find( it->id ); + FC_ASSERT( supply_itr != total_supplies.end() ); + FC_ASSERT( debt_itr != total_debts.end() ); + if( supply_itr->second != debt_itr->second ) + { + has_imbalanced_assets = true; + elog( "Genesis for asset ${aname} is not balanced\n" + " Debt is ${debt}\n" + " Supply is ${supply}\n", + ("debt", debt_itr->second) + ("supply", supply_itr->second) + ); + } + } + ++it; + } + FC_ASSERT( !has_imbalanced_assets ); + // Save tallied supplies for( const auto& item : total_supplies ) { @@ -555,8 +587,6 @@ void database::init_genesis(const genesis_state_type& genesis_state) } ); } - // TODO: Assert that bitasset debt = supply - // Create special witness account const witness_object& wit = create([&](witness_object& w) {}); FC_ASSERT( wit.id == GRAPHENE_NULL_WITNESS );