Skip to content

Commit

Permalink
GH-376 Renamed open_qc_sig_t to in_progress_qc_sig_t and open_qc_t to…
Browse files Browse the repository at this point in the history
… in_progress_qc_t.
  • Loading branch information
heifner committed Jul 24, 2024
1 parent 058b457 commit eccecad
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 96 deletions.
16 changes: 8 additions & 8 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ block_state::block_state(const block_header_state& prev, signed_block_ptr b, con
, block(std::move(b))
, strong_digest(compute_finality_digest())
, weak_digest(create_weak_digest(strong_digest))
, open_qc(active_finalizer_policy, pending_finalizer_policy ? pending_finalizer_policy->second : finalizer_policy_ptr{})
, in_progress_qc(active_finalizer_policy, pending_finalizer_policy ? pending_finalizer_policy->second : finalizer_policy_ptr{})
{
// ASSUMPTION FROM controller_impl::apply_block = all untrusted blocks will have their signatures pre-validated here
if( !skip_validate_signee ) {
Expand All @@ -37,7 +37,7 @@ block_state::block_state(const block_header_state& bhs,
, block(std::make_shared<signed_block>(signed_block_header{bhs.header}))
, strong_digest(compute_finality_digest())
, weak_digest(create_weak_digest(strong_digest))
, open_qc(active_finalizer_policy, pending_finalizer_policy ? pending_finalizer_policy->second : finalizer_policy_ptr{})
, in_progress_qc(active_finalizer_policy, pending_finalizer_policy ? pending_finalizer_policy->second : finalizer_policy_ptr{})
, valid(valid)
, pub_keys_recovered(true) // called by produce_block so signature recovery of trxs must have been done
, cached_trxs(std::move(trx_metas))
Expand Down Expand Up @@ -93,8 +93,8 @@ block_state_ptr block_state::create_if_genesis_block(const block_state_legacy& b
result.strong_digest = result.compute_finality_digest(); // all block_header_state data populated in result at this point
result.weak_digest = create_weak_digest(result.strong_digest);

// open_qc will not be used in the genesis block as finalizers will not vote on it, but still create it for consistency.
result.open_qc = open_qc_t{result.active_finalizer_policy, finalizer_policy_ptr{}};
// in_progress_qc will not be used in the genesis block as finalizers will not vote on it, but still create it for consistency.
result.in_progress_qc = in_progress_qc_t{result.active_finalizer_policy, finalizer_policy_ptr{}};

// build leaf_node and validation_tree
valid_t::finality_leaf_node_t leaf_node {
Expand Down Expand Up @@ -156,7 +156,7 @@ block_state::block_state(snapshot_detail::snapshot_block_state_v7&& sbs)
}
, strong_digest(compute_finality_digest())
, weak_digest(create_weak_digest(strong_digest))
, open_qc(active_finalizer_policy, pending_finalizer_policy ? pending_finalizer_policy->second : finalizer_policy_ptr{}) // just in case we receive votes
, in_progress_qc(active_finalizer_policy, pending_finalizer_policy ? pending_finalizer_policy->second : finalizer_policy_ptr{}) // just in case we receive votes
, valid(std::move(sbs.valid))
{
header_exts = header.validate_and_extract_header_extensions();
Expand All @@ -177,17 +177,17 @@ void block_state::set_trxs_metas( deque<transaction_metadata_ptr>&& trxs_metas,
// Called from vote threads
vote_result_t block_state::aggregate_vote(uint32_t connection_id, const vote_message& vote) {
auto finalizer_digest = vote.strong ? strong_digest.to_uint8_span() : std::span<const uint8_t>(weak_digest);
return open_qc.aggregate_vote(connection_id, vote, block_num(), finalizer_digest);
return in_progress_qc.aggregate_vote(connection_id, vote, block_num(), finalizer_digest);
}

// Only used for testing
vote_status_t block_state::has_voted(const bls_public_key& key) const {
return open_qc.has_voted(key);
return in_progress_qc.has_voted(key);
}

// Called from net threads
void block_state::verify_qc(const qc_t& qc) const {
open_qc.verify_qc(qc, strong_digest, weak_digest);
in_progress_qc.verify_qc(qc, strong_digest, weak_digest);
}

qc_claim_t block_state::extract_qc_claim() const {
Expand Down
6 changes: 3 additions & 3 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3718,15 +3718,15 @@ struct controller_impl {
block_state_ptr bsp = fork_db_fetch_bsp_on_branch_by_num(id, qc.block_num);
if (!bsp)
return {};
return bsp->open_qc.vote_metrics(qc);
return bsp->in_progress_qc.vote_metrics(qc);
}


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 {};
return bsp->open_qc.missing_votes(qc);
return bsp->in_progress_qc.missing_votes(qc);
}

// thread safe
Expand All @@ -3739,7 +3739,7 @@ struct controller_impl {
// net plugin subscribed to this signal. it will broadcast the vote message on receiving the signal
emit(voted_block, std::tuple{uint32_t{0}, vote_result_t::success, std::cref(vote)}, __FILE__, __LINE__);

// also aggregate our own vote into the open_qc for this block, 0 connection_id indicates our own vote
// also aggregate our own vote into the in_progress_qc for this block, 0 connection_id indicates our own vote
process_vote_message(0, vote);
});
}
Expand Down
62 changes: 31 additions & 31 deletions libraries/chain/finality/qc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ void qc_sig_t::verify(const finalizer_policy_ptr& fin_policy,

}

bool open_qc_sig_t::has_voted(size_t index) const {
bool in_progess_qc_sig_t::has_voted(size_t index) const {
return strong_votes.has_voted(index) || weak_votes.has_voted(index);
}

void open_qc_sig_t::votes_t::reflector_init() {
void in_progess_qc_sig_t::votes_t::reflector_init() {
processed = std::vector<bit_processed>(bitset.size());
for (size_t i = 0; i < bitset.size(); ++i) {
if (bitset[i]) {
Expand All @@ -109,50 +109,50 @@ void open_qc_sig_t::votes_t::reflector_init() {
}
}

bool open_qc_sig_t::votes_t::has_voted(size_t index) const {
bool in_progess_qc_sig_t::votes_t::has_voted(size_t index) const {
assert(index < processed.size());
return processed[index].value.load(std::memory_order_relaxed);
}


void open_qc_sig_t::votes_t::add_vote(size_t index, const bls_signature& signature) {
void in_progess_qc_sig_t::votes_t::add_vote(size_t index, const bls_signature& signature) {
processed[index].value.store(true, std::memory_order_relaxed);
bitset.set(index);
sig.aggregate(signature); // works even if _sig is default initialized (fp2::zero())
}

open_qc_sig_t::open_qc_sig_t()
in_progess_qc_sig_t::in_progess_qc_sig_t()
: _mtx(std::make_unique<std::mutex>()) {
}

open_qc_sig_t::open_qc_sig_t(size_t num_finalizers, uint64_t quorum, uint64_t max_weak_sum_before_weak_final)
in_progess_qc_sig_t::in_progess_qc_sig_t(size_t num_finalizers, uint64_t quorum, uint64_t max_weak_sum_before_weak_final)
: _mtx(std::make_unique<std::mutex>())
, quorum(quorum)
, max_weak_sum_before_weak_final(max_weak_sum_before_weak_final)
, weak_votes(num_finalizers)
, strong_votes(num_finalizers) {
}

open_qc_sig_t::open_qc_sig_t(const finalizer_policy_ptr& finalizer_policy)
: open_qc_sig_t(finalizer_policy->finalizers.size(),
finalizer_policy->threshold,
finalizer_policy->max_weak_sum_before_weak_final()) {
in_progess_qc_sig_t::in_progess_qc_sig_t(const finalizer_policy_ptr& finalizer_policy)
: in_progess_qc_sig_t(finalizer_policy->finalizers.size(),
finalizer_policy->threshold,
finalizer_policy->max_weak_sum_before_weak_final()) {
}

bool open_qc_sig_t::is_quorum_met() const {
bool in_progess_qc_sig_t::is_quorum_met() const {
std::lock_guard g(*_mtx);
return is_quorum_met_no_lock();
}

// called with held mutex
vote_result_t open_qc_sig_t::check_duplicate(size_t index) {
vote_result_t in_progess_qc_sig_t::check_duplicate(size_t index) {
if (strong_votes.bitset[index] || weak_votes.bitset[index])
return vote_result_t::duplicate;
return vote_result_t::success;
}

// called by add_vote, already protected by mutex
vote_result_t open_qc_sig_t::add_strong_vote(size_t index, const bls_signature& sig, uint64_t weight) {
vote_result_t in_progess_qc_sig_t::add_strong_vote(size_t index, const bls_signature& sig, uint64_t weight) {
strong_votes.add_vote(index, sig);
strong_sum += weight;

Expand Down Expand Up @@ -180,7 +180,7 @@ vote_result_t open_qc_sig_t::add_strong_vote(size_t index, const bls_signature&
}

// called by add_vote, already protected by mutex
vote_result_t open_qc_sig_t::add_weak_vote(size_t index, const bls_signature& sig, uint64_t weight) {
vote_result_t in_progess_qc_sig_t::add_weak_vote(size_t index, const bls_signature& sig, uint64_t weight) {
weak_votes.add_vote(index, sig);
weak_sum += weight;

Expand Down Expand Up @@ -212,7 +212,7 @@ 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,
vote_result_t in_progess_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) {
std::unique_lock g(*_mtx);
Expand All @@ -233,7 +233,7 @@ vote_result_t open_qc_sig_t::add_vote(uint32_t connection_id, block_num_type blo
}

// called by get_best_qc which acquires a mutex
qc_sig_t open_qc_sig_t::extract_qc_sig_from_open() const {
qc_sig_t in_progess_qc_sig_t::extract_qc_sig_from_open() const {
qc_sig_t qc_sig;

if( pending_state == state_t::strong ) {
Expand All @@ -250,7 +250,7 @@ qc_sig_t open_qc_sig_t::extract_qc_sig_from_open() const {
return qc_sig;
}

std::optional<qc_sig_t> open_qc_sig_t::get_best_qc() const {
std::optional<qc_sig_t> in_progess_qc_sig_t::get_best_qc() const {
std::lock_guard g(*_mtx);
// if this does not have a valid QC, consider received_qc_sig only
if( !is_quorum_met_no_lock() ) {
Expand All @@ -275,7 +275,7 @@ std::optional<qc_sig_t> open_qc_sig_t::get_best_qc() const {
return std::optional{qc_sig_t{ std::move(qc_sig_from_open) }};
}

bool open_qc_sig_t::set_received_qc_sig(const qc_sig_t& qc) {
bool in_progess_qc_sig_t::set_received_qc_sig(const qc_sig_t& qc) {
std::lock_guard g(*_mtx);
if (!received_qc_sig || (received_qc_sig->is_weak() && qc.is_strong())) {
received_qc_sig = qc;
Expand All @@ -284,16 +284,16 @@ bool open_qc_sig_t::set_received_qc_sig(const qc_sig_t& qc) {
return false;
}

bool open_qc_sig_t::received_qc_sig_is_strong() const {
bool in_progess_qc_sig_t::received_qc_sig_is_strong() const {
std::lock_guard g(*_mtx);
return received_qc_sig && received_qc_sig->is_strong();
}

bool open_qc_sig_t::is_quorum_met_no_lock() const {
bool in_progess_qc_sig_t::is_quorum_met_no_lock() const {
return is_quorum_met(pending_state);
}

std::optional<qc_t> open_qc_t::get_best_qc(block_num_type block_num) const {
std::optional<qc_t> in_progress_qc_t::get_best_qc(block_num_type block_num) const {
std::optional<qc_sig_t> active_best_qc = active_policy_sig.get_best_qc();
if (!active_best_qc) // active is always required
return {};
Expand All @@ -309,7 +309,7 @@ std::optional<qc_t> open_qc_t::get_best_qc(block_num_type block_num) const {
return std::optional<qc_t>{qc_t{block_num, std::move(*active_best_qc), {}}};
}

void open_qc_t::verify_qc(const qc_t& qc, const digest_type& strong_digest, const weak_digest_t& weak_digest) const {
void in_progress_qc_t::verify_qc(const qc_t& qc, const digest_type& strong_digest, const weak_digest_t& weak_digest) const {
if (qc.pending_policy_sig) {
EOS_ASSERT(pending_finalizer_policy, invalid_qc_claim,
"qc ${bn} contains pending policy signature for nonexistent pending finalizer policy", ("bn", qc.block_num));
Expand All @@ -324,7 +324,7 @@ void open_qc_t::verify_qc(const qc_t& qc, const digest_type& strong_digest, cons
}
}

bool open_qc_t::set_received_qc(const qc_t& qc) {
bool in_progress_qc_t::set_received_qc(const qc_t& qc) {
// qc should have already been verified via verify_qc, this EOS_ASSERT should never fire
EOS_ASSERT(!pending_policy_sig || qc.pending_policy_sig, invalid_qc_claim,
"qc ${bn} expected to have a pending policy signature", ("bn", qc.block_num));
Expand All @@ -336,14 +336,14 @@ bool open_qc_t::set_received_qc(const qc_t& qc) {
return active_better || pending_better;
}

bool open_qc_t::received_qc_is_strong() const {
bool in_progress_qc_t::received_qc_is_strong() const {
if (!pending_policy_sig) { // consider only active
return active_policy_sig.received_qc_sig_is_strong();
}
return active_policy_sig.received_qc_sig_is_strong() && pending_policy_sig->received_qc_sig_is_strong();
}

vote_result_t open_qc_t::aggregate_vote(uint32_t connection_id, const vote_message& vote,
vote_result_t in_progress_qc_t::aggregate_vote(uint32_t connection_id, const vote_message& vote,
block_num_type block_num, std::span<const uint8_t> finalizer_digest)
{
bool verified_sig = false;
Expand All @@ -357,7 +357,7 @@ vote_result_t open_qc_t::aggregate_vote(uint32_t connection_id, const vote_messa
return vote_result_t::success;
};

auto add_vote = [&](const finalizer_policy_ptr& finalizer_policy, open_qc_sig_t& open_qc_sig) -> vote_result_t {
auto add_vote = [&](const finalizer_policy_ptr& finalizer_policy, in_progess_qc_sig_t& open_qc_sig) -> vote_result_t {
const auto& finalizers = finalizer_policy->finalizers;
auto itr = std::ranges::find_if(finalizers, [&](const auto& finalizer) { return finalizer.public_key == vote.finalizer_key; });
vote_result_t s = vote_result_t::unknown_public_key;
Expand Down Expand Up @@ -397,9 +397,9 @@ vote_result_t open_qc_t::aggregate_vote(uint32_t connection_id, const vote_messa
return s;
}

vote_status_t open_qc_t::has_voted(const bls_public_key& key) const {
vote_status_t in_progress_qc_t::has_voted(const bls_public_key& key) const {
auto finalizer_has_voted = [](const finalizer_policy_ptr& policy,
const open_qc_sig_t& open_qc_sig,
const in_progess_qc_sig_t& open_qc_sig,
const bls_public_key& key) -> vote_status_t {
const auto& finalizers = policy->finalizers;
auto it = std::ranges::find_if(finalizers, [&](const auto& finalizer) { return finalizer.public_key == key; });
Expand All @@ -425,11 +425,11 @@ vote_status_t open_qc_t::has_voted(const bls_public_key& key) const {
return pending_status;
}

bool open_qc_t::is_quorum_met() const {
bool in_progress_qc_t::is_quorum_met() const {
return active_policy_sig.is_quorum_met() && (!pending_policy_sig || pending_policy_sig->is_quorum_met());
}

qc_vote_metrics_t open_qc_t::vote_metrics(const qc_t& qc) const {
qc_vote_metrics_t in_progress_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_t& results) {
Expand Down Expand Up @@ -474,7 +474,7 @@ qc_vote_metrics_t open_qc_t::vote_metrics(const qc_t& qc) const {
return result;
}

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

Expand Down
12 changes: 6 additions & 6 deletions libraries/chain/include/eosio/chain/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct block_state : public block_header_state { // block_header_state provi
signed_block_ptr block;
digest_type strong_digest; // finalizer_digest (strong, cached so we can quickly validate votes)
weak_digest_t weak_digest; // finalizer_digest (weak, cached so we can quickly validate votes)
open_qc_t open_qc; // where we accumulate votes we receive
in_progress_qc_t in_progress_qc; // where we accumulate votes we receive
std::optional<valid_t> valid;

// ------ updated for votes, used for fork_db ordering ------------------------------
Expand Down Expand Up @@ -110,10 +110,10 @@ struct block_state : public block_header_state { // block_header_state provi

uint32_t final_on_strong_qc_block_num() const { return core.final_on_strong_qc_block_num; }

std::optional<qc_t> get_best_qc() const { return open_qc.get_best_qc(block_num()); } // thread safe
bool received_qc_is_strong() const { return open_qc.received_qc_is_strong(); } // thread safe
std::optional<qc_t> get_best_qc() const { return in_progress_qc.get_best_qc(block_num()); } // thread safe
bool received_qc_is_strong() const { return in_progress_qc.received_qc_is_strong(); } // thread safe
// return true if better qc, thread safe
bool set_received_qc(const qc_t& qc) { return open_qc.set_received_qc(qc); }
bool set_received_qc(const qc_t& qc) { return in_progress_qc.set_received_qc(qc); }
// extract the qc_claim from block header finality_extension
qc_claim_t extract_qc_claim() const;

Expand All @@ -137,7 +137,7 @@ struct block_state : public block_header_state { // block_header_state provi
finality_data_t get_finality_data();

// connection_id only for logging
vote_result_t aggregate_vote(uint32_t connection_id, const vote_message& vote); // aggregate vote into open_qc
vote_result_t aggregate_vote(uint32_t connection_id, const vote_message& vote); // aggregate vote into in_progress_qc
vote_status_t has_voted(const bls_public_key& key) const;
void verify_qc(const qc_t& qc) const; // verify given qc_t is valid with respect block_state

Expand Down Expand Up @@ -185,4 +185,4 @@ using block_state_pair = std::pair<std::shared_ptr<block_state_legacy>, blo
FC_REFLECT( eosio::chain::valid_t::finality_leaf_node_t, (major_version)(minor_version)(block_num)(finality_digest)(action_mroot) )
FC_REFLECT( eosio::chain::valid_t, (validation_tree)(validation_mroots))
FC_REFLECT( eosio::chain::finality_data_t, (major_version)(minor_version)(active_finalizer_policy_generation)(final_on_strong_qc_block_num)(action_mroot)(reversible_blocks_mroot)(base_digest)(pending_finalizer_policy) )
FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(strong_digest)(weak_digest)(open_qc)(valid)(validated) )
FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(strong_digest)(weak_digest)(in_progress_qc)(valid)(validated) )
Loading

0 comments on commit eccecad

Please sign in to comment.