Skip to content

Commit

Permalink
Merge pull request #1559 from AntelopeIO/GH-1519-rm-emit
Browse files Browse the repository at this point in the history
IF: Remove controller emit of hotstuff messages
  • Loading branch information
heifner authored Aug 28, 2023
2 parents d6cf723 + 79bdc04 commit 5dfa47d
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 314 deletions.
32 changes: 0 additions & 32 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1974,22 +1974,6 @@ struct controller_impl {
pending->push();
}

void commit_hs_proposal_msg(hs_proposal_message_ptr msg){
emit( self.new_hs_proposal_message, msg );
}

void commit_hs_vote_msg(hs_vote_message_ptr msg){
emit( self.new_hs_vote_message, msg );
}

void commit_hs_new_view_msg(hs_new_view_message_ptr msg){
emit( self.new_hs_new_view_message, msg );
}

void commit_hs_new_block_msg(hs_new_block_message_ptr msg){
emit( self.new_hs_new_block_message, msg );
}

/**
* This method is called from other threads. The controller_impl should outlive those threads.
* However, to avoid race conditions, it means that the behavior of this function should not change
Expand Down Expand Up @@ -2983,22 +2967,6 @@ void controller::commit_block() {
my->commit_block(block_status::incomplete);
}

void controller::commit_hs_proposal_msg(hs_proposal_message_ptr msg) {
my->commit_hs_proposal_msg(msg);
}

void controller::commit_hs_vote_msg(hs_vote_message_ptr msg) {
my->commit_hs_vote_msg(msg);
}

void controller::commit_hs_new_view_msg(hs_new_view_message_ptr msg) {
my->commit_hs_new_view_msg(msg);
}

void controller::commit_hs_new_block_msg(hs_new_block_message_ptr msg) {
my->commit_hs_new_block_msg(msg);
}

deque<transaction_metadata_ptr> controller::abort_block() {
return my->abort_block();
}
Expand Down
29 changes: 0 additions & 29 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ namespace eosio { namespace vm { class wasm_allocator; }}

namespace eosio { namespace chain {

struct hs_proposal_message;
struct hs_vote_message;
struct hs_new_view_message;
struct hs_new_block_message;
using hs_proposal_message_ptr = std::shared_ptr<hs_proposal_message>;
using hs_vote_message_ptr = std::shared_ptr<hs_vote_message>;
using hs_new_view_message_ptr = std::shared_ptr<hs_new_view_message>;
using hs_new_block_message_ptr = std::shared_ptr<hs_new_block_message>;

class authorization_manager;

namespace resource_limits {
Expand Down Expand Up @@ -176,12 +167,6 @@ namespace eosio { namespace chain {
void sign_block( const signer_callback_type& signer_callback );
void commit_block();

void commit_hs_proposal_msg(hs_proposal_message_ptr msg);
void commit_hs_vote_msg(hs_vote_message_ptr msg);

void commit_hs_new_view_msg(hs_new_view_message_ptr msg);
void commit_hs_new_block_msg(hs_new_block_message_ptr msg);

// thread-safe
std::future<block_state_ptr> create_block_state_future( const block_id_type& id, const signed_block_ptr& b );
// thread-safe
Expand Down Expand Up @@ -352,20 +337,6 @@ namespace eosio { namespace chain {
signal<void(const transaction_metadata_ptr&)> accepted_transaction;
signal<void(std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&>)> applied_transaction;
signal<void(const int&)> bad_alloc;
signal<void(const hs_proposal_message_ptr&)> new_hs_proposal_message;
signal<void(const hs_vote_message_ptr&)> new_hs_vote_message;
signal<void(const hs_new_view_message_ptr&)> new_hs_new_view_message;
signal<void(const hs_new_block_message_ptr&)> new_hs_new_block_message;

/*
signal<void()> pre_apply_block;
signal<void()> post_apply_block;
signal<void()> abort_apply_block;
signal<void(const transaction_metadata_ptr&)> pre_apply_transaction;
signal<void(const transaction_trace_ptr&)> post_apply_transaction;
signal<void(const transaction_trace_ptr&)> pre_apply_action;
signal<void(const transaction_trace_ptr&)> post_apply_action;
*/

const apply_handler* find_apply_handler( account_name contract, scope_name scope, action_name act )const;
wasm_interface_collection& get_wasm_interface();
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/hotstuff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace eosio::chain {
quorum_certificate high_qc; //justification
};

using hs_message = std::variant<hs_vote_message, hs_proposal_message, hs_new_block_message, hs_new_view_message>;

struct finalizer_state {

bool chained_mode = false;
Expand Down
31 changes: 23 additions & 8 deletions libraries/hotstuff/chain_pacemaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ namespace eosio { namespace hotstuff {
{
}

void chain_pacemaker::register_bcast_function(std::function<void(const chain::hs_message&)> broadcast_hs_message) {
FC_ASSERT(broadcast_hs_message, "on_hs_message must be provided");
std::lock_guard g( _hotstuff_global_mutex ); // not actually needed but doesn't hurt
bcast_hs_message = std::move(broadcast_hs_message);
}

// Called internally by the chain_pacemaker to decide whether it should do something or not, based on feature activation.
// Only methods called by the outside need to call this; methods called by qc_chain only don't need to check for enable().
bool chain_pacemaker::enabled() const {
Expand Down Expand Up @@ -265,25 +271,31 @@ namespace eosio { namespace hotstuff {
}

void chain_pacemaker::send_hs_proposal_msg(const hs_proposal_message& msg, name id) {
hs_proposal_message_ptr msg_ptr = std::make_shared<hs_proposal_message>(msg);
_chain->commit_hs_proposal_msg(msg_ptr);
bcast_hs_message(msg);
}

void chain_pacemaker::send_hs_vote_msg(const hs_vote_message& msg, name id) {
hs_vote_message_ptr msg_ptr = std::make_shared<hs_vote_message>(msg);
_chain->commit_hs_vote_msg(msg_ptr);
bcast_hs_message(msg);
}

void chain_pacemaker::send_hs_new_block_msg(const hs_new_block_message& msg, name id) {
hs_new_block_message_ptr msg_ptr = std::make_shared<hs_new_block_message>(msg);
_chain->commit_hs_new_block_msg(msg_ptr);
bcast_hs_message(msg);
}

void chain_pacemaker::send_hs_new_view_msg(const hs_new_view_message& msg, name id) {
hs_new_view_message_ptr msg_ptr = std::make_shared<hs_new_view_message>(msg);
_chain->commit_hs_new_view_msg(msg_ptr);
bcast_hs_message(msg);
}

void chain_pacemaker::on_hs_msg(const eosio::chain::hs_message &msg) {
std::visit(overloaded{
[this](const hs_vote_message& m) { on_hs_vote_msg(m); },
[this](const hs_proposal_message& m) { on_hs_proposal_msg(m); },
[this](const hs_new_block_message& m) { on_hs_new_block_msg(m); },
[this](const hs_new_view_message& m) { on_hs_new_view_msg(m); },
}, msg);
}

// called from net threads
void chain_pacemaker::on_hs_proposal_msg(const hs_proposal_message& msg) {
if (! enabled())
return;
Expand All @@ -295,6 +307,7 @@ namespace eosio { namespace hotstuff {
prof.core_out();
}

// called from net threads
void chain_pacemaker::on_hs_vote_msg(const hs_vote_message& msg) {
if (! enabled())
return;
Expand All @@ -306,6 +319,7 @@ namespace eosio { namespace hotstuff {
prof.core_out();
}

// called from net threads
void chain_pacemaker::on_hs_new_block_msg(const hs_new_block_message& msg) {
if (! enabled())
return;
Expand All @@ -317,6 +331,7 @@ namespace eosio { namespace hotstuff {
prof.core_out();
}

// called from net threads
void chain_pacemaker::on_hs_new_view_msg(const hs_new_view_message& msg) {
if (! enabled())
return;
Expand Down
12 changes: 8 additions & 4 deletions libraries/hotstuff/include/eosio/hotstuff/chain_pacemaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ namespace eosio::hotstuff {
//class-specific functions

chain_pacemaker(controller* chain, std::set<account_name> my_producers, fc::logger& logger);
void register_bcast_function(std::function<void(const chain::hs_message&)> broadcast_hs_message);

void beat();

void on_hs_proposal_msg(const hs_proposal_message& msg); //consensus msg event handler
void on_hs_vote_msg(const hs_vote_message& msg); //confirmation msg event handler
void on_hs_new_view_msg(const hs_new_view_message& msg); //new view msg event handler
void on_hs_new_block_msg(const hs_new_block_message& msg); //new block msg event handler
void on_hs_msg(const hs_message& msg);

void get_state(finalizer_state& fs) const;

Expand Down Expand Up @@ -51,6 +49,11 @@ namespace eosio::hotstuff {
// Check if consensus upgrade feature is activated
bool enabled() const;

void on_hs_proposal_msg(const hs_proposal_message& msg); //consensus msg event handler
void on_hs_vote_msg(const hs_vote_message& msg); //confirmation msg event handler
void on_hs_new_view_msg(const hs_new_view_message& msg); //new view msg event handler
void on_hs_new_block_msg(const hs_new_block_message& msg); //new block msg event handler

// This serializes all messages (high-level requests) to the qc_chain core.
// For maximum safety, the qc_chain core will only process one request at a time.
// These requests can come directly from the net threads, or indirectly from a
Expand All @@ -67,6 +70,7 @@ namespace eosio::hotstuff {
chain::controller* _chain = nullptr;

qc_chain _qc_chain;
std::function<void(const chain::hs_message&)> bcast_hs_message;

uint32_t _quorum_threshold = 15; //FIXME/TODO: calculate from schedule
fc::logger& _logger;
Expand Down
30 changes: 13 additions & 17 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,12 @@ void chain_plugin::create_pacemaker(std::set<chain::account_name> my_producers)
my->_chain_pacemaker.emplace(&chain(), std::move(my_producers), hotstuff_logger);
}

void chain_plugin::register_pacemaker_bcast_function(std::function<void(const chain::hs_message&)> bcast_hs_message) {
EOS_ASSERT( my->_chain_pacemaker, plugin_config_exception, "chain_pacemaker not created" );
my->_chain_pacemaker->register_bcast_function(std::move(bcast_hs_message));
}


void chain_plugin::plugin_initialize(const variables_map& options) {
handle_sighup(); // Sets loggers
my->plugin_initialize(options);
Expand Down Expand Up @@ -2651,7 +2657,7 @@ read_only::get_finalizer_state_results
read_only::get_finalizer_state(const get_finalizer_state_params&, const fc::time_point& deadline ) const {
get_finalizer_state_results results;

if ( chain_pacemaker ) { // producer_plug is null when called from chain_plugin_tests.cpp and get_table_tests.cpp
if ( chain_pacemaker ) { // is null when called from chain_plugin_tests.cpp and get_table_tests.cpp
finalizer_state fs;
chain_pacemaker->get_state( fs );
results.chained_mode = fs.chained_mode;
Expand All @@ -2665,8 +2671,9 @@ read_only::get_finalizer_state(const get_finalizer_state_params&, const fc::time
results.high_qc = fs.high_qc;
results.current_qc = fs.current_qc;
results.schedule = fs.schedule;
for (auto proposal: fs.proposals) {
chain::hs_proposal_message & p = proposal.second;
results.proposals.reserve( fs.proposals.size() );
for (const auto& proposal : fs.proposals) {
const chain::hs_proposal_message& p = proposal.second;
results.proposals.push_back( hs_complete_proposal_message( p ) );
}
}
Expand All @@ -2675,20 +2682,9 @@ read_only::get_finalizer_state(const get_finalizer_state_params&, const fc::time

} // namespace chain_apis

void chain_plugin::notify_hs_vote_message( const hs_vote_message& msg ) {
my->_chain_pacemaker->on_hs_vote_msg(msg);
};

void chain_plugin::notify_hs_proposal_message( const hs_proposal_message& msg ) {
my->_chain_pacemaker->on_hs_proposal_msg(msg);
};

void chain_plugin::notify_hs_new_view_message( const hs_new_view_message& msg ) {
my->_chain_pacemaker->on_hs_new_view_msg(msg);
};

void chain_plugin::notify_hs_new_block_message( const hs_new_block_message& msg ) {
my->_chain_pacemaker->on_hs_new_block_msg(msg);
// called from net threads
void chain_plugin::notify_hs_message( const hs_message& msg ) {
my->_chain_pacemaker->on_hs_msg(msg);
};

void chain_plugin::notify_hs_block_produced() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ class read_only : public api_base {
uint8_t phase_counter = 0;
uint32_t block_height = 0;
uint64_t view_number = 0;
explicit hs_complete_proposal_message( const chain::hs_proposal_message & p ) {
explicit hs_complete_proposal_message( const chain::hs_proposal_message& p ) {
proposal_id = p.proposal_id;
block_id = p.block_id;
parent_id = p.parent_id;
Expand Down Expand Up @@ -1032,10 +1032,8 @@ class chain_plugin : public plugin<chain_plugin> {
const controller& chain() const;

void create_pacemaker(std::set<chain::account_name> my_producers);
void notify_hs_vote_message( const chain::hs_vote_message& msg );
void notify_hs_proposal_message( const chain::hs_proposal_message& msg );
void notify_hs_new_view_message( const chain::hs_new_view_message& msg );
void notify_hs_new_block_message( const chain::hs_new_block_message& msg );
void register_pacemaker_bcast_function(std::function<void(const chain::hs_message&)> bcast_hs_message);
void notify_hs_message( const chain::hs_message& msg );
void notify_hs_block_produced();

chain::chain_id_type get_chain_id() const;
Expand Down
3 changes: 2 additions & 1 deletion plugins/net_plugin/include/eosio/net_plugin/net_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <eosio/chain/application.hpp>
#include <eosio/net_plugin/protocol.hpp>
#include <eosio/chain_plugin/chain_plugin.hpp>
#include <eosio/producer_plugin/producer_plugin.hpp>

namespace eosio {
using namespace appbase;
Expand All @@ -26,7 +27,7 @@ namespace eosio {
net_plugin();
virtual ~net_plugin();

APPBASE_PLUGIN_REQUIRES((chain_plugin))
APPBASE_PLUGIN_REQUIRES((chain_plugin)(producer_plugin))
virtual void set_program_options(options_description& cli, options_description& cfg) override;
void handle_sighup() override;

Expand Down
5 changes: 1 addition & 4 deletions plugins/net_plugin/include/eosio/net_plugin/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ namespace eosio {
sync_request_message,
signed_block,
packed_transaction,
hs_vote_message,
hs_proposal_message,
hs_new_view_message,
hs_new_block_message>;
hs_message>;

} // namespace eosio

Expand Down
Loading

0 comments on commit 5dfa47d

Please sign in to comment.