Skip to content

Commit

Permalink
GH-1289 Combine head and head_id into one optional
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Jun 28, 2023
1 parent 3971ff6 commit 9012e7f
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 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;
std::optional<block_id_type> head_id;
std::optional<std::pair<signed_block_ptr, block_id_type>> head;

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_id && 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{};
}

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

0 comments on commit 9012e7f

Please sign in to comment.