Merge branch '436-fork-feed-protect' into develop

This commit is contained in:
theoreticalbts 2015-12-07 15:12:17 -05:00
commit ac267393fc
3 changed files with 38 additions and 26 deletions

View file

@ -23,6 +23,7 @@
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/asset_object.hpp>
#include <graphene/chain/hardfork.hpp>
#include <graphene/chain/market_evaluator.hpp>
#include <fc/uint128.hpp>
@ -416,15 +417,8 @@ bool database::check_call_orders(const asset_object& mia, bool enable_black_swan
auto limit_itr = limit_price_index.lower_bound( max_price );
auto limit_end = limit_price_index.upper_bound( min_price );
if( limit_itr == limit_end ) {
/*
if( head_block_num() > 300000 )
ilog( "no orders below between: ${p} and: ${m}",
("p", bitasset.current_feed.max_short_squeeze_price())
("m", max_price) );
*/
if( limit_itr == limit_end )
return false;
}
auto call_min = price::min( bitasset.options.short_backing_asset, mia.id );
auto call_max = price::max( bitasset.options.short_backing_asset, mia.id );
@ -434,14 +428,6 @@ bool database::check_call_orders(const asset_object& mia, bool enable_black_swan
bool filled_limit = false;
bool margin_called = false;
/*
if( head_block_num() >= 11510 && head_block_num() <= 11512) {
idump(("enter loop") );
auto tmp = call_itr;
while( tmp != call_end ) { edump( (*tmp) ); ++tmp; }
}
*/
while( !check_for_blackswan( mia, enable_black_swan ) && call_itr != call_end )
{
bool filled_call = false;
@ -457,9 +443,22 @@ bool database::check_call_orders(const asset_object& mia, bool enable_black_swan
match_price.validate();
// would be margin called, but there is no matching order #436
bool feed_protected = ( bitasset.current_feed.settlement_price > ~call_itr->call_price );
if( feed_protected && (head_block_time() > HARDFORK_436_TIME) )
return margin_called;
// would be margin called, but there is no matching order
if( match_price > ~call_itr->call_price )
return margin_called;
if( feed_protected )
{
ilog( "Feed protected margin call executing (HARDFORK_436_TIME not here yet)" );
idump( (*call_itr) );
idump( (*limit_itr) );
}
// idump((*call_itr));
// idump((*limit_itr));

View file

@ -27,3 +27,4 @@
#define HARDFORK_415_TIME (fc::time_point_sec( 1446652800 ))
#define HARDFORK_416_TIME (fc::time_point_sec( 1446652800 ))
#define HARDFORK_419_TIME (fc::time_point_sec( 1446652800 ))
#define HARDFORK_436_TIME (fc::time_point_sec( 2000000000 ))

View file

@ -23,6 +23,7 @@
#include <graphene/chain/database.hpp>
#include <graphene/chain/exceptions.hpp>
#include <graphene/chain/hardfork.hpp>
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/asset_object.hpp>
@ -183,23 +184,34 @@ BOOST_AUTO_TEST_CASE( margin_call_limit_test )
borrow( borrower, bitusd.amount(1000), asset(2000));
borrow( borrower2, bitusd.amount(1000), asset(4000) );
BOOST_REQUIRE_EQUAL( get_balance( borrower, bitusd ), 1000 );
BOOST_REQUIRE_EQUAL( get_balance( borrower2, bitusd ), 1000 );
BOOST_REQUIRE_EQUAL( get_balance( borrower , core ), init_balance - 2000 );
BOOST_REQUIRE_EQUAL( get_balance( borrower2, core ), init_balance - 4000 );
BOOST_CHECK_EQUAL( get_balance( borrower, bitusd ), 1000 );
BOOST_CHECK_EQUAL( get_balance( borrower2, bitusd ), 1000 );
BOOST_CHECK_EQUAL( get_balance( borrower , core ), init_balance - 2000 );
BOOST_CHECK_EQUAL( get_balance( borrower2, core ), init_balance - 4000 );
// this should trigger margin call that is below the call limit, but above the
// protection threshold.
BOOST_TEST_MESSAGE( "Creating a margin call that is NOT protected by the max short squeeze price" );
auto order = create_sell_order( borrower2, bitusd.amount(1000), core.amount(1400) );
BOOST_REQUIRE( order == nullptr );
if( db.head_block_time() <= HARDFORK_436_TIME )
{
BOOST_CHECK( order == nullptr );
BOOST_REQUIRE_EQUAL( get_balance( borrower2, core ), init_balance - 4000 + 1400 );
BOOST_REQUIRE_EQUAL( get_balance( borrower2, bitusd ), 0 );
BOOST_CHECK_EQUAL( get_balance( borrower2, core ), init_balance - 4000 + 1400 );
BOOST_CHECK_EQUAL( get_balance( borrower2, bitusd ), 0 );
BOOST_REQUIRE_EQUAL( get_balance( borrower, core ), init_balance - 2000 + 600 );
BOOST_REQUIRE_EQUAL( get_balance( borrower, bitusd ), 1000 );
BOOST_CHECK_EQUAL( get_balance( borrower, core ), init_balance - 2000 + 600 );
BOOST_CHECK_EQUAL( get_balance( borrower, bitusd ), 1000 );
}
else
{
BOOST_CHECK( order != nullptr );
BOOST_CHECK_EQUAL( get_balance( borrower, bitusd ), 1000 );
BOOST_CHECK_EQUAL( get_balance( borrower2, bitusd ), 0 );
BOOST_CHECK_EQUAL( get_balance( borrower , core ), init_balance - 2000 );
BOOST_CHECK_EQUAL( get_balance( borrower2, core ), init_balance - 4000 );
}
BOOST_TEST_MESSAGE( "Creating a margin call that is protected by the max short squeeze price" );
borrow( borrower, bitusd.amount(1000), asset(2000) );
@ -207,7 +219,7 @@ BOOST_AUTO_TEST_CASE( margin_call_limit_test )
// this should trigger margin call without protection from the price feed.
order = create_sell_order( borrower2, bitusd.amount(1000), core.amount(1800) );
BOOST_REQUIRE( order != nullptr );
BOOST_CHECK( order != nullptr );
} catch( const fc::exception& e) {
edump((e.to_detail_string()));
throw;