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

[1.1.0 -> main] Do not allow replay-blockchain without snapshot #1158

Merged
merged 5 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 28 additions & 5 deletions libraries/chain/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ namespace eosio { namespace chain {
else
head = {};
}

static std::optional<block_log_preamble> extract_block_log_preamble(const std::filesystem::path& block_dir,
const std::filesystem::path& retained_dir);
}; // block_log_impl

/// Would remove pre-existing block log and index, never write blocks into disk.
Expand Down Expand Up @@ -1459,8 +1462,8 @@ namespace eosio { namespace chain {
}

// static
std::optional<block_log::chain_context> block_log::extract_chain_context(const std::filesystem::path& block_dir,
const std::filesystem::path& retained_dir) {
std::optional<block_log_preamble> detail::block_log_impl::extract_block_log_preamble(const std::filesystem::path& block_dir,
const std::filesystem::path& retained_dir) {
std::filesystem::path first_block_file;
if (!retained_dir.empty() && std::filesystem::exists(retained_dir)) {
for_each_file_in_dir_matches(retained_dir, R"(blocks-1-\d+\.log)",
Expand All @@ -1474,9 +1477,9 @@ namespace eosio { namespace chain {
}

if (!first_block_file.empty()) {
return block_log_data(first_block_file).get_preamble().chain_context;
return block_log_data(first_block_file).get_preamble();
}

if (!retained_dir.empty() && std::filesystem::exists(retained_dir)) {
const std::regex my_filter(R"(blocks-\d+-\d+\.log)");
std::smatch what;
Expand All @@ -1489,12 +1492,22 @@ namespace eosio { namespace chain {
std::string file = p->path().filename().string();
if (!std::regex_match(file, what, my_filter))
continue;
return block_log_data(p->path()).chain_id();
return block_log_data(p->path()).get_preamble();
}
}
return {};
}

// static
std::optional<block_log::chain_context> block_log::extract_chain_context(const std::filesystem::path& block_dir,
const std::filesystem::path& retained_dir) {
auto preamble = detail::block_log_impl::extract_block_log_preamble(block_dir, retained_dir);
if (preamble) {
return preamble->chain_context;
}
return {};
}

// static
std::optional<genesis_state> block_log::extract_genesis_state(const std::filesystem::path& block_dir,
const std::filesystem::path& retained_dir) {
Expand All @@ -1516,6 +1529,16 @@ namespace eosio { namespace chain {
} , *context);
}

// static
uint32_t block_log::extract_first_block_num(const std::filesystem::path& block_dir,
const std::filesystem::path& retained_dir) {
auto preamble = detail::block_log_impl::extract_block_log_preamble(block_dir, retained_dir);
if (preamble) {
return preamble->first_block_num;
}
return 0;
}

// static
bool block_log::contains_genesis_state(uint32_t version, uint32_t first_block_num) {
return version < genesis_state_or_chain_id_version || first_block_num == 1;
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ namespace eosio::chain {
auto first_branch = (first == root->id()) ? root : get_block_impl(first);
auto second_branch = (second == root->id()) ? root : get_block_impl(second);

EOS_ASSERT(first_branch, fork_db_block_not_found, "block ${id} does not exist", ("id", first));
EOS_ASSERT(second_branch, fork_db_block_not_found, "block ${id} does not exist", ("id", second));
EOS_ASSERT(first_branch, fork_db_block_not_found, "block #${n} ${id} does not exist", ("n", block_header::num_from_id(first))("id", first));
EOS_ASSERT(second_branch, fork_db_block_not_found, "block #${n} ${id} does not exist", ("n", block_header::num_from_id(second))("id", second));

while( first_branch->block_num() > second_branch->block_num() )
{
Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/include/eosio/chain/block_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ namespace eosio { namespace chain {
extract_chain_id(const std::filesystem::path& data_dir,
const std::filesystem::path& retained_dir = std::filesystem::path{});

static uint32_t extract_first_block_num(const std::filesystem::path& block_dir,
const std::filesystem::path& retained_dir = std::filesystem::path{});

static void construct_index(const std::filesystem::path& block_file_name, const std::filesystem::path& index_file_name);

static bool contains_genesis_state(uint32_t version, uint32_t first_block_num);
Expand Down
5 changes: 5 additions & 0 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,11 @@ void chain_plugin_impl::plugin_initialize(const variables_map& options) {
ilog( "Replay requested: deleting state database" );
if( options.at( "truncate-at-block" ).as<uint32_t>() > 0 )
wlog( "The --truncate-at-block option does not work for a regular replay of the blockchain." );
if (!options.count( "snapshot" )) {
auto first_block = block_log::extract_first_block_num(blocks_dir, retained_dir);
EOS_ASSERT(first_block == 1, plugin_config_exception,
"replay-blockchain without snapshot requested without a full block log, first block: ${n}", ("n", first_block));
}
clear_directory_contents( chain_config->state_dir );
} else if( options.at( "truncate-at-block" ).as<uint32_t>() > 0 ) {
wlog( "The --truncate-at-block option can only be used with --hard-replay-blockchain." );
Expand Down