Skip to content

Commit

Permalink
GH-376 Rename vote_bitset to vote_bitset_t. Rename fin_auth_set to fi…
Browse files Browse the repository at this point in the history
…n_auth_set_t
  • Loading branch information
heifner committed Jul 24, 2024
1 parent 386c2ea commit 1da5de8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 79 deletions.
4 changes: 2 additions & 2 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3722,7 +3722,7 @@ struct controller_impl {
}


qc_vote_metrics_t::fin_auth_set missing_votes(const block_id_type& id, const qc_t& qc) const {
qc_vote_metrics_t::fin_auth_set_t missing_votes(const block_id_type& id, const qc_t& qc) const {
block_state_ptr bsp = fork_db_fetch_bsp_on_branch_by_num(id, qc.block_num);
if (!bsp)
return {};
Expand Down Expand Up @@ -5431,7 +5431,7 @@ qc_vote_metrics_t controller::vote_metrics(const block_id_type& id, const qc_t&
return my->vote_metrics(id, qc);
}

qc_vote_metrics_t::fin_auth_set controller::missing_votes(const block_id_type& id, const qc_t& qc) const {
qc_vote_metrics_t::fin_auth_set_t controller::missing_votes(const block_id_type& id, const qc_t& qc) const {
return my->missing_votes(id, qc);
}

Expand Down
24 changes: 12 additions & 12 deletions libraries/chain/finality/qc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace eosio::chain {

inline std::string bitset_to_string(const vote_bitset& bs) {
inline std::string bitset_to_string(const vote_bitset_t& bs) {
std::string r;
boost::to_string(bs, r);
return r;
}

inline vote_bitset vector_to_bitset(const std::vector<uint32_t>& v) {
inline vote_bitset_t vector_to_bitset(const std::vector<uint32_t>& v) {
return {v.cbegin(), v.cend()};
}

inline std::vector<uint32_t> bitset_to_vector(const vote_bitset& bs) {
inline std::vector<uint32_t> bitset_to_vector(const vote_bitset_t& bs) {
std::vector<uint32_t> r;
r.resize(bs.num_blocks());
boost::to_block_range(bs, r.begin());
Expand All @@ -29,7 +29,7 @@ void qc_sig_t::verify(const finalizer_policy_ptr& fin_policy,
auto num_finalizers = finalizers.size();

// utility to accumulate voted weights
auto weights = [&] ( const vote_bitset& votes_bitset ) -> uint64_t {
auto weights = [&] ( const vote_bitset_t& votes_bitset ) -> uint64_t {
EOS_ASSERT( num_finalizers == votes_bitset.size(), invalid_qc_claim,
"vote bitset size is not the same as the number of finalizers for the policy it refers to, "
"vote bitset size: ${s}, num of finalizers for the policy: ${n}",
Expand Down Expand Up @@ -220,8 +220,8 @@ vote_result_t open_qc_sig_t::add_weak_vote(size_t index, const bls_signature& si

// thread safe
vote_result_t open_qc_sig_t::add_vote(uint32_t connection_id, block_num_type block_num,
bool strong, size_t index,
const bls_signature& sig, uint64_t weight) {
bool strong, size_t index,
const bls_signature& sig, uint64_t weight) {
std::unique_lock g(*_mtx);
state_t pre_state = pending_state;
vote_result_t s = strong ? add_strong_vote(index, sig, weight)
Expand Down Expand Up @@ -432,7 +432,7 @@ bool open_qc_t::is_quorum_met() const {
qc_vote_metrics_t open_qc_t::vote_metrics(const qc_t& qc) const {
qc_vote_metrics_t result;

auto add_votes = [&](const finalizer_policy_ptr& finalizer_policy, const auto& votes, qc_vote_metrics_t::fin_auth_set& results) {
auto add_votes = [&](const finalizer_policy_ptr& finalizer_policy, const auto& votes, qc_vote_metrics_t::fin_auth_set_t& results) {
assert(votes.size() == finalizer_policy->finalizers.size());
size_t added = 0;
for (size_t i = 0; i < votes.size(); ++i) {
Expand All @@ -452,7 +452,7 @@ qc_vote_metrics_t open_qc_t::vote_metrics(const qc_t& qc) const {
added = add_votes(finalizer_policy, *qc_sig.weak_votes, result.weak_votes);
}
if (added != finalizer_policy->finalizers.size()) {
vote_bitset not_voted(finalizer_policy->finalizers.size());
vote_bitset_t not_voted(finalizer_policy->finalizers.size());
if (qc_sig.strong_votes) {
not_voted = *qc_sig.strong_votes;
}
Expand All @@ -474,17 +474,17 @@ qc_vote_metrics_t open_qc_t::vote_metrics(const qc_t& qc) const {
return result;
}

qc_vote_metrics_t::fin_auth_set open_qc_t::missing_votes(const qc_t& qc) const {
qc_vote_metrics_t::fin_auth_set_t open_qc_t::missing_votes(const qc_t& qc) const {
// all asserts are verified by verify_qc()
qc_vote_metrics_t::fin_auth_set not_voted;
qc_vote_metrics_t::fin_auth_set_t not_voted;

auto check_other = [](const auto& other_votes, size_t i) {
return other_votes && (*other_votes)[i];
};
auto add_not_voted = [&](const finalizer_policy_ptr& finalizer_policy, const qc_sig_t& qc_sig) {
assert(qc_sig.strong_votes || qc_sig.weak_votes);
const vote_bitset& votes = qc_sig.strong_votes ? *qc_sig.strong_votes : *qc_sig.weak_votes;
const std::optional<vote_bitset>& other_votes = qc_sig.strong_votes ? qc_sig.weak_votes : qc_sig.strong_votes;
const vote_bitset_t& votes = qc_sig.strong_votes ? *qc_sig.strong_votes : *qc_sig.weak_votes;
const std::optional<vote_bitset_t>& other_votes = qc_sig.strong_votes ? qc_sig.weak_votes : qc_sig.strong_votes;
const auto& finalizers = finalizer_policy->finalizers;
assert(votes.size() == finalizers.size());
assert(!other_votes || other_votes->size() == finalizers.size());
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ namespace eosio::chain {
qc_vote_metrics_t vote_metrics(const block_id_type& id, const qc_t& qc) const;
// return qc missing vote's finalizers, use instead of vote_metrics when only missing votes are needed
// thread-safe
qc_vote_metrics_t::fin_auth_set missing_votes(const block_id_type& id, const qc_t& qc) const;
qc_vote_metrics_t::fin_auth_set_t missing_votes(const block_id_type& id, const qc_t& qc) const;

void set_savanna_lib_id(const block_id_type& id);

Expand Down
62 changes: 29 additions & 33 deletions libraries/chain/include/eosio/chain/finality/qc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace eosio::chain {
using bls_aggregate_signature = fc::crypto::blslib::bls_aggregate_signature;
using bls_private_key = fc::crypto::blslib::bls_private_key;

using vote_bitset = fc::dynamic_bitset;
using vote_bitset_t = fc::dynamic_bitset;
using bls_key_map_t = std::map<bls_public_key, bls_private_key>;

constexpr std::array weak_bls_sig_postfix = { 'W', 'E', 'A', 'K' };
Expand Down Expand Up @@ -54,9 +54,9 @@ namespace eosio::chain {
bool is_weak() const { return !!weak_votes; }
bool is_strong() const { return !weak_votes; }

std::optional<vote_bitset> strong_votes;
std::optional<vote_bitset> weak_votes;
bls_aggregate_signature sig;
std::optional<vote_bitset_t> strong_votes;
std::optional<vote_bitset_t> weak_votes;
bls_aggregate_signature sig;

// called from net threads
void verify(const finalizer_policy_ptr& fin_policy, const digest_type& strong_digest, const weak_digest_t& weak_digest) const;
Expand Down Expand Up @@ -108,14 +108,14 @@ namespace eosio::chain {
friend struct fc::reflector_init_visitor<votes_t>;
friend struct fc::has_reflector_init<votes_t>;
friend class open_qc_sig_t;

vote_bitset bitset;
bls_aggregate_signature sig;
struct bit_processed {
alignas(hardware_destructive_interference_size)
std::atomic<bool> value;
};
std::vector<bit_processed> processed; // avoid locking mutex for _bitset duplicate check

vote_bitset_t bitset;
bls_aggregate_signature sig;
std::vector<bit_processed> processed; // avoid locking mutex for bitset duplicate check

void reflector_init();
public:
Expand Down Expand Up @@ -147,11 +147,11 @@ namespace eosio::chain {
}

vote_result_t add_vote(uint32_t connection_id,
block_num_type block_num,
bool strong,
size_t index,
const bls_signature& sig,
uint64_t weight);
block_num_type block_num,
bool strong,
size_t index,
const bls_signature& sig,
uint64_t weight);

bool has_voted(size_t index) const;
bool has_voted(bool strong, size_t index) const;
Expand All @@ -174,24 +174,20 @@ namespace eosio::chain {
friend struct fc::reflector<open_qc_sig_t>;
friend class qc_chain;
std::unique_ptr<std::mutex> _mtx;
std::optional<qc_sig_t> received_qc_sig; // best qc_t received from the network inside block extension
uint64_t quorum {0};
uint64_t max_weak_sum_before_weak_final {0}; // max weak sum before becoming weak_final
state_t pending_state { state_t::unrestricted };
uint64_t strong_sum {0}; // accumulated sum of strong votes so far
uint64_t weak_sum {0}; // accumulated sum of weak votes so far
votes_t weak_votes {0};
votes_t strong_votes {0};
std::optional<qc_sig_t> received_qc_sig; // best qc_t received from the network inside block extension
uint64_t quorum {0};
uint64_t max_weak_sum_before_weak_final {0}; // max weak sum before becoming weak_final
state_t pending_state { state_t::unrestricted };
uint64_t strong_sum {0}; // accumulated sum of strong votes so far
uint64_t weak_sum {0}; // accumulated sum of weak votes so far
votes_t weak_votes {0};
votes_t strong_votes {0};

// called by add_vote, already protected by mutex
vote_result_t add_strong_vote(size_t index,
const bls_signature& sig,
uint64_t weight);
vote_result_t add_strong_vote(size_t index, const bls_signature& sig, uint64_t weight);

// called by add_vote, already protected by mutex
vote_result_t add_weak_vote(size_t index,
const bls_signature& sig,
uint64_t weight);
vote_result_t add_weak_vote(size_t index, const bls_signature& sig, uint64_t weight);

bool is_quorum_met_no_lock() const;
qc_sig_t extract_qc_sig_from_open() const;
Expand All @@ -204,11 +200,11 @@ namespace eosio::chain {
return lhs->public_key < rhs->public_key;
};
};
using fin_auth_set = std::set<finalizer_authority_ptr, fin_auth_less>;
using fin_auth_set_t = std::set<finalizer_authority_ptr, fin_auth_less>;

fin_auth_set strong_votes;
fin_auth_set weak_votes;
fin_auth_set missing_votes;
fin_auth_set_t strong_votes;
fin_auth_set_t weak_votes;
fin_auth_set_t missing_votes;
};

/**
Expand All @@ -233,12 +229,12 @@ namespace eosio::chain {
void verify_qc(const qc_t& qc, const digest_type& strong_digest, const weak_digest_t& weak_digest) const;
qc_vote_metrics_t vote_metrics(const qc_t& qc) const;
// return qc missing vote's finalizers
qc_vote_metrics_t::fin_auth_set missing_votes(const qc_t& qc) const;
qc_vote_metrics_t::fin_auth_set_t missing_votes(const qc_t& qc) const;
// return true if better qc
bool set_received_qc(const qc_t& qc);
bool received_qc_is_strong() const;
vote_result_t aggregate_vote(uint32_t connection_id, const vote_message& vote,
block_num_type block_num, std::span<const uint8_t> finalizer_digest);
block_num_type block_num, std::span<const uint8_t> finalizer_digest);
vote_status_t has_voted(const bls_public_key& key) const;
bool is_quorum_met() const;

Expand Down
2 changes: 1 addition & 1 deletion plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ class producer_plugin_impl : public std::enable_shared_from_this<producer_plugin
}

void log_missing_votes(const signed_block_ptr& block, const block_id_type& id,
const qc_vote_metrics_t::fin_auth_set& missing_votes) {
const qc_vote_metrics_t::fin_auth_set_t& missing_votes) {
if (vote_logger.is_enabled(fc::log_level::info)) {
if (fc::time_point::now() - block->timestamp < fc::minutes(5) || (block->block_num() % 1000 == 0)) {
std::string not_voted;
Expand Down
Loading

0 comments on commit 1da5de8

Please sign in to comment.