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

Blocklog: Return std::optional for head_id #1351

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 17 additions & 25 deletions libraries/chain/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ namespace eosio { namespace chain {
inline static uint32_t default_initial_version = block_log::max_supported_version;

std::mutex mtx;
signed_block_ptr head;
block_id_type head_id;
std::optional<std::pair<signed_block_ptr, block_id_type>> head;
linh2931 marked this conversation as resolved.
Show resolved Hide resolved

virtual ~block_log_impl() = default;

Expand All @@ -482,16 +481,10 @@ namespace eosio { namespace chain {

virtual signed_block_ptr read_head() = 0;
void update_head(const signed_block_ptr& b, const std::optional<block_id_type>& id = {}) {
head = b;
if (id) {
head_id = *id;
} else {
if (head) {
head_id = b->calculate_id();
} else {
head_id = {};
}
}
if (b)
head = { b, id ? *id : b->calculate_id() };
else
head = {};
}
}; // block_log_impl

Expand All @@ -504,7 +497,7 @@ namespace eosio { namespace chain {
std::filesystem::remove(log_dir / "blocks.index");
}

uint32_t first_block_num() final { return head ? head->block_num() : first_block_number; }
uint32_t first_block_num() final { return head ? head->first->block_num() : first_block_number; }
void append(const signed_block_ptr& b, const block_id_type& id, const std::vector<char>& packed_block) final {
update_head(b, id);
}
Expand Down Expand Up @@ -591,7 +584,7 @@ namespace eosio { namespace chain {
}

uint64_t get_block_pos(uint32_t block_num) final {
if (!(head && block_num <= block_header::num_from_id(head_id) &&
if (!(head && block_num <= block_header::num_from_id(head->second) &&
block_num >= working_block_file_first_block_num()))
return block_log::npos;
index_file.seek(sizeof(uint64_t) * (block_num - index_first_block_num()));
Expand Down Expand Up @@ -707,7 +700,7 @@ namespace eosio { namespace chain {
uint32_t num_blocks;
this->block_file.seek_end(-sizeof(uint32_t));
fc::raw::unpack(this->block_file, num_blocks);
return this->head->block_num() - num_blocks + 1;
return this->head->first->block_num() - num_blocks + 1;
}

void reset(uint32_t first_bnum, std::variant<genesis_state, chain_id_type>&& chain_context, uint32_t version) {
Expand Down Expand Up @@ -740,7 +733,6 @@ namespace eosio { namespace chain {

this->reset(first_block_num, chain_id, block_log::max_supported_version);
this->head.reset();
head_id = {};
}

void flush() final {
Expand Down Expand Up @@ -804,7 +796,7 @@ namespace eosio { namespace chain {
size_t copy_from_pos = get_block_pos(first_block_num);
block_file.seek_end(-sizeof(uint32_t));
size_t copy_sz = block_file.tellp() - copy_from_pos;
const uint32_t num_blocks_in_log = chain::block_header::num_from_id(head_id) - first_block_num + 1;
const uint32_t num_blocks_in_log = chain::block_header::num_from_id(head->second) - first_block_num + 1;

const size_t offset_bytes = copy_from_pos - copy_to_pos;
const size_t offset_blocks = first_block_num - index_first_block_num;
Expand Down Expand Up @@ -992,7 +984,7 @@ namespace eosio { namespace chain {
block_file.close();
index_file.close();

catalog.add(preamble.first_block_num, this->head->block_num(), block_file.get_file_path().parent_path(),
catalog.add(preamble.first_block_num, this->head->first->block_num(), block_file.get_file_path().parent_path(),
"blocks");

using std::swap;
Expand All @@ -1007,7 +999,7 @@ namespace eosio { namespace chain {

preamble.ver = block_log::max_supported_version;
preamble.chain_context = preamble.chain_id();
preamble.first_block_num = this->head->block_num() + 1;
preamble.first_block_num = this->head->first->block_num() + 1;
preamble.write_to(block_file);
}

Expand All @@ -1018,7 +1010,7 @@ namespace eosio { namespace chain {
}

void post_append(uint64_t pos) final {
if (head->block_num() % stride == 0) {
if (head->first->block_num() % stride == 0) {
split_log();
}
}
Expand Down Expand Up @@ -1121,7 +1113,7 @@ namespace eosio { namespace chain {
if ((pos & prune_config.prune_threshold) != (end & prune_config.prune_threshold))
num_blocks_in_log = prune(fc::log_level::debug);
else
num_blocks_in_log = chain::block_header::num_from_id(head_id) - first_block_number + 1;
num_blocks_in_log = chain::block_header::num_from_id(head->second) - first_block_number + 1;
fc::raw::pack(block_file, num_blocks_in_log);
}

Expand All @@ -1142,7 +1134,7 @@ namespace eosio { namespace chain {
uint32_t prune(const fc::log_level& loglevel) {
if (!head)
return 0;
const uint32_t head_num = chain::block_header::num_from_id(head_id);
const uint32_t head_num = chain::block_header::num_from_id(head->second);
if (head_num - first_block_number < prune_config.prune_blocks)
return head_num - first_block_number + 1;

Expand Down Expand Up @@ -1252,12 +1244,12 @@ namespace eosio { namespace chain {

signed_block_ptr block_log::head() const {
std::lock_guard g(my->mtx);
return my->head;
return my->head ? my->head->first : signed_block_ptr{};
}

block_id_type block_log::head_id() const {
std::optional<block_id_type> block_log::head_id() const {
std::lock_guard g(my->mtx);
return my->head_id;
return my->head ? my->head->second : std::optional<block_id_type>{};
}

uint32_t block_log::first_block_num() const {
Expand Down
6 changes: 3 additions & 3 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ struct controller_impl {
void log_irreversible() {
EOS_ASSERT( fork_db.root(), fork_database_exception, "fork database not properly initialized" );

const block_id_type log_head_id = blog.head_id();
const bool valid_log_head = !log_head_id.empty();
const std::optional<block_id_type> log_head_id = blog.head_id();
const bool valid_log_head = !!log_head_id;

const auto lib_num = valid_log_head ? block_header::num_from_id(log_head_id) : (blog.first_block_num() - 1);
const auto lib_num = valid_log_head ? block_header::num_from_id(*log_head_id) : (blog.first_block_num() - 1);

auto root_id = fork_db.root()->id;

Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/block_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace eosio { namespace chain {

signed_block_ptr read_head()const; //use blocklog
signed_block_ptr head()const;
block_id_type head_id()const;
std::optional<block_id_type> head_id()const;

uint32_t first_block_num() const;

Expand Down
3 changes: 2 additions & 1 deletion tests/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ struct block_log_fixture {

void check_range_present(uint32_t first, uint32_t last) {
BOOST_REQUIRE_EQUAL(log->first_block_num(), first);
BOOST_REQUIRE_EQUAL(eosio::chain::block_header::num_from_id(log->head_id()), last);
BOOST_REQUIRE(log->head_id());
BOOST_REQUIRE_EQUAL(eosio::chain::block_header::num_from_id(*log->head_id()), last);
if(enable_read) {
for(auto i = first; i <= last; i++) {
std::vector<char> buff;
Expand Down