Skip to content

Commit

Permalink
GH-376 Rename valid_quorum_certificate to quorum_certificate_sig
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Jul 18, 2024
1 parent d86a666 commit e80b4b4
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ vote_info_vec block_state::get_votes() const {
}

// Called from net threads
void block_state::verify_qc(const valid_quorum_certificate& qc) const {
void block_state::verify_qc(const quorum_certificate_sig& qc) const {
const auto& finalizers = active_finalizer_policy->finalizers;
auto num_finalizers = finalizers.size();

Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/finality/finalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ vote_message_ptr finalizer::maybe_vote(const bls_public_key& pub_key,
}

// ----------------------------------------------------------------------------------------
inline bool has_voted_strong(const std::vector<finalizer_authority>& active_finalizers, const valid_quorum_certificate& qc, const bls_public_key& key) {
inline bool has_voted_strong(const std::vector<finalizer_authority>& active_finalizers, const quorum_certificate_sig& qc, const bls_public_key& key) {
assert(qc.is_strong());
auto it = std::find_if(active_finalizers.begin(),
active_finalizers.end(),
Expand All @@ -139,7 +139,7 @@ inline bool has_voted_strong(const std::vector<finalizer_authority>& active_fina
return false;
}

void my_finalizers_t::maybe_update_fsi(const block_state_ptr& bsp, const valid_quorum_certificate& received_qc) {
void my_finalizers_t::maybe_update_fsi(const block_state_ptr& bsp, const quorum_certificate_sig& received_qc) {
if (finalizers.empty())
return;

Expand Down
22 changes: 11 additions & 11 deletions libraries/chain/finality/quorum_certificate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,21 @@ vote_status pending_quorum_certificate::add_vote(uint32_t connection_id, block_n
}

// called by get_best_qc which acquires a mutex
valid_quorum_certificate pending_quorum_certificate::to_valid_quorum_certificate() const {
valid_quorum_certificate valid_qc;
quorum_certificate_sig pending_quorum_certificate::to_valid_quorum_certificate() const {
quorum_certificate_sig valid_qc_sig;

if( pending_state == state_t::strong ) {
valid_qc.strong_votes = strong_votes.bitset;
valid_qc.sig = strong_votes.sig;
valid_qc_sig.strong_votes = strong_votes.bitset;
valid_qc_sig.sig = strong_votes.sig;
} else if (is_quorum_met_no_lock()) {
valid_qc.strong_votes = strong_votes.bitset;
valid_qc.weak_votes = weak_votes.bitset;
valid_qc.sig = strong_votes.sig;
valid_qc.sig.aggregate(weak_votes.sig);
valid_qc_sig.strong_votes = strong_votes.bitset;
valid_qc_sig.weak_votes = weak_votes.bitset;
valid_qc_sig.sig = strong_votes.sig;
valid_qc_sig.sig.aggregate(weak_votes.sig);
} else
assert(0); // this should be called only when we have a valid qc.

return valid_qc;
return valid_qc_sig;
}

std::optional<quorum_certificate> pending_quorum_certificate::get_best_qc(block_num_type block_num) const {
Expand All @@ -193,7 +193,7 @@ std::optional<quorum_certificate> pending_quorum_certificate::get_best_qc(block_
}

// extract valid QC from pending_qc
valid_quorum_certificate valid_qc_from_pending = to_valid_quorum_certificate();
quorum_certificate_sig valid_qc_from_pending = to_valid_quorum_certificate();

// if valid_qc does not have value, consider valid_qc_from_pending only
if( !valid_qc ) {
Expand All @@ -209,7 +209,7 @@ std::optional<quorum_certificate> pending_quorum_certificate::get_best_qc(block_
return std::optional{quorum_certificate{ block_num, best_qc }};
}

void pending_quorum_certificate::set_valid_qc(const valid_quorum_certificate& qc) {
void pending_quorum_certificate::set_valid_qc(const quorum_certificate_sig& qc) {
std::lock_guard g(*_mtx);
valid_qc = qc;
}
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/eosio/chain/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct block_state : public block_header_state { // block_header_state provi

std::optional<quorum_certificate> get_best_qc() const { return pending_qc.get_best_qc(block_num()); } // thread safe
bool valid_qc_is_strong() const { return pending_qc.valid_qc_is_strong(); } // thread safe
void set_valid_qc(const valid_quorum_certificate& qc) { pending_qc.set_valid_qc(qc); }
void set_valid_qc(const quorum_certificate_sig& qc) { pending_qc.set_valid_qc(qc); }
// extract the qc_claim from block header finality_extension
qc_claim_t extract_qc_claim() const;

Expand All @@ -153,7 +153,7 @@ struct block_state : public block_header_state { // block_header_state provi
vote_status aggregate_vote(uint32_t connection_id, const vote_message& vote); // aggregate vote into pending_qc
vote_status_t has_voted(const bls_public_key& key) const;
vote_info_vec get_votes() const; // for testing, returns vote info from pending_qc
void verify_qc(const valid_quorum_certificate& qc) const; // verify given qc is valid with respect block_state
void verify_qc(const quorum_certificate_sig& qc) const; // verify given qc is valid with respect block_state

using bhs_t = block_header_state;
using bhsp_t = block_header_state_ptr;
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/finality/finalizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace eosio::chain {
}
}

void maybe_update_fsi(const block_state_ptr& bsp, const valid_quorum_certificate& received_qc);
void maybe_update_fsi(const block_state_ptr& bsp, const quorum_certificate_sig& received_qc);

size_t size() const { return finalizers.size(); } // doesn't change, thread safe
bool empty() const { return finalizers.empty(); } // doesn't change, thread safe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace eosio::chain {
max_exceeded // received too many votes for a connection
};

// valid_quorum_certificate
struct valid_quorum_certificate {
// quorum_certificate_sig
struct quorum_certificate_sig {
bool is_weak() const { return !!weak_votes; }
bool is_strong() const { return !weak_votes; }

Expand All @@ -41,7 +41,7 @@ namespace eosio::chain {
// quorum_certificate
struct quorum_certificate {
uint32_t block_num;
valid_quorum_certificate data;
quorum_certificate_sig data;

qc_claim_t to_qc_claim() const {
return {.block_num = block_num, .is_strong_qc = data.is_strong()};
Expand Down Expand Up @@ -132,13 +132,13 @@ namespace eosio::chain {
state_t state() const { std::lock_guard g(*_mtx); return pending_state; };

std::optional<quorum_certificate> get_best_qc(block_num_type block_num) const;
void set_valid_qc(const valid_quorum_certificate& qc);
void set_valid_qc(const quorum_certificate_sig& qc);
bool valid_qc_is_strong() const;
private:
friend struct fc::reflector<pending_quorum_certificate>;
friend class qc_chain;
std::unique_ptr<std::mutex> _mtx;
std::optional<valid_quorum_certificate> valid_qc; // best qc received from the network inside block extension
std::optional<quorum_certificate_sig> valid_qc; // best qc 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 };
Expand All @@ -159,13 +159,13 @@ namespace eosio::chain {

bool is_quorum_met_no_lock() const;
bool has_voted_no_lock(bool strong, size_t index) const;
valid_quorum_certificate to_valid_quorum_certificate() const;
quorum_certificate_sig to_valid_quorum_certificate() const;
};
} //eosio::chain


FC_REFLECT_ENUM(eosio::chain::vote_status, (success)(duplicate)(unknown_public_key)(invalid_signature)(unknown_block)(max_exceeded))
FC_REFLECT(eosio::chain::valid_quorum_certificate, (strong_votes)(weak_votes)(sig));
FC_REFLECT(eosio::chain::quorum_certificate_sig, (strong_votes)(weak_votes)(sig));
FC_REFLECT(eosio::chain::pending_quorum_certificate, (valid_qc)(quorum)(max_weak_sum_before_weak_final)(pending_state)(strong_sum)(weak_sum)(weak_votes)(strong_votes));
FC_REFLECT_ENUM(eosio::chain::pending_quorum_certificate::state_t, (unrestricted)(restricted)(weak_achieved)(weak_final)(strong));
FC_REFLECT(eosio::chain::pending_quorum_certificate::votes_t, (bitset)(sig));
Expand Down
4 changes: 2 additions & 2 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,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 finalizer_policy_ptr& active_finalizer_policy,
const valid_quorum_certificate& qc) {
const quorum_certificate_sig& qc) {
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::vector<std::string> not_voted;
Expand Down Expand Up @@ -663,7 +663,7 @@ class producer_plugin_impl : public std::enable_shared_from_this<producer_plugin

void update_vote_block_metrics(block_num_type block_num,
const finalizer_policy_ptr& active_finalizer_policy,
const valid_quorum_certificate& qc) {
const quorum_certificate_sig& qc) {
if (_update_vote_block_metrics) {
producer_plugin::vote_block_metrics m;
m.block_num = block_num;
Expand Down
42 changes: 21 additions & 21 deletions unittests/block_state_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
agg_sig.aggregate(sig_0);
agg_sig.aggregate(sig_2);

// create a valid_quorum_certificate
valid_quorum_certificate qc{strong_votes, {}, agg_sig};
// create a quorum_certificate_sig
quorum_certificate_sig qc{strong_votes, {}, agg_sig};

BOOST_REQUIRE_NO_THROW( bsp->verify_qc(qc) );
}
Expand All @@ -238,7 +238,7 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
agg_sig.aggregate(strong_sig);
agg_sig.aggregate(weak_sig);

valid_quorum_certificate qc(strong_votes, weak_votes, agg_sig);
quorum_certificate_sig qc(strong_votes, weak_votes, agg_sig);
BOOST_REQUIRE_NO_THROW( bsp->verify_qc(qc) );
}

Expand All @@ -253,8 +253,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
agg_sig.aggregate(sigs[i]);
}

// create a valid_quorum_certificate
valid_quorum_certificate qc(strong_votes, {}, agg_sig);
// create a quorum_certificate_sig
quorum_certificate_sig qc(strong_votes, {}, agg_sig);

BOOST_REQUIRE_NO_THROW( bsp->verify_qc(qc) );
}
Expand All @@ -270,8 +270,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
agg_sig.aggregate(sigs[i]);
}

// create a valid_quorum_certificate
valid_quorum_certificate qc({}, weak_votes, agg_sig);
// create a quorum_certificate_sig
quorum_certificate_sig qc({}, weak_votes, agg_sig);

BOOST_REQUIRE_NO_THROW( bsp->verify_qc(qc) );
}
Expand All @@ -284,8 +284,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
bls_signature sig_2 = private_key[2].sign(strong_digest.to_uint8_span());
agg_sig.aggregate(sig_2);

// create a valid_quorum_certificate
valid_quorum_certificate qc(strong_votes, {}, agg_sig);
// create a quorum_certificate_sig
quorum_certificate_sig qc(strong_votes, {}, agg_sig);

BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), invalid_qc_claim, eosio::testing::fc_exception_message_starts_with("strong quorum is not met") );
}
Expand All @@ -298,8 +298,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
bls_signature sig_2 = private_key[2].sign(weak_digest);
agg_sig.aggregate(sig_2);

// create a valid_quorum_certificate
valid_quorum_certificate qc({}, weak_votes, agg_sig);
// create a quorum_certificate_sig
quorum_certificate_sig qc({}, weak_votes, agg_sig);

BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), invalid_qc_claim, eosio::testing::fc_exception_message_starts_with("weak quorum is not met") );
}
Expand All @@ -317,8 +317,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
bls_signature sig = private_key[0].sign(strong_digest.to_uint8_span());
agg_sig.aggregate(sig);

// create a valid_quorum_certificate
valid_quorum_certificate qc(strong_votes, {}, agg_sig);
// create a quorum_certificate_sig
quorum_certificate_sig qc(strong_votes, {}, agg_sig);

BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), invalid_qc_claim, eosio::testing::fc_exception_message_starts_with("vote bitset size is not the same as the number of finalizers") );
}
Expand All @@ -336,8 +336,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
bls_signature sig = private_key[0].sign(weak_digest);
agg_sig.aggregate(sig);

// create a valid_quorum_certificate
valid_quorum_certificate qc({}, weak_votes, agg_sig);
// create a quorum_certificate_sig
quorum_certificate_sig qc({}, weak_votes, agg_sig);

BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), invalid_qc_claim, eosio::testing::fc_exception_message_starts_with("vote bitset size is not the same as the number of finalizers") );
}
Expand All @@ -353,8 +353,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
sig.aggregate(sig_0);
sig.aggregate(sig_2);

// create a valid_quorum_certificate
valid_quorum_certificate qc(strong_votes, {}, sig);
// create a quorum_certificate_sig
quorum_certificate_sig qc(strong_votes, {}, sig);

BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), invalid_qc_claim, eosio::testing::fc_exception_message_is("signature validation failed") );
}
Expand All @@ -370,8 +370,8 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
sig.aggregate(sig_0);
sig.aggregate(sig_2);

// create a valid_quorum_certificate
valid_quorum_certificate qc(strong_votes, {}, sig);
// create a quorum_certificate_sig
quorum_certificate_sig qc(strong_votes, {}, sig);

BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), invalid_qc_claim, eosio::testing::fc_exception_message_is("signature validation failed") );
}
Expand All @@ -389,7 +389,7 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
sig.aggregate(strong_sig);
sig.aggregate(weak_sig);

valid_quorum_certificate qc(strong_votes, weak_votes, sig);
quorum_certificate_sig qc(strong_votes, weak_votes, sig);
BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), invalid_qc_claim, eosio::testing::fc_exception_message_is("signature validation failed") );
}

Expand All @@ -406,7 +406,7 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
sig.aggregate(strong_sig);
sig.aggregate(weak_sig);

valid_quorum_certificate qc(strong_votes, weak_votes, sig);
quorum_certificate_sig qc(strong_votes, weak_votes, sig);
BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), invalid_qc_claim, eosio::testing::fc_exception_message_is("signature validation failed") );
}
} FC_LOG_AND_RETHROW();
Expand Down

0 comments on commit e80b4b4

Please sign in to comment.