Skip to content

Commit

Permalink
GH-2033 Add finalizer policy to block header extension
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Jan 9, 2024
1 parent b335157 commit a6626e0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
7 changes: 7 additions & 0 deletions libraries/chain/block_header_state_legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ namespace eosio::chain {
const checksum256_type& transaction_mroot,
const checksum256_type& action_mroot,
const std::optional<producer_authority_schedule>& new_producers,
std::optional<finalizer_policy>&& new_finalizer_policy,
vector<digest_type>&& new_protocol_feature_activations,
const protocol_feature_set& pfs
)const
Expand Down Expand Up @@ -206,6 +207,12 @@ namespace eosio::chain {
}
}

if (new_finalizer_policy) {
new_finalizer_policy->generation = 1; // TODO: do we allow more than one set during transition
emplace_extension(h.header_extensions, instant_finality_extension::extension_id(),
fc::raw::pack(instant_finality_extension{ {}, std::move(new_finalizer_policy), {} }));
}

return h;
}

Expand Down
10 changes: 7 additions & 3 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ struct building_block {
deque<transaction_receipt> pending_trx_receipts;
checksum_or_digests trx_mroot_or_receipt_digests {digests_t{}};
digests_t action_receipt_digests;
std::optional<finalizer_policy> new_finalizer_policy;

building_block_common(const vector<digest_type>& new_protocol_feature_activations) :
new_protocol_feature_activations(new_protocol_feature_activations)
Expand Down Expand Up @@ -532,7 +533,6 @@ struct building_block {

// Members below (as well as non-const members of building_block_common) start from initial state and are mutated as the block is built.
std::optional<proposer_policy> new_proposer_policy;
std::optional<finalizer_policy> new_finalizer_policy;

building_block_if(const block_header_state& parent, const building_block_input& input)
: building_block_common(input.new_protocol_feature_activations)
Expand Down Expand Up @@ -595,6 +595,10 @@ struct building_block {
[&](building_block_if& bb) -> R { return std::forward<F>(f)(bb); }}, v);
}

void set_proposed_finalizer_policy(const finalizer_policy& fin_pol) {
std::visit([&](auto& bb) { return bb.new_finalizer_policy = fin_pol; }, v);
}

deque<transaction_metadata_ptr> extract_trx_metas() {
return std::visit([](auto& bb) { return std::move(bb.pending_trx_metas); }, v);
}
Expand Down Expand Up @@ -713,7 +717,7 @@ struct building_block {

// in dpos, we create a signed_block here. In IF mode, we do it later (when we are ready to sign it)
auto block_ptr = std::make_shared<signed_block>(bb.pending_block_header_state.make_block_header(
transaction_mroot, action_mroot, bb.new_pending_producer_schedule,
transaction_mroot, action_mroot, bb.new_pending_producer_schedule, std::move(bb.new_finalizer_policy),
vector<digest_type>(bb.new_protocol_feature_activations), pfs));

block_ptr->transactions = std::move(bb.pending_trx_receipts);
Expand Down Expand Up @@ -2605,7 +2609,7 @@ struct controller_impl {
void set_proposed_finalizers(const finalizer_policy& fin_pol) {
assert(pending); // has to exist and be building_block since called from host function
auto& bb = std::get<building_block>(pending->_block_stage);
bb.apply_hs<void>([&](building_block::building_block_if& bb) { bb.new_finalizer_policy.emplace(fin_pol); });
bb.set_proposed_finalizer_policy(fin_pol);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ struct pending_block_header_state_legacy : public detail::block_header_state_leg
signed_block_header make_block_header( const checksum256_type& transaction_mroot,
const checksum256_type& action_mroot,
const std::optional<producer_authority_schedule>& new_producers,
std::optional<finalizer_policy>&& new_finalizer_policy,
vector<digest_type>&& new_protocol_feature_activations,
const protocol_feature_set& pfs)const;

Expand Down
10 changes: 2 additions & 8 deletions unittests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3858,9 +3858,6 @@ BOOST_AUTO_TEST_CASE(get_code_hash_tests) { try {
check("test"_n, 3);
} FC_LOG_AND_RETHROW() }

#if 0
// [greg todo] re-implement the test after https://github.com/AntelopeIO/leap/issues/1911 is done

// test set_finalizer host function serialization and tester set_finalizers
BOOST_AUTO_TEST_CASE(set_finalizer_test) { try {
validating_tester t;
Expand Down Expand Up @@ -3888,7 +3885,7 @@ BOOST_AUTO_TEST_CASE(set_finalizer_test) { try {
BOOST_TEST(fin_policy->finalizers.size() == finalizers.size());
BOOST_TEST(fin_policy->generation == 1);
BOOST_TEST(fin_policy->threshold == finalizers.size() / 3 * 2 + 1);

#if 0 // update after transition is complete: https://github.com/AntelopeIO/leap/issues/1911
// old dpos still in affect until block is irreversible
BOOST_TEST(block->confirmed == 0);
block_state_legacy_ptr block_state = t.control->fetch_block_state_by_id(block->calculate_id());
Expand All @@ -3906,10 +3903,7 @@ BOOST_AUTO_TEST_CASE(set_finalizer_test) { try {
block_state = t.control->fetch_block_state_by_id(block->calculate_id());
BOOST_REQUIRE(!!block_state);
BOOST_TEST(block_state->dpos_irreversible_blocknum == hs_dpos_irreversible_blocknum);

} FC_LOG_AND_RETHROW() }

#endif

} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit a6626e0

Please sign in to comment.