Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
use block from block_state instead of query
Browse files Browse the repository at this point in the history
  • Loading branch information
huangminghuang committed Apr 22, 2021
1 parent dcab780 commit 9e82ecf
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions plugins/state_history_plugin/state_history_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
uint16_t endpoint_port = 8080;
std::unique_ptr<tcp::acceptor> acceptor;

signed_block_ptr_variant get_block(uint32_t block_num) {
try {
return chain_plug->chain().fetch_block_by_number(block_num);
} catch (...) {
}
return {};
}

std::optional<chain::block_id_type> get_block_id(uint32_t block_num) {
std::optional<chain::block_id_type> result;

Expand Down Expand Up @@ -213,15 +205,11 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
send_update();
}

void set_result_block_header(get_blocks_result_v1&, uint32_t) {}
void set_result_block_header(get_blocks_result_v2& result, uint32_t block_num) {
void set_result_block_header(get_blocks_result_v1&, const signed_block_ptr& block) {}
void set_result_block_header(get_blocks_result_v2& result, const signed_block_ptr& block) {
bool fetch_block_header = std::get<get_blocks_request_v1>(*current_request).fetch_block_header;
if (fetch_block_header) {
std::visit([&result](auto block_ptr) {
if (block_ptr) {
result.block_header = static_cast<const signed_block_header&>(*block_ptr);
}
}, plugin->get_block(block_num));
result.block_header = static_cast<const signed_block_header&>(*block);
}
}

Expand All @@ -233,7 +221,7 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl

template <typename T>
std::enable_if_t<std::is_same_v<get_blocks_result_v1,T> || std::is_same_v<get_blocks_result_v2,T>>
send_update(T&& result) {
send_update(const signed_block_ptr& block, T&& result) {
need_to_send_update = true;
if (!send_queue.empty() || !max_messages_in_flight() )
return;
Expand All @@ -254,15 +242,15 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
if (prev_block_id)
result.prev_block = block_position{block_num - 1, *prev_block_id};
if (block_req.fetch_block) {
result.block = plugin->get_block(block_num);
result.block = signed_block_ptr_variant{block};
}
if (block_req.fetch_traces && plugin->trace_log) {
result.traces = plugin->trace_log->get_log_entry(block_num);
}
if (block_req.fetch_deltas && plugin->chain_state_log) {
result.deltas = plugin->chain_state_log->get_log_entry(block_num);
}
set_result_block_header(result, block_num);
set_result_block_header(result, block);
}
++block_num;
}
Expand Down Expand Up @@ -293,14 +281,14 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
}, result.block );
}

void send_update_for_block(block_position position) {
void send_update_for_block(const block_state_ptr& block_state) {
std::visit(
[&position, this](const auto& req) {
[&block_state, this](const auto& req) {
// send get_blocks_result_v1 when the request is get_blocks_request_v0 and
// send send_block_result_v2 when the request is get_blocks_request_ v1.
typename std::decay_t<decltype(req)>::response_type result;
result.head = position;
send_update(std::move(result));
result.head = { block_state->block_num, block_state->id };
send_update(block_state->block, std::move(result));
},
*current_request);
}
Expand All @@ -310,7 +298,7 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
if (!send_queue.empty() || !max_messages_in_flight())
return;

send_update_for_block({block_state->block_num, block_state->id});
send_update_for_block(block_state);
}

void send_update(bool changed = false) {
Expand All @@ -320,7 +308,7 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
!max_messages_in_flight())
return;
auto& chain = plugin->chain_plug->chain();
send_update_for_block({chain.head_block_num(), chain.head_block_id()});
send_update_for_block(chain.head_block_state());
}

template <typename F>
Expand Down

0 comments on commit 9e82ecf

Please sign in to comment.