Skip to content

Commit

Permalink
Use by_collateral when globally settling after hf
Browse files Browse the repository at this point in the history
We'd like to remove `by_price` index at some time in the future to
improve performance, thus we should avoid using the index after the
hardfork.
#1669
  • Loading branch information
abitmore committed Mar 23, 2019
1 parent 11432f3 commit 5f8549e
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions libraries/chain/db_market.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,61 @@ 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<call_order_index>().indices().get<by_price>();

auto maint_time = get_dynamic_global_properties().next_maintenance_time;
bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME ); // better rounding
bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME ); // call price caching issue

// 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 ) );
const auto& call_index = get_index_type<call_order_index>().indices();
const auto& call_price_index = call_index.get<by_price>();
const auto& call_collateral_index = call_index.get<by_collateral>();

auto call_min = price::min( bitasset.options.short_backing_asset, mia.id );
auto call_max = price::max( bitasset.options.short_backing_asset, mia.id );

auto call_price_itr = call_price_index.begin();
auto call_price_end = call_price_itr;
auto call_collateral_itr = call_collateral_index.begin();
auto call_collateral_end = call_collateral_itr;

if( before_core_hardfork_1270 )
{
call_price_itr = call_price_index.lower_bound( call_min );
call_price_end = call_price_index.upper_bound( call_max );
}
else
{
call_collateral_itr = call_collateral_index.lower_bound( call_min );
call_collateral_end = call_collateral_index.upper_bound( call_max );
}

asset pays;
while( call_itr != call_end )
while( ( before_core_hardfork_1270 && call_price_itr != call_price_end )
|| (!before_core_hardfork_1270 && call_collateral_itr != call_collateral_end ) )
{
const call_order_object& order = ( before_core_hardfork_1270 ? *call_price_itr : *call_collateral_itr );
if( before_core_hardfork_1270 )
++call_price_itr;
else
++call_collateral_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
}

Expand Down

0 comments on commit 5f8549e

Please sign in to comment.