Skip to content

Commit

Permalink
Merge branch 'GH-376-joint-qcs' of https://github.com/antelopeIO/spring
Browse files Browse the repository at this point in the history
… into consensus_changes_merge_target
  • Loading branch information
systemzax committed Jul 24, 2024
2 parents 11b02dc + eccecad commit 54ec6eb
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 139 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 @@ -159,7 +159,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 @@ -180,17 +180,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
33 changes: 17 additions & 16 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3724,15 +3724,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 @@ -3745,7 +3745,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 All @@ -3760,7 +3760,7 @@ struct controller_impl {

// extract current block extension and previous header extension
auto block_exts = b->validate_and_extract_extensions();
std::optional<finality_extension> prev_header_ext = prev.header_extension<finality_extension>();
const finality_extension* prev_finality_ext = prev.header_extension<finality_extension>();
std::optional<block_header_extension> header_ext = b->extract_header_extension(f_ext_id);

bool qc_extension_present = block_exts.count(qc_ext_id) != 0;
Expand All @@ -3775,7 +3775,7 @@ struct controller_impl {
"Block #${b} includes a QC block extension, but doesn't have a finality header extension",
("b", block_num) );

EOS_ASSERT( !prev_header_ext,
EOS_ASSERT( !prev_finality_ext,
invalid_qc_claim,
"Block #${b} doesn't have a finality header extension even though its predecessor does.",
("b", block_num) );
Expand All @@ -3790,7 +3790,7 @@ struct controller_impl {
// ensure the block does not have a QC and the QC claim of the current block has a block_num
// of the current block’s number and that it is a claim of a weak QC. Then return early.
// -------------------------------------------------------------------------------------------------
if (!prev_header_ext) {
if (!prev_finality_ext) {
EOS_ASSERT( !qc_extension_present && new_qc_claim.block_num == block_num && new_qc_claim.is_strong_qc == false,
invalid_qc_claim,
"Block #${b}, which is the finality transition block, doesn't have the expected extensions",
Expand All @@ -3801,10 +3801,9 @@ struct controller_impl {
// at this point both current block and its parent have IF extensions, and we are past the
// IF transition block
// ----------------------------------------------------------------------------------------
assert(header_ext && prev_header_ext);
assert(header_ext && prev_finality_ext);

const auto& prev_f_ext = *prev_header_ext;
const auto prev_qc_claim = prev_f_ext.qc_claim;
const auto& prev_qc_claim = prev_finality_ext->qc_claim;

// validate QC claim against previous block QC info

Expand Down Expand Up @@ -3971,25 +3970,27 @@ struct controller_impl {
auto qc_ext = bsp_in->block->extract_extension<quorum_certificate_extension>();
const qc_t& received_qc = qc_ext.qc;

block_state_ptr claimed = fork_db_fetch_bsp_on_branch_by_num( bsp_in->previous(), qc_ext.qc.block_num );
if( !claimed ) {
block_state_ptr claimed_bsp = fork_db_fetch_bsp_on_branch_by_num( bsp_in->previous(), qc_ext.qc.block_num );
if( !claimed_bsp ) {
dlog("qc not found in forkdb, qc: ${qc} for block ${bn} ${id}, previous ${p}",
("qc", qc_ext.qc.to_qc_claim())("bn", bsp_in->block_num())("id", bsp_in->id())("p", bsp_in->previous()));
return;
}

// Don't save the QC from block extension if the claimed block has a better or same received_qc
if (claimed->set_received_qc(received_qc)) {
dlog("set received qc: ${rqc} into claimed block ${bn} ${id}", ("rqc", qc_ext.qc.to_qc_claim())("bn", claimed->block_num())("id", claimed->id()));
if (claimed_bsp->set_received_qc(received_qc)) {
dlog("set received qc: ${rqc} into claimed block ${bn} ${id}", ("rqc", qc_ext.qc.to_qc_claim())
("bn", claimed_bsp->block_num())("id", claimed_bsp->id()));
} else {
dlog("qc not better, claimed->received: ${qbn} ${qid}, strong=${s}, received: ${rqc}, for block ${bn} ${id}",
("qbn", claimed->block_num())("qid", claimed->id())("s", !received_qc.is_weak()) // use is_weak() to avoid mutex on received_qc_is_strong()
("qbn", claimed_bsp->block_num())("qid", claimed_bsp->id())
("s", !received_qc.is_weak()) // use is_weak() to avoid mutex on received_qc_is_strong()
("rqc", qc_ext.qc.to_qc_claim())("bn", bsp_in->block_num())("id", bsp_in->id()));
}

if( received_qc.is_strong() ) {
if (received_qc.is_strong()) {
// Update finalizer safety information based on vote evidence
my_finalizers.maybe_update_fsi(claimed, received_qc);
my_finalizers.maybe_update_fsi(claimed_bsp, received_qc);
}
}

Expand Down
13 changes: 7 additions & 6 deletions libraries/chain/finality/finalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,20 @@ void my_finalizers_t::maybe_update_fsi(const block_state_ptr& bsp, const qc_t& r
return;

assert(bsp->active_finalizer_policy);
// qc should have already been verified via verify_qc, this EOS_ASSERT should never fire
EOS_ASSERT(!bsp->pending_finalizer_policy || received_qc.pending_policy_sig, invalid_qc_claim,
"qc ${bn} expected to have a pending policy signature", ("bn", received_qc.block_num));


// see comment on possible optimization in maybe_vote
std::lock_guard g(mtx);

bool updated = false;
for (auto& f : finalizers) {
if (has_voted_strong(bsp->active_finalizer_policy->finalizers, received_qc.active_policy_sig, f.first)) {
if (has_voted_strong(bsp->active_finalizer_policy->finalizers, received_qc.active_policy_sig, f.first)
|| (bsp->pending_finalizer_policy &&
has_voted_strong(bsp->pending_finalizer_policy->second->finalizers, *received_qc.pending_policy_sig, f.first))) {
updated |= f.second.maybe_update_fsi(bsp);
} else if (bsp->pending_finalizer_policy) {
assert(received_qc.pending_policy_sig);
if (has_voted_strong(bsp->pending_finalizer_policy->second->finalizers, *received_qc.pending_policy_sig, f.first)) {
updated |= f.second.maybe_update_fsi(bsp);
}
}
}

Expand Down
Loading

0 comments on commit 54ec6eb

Please sign in to comment.