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

Fix tests #1126

Merged
merged 5 commits into from
Jul 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"zero collateral with nonzero debt" is covered by this test case:

BOOST_TEST_MESSAGE( "attempting to claim all collateral without paying off debt" );
GRAPHENE_REQUIRE_THROW( cover( dan, bitusd.amount(0), asset(20000)), fc::exception );

price::call_price() will generate a division-by-zero exception, then the exception will be captured and converted to fc::exception, then be caught by callers. So I think this change is more an optimization rather than a fix. No?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect. The test fails, because boost captures the division by zero exception, as it occurs inside the modify functor (line 257), triggering the object to be deleted. As per boost docs, this is expected behavior:

https://www.boost.org/doc/libs/1_67_0/libs/multi_index/doc/reference/ord_indices.html#modify
Exception safety: Basic. If an exception is thrown by some user-provided operation (including mod), then the element pointed to by position is erased.


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 a index constraint was violated");
}

virtual void remove( const object& obj )override
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ if(MSVC)
set_source_files_properties( tests/serialization_tests.cpp PROPERTIES COMPILE_FLAGS "/bigobj" )
endif(MSVC)

file(GLOB SLOW_UNIT_TESTS "slow_tests/*.cpp")
add_executable( slow_chain_test ${SLOW_UNIT_TESTS} ${COMMON_SOURCES} )
target_link_libraries( slow_chain_test graphene_chain graphene_app graphene_account_history graphene_elasticsearch graphene_egenesis_none fc graphene_wallet ${PLATFORM_SPECIFIC_LIBS} )

file(GLOB PERFORMANCE_TESTS "performance/*.cpp")
add_executable( performance_test ${PERFORMANCE_TESTS} ${COMMON_SOURCES} )
target_link_libraries( performance_test graphene_chain graphene_app graphene_account_history graphene_elasticsearch graphene_egenesis_none fc ${PLATFORM_SPECIFIC_LIBS} )
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions tests/slow_tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <cstdlib>
#include <iostream>
#include <boost/test/included/unit_test.hpp>

extern uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP;

boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
std::srand(time(NULL));
std::cout << "Random number generator seeded to " << time(NULL) << std::endl;
const char* genesis_timestamp_str = getenv("GRAPHENE_TESTING_GENESIS_TIMESTAMP");
if( genesis_timestamp_str != nullptr )
{
GRAPHENE_TESTING_GENESIS_TIMESTAMP = std::stoul( genesis_timestamp_str );
}
std::cout << "GRAPHENE_TESTING_GENESIS_TIMESTAMP is " << GRAPHENE_TESTING_GENESIS_TIMESTAMP << std::endl;
return nullptr;
}
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
1 change: 1 addition & 0 deletions tests/tests/operation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ BOOST_AUTO_TEST_CASE( call_order_update_test )
GRAPHENE_REQUIRE_THROW( borrow( dan, bitusd.amount(80000), asset(0)), 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 );
GRAPHENE_REQUIRE_THROW( cover( dan, bitusd.amount(0), asset(20000-1)), fc::exception );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this new test for? Actually it will throw an exception due to triggering a blackswan which is not allowed when updating a call position. It worth testing, but better add a message above it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The underlying implementation now explicitly checks for zero collateral. This test was added to probe the edge case that collateral is not zero, but still too low to sustain the debt. I feel it is sufficiently described by the message, "attempting to claim all collateral without paying off debt"; even though it leaves one unit of collateral unclaimed, the spirit is the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 and 1 are absolutely different.


borrow( sam, bitusd.amount(1000), asset(10000));
transfer( sam, dan, bitusd.amount(1000) );
Expand Down