From e47b55c7c4cbdf5619b41d52443c638a2e500624 Mon Sep 17 00:00:00 2001 From: Anton Perkov Date: Fri, 2 Mar 2018 18:11:52 -0500 Subject: [PATCH] unit-test for system contract (failing) --- contracts/eosio.system/delegate_bandwith.hpp | 24 +++---- contracts/eosiolib/singleton.hpp | 2 +- tests/wasm_tests/eosio.system_tests.cpp | 70 +++++++++++++++++++- 3 files changed, 80 insertions(+), 16 deletions(-) diff --git a/contracts/eosio.system/delegate_bandwith.hpp b/contracts/eosio.system/delegate_bandwith.hpp index 633d6cd0f..7066cc271 100644 --- a/contracts/eosio.system/delegate_bandwith.hpp +++ b/contracts/eosio.system/delegate_bandwith.hpp @@ -110,9 +110,6 @@ namespace eosiosystem { require_auth( del.from ); - del_bandwidth_index_type del_index( SystemAccount, del.from ); - total_resources_index_type total_index( SystemAccount, del.receiver ); - //eosio_assert( is_account( del.receiver ), "can only delegate resources to an existing account" ); auto parameters = eosio_parameters_singleton::exists() ? eosio_parameters_singleton::get() @@ -130,9 +127,9 @@ namespace eosiosystem { eosio_assert( 0 < storage_bytes, "stake is too small to increase storage even by 1 byte" ); - print( "delegatebw: from = ", del.from, " receiver = ", del.receiver, "\n" ); + del_bandwidth_index_type del_index( SystemAccount, del.from ); auto itr = del_index.find( del.receiver); - if( itr != nullptr ) { + if( itr == nullptr ) { del_index.emplace( del.from, [&]( auto& dbo ){ dbo.from = del.from; dbo.to = del.receiver; @@ -151,6 +148,7 @@ namespace eosiosystem { }); } + total_resources_index_type total_index( SystemAccount, del.receiver ); auto tot_itr = total_index.find( del.receiver ); if( tot_itr == nullptr ) { tot_itr = &total_index.emplace( del.from, [&]( auto& tot ) { @@ -184,22 +182,19 @@ namespace eosiosystem { require_auth( del.from ); - del_bandwidth_index_type del_index( SystemAccount, del.from ); - total_resources_index_type total_index( SystemAccount, del.receiver ); - //eosio_assert( is_account( del.receiver ), "can only delegate resources to an existing account" ); - print ("undelegatebw: from = ", del.from, " receiver = ", del.receiver, "\n"); + del_bandwidth_index_type del_index( SystemAccount, del.from ); const auto& dbw = del_index.get( del.receiver ); - eosio_assert( dbw.net_weight >= del.unstake_net_quantity, "insufficient staked net bandwidth" ); eosio_assert( dbw.cpu_weight >= del.unstake_cpu_quantity, "insufficient staked cpu bandwidth" ); + eosio_assert( dbw.storage_bytes >= del.unstake_storage_bytes, "insufficient staked storage" ); - const auto& totals = total_index.get( del.receiver ); - system_token_type storage_stake_decrease = totals.storage_stake * del.unstake_storage_bytes / totals.storage_bytes; + system_token_type storage_stake_decrease = dbw.storage_stake * del.unstake_storage_bytes / dbw.storage_bytes; auto total_refund = system_token_type(del.unstake_cpu_quantity) + system_token_type(del.unstake_net_quantity) + storage_stake_decrease; - //eosio_assert( total_refund.quantity >= 0, "must unstake a positive amount" ); + + eosio_assert( total_refund.quantity >= 0, "must unstake a positive amount" ); del_index.update( dbw, del.from, [&]( auto& dbo ){ dbo.net_weight -= del.unstake_net_quantity; @@ -208,6 +203,8 @@ namespace eosiosystem { dbo.storage_bytes -= del.unstake_storage_bytes; }); + total_resources_index_type total_index( SystemAccount, del.receiver ); + const auto& totals = total_index.get( del.receiver ); total_index.update( totals, 0, [&]( auto& tot ) { tot.net_weight -= del.unstake_net_quantity; tot.cpu_weight -= del.unstake_cpu_quantity; @@ -218,7 +215,6 @@ namespace eosiosystem { set_resource_limits( totals.owner, totals.storage_bytes, totals.net_weight.quantity, totals.cpu_weight.quantity, 0 ); /// TODO: implement / enforce time delays on withdrawing - print( "undelegatebw: ", total_refund.quantity, "\n" ); currency::inline_transfer( SystemAccount, del.from, asset( static_cast( total_refund.quantity )), "unstake bandwidth" ); auto parameters = eosio_parameters_singleton::get(); diff --git a/contracts/eosiolib/singleton.hpp b/contracts/eosiolib/singleton.hpp index 64e73c923..12d085ceb 100644 --- a/contracts/eosiolib/singleton.hpp +++ b/contracts/eosiolib/singleton.hpp @@ -20,7 +20,7 @@ namespace eosio { uint64_t primary_key() const { return pk_value; } - EOSLIB_SERIALIZE( row, (value) ); + EOSLIB_SERIALIZE( row, (value) ) }; typedef eosio::multi_index table; diff --git a/tests/wasm_tests/eosio.system_tests.cpp b/tests/wasm_tests/eosio.system_tests.cpp index 55ed10267..703d2aa9f 100644 --- a/tests/wasm_tests/eosio.system_tests.cpp +++ b/tests/wasm_tests/eosio.system_tests.cpp @@ -105,6 +105,43 @@ BOOST_FIXTURE_TEST_CASE( delegate_to_myself, eosio_system_tester ) try { BOOST_REQUIRE_EQUAL( asset::from_string("400.0000 EOS"), get_balance( "alice" ) ); + //trying to unstake more net bandwith than at stake + BOOST_REQUIRE_EQUAL( error("condition: assertion failed: insufficient staked net bandwidth"), + push_action( N(alice), N(undelegatebw), mvo() + ("from", "alice") + ("receiver", "alice") + ("unstake_net", "200.0001 EOS") + ("unstake_cpu", "0.0000 EOS") + ("unstake_bytes", 0) ) + ); + + //trying to unstake more cpu bandwith than at stake + BOOST_REQUIRE_EQUAL( error("condition: assertion failed: insufficient staked cpu bandwidth"), + push_action(N(alice), N(undelegatebw), mvo() + ("from", "alice") + ("receiver", "alice") + ("unstake_net", "000.0000 EOS") + ("unstake_cpu", "100.0001 EOS") + ("unstake_bytes", 0) ) + ); + + //trying to unstake more storage than at stake + BOOST_REQUIRE_EQUAL( error("condition: assertion failed: insufficient staked storage"), + push_action(N(alice), N(undelegatebw), mvo() + ("from", "alice") + ("receiver", "alice") + ("unstake_net", "000.0000 EOS") + ("unstake_cpu", "000.0001 EOS") + ("unstake_bytes", bytes+1) ) + ); + + //check that nothing has changed + stake = get_total_stake( "alice" ); + BOOST_REQUIRE_EQUAL( asset::from_string("200.0000 EOS").amount, stake["net_weight"].as_uint64()); + BOOST_REQUIRE_EQUAL( asset::from_string("100.0000 EOS").amount, stake["cpu_weight"].as_uint64()); + BOOST_REQUIRE_EQUAL( asset::from_string("300.0000 EOS").amount, stake["storage_stake"].as_uint64()); + BOOST_REQUIRE_EQUAL( bytes, stake["storage_bytes"].as_uint64() ); + push_action(N(alice), N(undelegatebw), mvo() ("from", "alice") ("receiver", "alice") @@ -117,8 +154,39 @@ BOOST_FIXTURE_TEST_CASE( delegate_to_myself, eosio_system_tester ) try { std::cout << "STAKE: " << stake["net_weight"].as_uint64() << ' ' << stake["cpu_weight"].as_uint64() << std::endl; BOOST_REQUIRE_EQUAL( asset::from_string("0.0000 EOS").amount, stake["net_weight"].as_uint64()); BOOST_REQUIRE_EQUAL( asset::from_string("0.0000 EOS").amount, stake["cpu_weight"].as_uint64()); - //BOOST_REQUIRE_EQUAL( asset::from_string("0.0000 EOS").amount, stake["storage_stake"].as_uint64()); + BOOST_REQUIRE_EQUAL( 0, stake["storage_stake"].as_uint64()); + +} FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( stake_negative, eosio_system_tester ) try { + issue( "alice", "1000.0000 EOS", config::system_account_name ); + BOOST_REQUIRE_EQUAL( error("condition: assertion failed: must stake a positive amount"), + push_action( N(alice), N(delegatebw), mvo() + ("from", "alice") + ("receiver", "alice") + ("stake_net", "-0.0001 EOS") + ("stake_cpu", "0.0000 EOS") + ("stake_storage", "0.0000 EOS") ) + ); + + BOOST_REQUIRE_EQUAL( error("condition: assertion failed: must stake a positive amount"), + push_action( N(alice), N(delegatebw), mvo() + ("from", "alice") + ("receiver", "alice") + ("stake_net", "0.0000 EOS") + ("stake_cpu", "-0.0001 EOS") + ("stake_storage", "0.0000 EOS") ) + ); + + BOOST_REQUIRE_EQUAL( error("condition: assertion failed: must stake a positive amount"), + push_action( N(alice), N(delegatebw), mvo() + ("from", "alice") + ("receiver", "alice") + ("stake_net", "0.0000 EOS") + ("stake_cpu", "0.0000 EOS") + ("stake_storage", "-0.0001 EOS") ) + ); } FC_LOG_AND_RETHROW() -- GitLab