Skip to content

Commit

Permalink
Merge pull request #1161 from bitshares/852-boost166-rebase
Browse files Browse the repository at this point in the history
Fix boost 1.66 compatibility
  • Loading branch information
abitmore authored Jul 30, 2018
2 parents 09f2d0d + 0e68ee4 commit 308ff52
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
5 changes: 5 additions & 0 deletions libraries/chain/market_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ void_result call_order_update_evaluator::do_apply(const call_order_update_operat
call_obj = &*itr;
old_collateralization = call_obj->collateralization();
old_debt = call_obj->debt;
auto new_collateral = call_obj->collateral + o.delta_collateral.amount;
auto new_debt = *old_debt + o.delta_debt.amount;

// Forbid zero collateral with nonzero debt (avoids divide by zero when calculating call price below)
FC_ASSERT(!(new_collateral == 0 && new_debt != 0), "Cannot have zero collateral and nonzero debt");

d.modify( *call_obj, [&]( call_order_object& call ){
call.collateral += o.delta_collateral.amount;
Expand Down
23 changes: 19 additions & 4 deletions libraries/db/include/graphene/db/generic_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,25 @@ namespace graphene { namespace chain {

virtual void modify( const object& obj, const std::function<void(object&)>& m )override
{
assert( nullptr != dynamic_cast<const ObjectType*>(&obj) );
auto ok = _indices.modify( _indices.iterator_to( static_cast<const ObjectType&>(obj) ),
[&m]( ObjectType& o ){ m(o); } );
FC_ASSERT( ok, "Could not modify object, most likely a index constraint was violated" );
assert(nullptr != dynamic_cast<const ObjectType*>(&obj));
std::exception_ptr exc;
auto ok = _indices.modify(_indices.iterator_to(static_cast<const ObjectType&>(obj)),
[&m, &exc](ObjectType& o) mutable {
try {
m(o);
} catch (fc::exception e) {
exc = std::current_exception();
elog("Exception while modifying object: ${e} -- object may be corrupted",
("e", e));
} catch (...) {
exc = std::current_exception();
elog("Unknown exception while modifying object");
}
}
);
if (exc)
std::rethrow_exception(exc);
FC_ASSERT(ok, "Could not modify object, most likely an index constraint was violated");
}

virtual void remove( const object& obj )override
Expand Down
26 changes: 26 additions & 0 deletions tests/tests/database_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ BOOST_AUTO_TEST_CASE( undo_test )
}
}

/**
* Check that database modify() functors that throw do not get caught by boost, which will remove the object
*/
BOOST_AUTO_TEST_CASE(failed_modify_test)
{ try {
database db;
// Create dummy object
const auto& obj = db.create<account_balance_object>([](account_balance_object& obj) {
obj.owner = account_id_type(123);
});
account_balance_id_type obj_id = obj.id;
BOOST_CHECK_EQUAL(obj.owner.instance.value, 123);

// Modify dummy object, check that changes stick
db.modify(obj, [](account_balance_object& obj) {
obj.owner = account_id_type(234);
});
BOOST_CHECK_EQUAL(obj_id(db).owner.instance.value, 234);

// Throw exception when modifying object, check that object still exists after
BOOST_CHECK_THROW(db.modify(obj, [](account_balance_object& obj) {
throw 5;
}), int);
BOOST_CHECK_NE((long)db.find_object(obj_id), (long)nullptr);
} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_CASE( flat_index_test )
{ try {
ACTORS((sam));
Expand Down
8 changes: 6 additions & 2 deletions tests/tests/operation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ BOOST_AUTO_TEST_CASE( call_order_update_test )
BOOST_REQUIRE_EQUAL( get_balance( dan, bitusd ), 5000 );
BOOST_REQUIRE_EQUAL( get_balance( dan, core ), 10000000 - 20000 );

BOOST_TEST_MESSAGE( "increasing debt without increasing collateral again" );
BOOST_TEST_MESSAGE( "increasing debt a lot without increasing collateral, fails due to black swan" );
GRAPHENE_REQUIRE_THROW( borrow( dan, bitusd.amount(80000), asset(0)), fc::exception );
BOOST_TEST_MESSAGE( "attempting to claim most of collateral without paying off debt, fails due to black swan" );
GRAPHENE_REQUIRE_THROW( cover( dan, bitusd.amount(0), asset(20000-1)), fc::exception );
BOOST_TEST_MESSAGE( "attempting to claim all collateral without paying off debt" );
GRAPHENE_REQUIRE_THROW( cover( dan, bitusd.amount(0), asset(20000)), fc::exception );

Expand Down Expand Up @@ -243,8 +245,10 @@ BOOST_AUTO_TEST_CASE( old_call_order_update_test_after_hardfork_583 )
BOOST_REQUIRE_EQUAL( get_balance( dan, bitusd ), 5000 );
BOOST_REQUIRE_EQUAL( get_balance( dan, core ), 10000000 - 20000 );

BOOST_TEST_MESSAGE( "increasing debt without increasing collateral again" );
BOOST_TEST_MESSAGE( "increasing debt a lot without increasing collateral, fails due to black swan" );
GRAPHENE_REQUIRE_THROW( borrow( dan, bitusd.amount(80000), asset(0)), fc::exception );
BOOST_TEST_MESSAGE( "attempting to claim most of collateral without paying off debt, fails due to black swan" );
GRAPHENE_REQUIRE_THROW( cover( dan, bitusd.amount(0), asset(20000-1)), fc::exception );
BOOST_TEST_MESSAGE( "attempting to claim all collateral without paying off debt" );
GRAPHENE_REQUIRE_THROW( cover( dan, bitusd.amount(0), asset(20000)), fc::exception );

Expand Down

0 comments on commit 308ff52

Please # to comment.