From afa5cc22800c4b263c55791edce30dbe8e9782e1 Mon Sep 17 00:00:00 2001 From: Anton Perkov Date: Fri, 2 Mar 2018 15:08:47 -0500 Subject: [PATCH] system contract test fixes --- contracts/eosio.system/delegate_bandwith.hpp | 10 ++++-- contracts/eosio.system/eosio.system.abi | 2 +- contracts/eosio.system/eosio.system.cpp | 2 +- contracts/eosio.system/eosio.system.hpp | 4 +-- contracts/eosiolib/eosiolib.cpp | 11 +++++++ tests/wasm_tests/eosio.system_tests.cpp | 32 +++++++++++++++----- 6 files changed, 46 insertions(+), 15 deletions(-) diff --git a/contracts/eosio.system/delegate_bandwith.hpp b/contracts/eosio.system/delegate_bandwith.hpp index 7491708e5..633d6cd0f 100644 --- a/contracts/eosio.system/delegate_bandwith.hpp +++ b/contracts/eosio.system/delegate_bandwith.hpp @@ -130,6 +130,7 @@ 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" ); auto itr = del_index.find( del.receiver); if( itr != nullptr ) { del_index.emplace( del.from, [&]( auto& dbo ){ @@ -188,7 +189,9 @@ namespace eosiosystem { //eosio_assert( is_account( del.receiver ), "can only delegate resources to an existing account" ); - const auto& dbw = del_index.get(del.receiver); + print ("undelegatebw: from = ", del.from, " receiver = ", del.receiver, "\n"); + 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" ); @@ -196,7 +199,7 @@ namespace eosiosystem { system_token_type storage_stake_decrease = totals.storage_stake * del.unstake_storage_bytes / totals.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; @@ -215,7 +218,8 @@ 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 - currency::inline_transfer( SystemAccount, del.from, asset( total_refund.quantity ), "unstake bandwidth" ); + 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(); parameters.total_storage_bytes_reserved -= del.unstake_storage_bytes; diff --git a/contracts/eosio.system/eosio.system.abi b/contracts/eosio.system/eosio.system.abi index e797dc57d..f205724f3 100644 --- a/contracts/eosio.system/eosio.system.abi +++ b/contracts/eosio.system/eosio.system.abi @@ -52,7 +52,7 @@ {"name":"receiver", "type":"account_name"}, {"name":"unstake_net", "type":"asset"}, {"name":"unstake_cpu", "type":"asset"}, - {"name":"stake_storage_bytes", "type":"uint64"} + {"name":"unstake_bytes", "type":"uint64"} ] },{ "name": "total_resources", diff --git a/contracts/eosio.system/eosio.system.cpp b/contracts/eosio.system/eosio.system.cpp index 7d905ff0e..98df43bc9 100644 --- a/contracts/eosio.system/eosio.system.cpp +++ b/contracts/eosio.system/eosio.system.cpp @@ -11,7 +11,7 @@ extern "C" { /// The apply method implements the dispatch of events to this contract void apply( uint64_t code, uint64_t act ) { - print( eosio::name(code), "::", eosio::name(act) ); + //print( eosio::name(code), "::", eosio::name(act) ); eosiosystem::contract::apply( code, act ); } } diff --git a/contracts/eosio.system/eosio.system.hpp b/contracts/eosio.system/eosio.system.hpp index a0d7b394b..a3b956b86 100644 --- a/contracts/eosio.system/eosio.system.hpp +++ b/contracts/eosio.system/eosio.system.hpp @@ -30,7 +30,8 @@ namespace eosiosystem { } static void apply( account_name code, action_name act ) { - if( !eosio::dispatch::delegatebw, + if ( !eosio::dispatch( code, act ) ) { + if( !eosio::dispatch::delegatebw, typename delegate_bandwith::undelegatebw, typename voting::register_proxy, typename voting::unregister_proxy, @@ -40,7 +41,6 @@ namespace eosiosystem { typename voting::unstake_vote, typename voting::unstake_vote_deferred, nonce>( code, act) ) { - if ( !eosio::dispatch( code, act ) ) { eosio::print("Unexpected action: ", eosio::name(act), "\n"); eosio_assert( false, "received unexpected action"); } diff --git a/contracts/eosiolib/eosiolib.cpp b/contracts/eosiolib/eosiolib.cpp index e5d409764..7ccc721c6 100644 --- a/contracts/eosiolib/eosiolib.cpp +++ b/contracts/eosiolib/eosiolib.cpp @@ -510,4 +510,15 @@ void set_blockchain_parameters(const struct blockchain_parameters* params) { set_blockchain_parameters_packed( data.data(), data.size() ); } +int get_blockchain_parameters_packed(char* data, size_t datalen); + +void get_blockchain_parameters(struct blockchain_parameters* params) { + char buf[sizeof(struct blockchain_parameters)]; + size_t size = get_blockchain_parameters_packed( buf, sizeof(buf) ); + eosio_assert( size <= sizeof(buf), "buffer is too small" ); + static_assert(sizeof(blockchain_parameters) == sizeof(eosio::blockchain_parameters), "data structures are not the same"); + eosio::datastream ds( buf, size_t(size) ); + ds >> *reinterpret_cast(params); +} + } diff --git a/tests/wasm_tests/eosio.system_tests.cpp b/tests/wasm_tests/eosio.system_tests.cpp index 4784fd964..55ed10267 100644 --- a/tests/wasm_tests/eosio.system_tests.cpp +++ b/tests/wasm_tests/eosio.system_tests.cpp @@ -85,8 +85,7 @@ BOOST_AUTO_TEST_SUITE(eosio_system_tests) BOOST_FIXTURE_TEST_CASE( delegate_to_myself, eosio_system_tester ) try { issue( "alice", "1000.0000 EOS", config::system_account_name ); - auto balance = get_balance( "alice" ); - std::cout << "Balance: " << N(alice) << ": " << balance << std::endl; + BOOST_REQUIRE_EQUAL( asset::from_string("1000.0000 EOS"), get_balance( "alice" ) ); push_action(N(alice), N(delegatebw), mvo() ("from", "alice") @@ -97,13 +96,30 @@ BOOST_FIXTURE_TEST_CASE( delegate_to_myself, eosio_system_tester ) try { ); auto stake = get_total_stake( "alice" ); - BOOST_REQUIRE_EQUAL( 2000000, stake["net_weight"].as_uint64()); - BOOST_REQUIRE_EQUAL( 1000000, stake["cpu_weight"].as_uint64()); - BOOST_REQUIRE_EQUAL( 3000000, stake["storage_stake"].as_uint64()); + 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()); - balance = get_balance( "alice" ); - std::cout << "Balance: " << balance << std::endl; + auto bytes = stake["storage_bytes"].as_uint64(); + BOOST_REQUIRE_EQUAL( true, 0 < bytes ); - } FC_LOG_AND_RETHROW() + BOOST_REQUIRE_EQUAL( asset::from_string("400.0000 EOS"), get_balance( "alice" ) ); + + push_action(N(alice), N(undelegatebw), mvo() + ("from", "alice") + ("receiver", "alice") + ("unstake_net", "200.0000 EOS") + ("unstake_cpu", "100.0000 EOS") + ("unstake_bytes", bytes) + ); + + stake = get_total_stake( "alice" ); + 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()); + + +} FC_LOG_AND_RETHROW() BOOST_AUTO_TEST_SUITE_END() -- GitLab