Skip to content

Commit

Permalink
Merge pull request #220 from AntelopeIO/fin_on_strong_fin_digest
Browse files Browse the repository at this point in the history
Add final_on_strong_qc_block_num to finality digest calculation
  • Loading branch information
linh2931 authored Jun 3, 2024
2 parents b90e35e + 3edd277 commit 2b1228e
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 106 deletions.
1 change: 1 addition & 0 deletions libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ digest_type block_header_state::compute_finality_digest() const {
assert(active_finalizer_policy);
finality_digest_data_v1 finality_digest_data {
.active_finalizer_policy_generation = active_finalizer_policy->generation,
.final_on_strong_qc_block_num = core.final_on_strong_qc_block_num,
.finality_tree_digest = finality_mroot(),
.last_pending_finalizer_policy_and_base_digest = lpfp_base_digest
};
Expand Down
3 changes: 2 additions & 1 deletion libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,9 @@ finality_data_t block_state::get_finality_data() {
}

return {
// other fields take the default values set by finality_data_t definition
// major_version and minor_version take the default values set by finality_data_t definition
.active_finalizer_policy_generation = active_finalizer_policy->generation,
.final_on_strong_qc_block_num = core.final_on_strong_qc_block_num,
.action_mroot = action_mroot,
.base_digest = *base_digest,
.proposed_finalizer_policy = std::move(proposed_finalizer_policy)
Expand Down
3 changes: 2 additions & 1 deletion libraries/chain/include/eosio/chain/block_header_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct finality_digest_data_v1 {
uint32_t major_version{light_header_protocol_version_major};
uint32_t minor_version{light_header_protocol_version_minor};
uint32_t active_finalizer_policy_generation {0};
uint32_t final_on_strong_qc_block_num {0};
digest_type finality_tree_digest;
digest_type last_pending_finalizer_policy_and_base_digest;

Expand Down Expand Up @@ -158,4 +159,4 @@ FC_REFLECT( eosio::chain::block_header_state, (block_id)(header)
(active_proposer_policy)(proposer_policies)(finalizer_policies)
(finalizer_policy_generation)(last_pending_finalizer_policy_digest)(header_exts))

FC_REFLECT( eosio::chain::finality_digest_data_v1, (major_version)(minor_version)(active_finalizer_policy_generation)(finality_tree_digest)(last_pending_finalizer_policy_and_base_digest) )
FC_REFLECT( eosio::chain::finality_digest_data_v1, (major_version)(minor_version)(active_finalizer_policy_generation)(final_on_strong_qc_block_num)(finality_tree_digest)(last_pending_finalizer_policy_and_base_digest) )
3 changes: 2 additions & 1 deletion libraries/chain/include/eosio/chain/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct finality_data_t {
uint32_t major_version{light_header_protocol_version_major};
uint32_t minor_version{light_header_protocol_version_minor};
uint32_t active_finalizer_policy_generation{0};
uint32_t final_on_strong_qc_block_num{0};
digest_type action_mroot{};
digest_type base_digest{};
std::optional<finalizer_policy> proposed_finalizer_policy; // finalizer policy, if proposed in the block
Expand Down Expand Up @@ -177,5 +178,5 @@ using block_state_pair = std::pair<std::shared_ptr<block_state_legacy>, blo
// not exporting pending_qc or valid_qc
FC_REFLECT( eosio::chain::valid_t::finality_leaf_node_t, (major_version)(minor_version)(block_num)(finality_digest)(action_mroot) )
FC_REFLECT( eosio::chain::valid_t, (validation_tree)(validation_mroots))
FC_REFLECT( eosio::chain::finality_data_t, (major_version)(minor_version)(active_finalizer_policy_generation)(action_mroot)(base_digest)(proposed_finalizer_policy) )
FC_REFLECT( eosio::chain::finality_data_t, (major_version)(minor_version)(active_finalizer_policy_generation)(final_on_strong_qc_block_num)(action_mroot)(base_digest)(proposed_finalizer_policy) )
FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(strong_digest)(weak_digest)(pending_qc)(valid)(validated) )
198 changes: 99 additions & 99 deletions unittests/deep-mind/deep-mind.log

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions unittests/svnn_ibc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
// one-time genesis finality digest computation
finality_digest = fc::sha256::hash(eosio::chain::finality_digest_data_v1{
.active_finalizer_policy_generation = 1,
.final_on_strong_qc_block_num = finality_data.final_on_strong_qc_block_num,
.finality_tree_digest = digest_type(), //nothing to finalize yet
.last_pending_finalizer_policy_and_base_digest = afp_base_digest
});
Expand All @@ -237,6 +238,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
// compute digest for verification purposes
digest_type computed_finality_digest = fc::sha256::hash(eosio::chain::finality_digest_data_v1{
.active_finalizer_policy_generation = active_finalizer_policy.generation,
.final_on_strong_qc_block_num = finality_data.final_on_strong_qc_block_num,
.finality_tree_digest = is_genesis ? digest_type() : finality_root,
.last_pending_finalizer_policy_and_base_digest = afp_base_digest
});
Expand Down Expand Up @@ -336,6 +338,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 6)
("witness_hash", block_4_result.afp_base_digest)
("finality_mroot", block_4_result.finality_root)
)
Expand All @@ -352,6 +355,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 4)
("witness_hash", block_2_result.afp_base_digest)
("finality_mroot", block_2_result.finality_root)
)
Expand All @@ -374,6 +378,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 7)
("witness_hash", block_5_result.afp_base_digest)
("finality_mroot", block_5_result.finality_root)
)
Expand All @@ -390,6 +395,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 4)
("witness_hash", block_2_result.afp_base_digest)
("finality_mroot", block_2_result.finality_root)
)
Expand All @@ -415,6 +421,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 4)
("witness_hash", block_2_result.afp_base_digest)
("finality_mroot", block_2_result.finality_root)
)
Expand Down Expand Up @@ -556,6 +563,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 11)
("witness_hash", block_9_result.afp_base_digest)
("finality_mroot", block_9_result.finality_root)
)
Expand All @@ -572,6 +580,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 9)
("witness_hash", block_7_result.afp_base_digest)
("finality_mroot", block_7_result.finality_root)
)
Expand All @@ -596,6 +605,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 9)
("witness_hash", block_7_result.afp_base_digest)
("finality_mroot", block_7_result.finality_root)
)
Expand Down Expand Up @@ -676,6 +686,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 15)
("witness_hash", block_13_result.afp_base_digest)
("finality_mroot", block_13_result.finality_root)
)
Expand All @@ -692,6 +703,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 13)
("new_finalizer_policy", cluster.last_pending_finalizer_policy)
("witness_hash", block_11_result.base_digest)
("finality_mroot", block_11_result.finality_root)
Expand Down Expand Up @@ -722,6 +734,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 2)
("final_on_qc_block_num", 16)
("witness_hash", block_14_result.afp_base_digest)
("finality_mroot", block_14_result.finality_root)
)
Expand All @@ -738,6 +751,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("final_on_qc_block_num", 14)
("witness_hash", block_12_result.afp_base_digest)
("finality_mroot", block_12_result.finality_root)
)
Expand Down
4 changes: 4 additions & 0 deletions unittests/test-contracts/svnn_ibc/svnn_ibc.abi
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
"name": "finalizer_policy_generation",
"type": "uint32"
},
{
"name": "final_on_qc_block_num",
"type": "uint32"
},
{
"name": "new_finalizer_policy",
"type": "finalizer_policy_input?"
Expand Down
11 changes: 7 additions & 4 deletions unittests/test-contracts/svnn_ibc/svnn_ibc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ CONTRACT svnn_ibc : public contract {
//finalizer_policy_generation for this block
uint32_t finalizer_policy_generation;

uint32_t final_on_qc_block_num = 0;

std::optional<finalizer_policy_input> new_finalizer_policy;

//if a finalizer policy is present, witness_hash should be the base_digest. Otherwise, witness_hash should be the static_data_digest
Expand All @@ -329,15 +331,16 @@ CONTRACT svnn_ibc : public contract {

//returns hash of major_version + minor_version + finalizer_policy_generation + resolve_witness() + finality_mroot
checksum256 finality_digest() const {
std::array<uint8_t, 76> result;
std::array<uint8_t, 80> result;
memcpy(&result[0], (uint8_t *)&major_version, 4);
memcpy(&result[4], (uint8_t *)&minor_version, 4);
memcpy(&result[8], (uint8_t *)&finalizer_policy_generation, 4);
memcpy(&result[12], (uint8_t *)&final_on_qc_block_num, 4);
std::array<uint8_t, 32> arr1 = finality_mroot.extract_as_byte_array();
std::array<uint8_t, 32> arr2 = resolve_witness().extract_as_byte_array();
std::copy (arr1.cbegin(), arr1.cend(), result.begin() + 12);
std::copy (arr2.cbegin(), arr2.cend(), result.begin() + 44);
checksum256 hash = sha256(reinterpret_cast<char*>(result.data()), 76);
std::copy (arr1.cbegin(), arr1.cend(), result.begin() + 16);
std::copy (arr2.cbegin(), arr2.cend(), result.begin() + 48);
checksum256 hash = sha256(reinterpret_cast<char*>(result.data()), 80);
return hash;
};

Expand Down
Binary file modified unittests/test-contracts/svnn_ibc/svnn_ibc.wasm
Binary file not shown.

0 comments on commit 2b1228e

Please sign in to comment.