Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject a QC if bitset size of its strong_votes or weak_votes is invalid #346

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,13 @@ void block_state::verify_qc(const valid_quorum_certificate& qc) const {

// utility to accumulate voted weights
auto weights = [&] ( const vote_bitset& 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}",
("s", votes_bitset.size())("n", num_finalizers) );

uint64_t sum = 0;
assert(num_finalizers == votes_bitset.size());
auto n = std::min(num_finalizers, votes_bitset.size());
for (auto i = 0u; i < n; ++i) {
for (auto i = 0u; i < num_finalizers; ++i) {
if( votes_bitset[i] ) { // ith finalizer voted
sum += finalizers[i].weight;
}
Expand Down
38 changes: 38 additions & 0 deletions unittests/block_state_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,44 @@ BOOST_AUTO_TEST_CASE(verify_qc_test) try {
BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), block_validate_exception, eosio::testing::fc_exception_message_starts_with("weak quorum is not met") );
}

{ // strong QC bitset size does not match number of finalizers in the policy

// construct vote bitset with a size greater than num_finalizers
vote_bitset strong_votes(num_finalizers + 1);

// vote by finalizer 0
strong_votes[0] = 1;

// aggregate votes
bls_aggregate_signature agg_sig;
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);

BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), block_validate_exception, eosio::testing::fc_exception_message_starts_with("vote bitset size is not the same as the number of finalizers") );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the exception not invalid_qc_claim that is trigerred by the EOS_ASSERT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

invalid_qc_claim is derived from block_validate_exception:

FC_DECLARE_DERIVED_EXCEPTION( invalid_qc_claim, block_validate_exception,
.
Used block_validate_exception for keeping consistent with existing usage.

But it is better to be more specific. Changed all of the usages.

}

{ // weak QC bitset size does not match number of finalizers in the policy

// construct vote bitset with a size less than num_finalizers
vote_bitset weak_votes(num_finalizers - 1);

// vote by finalizer 0
weak_votes[0] = 1;

// aggregate votes
bls_aggregate_signature agg_sig;
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);

BOOST_CHECK_EXCEPTION( bsp->verify_qc(qc), block_validate_exception, eosio::testing::fc_exception_message_starts_with("vote bitset size is not the same as the number of finalizers") );
}

{ // strong QC with a wrong signing private key
vote_bitset strong_votes(num_finalizers);
strong_votes[0] = 1; // finalizer 0 voted with weight 1
Expand Down