Skip to content

Commit

Permalink
GH-3 Simplify by adding a vote_signal_t type
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Apr 16, 2024
1 parent 8f26a50 commit 6cc6820
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
16 changes: 8 additions & 8 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,14 +947,14 @@ struct controller_impl {
signal<void(const block_signal_params&)> accepted_block;
signal<void(const block_signal_params&)> irreversible_block;
signal<void(std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&>)> applied_transaction;
signal<void(const vote_signal_params&)> voted_block;
vote_signal_t voted_block;

vote_processor_t vote_processor{voted_block,
[this](const block_id_type& id) -> block_state_ptr {
return fork_db.apply_s<block_state_ptr>([&](const auto& forkdb) {
return forkdb.get_block(id);
});
}};
vote_processor_t vote_processor{voted_block,
[this](const block_id_type& id) -> block_state_ptr {
return fork_db.apply_s<block_state_ptr>([&](const auto& forkdb) {
return forkdb.get_block(id);
});
}};

int64_t set_proposed_producers( vector<producer_authority> producers );
int64_t set_proposed_producers_legacy( vector<producer_authority> producers );
Expand Down Expand Up @@ -5548,7 +5548,7 @@ signal<void(const block_signal_params&)>& controller::accepted_block_header() {
signal<void(const block_signal_params&)>& controller::accepted_block() { return my->accepted_block; }
signal<void(const block_signal_params&)>& controller::irreversible_block() { return my->irreversible_block; }
signal<void(std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&>)>& controller::applied_transaction() { return my->applied_transaction; }
signal<void(const vote_signal_params&)>& controller::voted_block() { return my->voted_block; }
vote_signal_t& controller::voted_block() { return my->voted_block; }

chain_id_type controller::extract_chain_id(snapshot_reader& snapshot) {
chain_snapshot_header header;
Expand Down
3 changes: 2 additions & 1 deletion libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace eosio::chain {
using block_signal_params = std::tuple<const signed_block_ptr&, const block_id_type&>;
// connection_id, vote result status, vote_message processed
using vote_signal_params = std::tuple<uint32_t, vote_status, const vote_message_ptr&>;
using vote_signal_t = signal<void(const vote_signal_params&)>;

enum class db_read_mode {
HEAD,
Expand Down Expand Up @@ -377,7 +378,7 @@ namespace eosio::chain {
signal<void(const block_signal_params&)>& irreversible_block();
signal<void(std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&>)>& applied_transaction();
// Unlike other signals, voted_block is signaled from other threads than the main thread.
signal<void(const vote_signal_params&)>& voted_block();
vote_signal_t& voted_block();

const apply_handler* find_apply_handler( account_name contract, scope_name scope, action_name act )const;
wasm_interface& get_wasm_interface();
Expand Down
15 changes: 6 additions & 9 deletions libraries/chain/include/eosio/chain/vote_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include <eosio/chain/hotstuff/hotstuff.hpp>
#include <eosio/chain/block_state.hpp>
#include <eosio/chain/controller.hpp>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>

namespace eosio { namespace chain {
namespace eosio::chain {

/**
* Process votes in a dedicated thread pool.
Expand All @@ -32,8 +33,6 @@ class vote_processor_t {
block_num_type block_num() const { return block_header::num_from_id(msg->block_id); }
};

using vote_signal_type = decltype(controller({},chain_id_type::empty_chain_id()).voted_block());

using vote_index_type = boost::multi_index_container< vote,
indexed_by<
ordered_non_unique<tag<by_block_num>,
Expand All @@ -48,7 +47,7 @@ class vote_processor_t {

using fetch_block_func_t = std::function<block_state_ptr(const block_id_type&)>;

vote_signal_type& vote_signal;
vote_signal_t& vote_signal;
fetch_block_func_t fetch_block_func;

std::mutex mtx;
Expand Down Expand Up @@ -119,7 +118,7 @@ class vote_processor_t {
}

public:
explicit vote_processor_t(vote_signal_type& vote_signal, fetch_block_func_t&& get_block)
explicit vote_processor_t(vote_signal_t& vote_signal, fetch_block_func_t&& get_block)
: vote_signal(vote_signal)
, fetch_block_func(get_block)
{}
Expand Down Expand Up @@ -148,9 +147,7 @@ class vote_processor_t {
while (!stopped) {
std::unique_lock g(mtx);
cv.wait(g, [&]() {
if (!index.empty() || stopped)
return true;
return false;
return !index.empty() || stopped;
});
if (stopped)
break;
Expand Down Expand Up @@ -240,4 +237,4 @@ class vote_processor_t {

};

} } //eosio::chain
} // namespace eosio::chain
2 changes: 1 addition & 1 deletion unittests/vote_processor_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ vote_message_ptr make_vote_message(const block_state_ptr& bsp) {
BOOST_AUTO_TEST_SUITE(vote_processor_tests)

BOOST_AUTO_TEST_CASE( vote_processor_test ) {
boost::signals2::signal<void(const vote_signal_params&)> voted_block;
vote_signal_t voted_block;

uint32_t received_connection_id = 0;
vote_status received_vote_status = vote_status::unknown_block;
Expand Down

0 comments on commit 6cc6820

Please sign in to comment.