Skip to content

Commit

Permalink
Simplify block_header_state_core::next() API.
Browse files Browse the repository at this point in the history
wip
  • Loading branch information
greg7mdp committed Jan 12, 2024
1 parent 9499630 commit 7253499
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
19 changes: 8 additions & 11 deletions libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ producer_authority block_header_state::get_scheduled_producer(block_timestamp_ty

#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};
}

Expand All @@ -27,7 +27,7 @@ block_header_state_core block_header_state_core::next(const uint32_t last_qc_blo

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 Down Expand Up @@ -78,16 +78,13 @@ block_header_state block_header_state::next(block_header_state_input& input) con
result.activated_protocol_features = activated_protocol_features;
}

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

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

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

// proposer policy
// ---------------
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/block_header_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct block_header_state_core {
std::optional<uint32_t> last_qc_block_num; //
uint32_t finalizer_policy_generation; //

block_header_state_core next(uint32_t last_qc_block_num, bool is_last_qc_strong) const;
block_header_state_core next(qc_info_t incoming) const;
};

struct block_header_state {
Expand Down
14 changes: 7 additions & 7 deletions unittests/block_header_state_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ BOOST_AUTO_TEST_CASE(block_header_state_core_state_transition_test)
// verifies the state is kept the same when old last_final_block_num
// and new last_final_block_num are the same
for (bool is_last_qc_strong: { true, false }) {
auto new_bhs_core = old_bhs_core.next(old_last_qc_block_num, is_last_qc_strong);
auto new_bhs_core = old_bhs_core.next({old_last_qc_block_num, is_last_qc_strong});
BOOST_REQUIRE_EQUAL(new_bhs_core.last_final_block_num, old_bhs_core.last_final_block_num);
BOOST_REQUIRE_EQUAL(*new_bhs_core.final_on_strong_qc_block_num, *old_bhs_core.final_on_strong_qc_block_num);
BOOST_REQUIRE_EQUAL(*new_bhs_core.last_qc_block_num, *old_bhs_core.last_qc_block_num);
}

// verifies state cannot be transitioned to a smaller last_qc_block_num
for (bool is_last_qc_strong: { true, false }) {
BOOST_REQUIRE_THROW(old_bhs_core.next(old_last_qc_block_num - 1, is_last_qc_strong), block_validate_exception);
BOOST_REQUIRE_THROW(old_bhs_core.next({old_last_qc_block_num - 1, is_last_qc_strong}), block_validate_exception);
}

// verifies state transition works when is_last_qc_strong is true
constexpr auto input_last_qc_block_num = 4u;
auto new_bhs_core = old_bhs_core.next(input_last_qc_block_num, true);
auto new_bhs_core = old_bhs_core.next({input_last_qc_block_num, true});
// old final_on_strong_qc block became final
BOOST_REQUIRE_EQUAL(new_bhs_core.last_final_block_num, old_final_on_strong_qc_block_num);
// old last_qc block became final_on_strong_qc block
Expand All @@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(block_header_state_core_state_transition_test)
BOOST_REQUIRE_EQUAL(*new_bhs_core.last_qc_block_num, input_last_qc_block_num);

// verifies state transition works when is_last_qc_strong is false
new_bhs_core = old_bhs_core.next(input_last_qc_block_num, false);
new_bhs_core = old_bhs_core.next({input_last_qc_block_num, false});
// last_final_block_num should not change
BOOST_REQUIRE_EQUAL(new_bhs_core.last_final_block_num, old_last_final_block_num);
// new final_on_strong_qc_block_num should not be present
Expand All @@ -75,7 +75,7 @@ BOOST_AUTO_TEST_CASE(block_header_state_core_3_chain_transition_test)

// block2 --> block3
constexpr auto block3_input_last_qc_block_num = 2u;
auto block3_bhs_core = block2_bhs_core.next(block3_input_last_qc_block_num, true);
auto block3_bhs_core = block2_bhs_core.next({block3_input_last_qc_block_num, true});
// last_final_block_num should be the same as old one
BOOST_REQUIRE_EQUAL(block3_bhs_core.last_final_block_num, block2_last_final_block_num);
// final_on_strong_qc_block_num should be same as old one
Expand All @@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(block_header_state_core_3_chain_transition_test)

// block3 --> block4
constexpr auto block4_input_last_qc_block_num = 3u;
auto block4_bhs_core = block3_bhs_core.next(block4_input_last_qc_block_num, true);
auto block4_bhs_core = block3_bhs_core.next({block4_input_last_qc_block_num, true});
// last_final_block_num should not change
BOOST_REQUIRE_EQUAL(block4_bhs_core.last_final_block_num, block2_last_final_block_num);
// final_on_strong_qc_block_num should be block3's last_qc_block_num
Expand All @@ -98,7 +98,7 @@ BOOST_AUTO_TEST_CASE(block_header_state_core_3_chain_transition_test)

// block4 --> block5
constexpr auto block5_input_last_qc_block_num = 4u;
auto block5_bhs_core = block4_bhs_core.next(block5_input_last_qc_block_num, true);
auto block5_bhs_core = block4_bhs_core.next({block5_input_last_qc_block_num, true});
// last_final_block_num should have a new value
BOOST_REQUIRE_EQUAL(block5_bhs_core.last_final_block_num, block4_final_on_strong_qc_block_num);
// final_on_strong_qc_block_num should be block4's last_qc_block_num
Expand Down

0 comments on commit 7253499

Please sign in to comment.