Skip to content

Commit

Permalink
Merge pull request #1672 from bitshares/pr-1669-global-settle-index
Browse files Browse the repository at this point in the history
Use by_collateral when globally settling
  • Loading branch information
abitmore authored Sep 14, 2019
2 parents 76701ec + fe596cf commit c2cafd1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
44 changes: 33 additions & 11 deletions libraries/chain/db_market.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ 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<call_order_index>().indices().get<by_price>() );
}
else
{
globally_settle_asset_impl( mia, settlement_price,
get_index_type<call_order_index>().indices().get<by_collateral>() );
}
}

template<typename IndexType>
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" );
Expand All @@ -65,34 +85,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<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

// 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
}

Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/hardfork.d/CORE_1669.hf
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions libraries/chain/include/graphene/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,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<typename IndexType>
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
Expand Down

0 comments on commit c2cafd1

Please sign in to comment.