From 498bdc13d991c4012675a81254f321c72fdf4246 Mon Sep 17 00:00:00 2001 From: Anton Perkov Date: Thu, 8 Mar 2018 13:55:57 -0500 Subject: [PATCH] more tests and bugfix in variant comparison --- contracts/eosio.system/eosio.system.abi | 9 ++ contracts/eosio.system/eosio.system.hpp | 3 +- contracts/eosio.system/voting.hpp | 12 +-- libraries/fc/src/variant.cpp | 7 +- .../testing/include/eosio/testing/tester.hpp | 4 +- tests/wasm_tests/eosio.system_tests.cpp | 94 ++++++++++++------- 6 files changed, 81 insertions(+), 48 deletions(-) diff --git a/contracts/eosio.system/eosio.system.abi b/contracts/eosio.system/eosio.system.abi index aa83a00cb..3aa0eac46 100644 --- a/contracts/eosio.system/eosio.system.abi +++ b/contracts/eosio.system/eosio.system.abi @@ -119,6 +119,12 @@ {"name":"producer_key", "type":"bytes"}, {"name":"prefs", "type":"eosio_parameters"} ] + },{ + "name": "unregprod", + "base": "", + "fields": [ + {"name":"producer", "type":"account_name"} + ] },{ "name": "stakevote", "base": "", @@ -166,6 +172,9 @@ },{ "name": "regproducer", "type": "regproducer" + },{ + "name": "unregprod", + "type": "unregprod" },{ "name": "stakevote", "type": "stakevote" diff --git a/contracts/eosio.system/eosio.system.hpp b/contracts/eosio.system/eosio.system.hpp index 77aa28a25..bf958dd04 100644 --- a/contracts/eosio.system/eosio.system.hpp +++ b/contracts/eosio.system/eosio.system.hpp @@ -72,7 +72,8 @@ namespace eosiosystem { typename voting::register_proxy, typename voting::unregister_proxy, typename voting::regproducer, - typename voting::vote_producer, + typename voting::unregprod, + typename voting::voteproducer, typename voting::stakevote, typename voting::unstakevote, typename voting::unstake_vote_deferred, diff --git a/contracts/eosio.system/voting.hpp b/contracts/eosio.system/voting.hpp index 99ed959e1..4f6c86b9d 100644 --- a/contracts/eosio.system/voting.hpp +++ b/contracts/eosio.system/voting.hpp @@ -118,13 +118,13 @@ namespace eosiosystem { } } - ACTION( SystemAccount, unregproducer ) { + ACTION( SystemAccount, unregprod ) { account_name producer; - EOSLIB_SERIALIZE( unregproducer, (producer) ) + EOSLIB_SERIALIZE( unregprod, (producer) ) }; - static void on( const unregproducer& unreg ) { + static void on( const unregprod& unreg ) { require_auth( unreg.producer ); producers_table producers_tbl( SystemAccount, SystemAccount ); @@ -388,12 +388,12 @@ namespace eosiosystem { }); } - ACTION( SystemAccount, vote_producer ) { + ACTION( SystemAccount, voteproducer ) { account_name voter; account_name proxy; std::vector producers; - EOSLIB_SERIALIZE( vote_producer, (voter)(proxy)(producers) ) + EOSLIB_SERIALIZE( voteproducer, (voter)(proxy)(producers) ) }; /** @@ -403,7 +403,7 @@ namespace eosiosystem { * @pre vp.voter must authorize this action * @pre voter must have previously staked some EOS for voting */ - static void on( const vote_producer& vp ) { + static void on( const voteproducer& vp ) { require_auth( vp.voter ); //validate input diff --git a/libraries/fc/src/variant.cpp b/libraries/fc/src/variant.cpp index 2f8d73c5c..4ed6c7582 100644 --- a/libraries/fc/src/variant.cpp +++ b/libraries/fc/src/variant.cpp @@ -754,16 +754,13 @@ string format_string( const string& format, const variant_object& args ) if( a.is_double() || b.is_double() ) return a.as_double() == b.as_double(); if( a.is_int64() || b.is_int64() ) return a.as_int64() == b.as_int64(); if( a.is_uint64() || b.is_uint64() ) return a.as_uint64() == b.as_uint64(); + if( a.is_array() || b.is_array() ) return a.get_array() == b.get_array(); return false; } bool operator != ( const variant& a, const variant& b ) { - if( a.is_string() || b.is_string() ) return a.as_string() != b.as_string(); - if( a.is_double() || b.is_double() ) return a.as_double() != b.as_double(); - if( a.is_int64() || b.is_int64() ) return a.as_int64() != b.as_int64(); - if( a.is_uint64() || b.is_uint64() ) return a.as_uint64() != b.as_uint64(); - return false; + return !( a == b ); } bool operator ! ( const variant& a ) diff --git a/libraries/testing/include/eosio/testing/tester.hpp b/libraries/testing/include/eosio/testing/tester.hpp index 4d0e3eda6..3fc9114ef 100644 --- a/libraries/testing/include/eosio/testing/tester.hpp +++ b/libraries/testing/include/eosio/testing/tester.hpp @@ -7,9 +7,9 @@ #include -#define REQUIRE_EQUAL_OBJECTS(a, b) BOOST_REQUIRE_EQUAL( true, a.is_object() ); \ +#define REQUIRE_EQUAL_OBJECTS(left, right) { auto a = left; auto b = right; BOOST_REQUIRE_EQUAL( true, a.is_object() ); \ BOOST_REQUIRE_EQUAL( true, b.is_object() ); \ - BOOST_REQUIRE_EQUAL_COLLECTIONS( a.get_object().begin(), a.get_object().end(), b.get_object().begin(), b.get_object().end() ); + BOOST_REQUIRE_EQUAL_COLLECTIONS( a.get_object().begin(), a.get_object().end(), b.get_object().begin(), b.get_object().end() ); } std::ostream& operator<<( std::ostream& osm, const fc::variant& v ); diff --git a/tests/wasm_tests/eosio.system_tests.cpp b/tests/wasm_tests/eosio.system_tests.cpp index e10d4bd49..b9aa4efe4 100644 --- a/tests/wasm_tests/eosio.system_tests.cpp +++ b/tests/wasm_tests/eosio.system_tests.cpp @@ -58,6 +58,10 @@ public: return base_tester::push_action( std::move(act), auth ? uint64_t(signer) : 0 ); } + uint32_t last_block_time() const { + return time_point_sec( control->head_block_time() ).sec_since_epoch(); + } + asset get_balance( const account_name& act ) { return get_currency_balance( config::system_account_name, symbol(SY(4,EOS)), act ); } @@ -457,6 +461,25 @@ void require_simple_voter(const fc::variant& vi) { BOOST_REQUIRE_EQUAL( 0, vi["last_unstake"].as_uint64() ); } +fc::variant simple_voter( account_name acct, uint64_t vote_stake, uint64_t ts ) { + return mutable_variant_object() + ("owner", acct) + ("proxy", name(0).to_string()) + ("last_update", ts) + ("is_proxy", 0) + ("staked", vote_stake) + ("unstaking", 0) + ("unstake_per_week", 0) + ("proxied_votes", 0) + ("producers", variants() ) + ("deferred_trx_id", 0) + ("last_unstake", 0); +} + +fc::variant simple_voter( account_name acct, const string& vote_stake, uint64_t ts ) { + return simple_voter( acct, asset::from_string( vote_stake ).amount, ts); +} + BOOST_FIXTURE_TEST_CASE( stake_add_more_partial_unstake, eosio_system_tester ) try { issue( "alice", "1000.0000 EOS", config::system_account_name ); @@ -519,32 +542,37 @@ BOOST_FIXTURE_TEST_CASE( stake_add_more_partial_unstake, eosio_system_tester ) t ); vi = get_voter_info( "alice" ); require_simple_voter( vi ); + REQUIRE_EQUAL_OBJECTS( simple_voter( "alice", "1000.0000 EOS", last_block_time()), get_voter_info( "alice" ) ); BOOST_REQUIRE_EQUAL( asset::from_string("0.0000 EOS").amount, vi["staked"].as_uint64() ); BOOST_REQUIRE_EQUAL( asset::from_string("1000.0000 EOS"), get_balance( "alice" ) ); } FC_LOG_AND_RETHROW() +static fc::variant producer_parameters_example(int n) { + return mutable_variant_object() + ("target_block_size", 1024 * 1024 + n) + ("max_block_size", 10 * 1024 + n) + ("target_block_acts_per_scope", 1000 + n) + ("max_block_acts_per_scope", 10000 + n) + ("target_block_acts", 1100 + n) + ("max_block_acts", 11000 + n) + ("max_storage_size", 2000 + n) + ("max_transaction_lifetime", 3600 + n) + ("max_transaction_exec_time", 9900 + n) + ("max_authority_depth", 6 + n) + ("max_inline_depth", 4 + n) + ("max_inline_action_size", 4096 + n) + ("max_generated_transaction_size", 64*1024 + n) + ("inflation_rate", 1050 + n) + ("storage_reserve_ratio", 100 + n); +} + + BOOST_FIXTURE_TEST_CASE( producer_register_unregister, eosio_system_tester ) try { issue( "alice", "1000.0000 EOS", config::system_account_name ); - fc::variant params = mutable_variant_object() - ("target_block_size", 1024 * 1024) - ("max_block_size", 10 * 1024) - ("target_block_acts_per_scope", 1001) - ("max_block_acts_per_scope", 10001) - ("target_block_acts", 1101) - ("max_block_acts", 11001) - ("max_storage_size", 2001) - ("max_transaction_lifetime", 3601) - ("max_transaction_exec_time", 9999) - ("max_authority_depth", 6) - ("max_inline_depth", 4) - ("max_inline_action_size", 4096) - ("max_generated_transaction_size", 64*1024) - ("inflation_rate", 1051) - ("storage_reserve_ratio", 1005); - + fc::variant params = producer_parameters_example(1); vector key = fc::raw::pack( fc::crypto::public_key( std::string("EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV") ) ); BOOST_REQUIRE_EQUAL( success(), push_action(N(alice), N(regproducer), mvo() ("producer", "alice") @@ -560,23 +588,8 @@ BOOST_FIXTURE_TEST_CASE( producer_register_unregister, eosio_system_tester ) try BOOST_REQUIRE_EQUAL( string(key.begin(), key.end()), to_string(info["packed_key"]) ); - //do regproducer again to change parameters - fc::variant params2 = mutable_variant_object() - ("target_block_size", 1024 * 1024 + 1) - ("max_block_size", 10 * 1024 + 1) - ("target_block_acts_per_scope", 1002) - ("max_block_acts_per_scope", 10002) - ("target_block_acts", 1102) - ("max_block_acts", 11002) - ("max_storage_size", 2002) - ("max_transaction_lifetime", 3602) - ("max_transaction_exec_time", 9997) - ("max_authority_depth", 7) - ("max_inline_depth", 5) - ("max_inline_action_size", 4095) - ("max_generated_transaction_size", 64*1025) - ("inflation_rate", 1052) - ("storage_reserve_ratio", 1006); + //call regproducer again to change parameters + fc::variant params2 = producer_parameters_example(2); vector key2 = fc::raw::pack( fc::crypto::public_key( std::string("EOSR16EPHFSKVYHBjQgxVGQPrwCxTg7BbZ69H9i4gztN9deKTEXYne4") ) ); BOOST_REQUIRE_EQUAL( success(), push_action(N(alice), N(regproducer), mvo() @@ -592,6 +605,19 @@ BOOST_FIXTURE_TEST_CASE( producer_register_unregister, eosio_system_tester ) try REQUIRE_EQUAL_OBJECTS( params2, info["prefs"] ); BOOST_REQUIRE_EQUAL( string(key2.begin(), key2.end()), to_string(info["packed_key"]) ); + //unregister producer + BOOST_REQUIRE_EQUAL( success(), push_action(N(alice), N(unregprod), mvo() + ("producer", "alice") + ) + ); + info = get_producer_info( "alice" ); + //key should be empty + BOOST_REQUIRE_EQUAL( true, to_string(info["packed_key"]).empty() ); + //everything else should stay the same + BOOST_REQUIRE_EQUAL( N(alice), info["owner"].as_uint64() ); + BOOST_REQUIRE_EQUAL( 0, info["total_votes"].as_uint64() ); + REQUIRE_EQUAL_OBJECTS( params2, info["prefs"] ); + } FC_LOG_AND_RETHROW() BOOST_AUTO_TEST_SUITE_END() -- GitLab