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

IF: Update fork database fork-choice rule - complex #2265

Merged
merged 28 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
472b1d4
GH-2125 Optimize fetch_block_branch
heifner Feb 8, 2024
7c19670
GH-2125 Update best fork logic of fork_database.
heifner Feb 13, 2024
8101af0
GH-2125 Update fork_database on strong vote
heifner Feb 13, 2024
5d2a839
GH-2125 Make last_qc_block_num non-optional
heifner Feb 14, 2024
29ddf49
Merge remote-tracking branch 'origin/hotstuff_integration' into GH-21…
heifner Feb 14, 2024
57371bd
GH-2125 Move get_activated_protocol_features to block_header_state[_l…
heifner Feb 14, 2024
6ab1146
GH-2125 Remove fork database adaptors and use fork database accessors…
heifner Feb 14, 2024
f8828ac
Merge remote-tracking branch 'origin/hotstuff_integration' into GH-21…
heifner Feb 15, 2024
d0f7bf7
Merge remote-tracking branch 'origin/hotstuff_integration' into GH-21…
heifner Feb 23, 2024
970528c
GH-2125 Add updated_core and most_recent_ancestor_with_qc to block_st…
heifner Feb 26, 2024
9e0a226
GH-2125 Revert unneeded changes
heifner Feb 26, 2024
17274e2
GH-2125 Potentially switch forks before beginning to produce a block
heifner Feb 27, 2024
842ed6c
GH-2125 Can't have pending block when applying a block
heifner Feb 27, 2024
37a7a17
GH-2125 Use vector instead of deque
heifner Feb 27, 2024
042ffde
GH-2125 Return current state even on error
heifner Feb 27, 2024
1eda923
GH-2125 Rename method to make more clear
heifner Feb 27, 2024
2767821
GH-2125 Use descriptive types
heifner Feb 27, 2024
bc5d464
GH-2125 Change order of eval
heifner Feb 27, 2024
c3847eb
GH-2125 Improve logging
heifner Feb 27, 2024
6a45921
GH-2125 Use descriptive enums instead of bools
heifner Feb 27, 2024
69373f4
GH-2125 Revert unneeded change
heifner Feb 27, 2024
f9dd443
GH-2125 Remove duplicate
heifner Feb 27, 2024
772d74e
GH-2125 Rename most_recent_ancestor_with_qc to best_qc_claim
heifner Feb 27, 2024
2153ba2
Merge remote-tracking branch 'origin/hotstuff_integration' into GH-21…
heifner Feb 27, 2024
39b3d3f
GH-2125 Only update best qc on state changes
heifner Feb 27, 2024
3ec3d79
GH-2125 Fix update_best_qc logic for processing descendants
heifner Feb 28, 2024
d9c1b87
GH-2125 use a counter instead of time to ensure unique
heifner Feb 28, 2024
e2f0d4d
GH-2125 Add some comments
heifner Feb 28, 2024
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
2 changes: 1 addition & 1 deletion libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ block_header_state block_header_state::next(block_header_state_input& input) con

uint16_t if_ext_id = instant_finality_extension::extension_id();
emplace_extension(result.header.header_extensions, if_ext_id, fc::raw::pack(new_if_ext));
result.header_exts.emplace(if_ext_id, std::move(new_if_ext));
result.header_exts.emplace(if_ext_id, std::move(new_if_ext)); // TODO: does not appear to be used

// add protocol_feature_activation extension
// -----------------------------------------
Expand Down
21 changes: 12 additions & 9 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ block_state::block_state(const block_header_state& prev, signed_block_ptr b, con
, strong_digest(compute_finalizer_digest())
, weak_digest(create_weak_digest(strong_digest))
, pending_qc(prev.active_finalizer_policy->finalizers.size(), prev.active_finalizer_policy->threshold, prev.active_finalizer_policy->max_weak_sum_before_weak_final())
, most_recent_ancestor_with_qc(core.latest_qc_claim())
, updated_core(core.next_metadata(most_recent_ancestor_with_qc))
{
// ASSUMPTION FROM controller_impl::apply_block = all untrusted blocks will have their signatures pre-validated here
if( !skip_validate_signee ) {
Expand All @@ -32,7 +34,9 @@ block_state::block_state(const block_header_state& bhs, deque<transaction_metada
, strong_digest(compute_finalizer_digest())
, weak_digest(create_weak_digest(strong_digest))
, pending_qc(bhs.active_finalizer_policy->finalizers.size(), bhs.active_finalizer_policy->threshold, bhs.active_finalizer_policy->max_weak_sum_before_weak_final())
, pub_keys_recovered(true) // probably not needed
, most_recent_ancestor_with_qc(core.latest_qc_claim())
, updated_core(core.next_metadata(most_recent_ancestor_with_qc))
, pub_keys_recovered(true) // called by produce_block so signature recovery of trxs must have been done
, cached_trxs(std::move(trx_metas))
{
block->transactions = std::move(trx_receipts);
Expand All @@ -49,6 +53,8 @@ block_state::block_state(const block_state_legacy& bsp) {
block_header_state::block_id = bsp.id();
header = bsp.header;
core = finality_core::create_core_for_genesis_block(bsp.block_num()); // [if todo] instant transition is not acceptable
most_recent_ancestor_with_qc = core.latest_qc_claim();
updated_core = core.next_metadata(most_recent_ancestor_with_qc);
activated_protocol_features = bsp.activated_protocol_features;

auto if_ext_id = instant_finality_extension::extension_id();
Expand Down Expand Up @@ -83,7 +89,8 @@ void block_state::set_trxs_metas( deque<transaction_metadata_ptr>&& trxs_metas,
}

// Called from net threads
std::pair<vote_status, std::optional<uint32_t>> block_state::aggregate_vote(const vote_message& vote) {
std::tuple<vote_status, pending_quorum_certificate::state_t>
block_state::aggregate_vote(const vote_message& vote) {
const auto& finalizers = active_finalizer_policy->finalizers;
auto it = std::find_if(finalizers.begin(),
finalizers.end(),
Expand All @@ -92,20 +99,16 @@ std::pair<vote_status, std::optional<uint32_t>> block_state::aggregate_vote(cons
if (it != finalizers.end()) {
auto index = std::distance(finalizers.begin(), it);
auto digest = vote.strong ? strong_digest.to_uint8_span() : std::span<const uint8_t>(weak_digest);
auto [status, strong] = pending_qc.add_vote(vote.strong,
return pending_qc.add_vote(block_num(),
vote.strong,
digest,
index,
vote.finalizer_key,
vote.sig,
finalizers[index].weight);

std::optional<uint32_t> new_lib{};
if (status == vote_status::success && strong)
new_lib = core.final_on_strong_qc_block_num;
return {status, new_lib};
} else {
wlog( "finalizer_key (${k}) in vote is not in finalizer policy", ("k", vote.finalizer_key) );
return {vote_status::unknown_public_key, {}};
return {vote_status::unknown_public_key, pending_quorum_certificate::state_t::unrestricted};
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
68 changes: 49 additions & 19 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ struct controller_impl {

// --------------- access fork_db root ----------------------------------------------------------------------
bool fork_db_has_root() const {
return fork_db.apply<bool>([&](const auto& forkdb) { return !!forkdb.root(); });
return fork_db.apply<bool>([&](const auto& forkdb) { return !!forkdb.has_root(); });
}

block_id_type fork_db_root_block_id() const {
Expand Down Expand Up @@ -1004,9 +1004,9 @@ struct controller_impl {
return prev->block_num();
}

void fork_db_reset_to_head() {
void fork_db_reset_root_to_head() {
return fork_db.apply<void>([&](auto& forkdb) {
forkdb.reset(*forkdb.chain_head);
forkdb.reset_root(*forkdb.chain_head);
});
}

Expand Down Expand Up @@ -1300,7 +1300,7 @@ struct controller_impl {
void replay(std::function<bool()> check_shutdown) {
auto blog_head = blog.head();
if( !fork_db_has_root() ) {
fork_db_reset_to_head();
fork_db_reset_root_to_head();
if (!blog_head)
return;
}
Expand Down Expand Up @@ -1335,7 +1335,7 @@ struct controller_impl {
ilog( "fork database head ${h}, root ${r}", ("h", pending_head->block_num())( "r", forkdb.root()->block_num() ) );
if( pending_head->block_num() < head->block_num() || head->block_num() < forkdb.root()->block_num() ) {
ilog( "resetting fork database with new last irreversible block as the new root: ${id}", ("id", head->id()) );
forkdb.reset( *head );
forkdb.reset_root( *head );
} else if( head->block_num() != forkdb.root()->block_num() ) {
auto new_root = forkdb.search_on_branch( pending_head->id(), head->block_num() );
EOS_ASSERT( new_root, fork_database_exception,
Expand All @@ -1357,22 +1357,22 @@ struct controller_impl {

if (snapshot_head_block != 0 && !blog_head) {
// loading from snapshot without a block log so fork_db can't be considered valid
forkdb.reset( *head );
forkdb.reset_root( *head );
} else if( !except_ptr && !check_shutdown() && forkdb.head() ) {
auto head_block_num = head->block_num();
auto branch = forkdb.fetch_branch( forkdb.head()->id() );
auto branch = fork_db.fetch_branch_from_head();
int rev = 0;
for( auto i = branch.rbegin(); i != branch.rend(); ++i ) {
if( check_shutdown() ) break;
if( (*i)->block_num() <= head_block_num ) continue;
++rev;
replay_push_block<BSP>( (*i)->block, controller::block_status::validated );
replay_push_block<BSP>( *i, controller::block_status::validated );
}
ilog( "${n} reversible blocks replayed", ("n",rev) );
}

if( !forkdb.head() ) {
forkdb.reset( *head );
forkdb.reset_root( *head );
}

auto end = fc::time_point::now();
Expand Down Expand Up @@ -1440,7 +1440,7 @@ struct controller_impl {
initialize_blockchain_state(genesis); // sets head to genesis state

if( !forkdb.head() ) {
forkdb.reset( *forkdb.chain_head );
forkdb.reset_root( *forkdb.chain_head );
linh2931 marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down Expand Up @@ -2780,10 +2780,8 @@ struct controller_impl {
const auto& bsp = std::get<std::decay_t<decltype(forkdb.chain_head)>>(cb.bsp);

if( s == controller::block_status::incomplete ) {
forkdb.add( bsp );
forkdb.mark_valid( bsp );
forkdb.add( bsp, true, false );
linh2931 marked this conversation as resolved.
Show resolved Hide resolved
emit( accepted_block_header, std::tie(bsp->block, bsp->id()) );
EOS_ASSERT( bsp == forkdb.head(), fork_database_exception, "committed block did not become the new head in fork database");
linh2931 marked this conversation as resolved.
Show resolved Hide resolved
} else if (s != controller::block_status::irreversible) {
forkdb.mark_valid( bsp );
}
Expand Down Expand Up @@ -3112,10 +3110,21 @@ struct controller_impl {
// called from net threads and controller's thread pool
vote_status process_vote_message( const vote_message& vote ) {
auto aggregate_vote = [&vote](auto& forkdb) -> std::pair<vote_status, std::optional<uint32_t>> {
auto bsp = forkdb.get_block(vote.proposal_id);
if (bsp)
return bsp->aggregate_vote(vote);
return {vote_status::unknown_block, {}};
auto bsp = forkdb.get_block(vote.proposal_id);
if (bsp) {
auto [status, state] = bsp->aggregate_vote(vote);
std::optional<uint32_t> new_lib{};
if (status == vote_status::success && pending_quorum_certificate::is_quorum_met(state)) {
if (state == pending_quorum_certificate::state_t::strong) {
new_lib = bsp->core.final_on_strong_qc_block_num;
forkdb.update_best_qc(bsp->id(), {.block_num = bsp->block_num(), .is_strong_qc = true});
} else {
forkdb.update_best_qc(bsp->id(), {.block_num = bsp->block_num(), .is_strong_qc = false});
}
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
}
return {status, new_lib};
}
return {vote_status::unknown_block, {}};
};
// TODO: https://github.com/AntelopeIO/leap/issues/2057
// TODO: Do not aggregate votes on block_state if in legacy block fork_db
Expand Down Expand Up @@ -3423,7 +3432,7 @@ struct controller_impl {

auto do_push = [&](auto& forkdb) {
if constexpr (std::is_same_v<BSP, typename std::decay_t<decltype(forkdb.chain_head)>>)
forkdb.add( bsp );
forkdb.add( bsp, false, false );

if (is_trusted_producer(b->producer)) {
trusted_producer_light_validation = true;
Expand Down Expand Up @@ -3473,7 +3482,7 @@ struct controller_impl {
*forkdb.chain_head, b, protocol_features.get_protocol_feature_set(), validator, skip_validate_signee);

if (s != controller::block_status::irreversible) {
forkdb.add(bsp, true);
forkdb.add(bsp, false, true);
}

emit(accepted_block_header, std::tie(bsp->block, bsp->id()));
Expand Down Expand Up @@ -3502,6 +3511,21 @@ struct controller_impl {
} FC_LOG_AND_RETHROW( )
}

void maybe_switch_forks(const forked_callback_t& cb, const trx_meta_cache_lookup& trx_lookup) {
auto maybe_switch = [&](auto& forkdb) {
if (read_mode != db_read_mode::IRREVERSIBLE) {
auto fork_head = forkdb.head();
if (forkdb.chain_head->id() != fork_head->id()) {
controller::block_report br;
maybe_switch_forks(br, fork_head, fork_head->is_valid() ? controller::block_status::validated : controller::block_status::complete,
cb, trx_lookup);
}
}
};

fork_db.apply<void>(maybe_switch);
}

template<class BSP>
void maybe_switch_forks( controller::block_report& br, const BSP& new_head, controller::block_status s,
const forked_callback_t& forked_cb, const trx_meta_cache_lookup& trx_lookup )
Expand Down Expand Up @@ -4267,6 +4291,12 @@ void controller::commit_block() {
my->commit_block(block_status::incomplete);
}

void controller::maybe_switch_forks(const forked_callback_t& cb, const trx_meta_cache_lookup& trx_lookup) {
validate_db_available_size();
my->maybe_switch_forks(cb, trx_lookup);
}


deque<transaction_metadata_ptr> controller::abort_block() {
return my->abort_block();
}
Expand Down
Loading
Loading