Skip to content

Commit

Permalink
GH-1523 Add hs_irreversible_blocknum to block_header_state and use it…
Browse files Browse the repository at this point in the history
… in fork_database to track lib
  • Loading branch information
heifner committed Sep 8, 2023
1 parent b1720ad commit 19f6323
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
6 changes: 6 additions & 0 deletions libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ namespace eosio { namespace chain {
return blocknums[ index ];
}

uint32_t block_header_state::get_last_irreversible_blocknum()const {
if (dpos_irreversible_blocknum > 0)
return dpos_irreversible_blocknum;
return hs_irreversible_blocknum.load();
}

pending_block_header_state block_header_state::next( block_timestamp_type when,
uint16_t num_prev_blocks_to_confirm )const
{
Expand Down
7 changes: 4 additions & 3 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,11 @@ struct controller_impl {

const auto fork_head = fork_db_head();

if( fork_head->dpos_irreversible_blocknum <= lib_num )
const uint32_t fork_lib = fork_head->get_last_irreversible_blocknum();
if( fork_lib <= lib_num )
return;

auto branch = fork_db.fetch_branch( fork_head->id, fork_head->dpos_irreversible_blocknum );
auto branch = fork_db.fetch_branch( fork_head->id, fork_lib );
try {

std::vector<std::future<std::vector<char>>> v;
Expand Down Expand Up @@ -469,7 +470,7 @@ struct controller_impl {
throw;
}

//db.commit( fork_head->dpos_irreversible_blocknum ); // redundant
//db.commit( fork_head->get_last_irreversible_blocknum ); // redundant

if( root_id != fork_db.root()->id ) {
branch.emplace_back(fork_db.root());
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/deep_mind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/account_object.hpp>
#include <eosio/chain/protocol_feature_manager.hpp>
#include <eosio/chain/finalizer_authority.hpp>
#include <fc/crypto/hex.hpp>

namespace {
Expand Down
33 changes: 30 additions & 3 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <eosio/chain/fork_database.hpp>
#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/finalizer_authority.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
Expand Down Expand Up @@ -42,7 +43,7 @@ namespace eosio { namespace chain {
ordered_unique< tag<by_lib_block_num>,
composite_key< block_state,
global_fun<const block_state&, bool, &block_state_is_valid>,
member<detail::block_header_state_common, uint32_t, &detail::block_header_state_common::dpos_irreversible_blocknum>,
const_mem_fun<block_header_state, uint32_t, &block_header_state::get_last_irreversible_blocknum>,
member<detail::block_header_state_common, uint32_t, &detail::block_header_state_common::block_num>,
member<block_header_state, block_id_type, &block_header_state::id>
>,
Expand All @@ -57,8 +58,8 @@ namespace eosio { namespace chain {
> fork_multi_index_type;

bool first_preferred( const block_header_state& lhs, const block_header_state& rhs ) {
return std::tie( lhs.dpos_irreversible_blocknum, lhs.block_num )
> std::tie( rhs.dpos_irreversible_blocknum, rhs.block_num );
return std::make_tuple( lhs.get_last_irreversible_blocknum(), lhs.block_num )
> std::make_tuple( rhs.get_last_irreversible_blocknum(), rhs.block_num );
}

struct fork_database_impl {
Expand Down Expand Up @@ -89,6 +90,7 @@ namespace eosio { namespace chain {
pair<branch_type, branch_type> fetch_branch_from_impl( const block_id_type& first,
const block_id_type& second )const;
void mark_valid_impl( const block_state_ptr& h );
void mark_irreversible_impl( block_id_type id );

void add_impl( const block_state_ptr& n,
bool ignore_duplicate, bool validate,
Expand Down Expand Up @@ -329,6 +331,26 @@ namespace eosio { namespace chain {
root = new_root;
}

void fork_database_impl::mark_irreversible_impl( block_id_type id ) {
EOS_ASSERT( root, fork_database_exception, "root not yet set" );

const uint32_t lib = block_header::num_from_id(id);
auto& by_id_idx = index.get<by_block_id>();

for (auto itr = by_id_idx.find( id ); itr != by_id_idx.end();) {
EOS_ASSERT(itr != by_id_idx.end(), fork_database_exception, "block state not in fork database; cannot mark as irreversible", ("id", id));

const bool at_root = id == root->id;
by_id_idx.modify(itr, [&id, lib](block_state_ptr& bsp) {
bsp->hs_irreversible_blocknum.store(lib);
id = bsp->header.previous;
});

itr = by_id_idx.find( id );
EOS_ASSERT( at_root || itr != by_id_idx.end(), fork_database_exception, "invariant violation: orphaned branch was present in forked database" );
}
}

block_header_state_ptr fork_database::get_block_header( const block_id_type& id )const {
std::shared_lock g( my->mtx );
return my->get_block_header_impl( id );
Expand Down Expand Up @@ -541,6 +563,11 @@ namespace eosio { namespace chain {
}
}

void fork_database::mark_irreversible( const block_id_type& id ) {
std::lock_guard g( my->mtx );
my->mark_irreversible_impl( id );
}

void fork_database::mark_valid( const block_state_ptr& h ) {
std::lock_guard g( my->mtx );
my->mark_valid_impl( h );
Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/include/eosio/chain/block_header_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <eosio/chain/protocol_feature_manager.hpp>
#include <eosio/chain/finalizer_set.hpp>
#include <eosio/chain/chain_snapshot.hpp>
#include <fc/copyable_atomic.hpp>
#include <future>

namespace eosio { namespace chain {
Expand Down Expand Up @@ -55,6 +56,7 @@ namespace detail {
uint32_t dpos_irreversible_blocknum = 0;
producer_authority_schedule active_schedule;
uint32_t last_proposed_finalizer_set_generation = 0; // TODO: Add to snapshot_block_header_state_v3
fc::copyable_atomic<uint32_t> hs_irreversible_blocknum; // TODO: Add to snapshot_block_header_state_v3
incremental_merkle blockroot_merkle;
flat_map<account_name,uint32_t> producer_to_last_produced;
flat_map<account_name,uint32_t> producer_to_last_implied_irb;
Expand Down Expand Up @@ -147,6 +149,7 @@ struct block_header_state : public detail::block_header_state_common {
bool skip_validate_signee = false )const;

uint32_t calc_dpos_last_irreversible( account_name producer_of_next_block )const;
uint32_t get_last_irreversible_blocknum()const;

producer_authority get_scheduled_producer( block_timestamp_type t )const;
const block_id_type& prev()const { return header.previous; }
Expand All @@ -167,6 +170,7 @@ FC_REFLECT( eosio::chain::detail::block_header_state_common,
(dpos_irreversible_blocknum)
(active_schedule)
(last_proposed_finalizer_set_generation)
(hs_irreversible_blocknum)
(blockroot_merkle)
(producer_to_last_produced)
(producer_to_last_implied_irb)
Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ namespace eosio { namespace chain {

void mark_valid( const block_state_ptr& h );

// Called by hotstuff to mark a fork branch irreversible
void mark_irreversible( const block_id_type& id );

static const uint32_t magic_number;

static const uint32_t min_supported_version;
Expand Down
1 change: 1 addition & 0 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <eosio/chain/snapshot.hpp>
#include <eosio/chain/subjective_billing.hpp>
#include <eosio/chain/deep_mind.hpp>
#include <eosio/chain/finalizer_authority.hpp>
#include <eosio/chain_plugin/trx_finality_status_processing.hpp>
#include <eosio/chain/permission_link_object.hpp>
#include <eosio/chain/global_property_object.hpp>
Expand Down

0 comments on commit 19f6323

Please sign in to comment.