Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use by_collateral when globally settling #1672

Merged
merged 5 commits into from
Sep 14, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 )
pmconrad marked this conversation as resolved.
Show resolved Hide resolved
{
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