From 9d60b972e6c082a96c26b7df82aaafa1a697be30 Mon Sep 17 00:00:00 2001 From: Anton Perkov Date: Thu, 15 Mar 2018 20:03:58 -0400 Subject: [PATCH] more tests and bugfixes for producer voting --- contracts/eosio.system/delegate_bandwith.hpp | 2 +- contracts/eosio.system/voting.hpp | 16 +++-- libraries/chain/include/eosio/chain/name.hpp | 4 +- tests/wasm_tests/eosio.system_tests.cpp | 63 ++++++++++++++++---- 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/contracts/eosio.system/delegate_bandwith.hpp b/contracts/eosio.system/delegate_bandwith.hpp index acbdd886f..98cedb4d8 100644 --- a/contracts/eosio.system/delegate_bandwith.hpp +++ b/contracts/eosio.system/delegate_bandwith.hpp @@ -116,7 +116,7 @@ namespace eosiosystem { if ( 0 < del.stake_storage_quantity.amount ) { auto parameters = global_state_singleton::exists() ? global_state_singleton::get() : common::get_default_parameters(); - auto token_supply = currency::get_total_supply();//.quantity; + auto token_supply = currency::get_total_supply(); //make sure that there is no posibility of overflow here uint64_t storage_bytes_estimated = ( parameters.max_storage_size - parameters.total_storage_bytes_reserved ) diff --git a/contracts/eosio.system/voting.hpp b/contracts/eosio.system/voting.hpp index 227a8058e..e20e9b64e 100644 --- a/contracts/eosio.system/voting.hpp +++ b/contracts/eosio.system/voting.hpp @@ -284,14 +284,19 @@ namespace eosiosystem { eosio::producer_schedule schedule; schedule.producers.reserve(21); - + /* + for (auto it = idx.begin(); it != idx.end(); ++it) { + print( it->total_votes, ", " ); + } + print ("\n"); + */ auto it = idx.end(); if (it == idx.begin()) { - return; + return; } --it; size_t n = 0; - while ( n < 21 ) { + while ( n < 21 && 0 < it->total_votes ) { if ( it->active() ) { schedule.producers.emplace_back(); schedule.producers.back().producer_name = it->owner; @@ -325,6 +330,9 @@ namespace eosiosystem { } --it; } + if ( n == 0 ) { //no active producers with votes > 0 + return; + } // should use producer_schedule_type from libraries/chain/include/eosio/chain/producer_schedule.hpp bytes packed_schedule = pack(schedule); set_active_producers( packed_schedule.data(), packed_schedule.size() ); @@ -465,7 +473,7 @@ namespace eosiosystem { votes += voter->proxied_votes; } - if ( old_producers ) { //old_producers == 0 if proxy has stoped being a proxy and votes were taken back from producers at that moment + if ( old_producers ) { //old_producers == 0 if proxy has stoped being a proxy and votes were taken back from the producers at that moment //revoke votes only from no longer elected std::vector revoked( old_producers->size() ); auto end_it = std::set_difference( old_producers->begin(), old_producers->end(), new_producers->begin(), new_producers->end(), revoked.begin() ); diff --git a/libraries/chain/include/eosio/chain/name.hpp b/libraries/chain/include/eosio/chain/name.hpp index f24f661a4..3db200959 100644 --- a/libraries/chain/include/eosio/chain/name.hpp +++ b/libraries/chain/include/eosio/chain/name.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace eosio { namespace chain { using std::string; @@ -72,8 +73,7 @@ namespace eosio { namespace chain { return *this; } - template - friend Stream& operator << ( Stream& out, const name& n ) { + friend std::ostream& operator << ( std::ostream& out, const name& n ) { return out << string(n); } diff --git a/tests/wasm_tests/eosio.system_tests.cpp b/tests/wasm_tests/eosio.system_tests.cpp index daddaf370..e40f9f323 100644 --- a/tests/wasm_tests/eosio.system_tests.cpp +++ b/tests/wasm_tests/eosio.system_tests.cpp @@ -399,7 +399,7 @@ BOOST_FIXTURE_TEST_CASE( stake_unstake_separate, eosio_system_tester ) try { BOOST_REQUIRE_EQUAL( bytes, total["storage_bytes"].as_uint64() ); //storage - BOOST_REQUIRE_EQUAL( success(), stake( "alice", "0.0000 EOS", "200.0000 EOS", "300.0000 EOS" ) ); + BOOST_REQUIRE_EQUAL( success(), stake( "alice", "0.0000 EOS", "0.0000 EOS", "300.0000 EOS" ) ); total = get_total_stake( "alice" ); BOOST_REQUIRE_EQUAL( asset::from_string("110.0000 EOS").amount, total["net_weight"].as_uint64()); BOOST_REQUIRE_EQUAL( asset::from_string("220.0000 EOS").amount, total["cpu_weight"].as_uint64()); @@ -412,7 +412,7 @@ BOOST_FIXTURE_TEST_CASE( stake_unstake_separate, eosio_system_tester ) try { total = get_total_stake( "alice" ); BOOST_REQUIRE_EQUAL( asset::from_string("10.0000 EOS").amount, total["net_weight"].as_uint64()); BOOST_REQUIRE_EQUAL( asset::from_string("220.0000 EOS").amount, total["cpu_weight"].as_uint64()); - BOOST_REQUIRE_EQUAL( asset::from_string("3300.0000 EOS").amount, total["storage_stake"].as_uint64()); + BOOST_REQUIRE_EQUAL( asset::from_string("330.0000 EOS").amount, total["storage_stake"].as_uint64()); BOOST_REQUIRE_EQUAL( bytes2, total["storage_bytes"].as_uint64()); //unstake net @@ -420,15 +420,16 @@ BOOST_FIXTURE_TEST_CASE( stake_unstake_separate, eosio_system_tester ) try { total = get_total_stake( "alice" ); BOOST_REQUIRE_EQUAL( asset::from_string("10.0000 EOS").amount, total["net_weight"].as_uint64()); BOOST_REQUIRE_EQUAL( asset::from_string("20.0000 EOS").amount, total["cpu_weight"].as_uint64()); - BOOST_REQUIRE_EQUAL( asset::from_string("3300.0000 EOS").amount, total["storage_stake"].as_uint64()); + BOOST_REQUIRE_EQUAL( asset::from_string("330.0000 EOS").amount, total["storage_stake"].as_uint64()); BOOST_REQUIRE_EQUAL( bytes2, total["storage_bytes"].as_uint64()); //unstake cpu BOOST_REQUIRE_EQUAL( success(), unstake( "alice", "0.0000 EOS", "0.0000 EOS", bytes2 / 2 ) ); total = get_total_stake( "alice" ); + auto storage_left = M("330.0000 EOS") - M("330.0000 EOS") * (bytes2 / 2) / bytes2; BOOST_REQUIRE_EQUAL( asset::from_string("10.0000 EOS").amount, total["net_weight"].as_uint64()); BOOST_REQUIRE_EQUAL( asset::from_string("20.0000 EOS").amount, total["cpu_weight"].as_uint64()); - BOOST_REQUIRE_EQUAL( asset::from_string("1650.0000 EOS").amount, total["storage_stake"].as_uint64()); + BOOST_REQUIRE_EQUAL( storage_left, total["storage_stake"].as_uint64()); BOOST_REQUIRE_EQUAL( bytes2 - bytes2/2, total["storage_bytes"].as_uint64()); } FC_LOG_AND_RETHROW() @@ -481,7 +482,7 @@ BOOST_FIXTURE_TEST_CASE( producer_register_unregister, eosio_system_tester ) try ); auto info = get_producer_info( "alice" ); - BOOST_REQUIRE_EQUAL( N(alice), info["owner"].as_uint64() ); + BOOST_REQUIRE_EQUAL( "alice", info["owner"].as_string() ); BOOST_REQUIRE_EQUAL( 0, info["total_votes"].as_uint64() ); REQUIRE_MATCHING_OBJECT( params, info["prefs"] ); BOOST_REQUIRE_EQUAL( string(key.begin(), key.end()), to_string(info["packed_key"]) ); @@ -499,7 +500,7 @@ BOOST_FIXTURE_TEST_CASE( producer_register_unregister, eosio_system_tester ) try ); info = get_producer_info( "alice" ); - BOOST_REQUIRE_EQUAL( N(alice), info["owner"].as_uint64() ); + BOOST_REQUIRE_EQUAL( "alice", info["owner"].as_string() ); BOOST_REQUIRE_EQUAL( 0, info["total_votes"].as_uint64() ); REQUIRE_MATCHING_OBJECT( params2, info["prefs"] ); BOOST_REQUIRE_EQUAL( string(key2.begin(), key2.end()), to_string(info["packed_key"]) ); @@ -513,7 +514,7 @@ BOOST_FIXTURE_TEST_CASE( producer_register_unregister, eosio_system_tester ) try //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( "alice", info["owner"].as_string() ); BOOST_REQUIRE_EQUAL( 0, info["total_votes"].as_uint64() ); REQUIRE_MATCHING_OBJECT( params2, info["prefs"] ); @@ -538,7 +539,7 @@ BOOST_FIXTURE_TEST_CASE( vote_for_producer, eosio_system_tester ) try { ) ); auto prod = get_producer_info( "alice" ); - BOOST_REQUIRE_EQUAL( N(alice), prod["owner"].as_uint64() ); + BOOST_REQUIRE_EQUAL( "alice", prod["owner"].as_string() ); BOOST_REQUIRE_EQUAL( 0, prod["total_votes"].as_uint64() ); REQUIRE_MATCHING_OBJECT( params, prod["prefs"]); BOOST_REQUIRE_EQUAL( string(key.begin(), key.end()), to_string(prod["packed_key"]) ); @@ -562,7 +563,7 @@ BOOST_FIXTURE_TEST_CASE( vote_for_producer, eosio_system_tester ) try { //check that producer parameters stay the same after voting prod = get_producer_info( "alice" ); BOOST_REQUIRE_EQUAL( 111111, prod["total_votes"].as_uint64() ); - BOOST_REQUIRE_EQUAL( N(alice), prod["owner"].as_uint64() ); + BOOST_REQUIRE_EQUAL( "alice", prod["owner"].as_string() ); REQUIRE_MATCHING_OBJECT( params, prod["prefs"]); BOOST_REQUIRE_EQUAL( string(key.begin(), key.end()), to_string(prod["packed_key"]) ); @@ -613,7 +614,7 @@ BOOST_FIXTURE_TEST_CASE( vote_for_producer, eosio_system_tester ) try { BOOST_REQUIRE_EQUAL( 0, prod["total_votes"].as_uint64() ); BOOST_REQUIRE_EQUAL( asset::from_string("3000.0000 EOS"), get_balance( "carol" ) ); //check that the producer parameters stay the same after all - BOOST_REQUIRE_EQUAL( N(alice), prod["owner"].as_uint64() ); + BOOST_REQUIRE_EQUAL( "alice", prod["owner"].as_string() ); REQUIRE_MATCHING_OBJECT( params, prod["prefs"]); BOOST_REQUIRE_EQUAL( string(key.begin(), key.end()), to_string(prod["packed_key"]) ); @@ -1258,4 +1259,46 @@ BOOST_FIXTURE_TEST_CASE( proxy_cannot_use_another_proxy, eosio_system_tester ) t } FC_LOG_AND_RETHROW() + +BOOST_FIXTURE_TEST_CASE( elect_poducers_and_parameters, eosio_system_tester ) try { + create_accounts( { N(producer1), N(producer2), N(producer3) } ); + BOOST_REQUIRE_EQUAL( success(), regproducer( "producer1", 1) ); + BOOST_REQUIRE_EQUAL( success(), regproducer( "producer2", 3) ); + BOOST_REQUIRE_EQUAL( success(), regproducer( "producer3", 5) ); + /* + issue( "alice", "1000.0000 EOS", config::system_account_name ); + BOOST_REQUIRE_EQUAL( success(), stake( "alice", "100.0000 EOS", "50.0000 EOS", "50.0000 EOS" ) ); + //vote for producers + BOOST_REQUIRE_EQUAL( success(), push_action(N(alice), N(voteproducer), mvo() + ("voter", "alice") + ("proxy", name(0).to_string() ) + ("producers", vector{ N(producer1) } ) + ) + ); + produce_blocks(100); + auto producer_keys = control->get_global_properties().active_producers.producers; + BOOST_REQUIRE_EQUAL( 1, producer_keys.size() ); + BOOST_REQUIRE_EQUAL( name("producer1"), producer_keys[0].producer_name ); +*/ + issue( "bob", "1000.0000 EOS", config::system_account_name ); + BOOST_REQUIRE_EQUAL( success(), stake( "bob", "200.0000 EOS", "100.0000 EOS", "50.0000 EOS" ) ); + BOOST_REQUIRE_EQUAL( success(), push_action(N(bob), N(voteproducer), mvo() + ("voter", "bob") + ("proxy", name(0).to_string() ) + ("producers", vector{ N(producer1), N(producer2) } ) + ) + ); + std::cout << "after push block" << std::endl; + produce_blocks(200); + auto producer_keys = control->get_global_properties().active_producers.producers; + std::cout << "producers: " << std::endl; + for (auto& x : producer_keys) { + std::cout << x.producer_name << std::endl; + } + BOOST_REQUIRE_EQUAL( 2, producer_keys.size() ); + BOOST_REQUIRE_EQUAL( name("producer2"), producer_keys[0].producer_name ); + BOOST_REQUIRE_EQUAL( name("producer1"), producer_keys[1].producer_name ); + +} FC_LOG_AND_RETHROW() + BOOST_AUTO_TEST_SUITE_END() -- GitLab