From e7259307b949a5b197b24003c261a286447083c5 Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 14 Dec 2023 16:43:08 +0000 Subject: [PATCH 1/8] Reload rotation timestamp in isTimeToRotate --- libethereum/InstanceMonitor.cpp | 15 ++++++--------- libethereum/InstanceMonitor.h | 9 ++------- .../unittests/libethereum/InstanceMonitorTest.cpp | 9 ++++----- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/libethereum/InstanceMonitor.cpp b/libethereum/InstanceMonitor.cpp index e2a6585db..b94a74bdc 100644 --- a/libethereum/InstanceMonitor.cpp +++ b/libethereum/InstanceMonitor.cpp @@ -46,23 +46,20 @@ void InstanceMonitor::initRotationParams( uint64_t _finishTimestamp ) { std::ofstream rotationInfoFile( m_rotationInfoFilePath.string() ); rotationInfoFile << rotationJson; - m_finishTimestamp = _finishTimestamp; - LOG( m_logger ) << "Set rotation time to " << m_finishTimestamp; + LOG( m_logger ) << "Set rotation time to " << _finishTimestamp; } bool InstanceMonitor::isTimeToRotate( uint64_t _finishTimestamp ) { if ( !fs::exists( m_rotationInfoFilePath ) ) { return false; } - return m_finishTimestamp <= _finishTimestamp; + return getRotationTimestamp() <= _finishTimestamp; } -void InstanceMonitor::restoreRotationParams() { - if ( fs::exists( m_rotationInfoFilePath ) ) { - std::ifstream rotationInfoFile( m_rotationInfoFilePath.string() ); - auto rotationJson = nlohmann::json::parse( rotationInfoFile ); - m_finishTimestamp = rotationJson["timestamp"].get< uint64_t >(); - } +uint64_t InstanceMonitor::getRotationTimestamp() { + std::ifstream rotationInfoFile( m_rotationInfoFilePath.string() ); + auto rotationJson = nlohmann::json::parse( rotationInfoFile ); + return rotationJson["timestamp"].get< uint64_t >(); } void InstanceMonitor::reportExitTimeReached( bool _reached ) { diff --git a/libethereum/InstanceMonitor.h b/libethereum/InstanceMonitor.h index 84867ddcc..621e94f23 100644 --- a/libethereum/InstanceMonitor.h +++ b/libethereum/InstanceMonitor.h @@ -36,23 +36,18 @@ class InstanceMonitor { public: explicit InstanceMonitor( const boost::filesystem::path& _rotationInfoFileDirPath, std::shared_ptr< StatusAndControl > _statusAndControl = nullptr ) - : m_finishTimestamp( 0 ), - m_rotationInfoFilePath( _rotationInfoFileDirPath / rotation_info_file_name ), + : m_rotationInfoFilePath( _rotationInfoFileDirPath / rotation_info_file_name ), m_statusAndControl( _statusAndControl ) { - restoreRotationParams(); reportExitTimeReached( false ); } void prepareRotation(); void initRotationParams( uint64_t _finishTimestamp ); bool isTimeToRotate( uint64_t _finishTimestamp ); + uint64_t getRotationTimestamp(); protected: - void restoreRotationParams(); - [[nodiscard]] uint64_t finishTimestamp() const { return m_finishTimestamp; } - [[nodiscard]] fs::path rotationInfoFilePath() const { return m_rotationInfoFilePath; } - uint64_t m_finishTimestamp; const fs::path m_rotationInfoFilePath; std::shared_ptr< StatusAndControl > m_statusAndControl; diff --git a/test/unittests/libethereum/InstanceMonitorTest.cpp b/test/unittests/libethereum/InstanceMonitorTest.cpp index 760cf3f58..9a4cd50e5 100644 --- a/test/unittests/libethereum/InstanceMonitorTest.cpp +++ b/test/unittests/libethereum/InstanceMonitorTest.cpp @@ -17,10 +17,6 @@ class InstanceMonitorMock: public InstanceMonitor { public: explicit InstanceMonitorMock(fs::path const &rotationFlagFilePath, std::shared_ptr statusAndControl) : InstanceMonitor(rotationFlagFilePath, statusAndControl) {}; - uint64_t getFinishTimestamp() { - return this->finishTimestamp(); - }; - fs::path getRotationInfoFilePath() { return this->rotationInfoFilePath(); } @@ -70,7 +66,7 @@ BOOST_AUTO_TEST_CASE( test_initRotationParams ) { uint64_t ts = 100; BOOST_REQUIRE( !fs::exists(instanceMonitor->getRotationInfoFilePath() ) ); instanceMonitor->initRotationParams(ts); - BOOST_CHECK_EQUAL(instanceMonitor->getFinishTimestamp(), ts); + BOOST_CHECK_EQUAL(instanceMonitor->getRotationTimestamp(), ts); BOOST_REQUIRE( fs::exists(instanceMonitor->getRotationInfoFilePath() ) ); @@ -98,6 +94,9 @@ BOOST_AUTO_TEST_CASE( test_isTimeToRotate_true ) { instanceMonitor->initRotationParams(50); BOOST_REQUIRE( instanceMonitor->isTimeToRotate( currentTime ) ); + + currentTime = 49; + BOOST_REQUIRE( !instanceMonitor->isTimeToRotate( currentTime ) ); } BOOST_AUTO_TEST_CASE( test_rotation ) { From 5d8a88d6b6237e0e2862d15a322f4dbed83f612c Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 14 Dec 2023 21:50:11 +0000 Subject: [PATCH 2/8] Rename getRotationTimestamp -> rotationTimestamp --- libethereum/InstanceMonitor.cpp | 4 ++-- libethereum/InstanceMonitor.h | 2 +- test/unittests/libethereum/InstanceMonitorTest.cpp | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/libethereum/InstanceMonitor.cpp b/libethereum/InstanceMonitor.cpp index b94a74bdc..b101c1876 100644 --- a/libethereum/InstanceMonitor.cpp +++ b/libethereum/InstanceMonitor.cpp @@ -53,10 +53,10 @@ bool InstanceMonitor::isTimeToRotate( uint64_t _finishTimestamp ) { if ( !fs::exists( m_rotationInfoFilePath ) ) { return false; } - return getRotationTimestamp() <= _finishTimestamp; + return rotationTimestamp() <= _finishTimestamp; } -uint64_t InstanceMonitor::getRotationTimestamp() { +uint64_t InstanceMonitor::rotationTimestamp() const { std::ifstream rotationInfoFile( m_rotationInfoFilePath.string() ); auto rotationJson = nlohmann::json::parse( rotationInfoFile ); return rotationJson["timestamp"].get< uint64_t >(); diff --git a/libethereum/InstanceMonitor.h b/libethereum/InstanceMonitor.h index 621e94f23..8e6b23ab3 100644 --- a/libethereum/InstanceMonitor.h +++ b/libethereum/InstanceMonitor.h @@ -43,9 +43,9 @@ class InstanceMonitor { void prepareRotation(); void initRotationParams( uint64_t _finishTimestamp ); bool isTimeToRotate( uint64_t _finishTimestamp ); - uint64_t getRotationTimestamp(); protected: + [[nodiscard]] uint64_t rotationTimestamp() const; [[nodiscard]] fs::path rotationInfoFilePath() const { return m_rotationInfoFilePath; } const fs::path m_rotationInfoFilePath; diff --git a/test/unittests/libethereum/InstanceMonitorTest.cpp b/test/unittests/libethereum/InstanceMonitorTest.cpp index 9a4cd50e5..bd1ebee0e 100644 --- a/test/unittests/libethereum/InstanceMonitorTest.cpp +++ b/test/unittests/libethereum/InstanceMonitorTest.cpp @@ -28,6 +28,10 @@ class InstanceMonitorMock: public InstanceMonitor { void removeFlagFileTest(){ this->reportExitTimeReached( false ); } + + uint64_t getRotationTimestamp() const { + return this->rotationTimestamp(); + } }; class InstanceMonitorTestFixture : public TestOutputHelperFixture { From 85c1b31ed9ca1d8415d53b2a35a4723e7dcfb185 Mon Sep 17 00:00:00 2001 From: badrogger Date: Tue, 19 Dec 2023 19:39:07 +0000 Subject: [PATCH 3/8] Add logs --- libethereum/InstanceMonitor.cpp | 8 +++++--- libethereum/InstanceMonitor.h | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/libethereum/InstanceMonitor.cpp b/libethereum/InstanceMonitor.cpp index b101c1876..bad99e37f 100644 --- a/libethereum/InstanceMonitor.cpp +++ b/libethereum/InstanceMonitor.cpp @@ -49,17 +49,19 @@ void InstanceMonitor::initRotationParams( uint64_t _finishTimestamp ) { LOG( m_logger ) << "Set rotation time to " << _finishTimestamp; } -bool InstanceMonitor::isTimeToRotate( uint64_t _finishTimestamp ) { +bool InstanceMonitor::isTimeToRotate( uint64_t _blockTimestamp ) const { if ( !fs::exists( m_rotationInfoFilePath ) ) { return false; } - return rotationTimestamp() <= _finishTimestamp; + return rotationTimestamp() <= _blockTimestamp; } uint64_t InstanceMonitor::rotationTimestamp() const { std::ifstream rotationInfoFile( m_rotationInfoFilePath.string() ); auto rotationJson = nlohmann::json::parse( rotationInfoFile ); - return rotationJson["timestamp"].get< uint64_t >(); + auto timestamp = rotationJson["timestamp"].get< uint64_t >(); + LOG( m_logger ) << "Rotation scheduled for " << timestamp; + return timestamp; } void InstanceMonitor::reportExitTimeReached( bool _reached ) { diff --git a/libethereum/InstanceMonitor.h b/libethereum/InstanceMonitor.h index 8e6b23ab3..8470a2bbf 100644 --- a/libethereum/InstanceMonitor.h +++ b/libethereum/InstanceMonitor.h @@ -42,7 +42,7 @@ class InstanceMonitor { } void prepareRotation(); void initRotationParams( uint64_t _finishTimestamp ); - bool isTimeToRotate( uint64_t _finishTimestamp ); + bool isTimeToRotate( uint64_t _blockTimestamp ) const; protected: [[nodiscard]] uint64_t rotationTimestamp() const; @@ -56,5 +56,5 @@ class InstanceMonitor { void reportExitTimeReached( bool _reached ); private: - dev::Logger m_logger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) }; + mutable dev::Logger m_logger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) }; }; From dd6fdae20cc6fd624794e371204f2f2807fbc1fb Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 21 Dec 2023 17:32:46 +0000 Subject: [PATCH 4/8] Fix Net tests --- libweb3jsonrpc/Net.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libweb3jsonrpc/Net.h b/libweb3jsonrpc/Net.h index bc53e0c2b..dbf63c355 100644 --- a/libweb3jsonrpc/Net.h +++ b/libweb3jsonrpc/Net.h @@ -44,7 +44,7 @@ class Net : public NetFace { virtual bool net_listening() override; private: - const dev::eth::ChainParams& m_chainParams; + const dev::eth::ChainParams m_chainParams; }; } // namespace rpc From b3f1c0743023958fe737fffa58b720b571eb9333 Mon Sep 17 00:00:00 2001 From: badrogger Date: Wed, 3 Jan 2024 19:40:54 +0000 Subject: [PATCH 5/8] Handle malformed/missing rotationInfoFile --- libethereum/InstanceMonitor.cpp | 35 +++++++++++++------ libethereum/InstanceMonitor.h | 14 ++++++++ .../libethereum/InstanceMonitorTest.cpp | 9 +++++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/libethereum/InstanceMonitor.cpp b/libethereum/InstanceMonitor.cpp index bad99e37f..595175e0b 100644 --- a/libethereum/InstanceMonitor.cpp +++ b/libethereum/InstanceMonitor.cpp @@ -40,28 +40,43 @@ void InstanceMonitor::prepareRotation() { } void InstanceMonitor::initRotationParams( uint64_t _finishTimestamp ) { - nlohmann::json rotationJson = nlohmann::json::object(); - rotationJson["timestamp"] = _finishTimestamp; + try { + nlohmann::json rotationJson = nlohmann::json::object(); + rotationJson["timestamp"] = _finishTimestamp; - std::ofstream rotationInfoFile( m_rotationInfoFilePath.string() ); - rotationInfoFile << rotationJson; + std::ofstream rotationInfoFile( m_rotationInfoFilePath.string() ); + rotationInfoFile << rotationJson; - LOG( m_logger ) << "Set rotation time to " << _finishTimestamp; + LOG( m_logger ) << "Set rotation time to " << _finishTimestamp; + } catch ( ... ) { + LOG( m_logger ) << "Setting rotation timestamp failed"; + throw_with_nested( std::runtime_error( "cannot save rotation timestamp" ) ); + } } bool InstanceMonitor::isTimeToRotate( uint64_t _blockTimestamp ) const { if ( !fs::exists( m_rotationInfoFilePath ) ) { return false; } - return rotationTimestamp() <= _blockTimestamp; + try { + auto _rotationTimestamp = rotationTimestamp(); + return _rotationTimestamp <= _blockTimestamp; + } catch ( InvalidRotationInfoFileException& ex ) { + return false; + } } uint64_t InstanceMonitor::rotationTimestamp() const { std::ifstream rotationInfoFile( m_rotationInfoFilePath.string() ); - auto rotationJson = nlohmann::json::parse( rotationInfoFile ); - auto timestamp = rotationJson["timestamp"].get< uint64_t >(); - LOG( m_logger ) << "Rotation scheduled for " << timestamp; - return timestamp; + try { + auto rotationJson = nlohmann::json::parse( rotationInfoFile ); + auto timestamp = rotationJson["timestamp"].get< uint64_t >(); + LOG( m_logger ) << "Rotation scheduled for " << timestamp; + return timestamp; + } catch ( ... ) { + LOG( m_logger ) << "Rotation file is malformed or missing"; + throw InvalidRotationInfoFileException( m_rotationInfoFilePath ); + } } void InstanceMonitor::reportExitTimeReached( bool _reached ) { diff --git a/libethereum/InstanceMonitor.h b/libethereum/InstanceMonitor.h index 8470a2bbf..293a068f6 100644 --- a/libethereum/InstanceMonitor.h +++ b/libethereum/InstanceMonitor.h @@ -55,6 +55,20 @@ class InstanceMonitor { void reportExitTimeReached( bool _reached ); + class InvalidRotationInfoFileException : public std::exception { + protected: + std::string what_str; + + public: + boost::filesystem::path path; + + InvalidRotationInfoFileException( const boost::filesystem::path& _path ) : path( _path ) { + what_str = "File " + path.string() + " is malformed or missing"; + } + virtual const char* what() const noexcept override { return what_str.c_str(); } + }; + + private: mutable dev::Logger m_logger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) }; }; diff --git a/test/unittests/libethereum/InstanceMonitorTest.cpp b/test/unittests/libethereum/InstanceMonitorTest.cpp index bd1ebee0e..137b70555 100644 --- a/test/unittests/libethereum/InstanceMonitorTest.cpp +++ b/test/unittests/libethereum/InstanceMonitorTest.cpp @@ -80,6 +80,15 @@ BOOST_AUTO_TEST_CASE( test_initRotationParams ) { BOOST_CHECK_EQUAL(rotateJson["timestamp"].get< uint64_t >(), ts); } + +BOOST_AUTO_TEST_CASE( test_isTimeToRotate_invalid_file ) { + uint64_t currentTime = 100; + std::ofstream rotationInfoFile(instanceMonitor->getRotationInfoFilePath().string() ); + rotationInfoFile << "Broken file"; + BOOST_REQUIRE( !instanceMonitor->isTimeToRotate( currentTime ) ); +} + + BOOST_AUTO_TEST_CASE( test_isTimeToRotate_false ) { uint64_t currentTime = 100; uint64_t finishTime = 200; From 573f3c059213ff234067b408c33e19163a2da537 Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 4 Jan 2024 14:00:54 +0000 Subject: [PATCH 6/8] Rename rotation.txt -> rotation.json --- libethereum/InstanceMonitor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libethereum/InstanceMonitor.cpp b/libethereum/InstanceMonitor.cpp index 595175e0b..b3a48a74b 100644 --- a/libethereum/InstanceMonitor.cpp +++ b/libethereum/InstanceMonitor.cpp @@ -32,7 +32,7 @@ using namespace dev; namespace fs = boost::filesystem; -const std::string InstanceMonitor::rotation_info_file_name = "rotation.txt"; +const std::string InstanceMonitor::rotation_info_file_name = "rotation.json"; void InstanceMonitor::prepareRotation() { reportExitTimeReached( true ); From 1600857ba45aeb2703235cc6c4d41ac3e973f14d Mon Sep 17 00:00:00 2001 From: badrogger Date: Thu, 4 Jan 2024 18:24:26 +0000 Subject: [PATCH 7/8] Add error logger in InstanceMonitor --- libethereum/InstanceMonitor.cpp | 12 ++++++------ libethereum/InstanceMonitor.h | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/libethereum/InstanceMonitor.cpp b/libethereum/InstanceMonitor.cpp index b3a48a74b..d3dd72db1 100644 --- a/libethereum/InstanceMonitor.cpp +++ b/libethereum/InstanceMonitor.cpp @@ -47,9 +47,9 @@ void InstanceMonitor::initRotationParams( uint64_t _finishTimestamp ) { std::ofstream rotationInfoFile( m_rotationInfoFilePath.string() ); rotationInfoFile << rotationJson; - LOG( m_logger ) << "Set rotation time to " << _finishTimestamp; + LOG( m_info_logger ) << "Set rotation time to " << _finishTimestamp; } catch ( ... ) { - LOG( m_logger ) << "Setting rotation timestamp failed"; + LOG( m_error_logger ) << "Setting rotation timestamp failed"; throw_with_nested( std::runtime_error( "cannot save rotation timestamp" ) ); } } @@ -71,18 +71,18 @@ uint64_t InstanceMonitor::rotationTimestamp() const { try { auto rotationJson = nlohmann::json::parse( rotationInfoFile ); auto timestamp = rotationJson["timestamp"].get< uint64_t >(); - LOG( m_logger ) << "Rotation scheduled for " << timestamp; + LOG( m_info_logger ) << "Rotation scheduled for " << timestamp; return timestamp; } catch ( ... ) { - LOG( m_logger ) << "Rotation file is malformed or missing"; + LOG( m_error_logger ) << "Rotation file is malformed or missing"; throw InvalidRotationInfoFileException( m_rotationInfoFilePath ); } } void InstanceMonitor::reportExitTimeReached( bool _reached ) { if ( m_statusAndControl ) { - LOG( m_logger ) << "Setting ExitTimeReached = " << _reached; + LOG( m_info_logger ) << "Setting ExitTimeReached = " << _reached; m_statusAndControl->setExitState( StatusAndControl::ExitTimeReached, _reached ); } else - LOG( m_logger ) << "Simulating setting ExitTimeReached = " << _reached; + LOG( m_info_logger ) << "Simulating setting ExitTimeReached = " << _reached; } diff --git a/libethereum/InstanceMonitor.h b/libethereum/InstanceMonitor.h index 293a068f6..4e241ad4d 100644 --- a/libethereum/InstanceMonitor.h +++ b/libethereum/InstanceMonitor.h @@ -70,5 +70,6 @@ class InstanceMonitor { private: - mutable dev::Logger m_logger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) }; + mutable dev::Logger m_info_logger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) }; + mutable dev::Logger m_error_logger{ createLogger( dev::VerbosityError, "instance-monitor" ) }; }; From 178584719e84cc8d2f984017af35f5fe9d85a090 Mon Sep 17 00:00:00 2001 From: badrogger Date: Fri, 5 Jan 2024 11:50:56 +0000 Subject: [PATCH 8/8] Rename m_info_logger -> m_infoLogger --- libethereum/InstanceMonitor.cpp | 12 ++++++------ libethereum/InstanceMonitor.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libethereum/InstanceMonitor.cpp b/libethereum/InstanceMonitor.cpp index d3dd72db1..ed180f6fc 100644 --- a/libethereum/InstanceMonitor.cpp +++ b/libethereum/InstanceMonitor.cpp @@ -47,9 +47,9 @@ void InstanceMonitor::initRotationParams( uint64_t _finishTimestamp ) { std::ofstream rotationInfoFile( m_rotationInfoFilePath.string() ); rotationInfoFile << rotationJson; - LOG( m_info_logger ) << "Set rotation time to " << _finishTimestamp; + LOG( m_infoLogger ) << "Set rotation time to " << _finishTimestamp; } catch ( ... ) { - LOG( m_error_logger ) << "Setting rotation timestamp failed"; + LOG( m_errorLogger ) << "Setting rotation timestamp failed"; throw_with_nested( std::runtime_error( "cannot save rotation timestamp" ) ); } } @@ -71,18 +71,18 @@ uint64_t InstanceMonitor::rotationTimestamp() const { try { auto rotationJson = nlohmann::json::parse( rotationInfoFile ); auto timestamp = rotationJson["timestamp"].get< uint64_t >(); - LOG( m_info_logger ) << "Rotation scheduled for " << timestamp; + LOG( m_infoLogger ) << "Rotation scheduled for " << timestamp; return timestamp; } catch ( ... ) { - LOG( m_error_logger ) << "Rotation file is malformed or missing"; + LOG( m_errorLogger ) << "Rotation file is malformed or missing"; throw InvalidRotationInfoFileException( m_rotationInfoFilePath ); } } void InstanceMonitor::reportExitTimeReached( bool _reached ) { if ( m_statusAndControl ) { - LOG( m_info_logger ) << "Setting ExitTimeReached = " << _reached; + LOG( m_infoLogger ) << "Setting ExitTimeReached = " << _reached; m_statusAndControl->setExitState( StatusAndControl::ExitTimeReached, _reached ); } else - LOG( m_info_logger ) << "Simulating setting ExitTimeReached = " << _reached; + LOG( m_infoLogger ) << "Simulating setting ExitTimeReached = " << _reached; } diff --git a/libethereum/InstanceMonitor.h b/libethereum/InstanceMonitor.h index 4e241ad4d..688fddcbd 100644 --- a/libethereum/InstanceMonitor.h +++ b/libethereum/InstanceMonitor.h @@ -70,6 +70,6 @@ class InstanceMonitor { private: - mutable dev::Logger m_info_logger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) }; - mutable dev::Logger m_error_logger{ createLogger( dev::VerbosityError, "instance-monitor" ) }; + mutable dev::Logger m_infoLogger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) }; + mutable dev::Logger m_errorLogger{ createLogger( dev::VerbosityError, "instance-monitor" ) }; };