Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IF: SHiP -- Provide finality_data for Transition blocks #2362

Merged
merged 12 commits into from
Apr 4, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4316,8 +4316,72 @@ struct controller_impl {
}
}

// This is only used during Savanna transition, which is a one-time occurrance
// and the number of Transition block is small.
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
// It is OK to calculate from Savanna Genesis block for each Transition block.
std::optional<finality_data_t> get_transition_block_finality_data(const block_state_legacy_ptr& head) const {
fork_database_legacy_t::branch_t legacy_branch;
block_state_legacy_ptr legacy_root;
fork_db.apply_l<void>([&](const auto& forkdb) {
legacy_root = forkdb.root();
legacy_branch = forkdb.fetch_branch(head->id());
});

block_state_ptr prev;
auto bitr = legacy_branch.rbegin();

// search for the first block having instant_finality_extension
// and create the Savanna Genesis Block
if (legacy_root->header.contains_header_extension(instant_finality_extension::extension_id())) {
prev = block_state::create_if_genesis_block(*legacy_root);
} else {
heifner marked this conversation as resolved.
Show resolved Hide resolved
for (; bitr != legacy_branch.rend(); ++bitr) {
if ((*bitr)->header.contains_header_extension(instant_finality_extension::extension_id())) {
prev = block_state::create_if_genesis_block(*(*bitr));
++bitr;
break;
}
}
}

assert(prev);
const bool skip_validate_signee = true; // validated already

for (; bitr != legacy_branch.rend(); ++bitr) {
auto new_bsp = std::make_shared<block_state>(
*prev,
(*bitr)->block,
protocol_features.get_protocol_feature_set(),
validator_t{}, skip_validate_signee);

assert((*bitr)->action_receipt_digests_savanna);
auto digests = *((*bitr)->action_receipt_digests_savanna);
new_bsp->action_mroot = calculate_merkle(std::move(digests)); // required by finality_data
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved

prev = new_bsp;
}

assert(prev);
return prev->get_finality_data();
}

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(); });
return apply<std::optional<finality_data_t>>(chain_head, overloaded{
[&](const block_state_legacy_ptr& head) -> std::optional<finality_data_t> {
// When in Legacy, if it is during transition to Savana, we need to
// build finality_data for the corresponding Savanna block
if (head->header.contains_header_extension(instant_finality_extension::extension_id())) {
// during transition
return get_transition_block_finality_data(head);
} else {
// pre transition
return {};
}
},
[](const block_state_ptr& head) {
// Returns finality_data from chain_head because we are in Savanna
return head->get_finality_data();
}});
}

uint32_t earliest_available_block_num() const {
Expand Down
Loading