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

Added leap-util subcommand to check for a clean shutdown #914

Merged
merged 4 commits into from
Apr 17, 2023
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
54 changes: 54 additions & 0 deletions programs/leap-util/actions/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ FC_REFLECT(chainbase::environment, (debug) (os) (arch) (boost_version) (compiler

void chain_actions::setup(CLI::App& app) {
auto* sub = app.add_subcommand("chain-state", "chain utility");
sub->add_option("--state-dir", opt->sstate_state_dir, "The location of the state directory (absolute path or relative to the current directory)")->capture_default_str();
sub->require_subcommand();
sub->fallthrough();

Expand All @@ -47,6 +48,12 @@ void chain_actions::setup(CLI::App& app) {
// properly return err code in main
if(rc) throw(CLI::RuntimeError(rc));
});

sub->add_subcommand("last-shutdown-state", "indicate whether last shutdown was clean or not")->callback([&]() {
int rc = run_subcommand_sstate();
// properly return err code in main
if(rc) throw(CLI::RuntimeError(rc));
});
}

int chain_actions::run_subcommand_build() {
Expand All @@ -62,5 +69,52 @@ int chain_actions::run_subcommand_build() {
std::cout << fc::json::to_pretty_string(chainbase::environment()) << std::endl;
}

return 0;
}

int chain_actions::run_subcommand_sstate() {
bfs::path state_dir = "";

// default state dir, if none specified
if(opt->sstate_state_dir.empty()) {
auto root = fc::app_path();
auto default_data_dir = root / "eosio" / "nodeos" / "data" ;
state_dir = default_data_dir / config::default_state_dir_name;
}
else {
// adjust if path relative
state_dir = opt->sstate_state_dir;
if(state_dir.is_relative()) {
state_dir = bfs::current_path() / state_dir;
}
}

auto shared_mem_path = state_dir / "shared_memory.bin";

if(!bfs::exists(shared_mem_path)) {
std::cerr << "Unable to read database status: file not found: " << shared_mem_path << std::endl;
return -1;
}

char header[chainbase::header_size];
std::ifstream hs(shared_mem_path.generic_string(), std::ifstream::binary);
hs.read(header, chainbase::header_size);
if(hs.fail()) {
std::cerr << "Unable to read database status: file invalid or corrupt" << shared_mem_path << std::endl;
return -1;
}

chainbase::db_header* dbheader = reinterpret_cast<chainbase::db_header*>(header);
if(dbheader->id != chainbase::header_id) {
std::string what_str("\"" + state_dir.generic_string() + "\" database format not compatible with this version of chainbase.");
std::cerr << what_str << std::endl;
return -1;
}
if(dbheader->dirty) {
std::cout << "Database dirty flag is set, shutdown was not clean" << std::endl;
return -1;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check that dbheader's header_id is expected value, as it's possible the format will change in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, done


std::cout << "Database state is clean" << std::endl;
return 0;
}
2 changes: 2 additions & 0 deletions programs/leap-util/actions/chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
struct chain_options {
bool build_just_print = false;
std::string build_output_file = "";
std::string sstate_state_dir = "";
};

class chain_actions : public sub_command<chain_options> {
Expand All @@ -14,4 +15,5 @@ class chain_actions : public sub_command<chain_options> {

// callbacks
int run_subcommand_build();
int run_subcommand_sstate();
};