Skip to content

Commit

Permalink
GH-16 Add apply chain_head function that takes a legacy and a savanna…
Browse files Browse the repository at this point in the history
… lambda.
  • Loading branch information
heifner committed Apr 18, 2024
1 parent 9498355 commit 41eedfe
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ R apply(const block_handle& bh, F&& f) {
}, bh.internal());
}

template <class R, class F, class S>
R apply(const block_handle& bh, F&& f, S&& s) {
if constexpr (std::is_same_v<void, R>)
std::visit<void>(overloaded{[&](const block_state_legacy_ptr& head) { std::forward<F>(f)(head); },
[&](const block_state_ptr& head) { std::forward<S>(s)(head); }
}, bh.internal());
else
return std::visit<R>(overloaded{[&](const block_state_legacy_ptr& head) -> R { return std::forward<F>(f)(head); },
[&](const block_state_ptr& head) -> R { return std::forward<S>(s)(head); }
}, bh.internal());
}

// apply savanna block_state
template <class R, class F>
R apply_s(const block_handle& bh, F&& f) {
Expand Down Expand Up @@ -3199,18 +3211,19 @@ struct controller_impl {

if (auto* dm_logger = get_deep_mind_logger(false)) {
auto fd = head_finality_data();
apply_l<void>(chain_head, [&](const auto& head) {
if (head->block->contains_header_extension(instant_finality_extension::extension_id())) {
assert(fd);
dm_logger->on_accepted_block_v2(fork_db_root_block_num(), head->block, *fd);
} else {
dm_logger->on_accepted_block(head);
}
});
apply_s<void>(chain_head, [&](const auto& head) {
assert(fd);
dm_logger->on_accepted_block_v2(fork_db_root_block_num(), head->block, *fd);
});
apply<void>(chain_head,
[&](const block_state_legacy_ptr& head) {
if (head->block->contains_header_extension(instant_finality_extension::extension_id())) {
assert(fd);
dm_logger->on_accepted_block_v2(fork_db_root_block_num(), head->block, *fd);
} else {
dm_logger->on_accepted_block(head);
}
},
[&](const block_state_ptr& head) {
assert(fd);
dm_logger->on_accepted_block_v2(fork_db_root_block_num(), head->block, *fd);
});
}

if (s == controller::block_status::incomplete) {
Expand Down

0 comments on commit 41eedfe

Please sign in to comment.