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

check max_supply before borrowing MPAs #1498

Merged
merged 9 commits into from
Jan 25, 2019
3 changes: 3 additions & 0 deletions libraries/chain/market_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ void_result call_order_update_evaluator::do_evaluate(const call_order_update_ope
FC_ASSERT( _debt_asset->is_market_issued(), "Unable to cover ${sym} as it is not a collateralized asset.",
("sym", _debt_asset->symbol) );

FC_ASSERT( _debt_asset->dynamic_data(d).current_supply + o.delta_debt.amount <= _debt_asset->options.max_supply,
"Borrowing this quantity would exceed MAX_SUPPLY" );

_bitasset_data = &_debt_asset->bitasset_data(d);

/// if there is a settlement for this asset, then no further margin positions may be taken and
Expand Down
2 changes: 1 addition & 1 deletion tests/common/database_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ const asset_object& database_fixture::create_bitasset(
creator.issuer = issuer;
creator.fee = asset();
creator.symbol = name;
creator.common_options.max_supply = GRAPHENE_MAX_SHARE_SUPPLY;
creator.common_options.max_supply = GRAPHENE_MAX_SHARE_SUPPLY / 2;
pmconrad marked this conversation as resolved.
Show resolved Hide resolved
creator.precision = precision;
creator.common_options.market_fee_percent = market_fee_percent;
if( issuer == GRAPHENE_WITNESS_ACCOUNT )
Expand Down
68 changes: 67 additions & 1 deletion tests/tests/operation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ BOOST_AUTO_TEST_CASE( call_order_update_validation_test )

op.extensions.value.target_collateral_ratio = 65535;
op.validate(); // still valid

}

// Tests that target_cr option can't be set before hard fork core-834
Expand Down Expand Up @@ -2004,6 +2003,73 @@ BOOST_AUTO_TEST_CASE( reserve_asset_test )
}
}

BOOST_AUTO_TEST_CASE( call_order_update_evaluator_test )
{
try
{
ACTORS( (alice) (bob) );
transfer(committee_account, alice_id, asset(1000000 * GRAPHENE_BLOCKCHAIN_PRECISION));

const auto& bitusd = create_bitasset( "USDBIT", alice_id );
const auto& core = asset_id_type()(db);

BOOST_TEST_MESSAGE( "Setting price feed to $100000 / 1" );
update_feed_producers( bitusd, {alice_id} );
price_feed current_feed;
current_feed.settlement_price = bitusd.amount( 100000 ) / core.amount(1);
publish_feed( bitusd, alice, current_feed );

{
BOOST_TEST_MESSAGE( "Attempting a call_order_update that exceeds max_supply" );
call_order_update_operation op;
op.funding_account = alice_id;
op.delta_collateral = asset( 1000000 * GRAPHENE_BLOCKCHAIN_PRECISION );
op.delta_debt = asset( bitusd.options.max_supply + 1, bitusd.id );
transaction tx;
tx.operations.push_back( op );
set_expiration( db, tx );
GRAPHENE_REQUIRE_THROW(PUSH_TX( db, tx, database::skip_tapos_check | database::skip_transaction_signatures ), fc::exception );
}

{
BOOST_TEST_MESSAGE( "Creating 2 bitusd and transferring to bob (increases current supply)" );
call_order_update_operation op;
op.funding_account = alice_id;
op.delta_collateral = asset( 100 * GRAPHENE_BLOCKCHAIN_PRECISION );
op.delta_debt = asset( 2, bitusd.id );
transaction tx;
tx.operations.push_back( op );
set_expiration( db, tx );
PUSH_TX( db, tx, database::skip_tapos_check | database::skip_transaction_signatures );
transfer( alice, bob, asset( 2, bitusd.id ) );
}

{
BOOST_TEST_MESSAGE( "Again attempting a call_order_update_operation that is max_supply - 1 (should throw)" );
call_order_update_operation op;
op.funding_account = alice_id;
op.delta_collateral = asset( 100000 * GRAPHENE_BLOCKCHAIN_PRECISION );
op.delta_debt = asset( bitusd.options.max_supply - 1, bitusd.id );
transaction tx;
tx.operations.push_back( op );
set_expiration( db, tx );
GRAPHENE_REQUIRE_THROW(PUSH_TX( db, tx, database::skip_tapos_check | database::skip_transaction_signatures ), fc::exception);
}

{
BOOST_TEST_MESSAGE( "Again attempting a call_order_update_operation that equals max_supply (should work)" );
call_order_update_operation op;
op.funding_account = alice_id;
op.delta_collateral = asset( 100000 * GRAPHENE_BLOCKCHAIN_PRECISION );
op.delta_debt = asset( bitusd.options.max_supply - 2, bitusd.id );
transaction tx;
tx.operations.push_back( op );
set_expiration( db, tx );
PUSH_TX( db, tx, database::skip_tapos_check | database::skip_transaction_signatures );
}
} FC_LOG_AND_RETHROW()
}

/**
* This test demonstrates how using the call_order_update_operation to
* trigger a margin call is legal if there is a matching order.
Expand Down