From d5a47f15fbbbc2874ade10701273e11ff77e83e8 Mon Sep 17 00:00:00 2001 From: Wang Zhi Date: Tue, 18 Jun 2019 10:19:06 +0800 Subject: [PATCH] New BP election algorithm --- contracts/eosio.system/eosio.system.abi | 14 +++- contracts/eosio.system/eosio.system.cpp | 4 +- contracts/eosio.system/eosio.system.hpp | 29 ++++--- contracts/eosio.system/voting.cpp | 105 +++++++++++++++++++++++- 4 files changed, 135 insertions(+), 17 deletions(-) diff --git a/contracts/eosio.system/eosio.system.abi b/contracts/eosio.system/eosio.system.abi index fee0ab3eb..289ebe133 100644 --- a/contracts/eosio.system/eosio.system.abi +++ b/contracts/eosio.system/eosio.system.abi @@ -39,7 +39,8 @@ {"name":"total_votes", "type":"float64"}, {"name":"producer_key", "type":"public_key"}, {"name":"all_stake", "type":"int64"}, - {"name":"is_active", "type":"bool"} + {"name":"is_active", "type":"bool"}, + {"name":"location", "type":"uint16"} ] },{ "name": "bidname", @@ -294,8 +295,7 @@ "base": "", "fields": [ {"name":"owner", "type":"account_name"}, - {"name":"seq_num", "type":"uint16"}, - {"name":"all_stake", "type":"int64"} + {"name":"seq_num", "type":"uint16"} ] },{ @@ -337,6 +337,10 @@ {"name":"seq", "type":"uint16"}, {"name":"level", "type":"uint8"} ] + },{ + "name": "testnewelec", + "base": "", + "fields": [] },{ "name": "setram", "base": "", @@ -539,6 +543,10 @@ "name": "seqproducer", "type": "seqproducer", "ricardian_contract": "" + },{ + "name": "testnewelec", + "type": "testnewelec", + "ricardian_contract": "" },{ "name": "regproxy", "type": "regproxy", diff --git a/contracts/eosio.system/eosio.system.cpp b/contracts/eosio.system/eosio.system.cpp index d86f59d92..807793a75 100644 --- a/contracts/eosio.system/eosio.system.cpp +++ b/contracts/eosio.system/eosio.system.cpp @@ -93,6 +93,8 @@ namespace eosiosystem { _producers.modify( prod, 0, [&](auto& p) { p.deactivate(); }); + + active_producer_seq(producer, public_key(), false); } void system_contract::bidname( account_name bidder, account_name newname, asset bid ) { @@ -204,7 +206,7 @@ EOSIO_ABI( eosiosystem::system_contract, // delegate_bandwidth.cpp (buyrambytes)(buyram)(sellram)(delegatebw)(undelegatebw)(refund) // voting.cpp - (regproducer)(unregprod)(voteproducer)(regproxy)(clsprods2)(seqproducer) + (regproducer)(unregprod)(voteproducer)(regproxy)(clsprods2)(seqproducer)(testnewelec) // producer_pay.cpp (onblock)(claimrewards) ) diff --git a/contracts/eosio.system/eosio.system.hpp b/contracts/eosio.system/eosio.system.hpp index cabd91e6d..d0424aa76 100644 --- a/contracts/eosio.system/eosio.system.hpp +++ b/contracts/eosio.system/eosio.system.hpp @@ -93,7 +93,6 @@ namespace eosiosystem { struct producer_info_ext { account_name owner; uint16_t seq_num = 1; // from 1 to 21 - int64_t all_stake = 0; uint64_t primary_key()const { return owner; } EOSLIB_SERIALIZE( producer_info_ext, (owner)(seq_num) ) @@ -102,12 +101,13 @@ namespace eosiosystem { struct prod_meta { account_name owner; - double total_votes = 0; + double total_votes = 0; // total votes eosio::public_key producer_key; /// a packed public key object - int64_t all_stake = 0; + int64_t all_stake = 0; // total original votes (buy yta amount) bool is_active = true; + uint16_t location = 0; - EOSLIB_SERIALIZE( prod_meta, (owner)(total_votes)(producer_key)(all_stake)(is_active) ) + EOSLIB_SERIALIZE( prod_meta, (owner)(total_votes)(producer_key)(all_stake)(is_active)(location) ) }; struct producers_seq { @@ -260,14 +260,8 @@ namespace eosiosystem { void seqproducer( const account_name producer, uint16_t seq , uint8_t level ); - void rm_producer_seq( const account_name producer, uint16_t seq ); - - void add_producer_seq( const account_name producer, uint16_t seq , uint8_t level ); - - void active_producer_seq( const account_name producer, const eosio::public_key& producer_key, bool isactive); - - void update_producers_seq_totalvotes( uint16_t seq_num, account_name owner, double total_votes); - //##YTA-Change end: + void testnewelec(); +//##YTA-Change end: void setram( uint64_t max_ram_size ); @@ -287,8 +281,19 @@ namespace eosiosystem { void bidname( account_name bidder, account_name newname, asset bid ); private: void update_elected_producers( block_timestamp timestamp ); + //##YTA-Change start: void update_elected_producers_yta( block_timestamp timestamp ); + + std::pair getProducerForSeq(uint64_t seq_num ); + + void rm_producer_seq( const account_name producer, uint16_t seq ); + + void add_producer_seq( const account_name producer, uint16_t seq , uint8_t level ); + + void active_producer_seq( const account_name producer, const eosio::public_key& producer_key, bool isactive); + + void update_producers_seq_totalvotes( uint16_t seq_num, account_name owner, double total_votes); //##YTA-Change end: // Implementation details: diff --git a/contracts/eosio.system/voting.cpp b/contracts/eosio.system/voting.cpp index 6add8bac1..afbfb621e 100644 --- a/contracts/eosio.system/voting.cpp +++ b/contracts/eosio.system/voting.cpp @@ -269,10 +269,113 @@ namespace eosiosystem { }); } + void system_contract::testnewelec() { + require_auth( _self ); + + std::vector< std::pair > top_producers; + top_producers.reserve(21); + + + for( uint16_t seq_num = 1; seq_num <= 21 ; seq_num++ ) { + std::pair ppinfo = getProducerForSeq( seq_num ); + if( ppinfo.first.producer_name != 0 ) { + top_producers.emplace_back( ppinfo ); + } + } + + if ( top_producers.size() < _gstate.last_producer_schedule_size ) { + return; + } + + /// sort by producer name + std::sort( top_producers.begin(), top_producers.end() ); + + std::vector producers; + + producers.reserve(top_producers.size()); + for( const auto& item : top_producers ) + producers.push_back(item.first); + +/* + print("producers start------------------------------\n"); + for( const auto& item : producers ) { + print("producer -", (name{item.producer_name}), "--"); + + //std::string str(std::begin(item.block_signing_key.data), std::end(item.block_signing_key.data)); + //for(size_t i=0; i<33; i++) { + // char c = item.block_signing_key.data[i]; + // printf("%c",c); + //} + //print(str); + + print("\n"); + } + print("producers end------------------------------\n"); +*/ + } + void system_contract::update_elected_producers_yta( block_timestamp block_time ) { _gstate.last_producer_schedule_update = block_time; - } + std::vector< std::pair > top_producers; + top_producers.reserve(21); + + + for( uint16_t seq_num = 1; seq_num <= 21 ; seq_num++ ) { + std::pair ppinfo = getProducerForSeq( seq_num ); + if( ppinfo.first.producer_name != 0 ) { + top_producers.emplace_back( ppinfo ); + } + } + + if ( top_producers.size() < _gstate.last_producer_schedule_size ) { + return; + } + + /// sort by producer name + std::sort( top_producers.begin(), top_producers.end() ); + + std::vector producers; + + producers.reserve(top_producers.size()); + for( const auto& item : top_producers ) + producers.push_back(item.first); + + bytes packed_schedule = pack(producers); + + if( set_proposed_producers( packed_schedule.data(), packed_schedule.size() ) >= 0 ) { + _gstate.last_producer_schedule_size = static_cast( top_producers.size() ); + } + + } + + std::pair system_contract::getProducerForSeq(uint64_t seq_num ) { + producers_seq_table _prodseq(_self, seq_num); + auto ps_itr = _prodseq.find (seq_num); + if( ps_itr == _prodseq.end() ) + return std::pair({{0, eosio::public_key{}}, 0}); + + if (ps_itr->prods_all.begin() == ps_itr->prods_all.end() ) + return std::pair({{0, eosio::public_key{}}, 0}); + + double total_votes = 0; + bool is_find = false; + auto max = ps_itr->prods_all.begin(); + for( auto it = ps_itr->prods_all.begin(); it != ps_itr->prods_all.end(); it++ ) { + if(it->is_active && it->total_votes > total_votes) { + max = it; + total_votes = it->total_votes; + is_find = true; + } + } + + if(is_find) + return std::pair({{max->owner, max->producer_key}, max->location}); + + return std::pair({{0, eosio::public_key{}}, 0}); + } + + //##YTA-Change end: void system_contract::update_elected_producers( block_timestamp block_time ) { -- GitLab