diff --git a/skaled/main.cpp b/skaled/main.cpp index 7dd46d88f..dc01620c5 100644 --- a/skaled/main.cpp +++ b/skaled/main.cpp @@ -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" ) ) { @@ -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 { @@ -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 ); } diff --git a/test/unittests/libdevcore/StatusAndControlTest.cpp b/test/unittests/libdevcore/StatusAndControlTest.cpp new file mode 100644 index 000000000..fc6374fde --- /dev/null +++ b/test/unittests/libdevcore/StatusAndControlTest.cpp @@ -0,0 +1,86 @@ +#include +#include + +#include + +#include +#include +#include + +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(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()