Skip to content

Commit

Permalink
Merge pull request #410 from AntelopeIO/pending_pol_gen
Browse files Browse the repository at this point in the history
Add pending policy generation number to finality digest
  • Loading branch information
linh2931 authored Jul 26, 2024
2 parents f2ba289 + b4cb8e7 commit 7f3ce63
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 135 deletions.
7 changes: 4 additions & 3 deletions libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ 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,
.finality_tree_digest = finality_mroot(),
.l2_commitments_digest = fc::sha256::hash(level_2_commitments)
.active_finalizer_policy_generation = active_finalizer_policy->generation,
.last_pending_finalizer_policy_generation = get_last_pending_finalizer_policy().generation,
.finality_tree_digest = finality_mroot(),
.l2_commitments_digest = fc::sha256::hash(level_2_commitments)
};

return fc::sha256::hash(finality_digest_data);
Expand Down
18 changes: 9 additions & 9 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,15 @@ finality_data_t block_state::get_finality_data() {

return {
// major_version and minor_version take the default values set by finality_data_t definition
.active_finalizer_policy_generation = active_finalizer_policy->generation,
.action_mroot = action_mroot,
.reversible_blocks_mroot = core.get_reversible_blocks_mroot(),
.latest_qc_claim_block_num = latest_qc_claim_block_num,
.latest_qc_claim_finality_digest = blk_ref.finality_digest,
.latest_qc_claim_timestamp = blk_ref.timestamp,
.timestamp = timestamp(),
.base_digest = *base_digest,
.pending_finalizer_policy = std::move(pending_fin_pol)
.active_finalizer_policy_generation = active_finalizer_policy->generation,
.action_mroot = action_mroot,
.reversible_blocks_mroot = core.get_reversible_blocks_mroot(),
.latest_qc_claim_block_num = latest_qc_claim_block_num,
.latest_qc_claim_finality_digest = blk_ref.finality_digest,
.latest_qc_claim_timestamp = blk_ref.timestamp,
.base_digest = *base_digest,
.pending_finalizer_policy = std::move(pending_fin_pol),
.last_pending_finalizer_policy_generation = get_last_pending_finalizer_policy().generation
};
}

Expand Down
5 changes: 3 additions & 2 deletions libraries/chain/include/eosio/chain/block_header_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ struct level_2_commitments_t {
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 active_finalizer_policy_generation{0};
uint32_t last_pending_finalizer_policy_generation{0}; // use active_finalizer_policy_generation if pending_finalizer_policy does not exist
digest_type finality_tree_digest{};
digest_type l2_commitments_digest{};
};
Expand Down Expand Up @@ -182,4 +183,4 @@ FC_REFLECT( eosio::chain::block_header_state, (block_id)(header)

FC_REFLECT( eosio::chain::level_3_commitments_t, (reversible_blocks_mroot)(latest_qc_claim_block_num )(latest_qc_claim_finality_digest)(latest_qc_claim_timestamp)(timestamp)(base_digest))
FC_REFLECT( eosio::chain::level_2_commitments_t, (last_pending_fin_pol_digest)(last_pending_fin_pol_start_num)(l3_commitments_digest) )
FC_REFLECT( eosio::chain::finality_digest_data_v1, (major_version)(minor_version)(active_finalizer_policy_generation)(finality_tree_digest)(l2_commitments_digest) )
FC_REFLECT( eosio::chain::finality_digest_data_v1, (major_version)(minor_version)(active_finalizer_policy_generation)(last_pending_finalizer_policy_generation)(finality_tree_digest)(l2_commitments_digest) )
6 changes: 4 additions & 2 deletions libraries/chain/include/eosio/chain/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ struct finality_data_t {
block_num_type latest_qc_claim_block_num{0};
digest_type latest_qc_claim_finality_digest;
block_timestamp_type latest_qc_claim_timestamp;
block_timestamp_type timestamp; // This is the timestamp of the current block.
digest_type base_digest{};

// Finalizer policy if one is promoted to pending in the block.
// Use string format for public key in the policy for easier uses.
std::optional<finalizer_policy_with_string_key> pending_finalizer_policy;

uint32_t last_pending_finalizer_policy_generation{0}; // // use active_finalizer_policy_generation if pending_finalizer_policy does not exist
};

struct block_state : public block_header_state { // block_header_state provides parent link
Expand Down Expand Up @@ -190,5 +192,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)(timestamp)(parent_timestamp)(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)(reversible_blocks_mroot)(latest_qc_claim_block_num)(latest_qc_claim_finality_digest)(latest_qc_claim_timestamp)(timestamp)(base_digest)(pending_finalizer_policy) )
FC_REFLECT( eosio::chain::finality_data_t, (major_version)(minor_version)(active_finalizer_policy_generation)(action_mroot)(reversible_blocks_mroot)(latest_qc_claim_block_num)(latest_qc_claim_finality_digest)(latest_qc_claim_timestamp)(base_digest)(pending_finalizer_policy)(last_pending_finalizer_policy_generation) )
FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(strong_digest)(weak_digest)(aggregating_qc)(valid)(validated) )
4 changes: 2 additions & 2 deletions libraries/state_history/abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,9 @@ extern const char* const state_history_plugin_abi = R"({
{ "name": "latest_qc_claim_block_num", "type": "uint32" },
{ "name": "latest_qc_claim_finality_digest", "type": "checksum256" },
{ "name": "latest_qc_claim_timestamp", "type": "block_timestamp_type" },
{ "name": "timestamp", "type": "block_timestamp_type" },
{ "name": "base_digest", "type": "checksum256" },
{ "name": "pending_finalizer_policy", "type": "finalizer_policy_with_string_key?" }
{ "name": "pending_finalizer_policy", "type": "finalizer_policy_with_string_key?" },
{ "name": "last_pending_finalizer_policy_generation", "type": "uint32" }
]
}
],
Expand Down
190 changes: 95 additions & 95 deletions unittests/deep-mind/deep-mind.log

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions unittests/finality_proof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ namespace finality_proof {
.latest_qc_claim_block_num = finality_data.latest_qc_claim_block_num,
.latest_qc_claim_finality_digest = finality_data.latest_qc_claim_finality_digest,
.latest_qc_claim_timestamp = finality_data.latest_qc_claim_timestamp,
.timestamp = finality_data.timestamp,
.timestamp = timestamp,
.base_digest = base_digest
});

Expand All @@ -244,9 +244,10 @@ namespace finality_proof {

// compute digest for verification purposes
digest_type finality_digest = fc::sha256::hash(finality_digest_data_v1{
.active_finalizer_policy_generation = is_genesis ? 1 : active_finalizer_policy.generation,
.finality_tree_digest = finality_root,
.l2_commitments_digest = level_2_commitments_digest
.active_finalizer_policy_generation = is_genesis ? 1 : active_finalizer_policy.generation,
.last_pending_finalizer_policy_generation = is_genesis ? 1 : last_pending_finalizer_policy.generation,
.finality_tree_digest = finality_root,
.l2_commitments_digest = level_2_commitments_digest
});

// compute finality leaf
Expand Down Expand Up @@ -315,4 +316,4 @@ namespace finality_proof {

};

}
}
29 changes: 16 additions & 13 deletions unittests/svnn_ibc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("qc_block", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_3_result.level_2_commitments_digest)
("finality_mroot", block_3_result.finality_root)
)
Expand All @@ -129,7 +129,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("finality_data", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_2_result.level_2_commitments_digest)
("finality_mroot", block_2_result.finality_root)
)
Expand All @@ -152,7 +152,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("qc_block", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_3_result.level_2_commitments_digest)
("finality_mroot", block_3_result.finality_root)
)
Expand Down Expand Up @@ -187,7 +187,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("qc_block", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_4_result.level_2_commitments_digest)
("finality_mroot", block_4_result.finality_root)
)
Expand All @@ -203,7 +203,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("finality_data", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_2_result.level_2_commitments_digest)
("finality_mroot", block_2_result.finality_root)
)
Expand All @@ -229,7 +229,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("finality_data", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_2_result.level_2_commitments_digest)
("finality_mroot", block_2_result.finality_root)
)
Expand Down Expand Up @@ -388,7 +388,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("qc_block", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_8_result.level_2_commitments_digest)
("finality_mroot", block_8_result.finality_root)
)
Expand All @@ -404,7 +404,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("finality_data", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_7_result.level_2_commitments_digest)
("finality_mroot", block_7_result.finality_root)
)
Expand All @@ -429,7 +429,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("finality_data", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("witness_hash", block_7_result.level_2_commitments_digest)
("finality_mroot", block_7_result.finality_root)
)
Expand Down Expand Up @@ -493,7 +493,8 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("qc_block", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("last_pending_finalizer_policy_generation", 2)
("witness_hash", block_11_result.level_2_commitments_digest)
("finality_mroot", block_11_result.finality_root)
)
Expand All @@ -509,7 +510,8 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("finality_data", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("last_pending_finalizer_policy_generation", 2)
("new_finalizer_policy", cluster.last_pending_finalizer_policy)
("witness_hash", block_10_result.level_3_commitments_digest)
("last_pending_finalizer_policy_start_num", block_10_result.last_pending_finalizer_policy_start_num )
Expand Down Expand Up @@ -541,7 +543,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("qc_block", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 2)
("active_finalizer_policy_generation", 2)
("witness_hash", block_12_result.level_2_commitments_digest)
("finality_mroot", block_12_result.finality_root)
)
Expand All @@ -557,7 +559,8 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("finality_data", mvo()
("major_version", 1)
("minor_version", 0)
("finalizer_policy_generation", 1)
("active_finalizer_policy_generation", 1)
("last_pending_finalizer_policy_generation", 2)
("witness_hash", block_11_result.level_2_commitments_digest)
("finality_mroot", block_11_result.finality_root)
)
Expand Down
7 changes: 5 additions & 2 deletions unittests/test-contracts/savanna/common/savanna.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ namespace savanna {
uint32_t minor_version;

//finalizer_policy_generation for this block
uint32_t finalizer_policy_generation;
uint32_t active_finalizer_policy_generation;
std::optional<uint32_t> last_pending_finalizer_policy_generation;

//Allows the contract to obtain knowledge about them and to record them in its internal state.
std::optional<finalizer_policy_input> new_finalizer_policy;
Expand Down Expand Up @@ -343,9 +344,11 @@ namespace savanna {
//internal representation of finality data
struct block_finality_data_internal : block_finality_data {
checksum256 resolved_witness_hash;
uint32_t last_pending_finalizer_policy_generation = 0;

block_finality_data_internal(const block_finality_data& base) : block_finality_data(base){
resolved_witness_hash = base.resolve_witness();
last_pending_finalizer_policy_generation = base.last_pending_finalizer_policy_generation.has_value() ? base.last_pending_finalizer_policy_generation.value() : active_finalizer_policy_generation;
}

checksum256 finality_digest() const {
Expand All @@ -354,7 +357,7 @@ namespace savanna {
return hash;
}

EOSLIB_SERIALIZE(block_finality_data_internal, (major_version)(minor_version)(finalizer_policy_generation)(finality_mroot)(resolved_witness_hash))
EOSLIB_SERIALIZE(block_finality_data_internal, (major_version)(minor_version)(active_finalizer_policy_generation)(last_pending_finalizer_policy_generation)(finality_mroot)(resolved_witness_hash))
};

//used in "heavy" proofs, where verification of finality digest is performed
Expand Down
6 changes: 5 additions & 1 deletion unittests/test-contracts/savanna/ibc/ibc.abi
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@
"type": "uint32"
},
{
"name": "finalizer_policy_generation",
"name": "active_finalizer_policy_generation",
"type": "uint32"
},
{
"name": "last_pending_finalizer_policy_generation",
"type": "uint32?"
},
{
"name": "new_finalizer_policy",
"type": "finalizer_policy_input?"
Expand Down
2 changes: 1 addition & 1 deletion unittests/test-contracts/savanna/ibc/ibc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ finalizer_policy_input ibc::_get_stored_finalizer_policy(const uint64_t finalize
void ibc::_check_finality_proof(const finality_proof& finality_proof, const block_proof_of_inclusion& target_block_proof_of_inclusion){

//attempt to retrieve the stored policy with the correct generation number
finalizer_policy_input finalizer_policy = _get_stored_finalizer_policy(finality_proof.qc_block.finalizer_policy_generation);
finalizer_policy_input finalizer_policy = _get_stored_finalizer_policy(finality_proof.qc_block.active_finalizer_policy_generation);

//verify QC. If QC is valid, it means that we have reached finality on the block referenced by the finality_mroot
_check_qc(finality_proof.qc, block_finality_data_internal(finality_proof.qc_block).finality_digest(), finalizer_policy);
Expand Down
Binary file modified unittests/test-contracts/savanna/ibc/ibc.wasm
Binary file not shown.

0 comments on commit 7f3ce63

Please sign in to comment.