Skip to content

Commit

Permalink
Merge pull request #914 from AntelopeIO/leaputil-shutdown-state
Browse files Browse the repository at this point in the history
Added leap-util subcommand to check for a clean shutdown
  • Loading branch information
vladtr authored Apr 17, 2023
2 parents 2ef8789 + b837aff commit d849c9c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
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;
}

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();
};

0 comments on commit d849c9c

Please sign in to comment.