Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/gh_2034_part2' into GH-2045-if-t…
Browse files Browse the repository at this point in the history
…ransision
  • Loading branch information
heifner committed Jan 13, 2024
2 parents 3b15160 + 854e9db commit 8408763
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 118 deletions.
108 changes: 71 additions & 37 deletions libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ producer_authority block_header_state::get_scheduled_producer(block_timestamp_ty
return detail::get_scheduled_producer(active_proposer_policy->proposer_schedule.producers, t);
}

const vector<digest_type>& block_header_state::get_new_protocol_feature_activations()const {
return detail::get_new_protocol_feature_activations(header_exts);
}

#warning Add last_proposed_finalizer_policy_generation to snapshot_block_header_state_v3, see header file TODO

block_header_state_core block_header_state_core::next(const uint32_t last_qc_block_num, bool is_last_qc_strong) const {
block_header_state_core block_header_state_core::next(qc_info_t incoming) const {
// no state change if last_qc_block_num is the same
if (last_qc_block_num == this->last_qc_block_num) {
if (incoming.last_qc_block_num == this->last_qc_block_num) {
return {*this};
}

EOS_ASSERT(last_qc_block_num > this->last_qc_block_num, block_validate_exception,
EOS_ASSERT(incoming.last_qc_block_num > this->last_qc_block_num, block_validate_exception,
"new last_qc_block_num must be greater than old last_qc_block_num");

auto old_last_qc_block_num = this->last_qc_block_num;
auto old_final_on_strong_qc_block_num = this->final_on_strong_qc_block_num;

block_header_state_core result{*this};

if (is_last_qc_strong) {
if (incoming.is_last_qc_strong) {
// last QC is strong. We can progress forward.

// block with old final_on_strong_qc_block_num becomes irreversible
Expand All @@ -48,7 +52,7 @@ block_header_state_core block_header_state_core::next(const uint32_t last_qc_blo
}

// new last_qc_block_num is always the input last_qc_block_num.
result.last_qc_block_num = last_qc_block_num;
result.last_qc_block_num = incoming.last_qc_block_num;

return result;
}
Expand All @@ -69,7 +73,25 @@ block_header_state block_header_state::next(block_header_state_input& input) con
.schedule_version = header.schedule_version
};

result.active_finalizer_policy = active_finalizer_policy;
// activated protocol features
// ---------------------------
if (!input.new_protocol_feature_activations.empty()) {
result.activated_protocol_features = std::make_shared<protocol_feature_activation_set>(
*activated_protocol_features, input.new_protocol_feature_activations);
} else {
result.activated_protocol_features = activated_protocol_features;
}

// block_header_state_core
// -----------------------
result.core = input.qc_info ? core.next(*input.qc_info) : core;

// proposal_mtree and finality_mtree
// ---------------------------------
// [greg todo] ??

// proposer policy
// ---------------
result.active_proposer_policy = active_proposer_policy;

if(!proposer_policies.empty()) {
Expand All @@ -83,40 +105,51 @@ block_header_state block_header_state::next(block_header_state_input& input) con
result.proposer_policies = proposer_policies;
}
}

if (input.new_proposer_policy) {
// called when assembling the block
result.proposer_policies[result.header.timestamp] = input.new_proposer_policy;
}

// core
// ----
if (input.qc_info)
result.core = core.next(input.qc_info->last_qc_block_num, input.qc_info->is_last_qc_strong);
else
result.core = core;
// finalizer policy
// ----------------
result.active_finalizer_policy = active_finalizer_policy;

if (!input.new_protocol_feature_activations.empty()) {
result.activated_protocol_features = std::make_shared<protocol_feature_activation_set>(
*activated_protocol_features, input.new_protocol_feature_activations);
} else {
result.activated_protocol_features = activated_protocol_features;
}
// [greg todo] correct support for new finalizer_policy activation using finalizer_policies map

// add block header extensions
// ---------------------------
if (input.new_finalizer_policy)
++input.new_finalizer_policy->generation;

std::optional<qc_info_t> qc_info = input.qc_info;
if (!qc_info) {
// [greg todo]: copy the one from the previous block (look in header.header_extensions)

// add IF block header extension
// -----------------------------
uint16_t if_ext_id = instant_finality_extension::extension_id();
auto if_entry = header_exts.lower_bound(if_ext_id);
auto& if_ext = std::get<instant_finality_extension>(if_entry->second);

instant_finality_extension new_if_ext {if_ext.qc_info,
std::move(input.new_finalizer_policy),
std::move(input.new_proposer_policy)};
if (input.qc_info)
new_if_ext.qc_info = *input.qc_info;

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));

// add protocol_feature_activation extension
// -----------------------------------------
if (!input.new_protocol_feature_activations.empty()) {
uint16_t ext_id = protocol_feature_activation::extension_id();
protocol_feature_activation pfa_ext{.protocol_features = std::move(input.new_protocol_feature_activations)};

emplace_extension(result.header.header_extensions, ext_id, fc::raw::pack(pfa_ext));
result.header_exts.emplace(ext_id, std::move(pfa_ext));
}

emplace_extension(result.header.header_extensions, instant_finality_extension::extension_id(),
fc::raw::pack(instant_finality_extension{qc_info,
std::move(input.new_finalizer_policy),
std::move(input.new_proposer_policy)}));


// Finally update block id from header
// -----------------------------------
result.id = result.header.calculate_id();

return result;
}

Expand All @@ -137,20 +170,21 @@ block_header_state block_header_state::next(const signed_block_header& h, const

auto exts = h.validate_and_extract_header_extensions();

// handle protocol_feature_activation from incoming block
// ------------------------------------------------------
// retrieve protocol_feature_activation from incoming block header extension
// -------------------------------------------------------------------------
vector<digest_type> new_protocol_feature_activations;
if( exts.count(protocol_feature_activation::extension_id() > 0) ) {
const auto& entry = exts.lower_bound(protocol_feature_activation::extension_id());
new_protocol_feature_activations = std::move(std::get<protocol_feature_activation>(entry->second).protocol_features);
auto pfa_entry = exts.lower_bound(protocol_feature_activation::extension_id());
auto& pfa_ext = std::get<protocol_feature_activation>(pfa_entry->second);
new_protocol_feature_activations = std::move(pfa_ext.protocol_features);
}

// retrieve instant_finality_extension data from block extension
// -------------------------------------------------------------
// retrieve instant_finality_extension data from block header extension
// --------------------------------------------------------------------
EOS_ASSERT(exts.count(instant_finality_extension::extension_id() > 0), misc_exception,
"Instant Finality Extension is expected to be present in all block headers after switch to IF");
const auto& if_entry = exts.lower_bound(instant_finality_extension::extension_id());
const auto& if_ext = std::get<instant_finality_extension>(if_entry->second);
auto if_entry = exts.lower_bound(instant_finality_extension::extension_id());
auto& if_ext = std::get<instant_finality_extension>(if_entry->second);

building_block_input bb_input{
.parent_id = id,
Expand Down
13 changes: 4 additions & 9 deletions libraries/chain/block_header_state_legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,10 @@ namespace eosio::chain {
}

/**
* Reference cannot outlive *this. Assumes header_exts is not mutated after instatiation.
* Reference cannot outlive *this. Assumes header_exts is not mutated after instantiation.
*/
const vector<digest_type>& block_header_state_legacy::get_new_protocol_feature_activations()const {
static const vector<digest_type> no_activations{};

if( header_exts.count(protocol_feature_activation::extension_id()) == 0 )
return no_activations;

return std::get<protocol_feature_activation>(header_exts.lower_bound(protocol_feature_activation::extension_id())->second).protocol_features;
return detail::get_new_protocol_feature_activations(header_exts);
}

block_header_state_legacy::block_header_state_legacy( legacy::snapshot_block_header_state_v2&& snapshot )
Expand All @@ -451,10 +446,10 @@ namespace eosio::chain {
producer_to_last_implied_irb = std::move(snapshot.producer_to_last_implied_irb);
valid_block_signing_authority = block_signing_authority_v0{ 1, {{std::move(snapshot.block_signing_key), 1}} };
confirm_count = std::move(snapshot.confirm_count);
id = std::move(snapshot.id);
id = snapshot.id;
header = std::move(snapshot.header);
pending_schedule.schedule_lib_num = snapshot.pending_schedule.schedule_lib_num;
pending_schedule.schedule_hash = std::move(snapshot.pending_schedule.schedule_hash);
pending_schedule.schedule_hash = snapshot.pending_schedule.schedule_hash;
pending_schedule.schedule = producer_authority_schedule( snapshot.pending_schedule.schedule );
activated_protocol_features = std::move(snapshot.activated_protocol_features);
}
Expand Down
41 changes: 27 additions & 14 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,38 @@

namespace eosio::chain {

block_state::block_state(const block_header_state& prev, signed_block_ptr b, const protocol_feature_set& pfs,
const validator_t& validator, bool skip_validate_signee)
: block_header_state(prev.next(*b, pfs, validator))
, block(std::move(b))
{}
block_state::block_state(const block_header_state& prev, signed_block_ptr b, const protocol_feature_set& pfs,
const validator_t& validator, bool skip_validate_signee)
: block_header_state(prev.next(*b, pfs, validator))
, block(std::move(b))
{}

#if 0
block_state::block_state(pending_block_header_state&& cur,
signed_block_ptr&& b,
deque<transaction_metadata_ptr>&& trx_metas,
const protocol_feature_set& pfs,
const validator_t& validator,
const signer_callback_type& signer
)
block_state::block_state(pending_block_header_state&& cur,
signed_block_ptr&& b,
deque<transaction_metadata_ptr>&& trx_metas,
const protocol_feature_set& pfs,
const validator_t& validator,
const signer_callback_type& signer
)
:block_header_state( inject_additional_signatures( std::move(cur), *b, pfs, validator, signer ) )
,block( std::move(b) )
,_pub_keys_recovered( true ) // called by produce_block so signature recovery of trxs must have been done
,_cached_trxs( std::move(trx_metas) )
{}
,cached_trxs( std::move(trx_metas) )
{}
#endif

deque<transaction_metadata_ptr> block_state::extract_trxs_metas() {
pub_keys_recovered = false;
auto result = std::move(cached_trxs);
cached_trxs.clear();
return result;
}

void block_state::set_trxs_metas( deque<transaction_metadata_ptr>&& trxs_metas, bool keys_recovered ) {
pub_keys_recovered = keys_recovered;
cached_trxs = std::move( trxs_metas );
}


} /// eosio::chain
Loading

0 comments on commit 8408763

Please sign in to comment.