diff --git a/libraries/chain/db_market.cpp b/libraries/chain/db_market.cpp index 3d42f9abc9..4af3c6c810 100644 --- a/libraries/chain/db_market.cpp +++ b/libraries/chain/db_market.cpp @@ -52,6 +52,26 @@ namespace graphene { namespace chain { namespace detail { * No more asset updates may be issued. */ void database::globally_settle_asset( const asset_object& mia, const price& settlement_price ) +{ + auto maint_time = get_dynamic_global_properties().next_maintenance_time; + bool before_core_hardfork_1669 = ( maint_time <= HARDFORK_CORE_1669_TIME ); // whether to use call_price + + if( before_core_hardfork_1669 ) + { + globally_settle_asset_impl( mia, settlement_price, + get_index_type().indices().get() ); + } + else + { + globally_settle_asset_impl( mia, settlement_price, + get_index_type().indices().get() ); + } +} + +template +void database::globally_settle_asset_impl( const asset_object& mia, + const price& settlement_price, + const IndexType& call_index ) { try { const asset_bitasset_data_object& bitasset = mia.bitasset_data(*this); FC_ASSERT( !bitasset.has_settlement(), "black swan already occurred, it should not happen again" ); @@ -62,34 +82,36 @@ void database::globally_settle_asset( const asset_object& mia, const price& sett const asset_dynamic_data_object& mia_dyn = mia.dynamic_asset_data_id(*this); auto original_mia_supply = mia_dyn.current_supply; - const auto& call_price_index = get_index_type().indices().get(); - auto maint_time = get_dynamic_global_properties().next_maintenance_time; bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME ); // better rounding // cancel all call orders and accumulate it into collateral_gathered - auto call_itr = call_price_index.lower_bound( price::min( bitasset.options.short_backing_asset, mia.id ) ); - auto call_end = call_price_index.upper_bound( price::max( bitasset.options.short_backing_asset, mia.id ) ); + auto call_itr = call_index.lower_bound( price::min( bitasset.options.short_backing_asset, mia.id ) ); + auto call_end = call_index.upper_bound( price::max( bitasset.options.short_backing_asset, mia.id ) ); + asset pays; while( call_itr != call_end ) { + const call_order_object& order = *call_itr; + ++call_itr; + if( before_core_hardfork_342 ) { - pays = call_itr->get_debt() * settlement_price; // round down, in favor of call order + pays = order.get_debt() * settlement_price; // round down, in favor of call order // Be here, the call order can be paying nothing if( pays.amount == 0 && !bitasset.is_prediction_market ) // TODO remove this warning after hard fork core-342 - wlog( "Something for nothing issue (#184, variant E) occurred at block #${block}", ("block",head_block_num()) ); + wlog( "Something for nothing issue (#184, variant E) occurred at block #${block}", + ("block",head_block_num()) ); } else - pays = call_itr->get_debt().multiply_and_round_up( settlement_price ); // round up, in favor of global settlement fund + pays = order.get_debt().multiply_and_round_up( settlement_price ); // round up in favor of global-settle fund - if( pays > call_itr->get_collateral() ) - pays = call_itr->get_collateral(); + if( pays > order.get_collateral() ) + pays = order.get_collateral(); collateral_gathered += pays; - const auto& order = *call_itr; - ++call_itr; + FC_ASSERT( fill_call_order( order, pays, order.get_debt(), settlement_price, true ) ); // call order is maker } diff --git a/libraries/chain/hardfork.d/CORE_1669.hf b/libraries/chain/hardfork.d/CORE_1669.hf new file mode 100644 index 0000000000..e185ed52ee --- /dev/null +++ b/libraries/chain/hardfork.d/CORE_1669.hf @@ -0,0 +1,4 @@ +// bitshares-core issue #1669 Stop using call_price when globally settling +#ifndef HARDFORK_CORE_1669_TIME +#define HARDFORK_CORE_1669_TIME (fc::time_point_sec( 1600000000 )) // a temporary date in the future +#endif diff --git a/libraries/chain/include/graphene/chain/database.hpp b/libraries/chain/include/graphene/chain/database.hpp index 437c50d8dd..241d9906b5 100644 --- a/libraries/chain/include/graphene/chain/database.hpp +++ b/libraries/chain/include/graphene/chain/database.hpp @@ -353,6 +353,13 @@ namespace graphene { namespace chain { void cancel_bid(const collateral_bid_object& bid, bool create_virtual_op = true); void execute_bid( const collateral_bid_object& bid, share_type debt_covered, share_type collateral_from_fund, const price_feed& current_feed ); + private: + template + void globally_settle_asset_impl( const asset_object& bitasset, + const price& settle_price, + const IndexType& call_index ); + + public: /** * @brief Process a new limit order through the markets * @param order The new order to process