Skip to content

Commit

Permalink
Merge pull request #2321 from AntelopeIO/ship_finality_info_support
Browse files Browse the repository at this point in the history
IF:  SHiP changes to support IBC services (adding finality_data log)
  • Loading branch information
linh2931 authored Mar 27, 2024
2 parents 20520cc + f1a1893 commit 39cd75a
Show file tree
Hide file tree
Showing 14 changed files with 638 additions and 357 deletions.
16 changes: 15 additions & 1 deletion libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ block_state::block_state(const block_header_state& bhs,
const std::optional<valid_t>& valid,
const std::optional<quorum_certificate>& qc,
const signer_callback_type& signer,
const block_signing_authority& valid_block_signing_authority)
const block_signing_authority& valid_block_signing_authority,
const digest_type& action_mroot)
: block_header_state(bhs)
, block(std::make_shared<signed_block>(signed_block_header{bhs.header}))
, strong_digest(compute_finality_digest())
Expand All @@ -40,6 +41,7 @@ block_state::block_state(const block_header_state& bhs,
, valid(valid)
, pub_keys_recovered(true) // called by produce_block so signature recovery of trxs must have been done
, cached_trxs(std::move(trx_metas))
, action_mroot(action_mroot)
{
block->transactions = std::move(trx_receipts);

Expand Down Expand Up @@ -96,6 +98,7 @@ block_state_ptr block_state::create_if_genesis_block(const block_state_legacy& b
result.validated = bsp.is_valid();
result.pub_keys_recovered = bsp._pub_keys_recovered;
result.cached_trxs = bsp._cached_trxs;
result.action_mroot = action_mroot_svnn;

return result_ptr;
}
Expand Down Expand Up @@ -305,6 +308,17 @@ digest_type block_state::get_finality_mroot_claim(const qc_claim_t& qc_claim) co
return get_validation_mroot(next_core_metadata.final_on_strong_qc_block_num);
}

finality_data_t block_state::get_finality_data() {
if (!base_digest) {
base_digest = compute_base_digest(); // cache it
}
return {
// other fields take the default values set by finality_data_t definition
.action_mroot = action_mroot,
.base_digest = *base_digest
};
}

void inject_additional_signatures( signed_block& b, const std::vector<signature_type>& additional_signatures)
{
if (!additional_signatures.empty()) {
Expand Down
16 changes: 13 additions & 3 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ struct assembled_block {
deque<transaction_receipt> trx_receipts; // Comes from building_block::pending_trx_receipts
std::optional<valid_t> valid; // Comes from assemble_block
std::optional<quorum_certificate> qc; // QC to add as block extension to new block
digest_type action_mroot;

block_header_state& get_bhs() { return bhs; }
};
Expand Down Expand Up @@ -365,7 +366,7 @@ struct assembled_block {
[&](assembled_block_if& ab) {
auto bsp = std::make_shared<block_state>(ab.bhs, std::move(ab.trx_metas),
std::move(ab.trx_receipts), ab.valid, ab.qc, signer,
valid_block_signing_authority);
valid_block_signing_authority, ab.action_mroot);
return completed_block{block_handle{std::move(bsp)}};
}},
v);
Expand Down Expand Up @@ -759,6 +760,7 @@ struct building_block {
// have one.
if (!validating_bsp->valid) {
validating_bsp->valid = bb.parent.new_valid(bhs, action_mroot);
validating_bsp->action_mroot = action_mroot; // caching for constructing finality_data. Only needed when block is commited.
}
} else {
// Create the valid structure for producing
Expand All @@ -771,7 +773,8 @@ struct building_block {
std::move(bb.pending_trx_metas),
std::move(bb.pending_trx_receipts),
valid,
qc_data.qc
qc_data.qc,
action_mroot // caching for constructing finality_data.
};

return assembled_block{.v = std::move(ab)};
Expand Down Expand Up @@ -4245,6 +4248,10 @@ struct controller_impl {
}
}

std::optional<finality_data_t> head_finality_data() const {
return apply_s<std::optional<finality_data_t>>(chain_head, [](const block_state_ptr& head) { return head->get_finality_data(); });
}

uint32_t earliest_available_block_num() const {
return (blog.first_block_num() != 0) ? blog.first_block_num() : fork_db_root_block_num();
}
Expand Down Expand Up @@ -4768,6 +4775,10 @@ const signed_block_ptr& controller::head_block()const {
return my->chain_head.block();
}

std::optional<finality_data_t> controller::head_finality_data() const {
return my->head_finality_data();
}

uint32_t controller::fork_db_head_block_num()const {
return my->fork_db_head_block_num();
}
Expand Down Expand Up @@ -4823,7 +4834,6 @@ time_point controller::last_irreversible_block_time() const {
return my->fork_db_root_timestamp().to_time_point();
}


const dynamic_global_property_object& controller::get_dynamic_global_properties()const {
return my->db.get<dynamic_global_property_object>();
}
Expand Down
19 changes: 18 additions & 1 deletion libraries/chain/include/eosio/chain/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ struct valid_t {
std::vector<digest_type> validation_mroots;
};

// This is mostly used by SHiP to stream finality_data
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};
digest_type action_mroot{};
digest_type base_digest{};
};

struct block_state : public block_header_state { // block_header_state provides parent link
// ------ data members -------------------------------------------------------------
signed_block_ptr block;
Expand All @@ -69,6 +78,8 @@ struct block_state : public block_header_state { // block_header_state provi
// ------ data members caching information available elsewhere ----------------------
bool pub_keys_recovered = false;
deque<transaction_metadata_ptr> cached_trxs;
digest_type action_mroot; // For finality_data sent to SHiP
std::optional<digest_type> base_digest; // For finality_data sent to SHiP

// ------ private methods -----------------------------------------------------------
bool is_valid() const { return validated; }
Expand All @@ -81,6 +92,7 @@ struct block_state : public block_header_state { // block_header_state provi
friend struct fc::reflector<block_state>;
friend struct controller_impl;
friend struct completed_block;
friend struct building_block;
public:
// ------ functions -----------------------------------------------------------------
const block_id_type& id() const { return block_header_state::id(); }
Expand All @@ -107,6 +119,9 @@ struct block_state : public block_header_state { // block_header_state provi
// Returns finality_mroot_claim of the current block
digest_type get_finality_mroot_claim(const qc_claim_t& qc_claim) const;

// Returns finality_data of the current block
finality_data_t get_finality_data();

// vote_status
vote_status aggregate_vote(const vote_message& vote); // aggregate vote into pending_qc
void verify_qc(const valid_quorum_certificate& qc) const; // verify given qc is valid with respect block_state
Expand All @@ -128,7 +143,8 @@ struct block_state : public block_header_state { // block_header_state provi
const std::optional<valid_t>& valid,
const std::optional<quorum_certificate>& qc,
const signer_callback_type& signer,
const block_signing_authority& valid_block_signing_authority);
const block_signing_authority& valid_block_signing_authority,
const digest_type& action_mroot);

static std::shared_ptr<block_state> create_if_genesis_block(const block_state_legacy& bsp);

Expand All @@ -146,4 +162,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))
FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(strong_digest)(weak_digest)(pending_qc)(valid_qc)(valid)(validated) )
3 changes: 3 additions & 0 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ namespace eosio::chain {
const signed_block_ptr& head_block()const;
// returns nullptr after instant finality enabled
block_state_legacy_ptr head_block_state_legacy()const;
// returns finality_data associated with chain head for SHiP when in Savanna,
// std::nullopt in Legacy
std::optional<finality_data_t> head_finality_data() const;

uint32_t fork_db_head_block_num()const;
block_id_type fork_db_head_block_id()const;
Expand Down
27 changes: 25 additions & 2 deletions libraries/state_history/abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ extern const char* const state_history_plugin_abi = R"({
{ "name": "fetch_deltas", "type": "bool" }
]
},
{
"name": "get_blocks_request_v1", "fields": [
{ "name": "start_block_num", "type": "uint32" },
{ "name": "end_block_num", "type": "uint32" },
{ "name": "max_messages_in_flight", "type": "uint32" },
{ "name": "have_positions", "type": "block_position[]" },
{ "name": "irreversible_only", "type": "bool" },
{ "name": "fetch_block", "type": "bool" },
{ "name": "fetch_traces", "type": "bool" },
{ "name": "fetch_deltas", "type": "bool" },
{ "name": "fetch_finality_data", "type": "bool" }
]
},
{
"name": "get_blocks_ack_request_v0", "fields": [
{ "name": "num_messages", "type": "uint32" }
Expand All @@ -46,7 +59,8 @@ extern const char* const state_history_plugin_abi = R"({
{ "name": "prev_block", "type": "block_position?" },
{ "name": "block", "type": "bytes?" },
{ "name": "traces", "type": "bytes?" },
{ "name": "deltas", "type": "bytes?" }
{ "name": "deltas", "type": "bytes?" },
{ "name": "finality_data", "type": "bytes?" }
]
},
{
Expand Down Expand Up @@ -546,13 +560,22 @@ extern const char* const state_history_plugin_abi = R"({
{ "type": "uint32", "name": "account_cpu_usage_average_window" },
{ "type": "uint32", "name": "account_net_usage_average_window" }
]
},
{
"name": "finality_data", "fields": [
{ "name": "major_version", "type": "uint32" },
{ "name": "minor_version", "type": "uint32" },
{ "name": "active_finalizer_policy_generation", "type": "uint32" },
{ "name": "action_mroot", "type": "checksum256" },
{ "name": "base_digest", "type": "checksum256" }
]
}
],
"types": [
{ "new_type_name": "transaction_id", "type": "checksum256" }
],
"variants": [
{ "name": "request", "types": ["get_status_request_v0", "get_blocks_request_v0", "get_blocks_ack_request_v0"] },
{ "name": "request", "types": ["get_status_request_v0", "get_blocks_request_v0", "get_blocks_request_v1", "get_blocks_ack_request_v0"] },
{ "name": "result", "types": ["get_status_result_v0", "get_blocks_result_v0"] },
{ "name": "action_receipt", "types": ["action_receipt_v0"] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ datastream<ST>& operator<<(datastream<ST>& ds, const eosio::state_history::get_b
history_pack_big_bytes(ds, obj.block);
history_pack_big_bytes(ds, obj.traces);
history_pack_big_bytes(ds, obj.deltas);
history_pack_big_bytes(ds, obj.finality_data);
return ds;
}

Expand Down
11 changes: 9 additions & 2 deletions libraries/state_history/include/eosio/state_history/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ struct get_blocks_request_v0 {
bool fetch_deltas = false;
};

struct get_blocks_request_v1 : get_blocks_request_v0 {
bool fetch_finality_data = false;
};

struct get_blocks_ack_request_v0 {
uint32_t num_messages = 0;
};
Expand All @@ -119,9 +123,11 @@ struct get_blocks_result_base {
struct get_blocks_result_v0 : get_blocks_result_base {
std::optional<bytes> traces;
std::optional<bytes> deltas;
std::optional<bytes> finality_data;
};

using state_request = std::variant<get_status_request_v0, get_blocks_request_v0, get_blocks_ack_request_v0>;
using state_request = std::variant<get_status_request_v0, get_blocks_request_v0, get_blocks_request_v1, get_blocks_ack_request_v0>;
using get_blocks_request = std::variant<get_blocks_request_v0, get_blocks_request_v1>;
using state_result = std::variant<get_status_result_v0, get_blocks_result_v0>;

} // namespace state_history
Expand All @@ -133,7 +139,8 @@ FC_REFLECT(eosio::state_history::block_position, (block_num)(block_id));
FC_REFLECT_EMPTY(eosio::state_history::get_status_request_v0);
FC_REFLECT(eosio::state_history::get_status_result_v0, (head)(last_irreversible)(trace_begin_block)(trace_end_block)(chain_state_begin_block)(chain_state_end_block)(chain_id));
FC_REFLECT(eosio::state_history::get_blocks_request_v0, (start_block_num)(end_block_num)(max_messages_in_flight)(have_positions)(irreversible_only)(fetch_block)(fetch_traces)(fetch_deltas));
FC_REFLECT_DERIVED(eosio::state_history::get_blocks_request_v1, (eosio::state_history::get_blocks_request_v0), (fetch_finality_data));
FC_REFLECT(eosio::state_history::get_blocks_ack_request_v0, (num_messages));
FC_REFLECT(eosio::state_history::get_blocks_result_base, (head)(last_irreversible)(this_block)(prev_block)(block));
FC_REFLECT_DERIVED(eosio::state_history::get_blocks_result_v0, (eosio::state_history::get_blocks_result_base), (traces)(deltas));
FC_REFLECT_DERIVED(eosio::state_history::get_blocks_result_v0, (eosio::state_history::get_blocks_result_base), (traces)(deltas)(finality_data));
// clang-format on
Loading

0 comments on commit 39cd75a

Please sign in to comment.