Skip to content

Commit

Permalink
Merge pull request #2093 from skalenetwork/reset-subsystem-status
Browse files Browse the repository at this point in the history
Reset subsystem status
  • Loading branch information
DmytroNazarenko authored Feb 10, 2025
2 parents c27e6c6 + 21277e6 commit 3ff7836
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
11 changes: 9 additions & 2 deletions skaled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,8 +1115,6 @@ int main( int argc, char** argv ) try {
}
}

std::shared_ptr< StatusAndControl > statusAndControl = std::make_shared< StatusAndControlFile >(
boost::filesystem::path( configPath ).remove_filename() );
// for now, leave previous values in file (for case of crash)

if ( vm.count( "main-net-url" ) ) {
Expand Down Expand Up @@ -1579,6 +1577,14 @@ int main( int argc, char** argv ) try {
chainParams.nodeInfo.sgxServerUrl = strURL;
}

std::shared_ptr< StatusAndControl > statusAndControl = std::make_shared< StatusAndControlFile >(
boost::filesystem::path( configPath ).remove_filename() );

// Reset subsystem running status before initialization procedure started
statusAndControl->setSubsystemRunning( StatusAndControl::SnapshotDownloader, false );
statusAndControl->setSubsystemRunning( StatusAndControl::Blockchain, false );
statusAndControl->setSubsystemRunning( StatusAndControl::Rpc, false );

std::shared_ptr< SharedSpace > sharedSpace;
if ( vm.count( "shared-space-path" ) ) {
try {
Expand Down Expand Up @@ -1814,6 +1820,7 @@ int main( int argc, char** argv ) try {

// this must be last! (or client will be mining blocks before this!)
g_client->startWorking();

statusAndControl->setSubsystemRunning( StatusAndControl::Blockchain, true );
}

Expand Down
86 changes: 86 additions & 0 deletions test/unittests/libdevcore/StatusAndControlTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <libdevcore/StatusAndControl.h>
#include <test/tools/libtesteth/TestHelper.h>

#include <fstream>

#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <json.hpp>

using namespace dev;
using namespace dev::eth;
using namespace dev::test;

namespace fs = boost::filesystem;

class StatusAndControlTestFixture : public TestOutputHelperFixture {
public:
fs::path statusFolderPath = "test-status";
std::string statusFileName = "skaled.status";

StatusAndControlTestFixture() {
fs::create_directory(statusFolderPath);

statusFilePath = statusFolderPath/"skaled.status";
}

fs::path statusFilePath;

~StatusAndControlTestFixture() override {
return;
if (fs::exists(statusFilePath)) {
fs::remove(statusFilePath);
}
if (fs::exists(statusFolderPath/"skaled.status")) {
fs::remove(statusFolderPath/"skaled.status");
}
if (fs::exists(statusFolderPath)) {
fs::remove(statusFolderPath);
}
};
};

BOOST_FIXTURE_TEST_SUITE( StatusAndControlSuite, StatusAndControlTestFixture )


nlohmann::json::object_t readJson( const fs::path& path ) {
std::ifstream File(path);
std::stringstream Buffer;
Buffer << File.rdbuf();
File.close();
return nlohmann::json::parse(Buffer.str());
}


BOOST_AUTO_TEST_CASE(test_status_file_creation) {
auto Status = std::make_shared<StatusAndControlFile>(statusFolderPath, statusFileName);

// Set and verify exit states
Status->setExitState(StatusAndControl::ExitState::ClearDataDir, true);
Status->setExitState(StatusAndControl::ExitState::StartAgain, true);
Status->setExitState(StatusAndControl::ExitState::StartFromSnapshot, true);
Status->setExitState(StatusAndControl::ExitState::ExitTimeReached, true);

// Set and verify subsystem running states
Status->setSubsystemRunning(StatusAndControl::Subsystem::SnapshotDownloader, true);
Status->setSubsystemRunning(StatusAndControl::Subsystem::WaitingForTimestamp, true);
Status->setSubsystemRunning(StatusAndControl::Subsystem::Blockchain, true);
Status->setSubsystemRunning(StatusAndControl::Subsystem::Rpc, true);

// Verify all fields in JSON
auto StatusJson = readJson(statusFilePath);

// Verify subsystemRunning
BOOST_REQUIRE(StatusJson["subsystemRunning"]["SnapshotDownloader"] == true);
BOOST_REQUIRE(StatusJson["subsystemRunning"]["WaitingForTimestamp"] == true);
BOOST_REQUIRE(StatusJson["subsystemRunning"]["Blockchain"] == true);
BOOST_REQUIRE(StatusJson["subsystemRunning"]["Rpc"] == true);

// Verify exitState
BOOST_REQUIRE(StatusJson["exitState"]["ClearDataDir"] == true);
BOOST_REQUIRE(StatusJson["exitState"]["StartAgain"] == true);
BOOST_REQUIRE(StatusJson["exitState"]["StartFromSnapshot"] == true);
BOOST_REQUIRE(StatusJson["exitState"]["ExitTimeReached"] == true);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 3ff7836

Please sign in to comment.