From 9699be84e6f9436c0fd443eb2f9c12fabc64b47e Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 9 Jul 2015 11:40:37 -0400 Subject: [PATCH] Progress on #144 --- libraries/chain/db_balance.cpp | 8 +++++++- .../chain/include/graphene/chain/database.hpp | 2 ++ .../graphene/chain/protocol/fee_schedule.hpp | 2 +- libraries/chain/protocol/account.cpp | 3 ++- libraries/chain/protocol/fee_schedule.cpp | 2 +- libraries/chain/protocol/operations.cpp | 2 +- .../chain/transaction_evaluation_state.cpp | 1 - tests/tests/authority_tests.cpp | 3 ++- tests/tests/fee_tests.cpp | 20 +++++++++++-------- tests/tests/operation_tests.cpp | 4 ++-- tests/tests/uia_tests.cpp | 16 +++++++++++++-- 11 files changed, 44 insertions(+), 19 deletions(-) diff --git a/libraries/chain/db_balance.cpp b/libraries/chain/db_balance.cpp index 1b7e03cd..9fab9fb3 100644 --- a/libraries/chain/db_balance.cpp +++ b/libraries/chain/db_balance.cpp @@ -38,6 +38,11 @@ asset database::get_balance(const account_object& owner, const asset_object& ass return get_balance(owner.get_id(), asset_obj.get_id()); } +string database::to_pretty_string( const asset& a )const +{ + return a.asset_id(*this).amount_to_pretty_string(a.amount); +} + void database::adjust_balance(account_id_type account, asset delta ) { try { if( delta.amount == 0 ) @@ -54,7 +59,8 @@ void database::adjust_balance(account_id_type account, asset delta ) b.balance = delta.amount.value; }); } else { - FC_ASSERT(delta.amount > 0 || itr->get_balance() >= -delta); + if( delta.amount < 0 ) + FC_ASSERT( itr->get_balance() >= -delta, "Insufficient Balance: ${b} is less than required ${r}", ("b",to_pretty_string(itr->get_balance()))("r",to_pretty_string(-delta))); modify(*itr, [delta](account_balance_object& b) { b.adjust_balance(delta); }); diff --git a/libraries/chain/include/graphene/chain/database.hpp b/libraries/chain/include/graphene/chain/database.hpp index 1efc7eee..1483bc89 100644 --- a/libraries/chain/include/graphene/chain/database.hpp +++ b/libraries/chain/include/graphene/chain/database.hpp @@ -172,6 +172,8 @@ namespace graphene { namespace chain { void set_applied_operation_result( uint32_t op_id, const operation_result& r ); const vector& get_applied_operations()const; + string to_pretty_string( const asset& a )const; + /** * This signal is emitted after all operations and virtual operation for a * block have been applied but before the get_applied_operations() are cleared. diff --git a/libraries/chain/include/graphene/chain/protocol/fee_schedule.hpp b/libraries/chain/include/graphene/chain/protocol/fee_schedule.hpp index ba3e59f2..67570998 100644 --- a/libraries/chain/include/graphene/chain/protocol/fee_schedule.hpp +++ b/libraries/chain/include/graphene/chain/protocol/fee_schedule.hpp @@ -46,7 +46,7 @@ namespace graphene { namespace chain { * @note must be sorted by fee_parameters.which() and have no duplicates */ flat_set parameters; - uint32_t scale; ///< fee * scale / GRAPHENE_100_PERCENT + uint32_t scale = GRAPHENE_100_PERCENT; ///< fee * scale / GRAPHENE_100_PERCENT }; typedef fee_schedule fee_schedule_type; diff --git a/libraries/chain/protocol/account.cpp b/libraries/chain/protocol/account.cpp index 2d1e4e80..d8d9100f 100644 --- a/libraries/chain/protocol/account.cpp +++ b/libraries/chain/protocol/account.cpp @@ -125,7 +125,8 @@ share_type account_create_operation::calculate_fee( const fee_parameters_type& k core_fee_required = k.premium_fee; // Authorities and vote lists can be arbitrarily large, so charge a data fee for big ones - core_fee_required += calculate_data_fee( fc::raw::pack_size(*this), k.price_per_kbyte ); + auto data_fee = calculate_data_fee( fc::raw::pack_size(*this), k.price_per_kbyte ); + core_fee_required += data_fee; return core_fee_required; } diff --git a/libraries/chain/protocol/fee_schedule.cpp b/libraries/chain/protocol/fee_schedule.cpp index d5633a74..8636691f 100644 --- a/libraries/chain/protocol/fee_schedule.cpp +++ b/libraries/chain/protocol/fee_schedule.cpp @@ -81,6 +81,7 @@ namespace graphene { namespace chain { *this = get_default(); for( fee_parameters& i : parameters ) i.visit( zero_fee_visitor() ); + this->scale = 0; } asset fee_schedule::calculate_fee( const operation& op, const price& core_exchange_rate )const @@ -89,7 +90,6 @@ namespace graphene { namespace chain { fee_parameters params; params.set_which(op.which()); auto itr = parameters.find(params); if( itr != parameters.end() ) params = *itr; - //idump( (params) ); auto base_value = op.visit( calc_fee_visitor( params ) ); auto scaled = fc::uint128(base_value) * scale; scaled /= GRAPHENE_100_PERCENT; diff --git a/libraries/chain/protocol/operations.cpp b/libraries/chain/protocol/operations.cpp index 168b3b7b..7dba1c8d 100644 --- a/libraries/chain/protocol/operations.cpp +++ b/libraries/chain/protocol/operations.cpp @@ -189,7 +189,7 @@ void operation_get_required_authorities( const operation& op, flat_set& owner, vector& other ) { - + op.visit( operation_get_required_auth( active, owner, other ) ); } } } // namespace graphene::chain diff --git a/libraries/chain/transaction_evaluation_state.cpp b/libraries/chain/transaction_evaluation_state.cpp index 870d3771..7e8a6eed 100644 --- a/libraries/chain/transaction_evaluation_state.cpp +++ b/libraries/chain/transaction_evaluation_state.cpp @@ -54,7 +54,6 @@ namespace graphene { namespace chain { bool transaction_evaluation_state::check_authority( const authority& au, authority::classification auth_class, int depth ) { try { - if( (!_is_proposed_trx) && (_db->get_node_properties().skip_flags & database::skip_authority_check) ) return true; if( (!_is_proposed_trx) && (_db->get_node_properties().skip_flags & database::skip_transaction_signatures) ) diff --git a/tests/tests/authority_tests.cpp b/tests/tests/authority_tests.cpp index 6f0c1320..a3321b38 100644 --- a/tests/tests/authority_tests.cpp +++ b/tests/tests/authority_tests.cpp @@ -329,7 +329,7 @@ BOOST_AUTO_TEST_CASE( proposed_single_account ) active_set.clear(); other.clear(); - operation_get_required_authorities(op,active_set,owner_set,other); + operation_get_required_authorities(op.proposed_ops.front().op,active_set,owner_set,other); BOOST_CHECK_EQUAL(active_set.size(), 1); BOOST_CHECK_EQUAL(owner_set.size(), 0); BOOST_CHECK_EQUAL(other.size(), 0); @@ -351,6 +351,7 @@ BOOST_AUTO_TEST_CASE( proposed_single_account ) proposal_update_operation pup; pup.proposal = proposal.id; + pup.fee_paying_account = nathan.id; pup.active_approvals_to_add.insert(nathan.id); trx.operations = {pup}; diff --git a/tests/tests/fee_tests.cpp b/tests/tests/fee_tests.cpp index a8e5033b..44af38f5 100644 --- a/tests/tests/fee_tests.cpp +++ b/tests/tests/fee_tests.cpp @@ -26,8 +26,11 @@ using namespace graphene::chain; BOOST_FIXTURE_TEST_SUITE( fee_tests, database_fixture ) +#define CORE uint64_t(GRAPHENE_BLOCKCHAIN_PRECISION) + BOOST_AUTO_TEST_CASE( cashback_test ) { try { + wlog(""); /* Account Structure used in this test * * * * /-----------------\ /-------------------\ * @@ -56,8 +59,8 @@ BOOST_AUTO_TEST_CASE( cashback_test ) PREP_ACTOR(dumy); PREP_ACTOR(stud); PREP_ACTOR(pleb); - transfer(account_id_type(), life_id, asset(100000000)); - transfer(account_id_type(), reggie_id, asset(100000000)); + transfer(account_id_type(), life_id, asset(500000*CORE)); + transfer(account_id_type(), reggie_id, asset(500000*CORE)); upgrade_to_lifetime_member(life_id); upgrade_to_lifetime_member(reggie_id); enable_fees(); @@ -75,6 +78,7 @@ BOOST_AUTO_TEST_CASE( cashback_test ) op.active = authority(1, public_key_type(actor_name ## _private_key.get_public_key()), 1); \ op.owner = op.active; \ op.fee = fees->calculate_fee(op); \ + idump((op.fee)); \ trx.operations = {op}; \ trx.sign( registrar_name ## _private_key); \ actor_name ## _id = PUSH_TX( db, trx ).operation_results.front().get(); \ @@ -83,13 +87,13 @@ BOOST_AUTO_TEST_CASE( cashback_test ) CustomRegisterActor(ann, life, life, 75); - transfer(life_id, ann_id, asset(1000000)); + transfer(life_id, ann_id, asset(3000*CORE)); upgrade_to_annual_member(ann_id); CustomRegisterActor(dumy, reggie, life, 75); CustomRegisterActor(stud, reggie, ann, 80); - transfer(life_id, stud_id, asset(1000000)); + transfer(life_id, stud_id, asset(20000*CORE)); upgrade_to_lifetime_member(stud_id); CustomRegisterActor(pleb, reggie, stud, 95); @@ -103,10 +107,10 @@ BOOST_AUTO_TEST_CASE( cashback_test ) BOOST_CHECK_EQUAL(ann_id(db).cashback_balance(db).balance.amount.value, 40000); BOOST_CHECK_EQUAL(stud_id(db).cashback_balance(db).balance.amount.value, 8000); - transfer(stud_id, scud_id, asset(500000)); - transfer(scud_id, pleb_id, asset(400000)); - transfer(pleb_id, dumy_id, asset(300000)); - transfer(dumy_id, reggie_id, asset(200000)); + transfer(stud_id, scud_id, asset(5*CORE)); + transfer(scud_id, pleb_id, asset(4*CORE)); + transfer(pleb_id, dumy_id, asset(3*CORE)); + transfer(dumy_id, reggie_id, asset(2*CORE)); generate_blocks(db.get_dynamic_global_properties().next_maintenance_time); diff --git a/tests/tests/operation_tests.cpp b/tests/tests/operation_tests.cpp index a4b0347e..204efc6a 100644 --- a/tests/tests/operation_tests.cpp +++ b/tests/tests/operation_tests.cpp @@ -1128,10 +1128,10 @@ BOOST_AUTO_TEST_CASE( witness_withdraw_pay_test ) ) >> GRAPHENE_CORE_ASSET_CYCLE_RATE_BITS ; // change this if ref_budget changes - BOOST_CHECK_EQUAL( ref_budget, 624 ); + BOOST_CHECK_EQUAL( ref_budget, 594 ); const uint64_t witness_ppb = ref_budget * 10 / 23 + 1; // change this if ref_budget changes - BOOST_CHECK_EQUAL( witness_ppb, 272 ); + BOOST_CHECK_EQUAL( witness_ppb, 259 ); // following two inequalities need to hold for maximal code coverage BOOST_CHECK_LT( witness_ppb * 2, ref_budget ); BOOST_CHECK_GT( witness_ppb * 3, ref_budget ); diff --git a/tests/tests/uia_tests.cpp b/tests/tests/uia_tests.cpp index f835198b..0b499251 100644 --- a/tests/tests/uia_tests.cpp +++ b/tests/tests/uia_tests.cpp @@ -179,6 +179,7 @@ BOOST_AUTO_TEST_CASE( transfer_whitelist_uia ) upgrade_to_lifetime_member(dan); trx.clear(); + BOOST_TEST_MESSAGE( "Atempting to transfer asset ADVANCED from nathan to dan when dan is not whitelisted, should fail" ); transfer_operation op; op.fee = advanced.amount(0); op.from = nathan.id; @@ -188,39 +189,48 @@ BOOST_AUTO_TEST_CASE( transfer_whitelist_uia ) //Fail because dan is not whitelisted. GRAPHENE_REQUIRE_THROW(PUSH_TX( db, trx, ~0 ), transfer_to_account_not_whitelisted ); + BOOST_TEST_MESSAGE( "Adding dan to whitelist for asset ADVANCED" ); account_whitelist_operation wop; wop.authorizing_account = account_id_type(); wop.account_to_list = dan.id; wop.new_listing = account_whitelist_operation::white_listed; trx.operations.back() = wop; PUSH_TX( db, trx, ~0 ); + BOOST_TEST_MESSAGE( "Attempting to trnsfer from nathan to dan after whitelisting dan, should succeed" ); trx.operations.back() = op; PUSH_TX( db, trx, ~0 ); BOOST_CHECK_EQUAL(get_balance(nathan, advanced), 900); BOOST_CHECK_EQUAL(get_balance(dan, advanced), 100); + BOOST_TEST_MESSAGE( "Attempting to blacklist nathan" ); wop.new_listing |= account_whitelist_operation::black_listed; wop.account_to_list = nathan.id; trx.operations.back() = wop; PUSH_TX( db, trx, ~0 ); + BOOST_TEST_MESSAGE( "Attempting to transfer from nathan after blacklisting, should fail" ); op.amount = advanced.amount(50); trx.operations.back() = op; //Fail because nathan is blacklisted GRAPHENE_REQUIRE_THROW(PUSH_TX( db, trx, ~0 ), transfer_from_account_not_whitelisted ); + + + BOOST_TEST_MESSAGE( "Attempting to burn from nathan after blacklisting, should fail" ); asset_reserve_operation burn; burn.payer = nathan.id; burn.amount_to_reserve = advanced.amount(10); - trx.operations.emplace_back(burn); + trx.operations.back() = burn; //Fail because nathan is blacklisted GRAPHENE_REQUIRE_THROW(PUSH_TX( db, trx, ~0 ), fc::exception); + BOOST_TEST_MESSAGE( "Attempting transfer from dan back to nathan, should fail because nathan is blacklisted" ); std::swap(op.from, op.to); trx.operations.back() = op; //Fail because nathan is blacklisted GRAPHENE_REQUIRE_THROW(PUSH_TX( db, trx, ~0 ), fc::exception); { + BOOST_TEST_MESSAGE( "Changing the blacklist authority to dan" ); asset_update_operation op; op.asset_to_update = advanced.id; op.new_options = advanced.options; @@ -231,11 +241,13 @@ BOOST_AUTO_TEST_CASE( transfer_whitelist_uia ) BOOST_CHECK(advanced.options.blacklist_authorities.find(dan.id) != advanced.options.blacklist_authorities.end()); } + BOOST_TEST_MESSAGE( "Attempting to transfer from dan back to nathan" ); trx.operations.back() = op; PUSH_TX( db, trx, ~0 ); BOOST_CHECK_EQUAL(get_balance(nathan, advanced), 950); BOOST_CHECK_EQUAL(get_balance(dan, advanced), 50); + BOOST_TEST_MESSAGE( "Blacklisting nathan by dan" ); wop.authorizing_account = dan.id; wop.account_to_list = nathan.id; wop.new_listing = account_whitelist_operation::black_listed; @@ -266,7 +278,7 @@ BOOST_AUTO_TEST_CASE( transfer_whitelist_uia ) burn.payer = dan.id; burn.amount_to_reserve = advanced.amount(10); - trx.operations.emplace_back(burn); + trx.operations.back() = burn; PUSH_TX(db, trx, ~0); BOOST_CHECK_EQUAL(get_balance(dan, advanced), 40); } catch(fc::exception& e) {