From e7bc6ea13393e020aba159548221d5f48072fd35 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Mon, 21 Oct 2024 15:44:22 +0100 Subject: [PATCH 1/7] IS 968 fix functional tests for historic build --- libskale/SnapshotManager.cpp | 24 ++++++++++++++++-------- libweb3jsonrpc/Skale.cpp | 4 +--- libweb3jsonrpc/Skale.h | 2 +- skaled/main.cpp | 2 +- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/libskale/SnapshotManager.cpp b/libskale/SnapshotManager.cpp index 77a25c902..e5d655dc7 100644 --- a/libskale/SnapshotManager.cpp +++ b/libskale/SnapshotManager.cpp @@ -160,11 +160,15 @@ void SnapshotManager::restoreSnapshot( unsigned _blockNumber ) { UnsafeRegion::lock ur_lock; - std::vector< std::string > volumes; - if ( chainParams.nodeInfo.archiveMode && _blockNumber == 0 ) - volumes = coreVolumes; - else + std::vector< std::string > volumes = coreVolumes; +#ifdef HISTORIC_STATE + if ( _blockNumber > 0 ) volumes = allVolumes; +#endif + // if ( chainParams.nodeInfo.archiveMode && _blockNumber == 0 ) + // volumes = coreVolumes; + // else + // volumes = allVolumes; int dummy_counter = 0; for ( const string& vol : volumes ) { @@ -747,11 +751,15 @@ void SnapshotManager::computeSnapshotHash( unsigned _blockNumber, bool is_checki int dummy_counter = 0; - std::vector< std::string > volumes; - if ( chainParams.nodeInfo.archiveMode && _blockNumber == 0 ) - volumes = coreVolumes; - else + std::vector< std::string > volumes = coreVolumes; +#ifdef HISTORIC_STATE + if ( _blockNumber > 0 ) volumes = allVolumes; +#endif + // if ( chainParams.nodeInfo.archiveMode && _blockNumber == 0 ) + // volumes = coreVolumes; + // else + // volumes = allVolumes; for ( const auto& volume : volumes ) { int res = btrfs.subvolume.property_set( diff --git a/libweb3jsonrpc/Skale.cpp b/libweb3jsonrpc/Skale.cpp index 8e81dad4e..c2c1d6f42 100644 --- a/libweb3jsonrpc/Skale.cpp +++ b/libweb3jsonrpc/Skale.cpp @@ -582,8 +582,7 @@ std::string Skale::oracle_checkResult( std::string& receipt ) { namespace snapshot { bool download( const std::string& strURLWeb3, unsigned& block_number, const fs::path& saveTo, - fn_progress_t onProgress, bool isBinaryDownload, std::string* pStrErrorDescription, - bool forArchiveNode ) { + fn_progress_t onProgress, bool isBinaryDownload, std::string* pStrErrorDescription ) { if ( pStrErrorDescription ) pStrErrorDescription->clear(); std::ofstream f; @@ -636,7 +635,6 @@ bool download( const std::string& strURLWeb3, unsigned& block_number, const fs:: joIn["method"] = "skale_getSnapshot"; nlohmann::json joParams = nlohmann::json::object(); joParams["blockNumber"] = block_number; - joParams["forArchiveNode"] = forArchiveNode; joIn["params"] = joParams; skutils::rest::data_t d = cli.call( joIn ); if ( !d.err_s_.empty() ) { diff --git a/libweb3jsonrpc/Skale.h b/libweb3jsonrpc/Skale.h index 93f3ded44..3a39db3e7 100644 --- a/libweb3jsonrpc/Skale.h +++ b/libweb3jsonrpc/Skale.h @@ -118,7 +118,7 @@ typedef std::function< bool( size_t idxChunck, size_t cntChunks ) > fn_progress_ extern bool download( const std::string& strURLWeb3, unsigned& block_number, const fs::path& saveTo, fn_progress_t onProgress, bool isBinaryDownload = true, - std::string* pStrErrorDescription = nullptr, bool forArchiveNode = false ); + std::string* pStrErrorDescription = nullptr ); }; // namespace snapshot diff --git a/skaled/main.cpp b/skaled/main.cpp index 03a02ffa5..386a985fc 100644 --- a/skaled/main.cpp +++ b/skaled/main.cpp @@ -266,7 +266,7 @@ void downloadSnapshot( unsigned block_number, std::shared_ptr< SnapshotManager > << cc::normal( " of " ) << cc::size10( cntChunks ) << "\r"; return true; // continue download }, - isBinaryDownload, &strErrorDescription, chainParams.nodeInfo.archiveMode ); + isBinaryDownload, &strErrorDescription ); std::cout << " \r"; // clear // progress // line From e4d710ad0b68e2d37c357866847060efe77c372f Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Mon, 21 Oct 2024 19:26:49 +0100 Subject: [PATCH 2/7] IS 968 fix functional tests for historic build --- libskale/SnapshotManager.cpp | 13 +++++++++++++ skaled/main.cpp | 19 +++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/libskale/SnapshotManager.cpp b/libskale/SnapshotManager.cpp index e5d655dc7..7340e8354 100644 --- a/libskale/SnapshotManager.cpp +++ b/libskale/SnapshotManager.cpp @@ -184,6 +184,19 @@ void SnapshotManager::restoreSnapshot( unsigned _blockNumber ) { batched_io::test_crash_before_commit( "SnapshotManager::doSnapshot" ); } // for + + if ( _blockNumber > 0 ) { +#ifdef HISTORIC_STATE + for ( const string& vol : allVolumes ) { + // continue if already present + if ( fs::exists( dataDir / vol ) && 0 == btrfs.present( ( dataDir / vol ).c_str() ) ) + continue; + + // create if not created yet ( only makes sense for historic nodes and 0 block number ) + btrfs.subvolume.create( ( dataDir / vol ).c_str() ); + } // for +#endif + } } // exceptions: diff --git a/skaled/main.cpp b/skaled/main.cpp index 386a985fc..f9827c4c4 100644 --- a/skaled/main.cpp +++ b/skaled/main.cpp @@ -251,8 +251,7 @@ void downloadSnapshot( unsigned block_number, std::shared_ptr< SnapshotManager > const std::string& strURLWeb3, const ChainParams& chainParams ) { fs::path saveTo; try { - clog( VerbosityInfo, "downloadSnapshot" ) - << cc::normal( "Will download snapshot from " ) << cc::u( strURLWeb3 ) << std::endl; + clog( VerbosityInfo, "downloadSnapshot" ) << "Will download snapshot from " << strURLWeb3; try { bool isBinaryDownload = true; @@ -262,8 +261,7 @@ void downloadSnapshot( unsigned block_number, std::shared_ptr< SnapshotManager > strURLWeb3, block_number, saveTo, [&]( size_t idxChunck, size_t cntChunks ) -> bool { clog( VerbosityInfo, "downloadSnapshot" ) - << cc::normal( "... download progress ... " ) << cc::size10( idxChunck ) - << cc::normal( " of " ) << cc::size10( cntChunks ) << "\r"; + << "... download progress ... " << idxChunck << " of " << cntChunks << "\r"; return true; // continue download }, isBinaryDownload, &strErrorDescription ); @@ -281,14 +279,12 @@ void downloadSnapshot( unsigned block_number, std::shared_ptr< SnapshotManager > std::throw_with_nested( std::runtime_error( "Exception while downloading snapshot" ) ); } clog( VerbosityInfo, "downloadSnapshot" ) - << cc::success( "Snapshot download success for block " ) - << cc::u( to_string( block_number ) ) << std::endl; + << "Snapshot download success for block " << to_string( block_number ); try { snapshotManager->importDiff( block_number ); } catch ( ... ) { - std::throw_with_nested( std::runtime_error( - cc::fatal( "FATAL:" ) + " " + - cc::error( "Exception while importing downloaded snapshot: " ) ) ); + std::throw_with_nested( + std::runtime_error( "FATAL: Exception while importing downloaded snapshot: " ) ); } /// HACK refactor this piece of code! /// @@ -304,7 +300,7 @@ void downloadSnapshot( unsigned block_number, std::shared_ptr< SnapshotManager > } if ( db_path.empty() ) { clog( VerbosityError, "downloadSnapshot" ) - << cc::fatal( "Snapshot downloaded without " + prefix + " db" ) << std::endl; + << "Snapshot downloaded without " + prefix + " db"; return; } @@ -315,8 +311,7 @@ void downloadSnapshot( unsigned block_number, std::shared_ptr< SnapshotManager > } catch ( ... ) { std::throw_with_nested( - std::runtime_error( cc::fatal( "FATAL:" ) + " " + - cc::error( "Exception while processing downloaded snapshot: " ) ) ); + std::runtime_error( "FATAL: Exception while processing downloaded snapshot: " ) ); } if ( !saveTo.empty() ) fs::remove( saveTo ); From a5492a90c049c79ae14eecd35af5e6d5e50fcb2e Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 22 Oct 2024 10:15:15 +0100 Subject: [PATCH 3/7] IS 968 fix functional tests for historic build --- libskale/SnapshotManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libskale/SnapshotManager.cpp b/libskale/SnapshotManager.cpp index 7340e8354..c0c5f0c02 100644 --- a/libskale/SnapshotManager.cpp +++ b/libskale/SnapshotManager.cpp @@ -185,7 +185,7 @@ void SnapshotManager::restoreSnapshot( unsigned _blockNumber ) { } // for - if ( _blockNumber > 0 ) { + if ( _blockNumber == 0 ) { #ifdef HISTORIC_STATE for ( const string& vol : allVolumes ) { // continue if already present From 06a445ac8ad61dd7293b5202efd1e9844a284a9e Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 22 Oct 2024 15:27:49 +0100 Subject: [PATCH 4/7] IS 1083 fix snapshot downloading priority --- skaled/main.cpp | 51 +++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/skaled/main.cpp b/skaled/main.cpp index 03a02ffa5..b6eb27657 100644 --- a/skaled/main.cpp +++ b/skaled/main.cpp @@ -1606,27 +1606,10 @@ int main( int argc, char** argv ) try { std::vector< std::string > coreVolumes = { BlockChain::getChainDirName( chainParams ), "filestorage", "prices_" + chainParams.nodeInfo.id.str() + ".db", "blocks_" + chainParams.nodeInfo.id.str() + ".db" }; - std::vector< std::string > archiveVolumes = {}; - if ( chainParams.nodeInfo.archiveMode ) { -#ifdef HISTORIC_STATE - archiveVolumes.insert( archiveVolumes.end(), { "historic_roots", "historic_state" } ); -#endif - } snapshotManager.reset( new SnapshotManager( chainParams, getDataDir(), sharedSpace ? sharedSpace->getPath() : "" ) ); } - bool downloadGenesisForSyncNode = false; - if ( chainParams.nodeInfo.syncNode ) { - auto bc = BlockChain( chainParams, getDataDir() ); - if ( bc.number() == 0 ) { - downloadSnapshotFlag = true; - if ( chainParams.nodeInfo.syncFromCatchup ) { - downloadGenesisForSyncNode = true; - } - } - } - if ( downloadSnapshotFlag ) { statusAndControl->setExitState( StatusAndControl::StartAgain, true ); statusAndControl->setExitState( StatusAndControl::StartFromSnapshot, true ); @@ -1637,18 +1620,8 @@ int main( int argc, char** argv ) try { sharedSpace_lock.reset( new std::lock_guard< SharedSpace >( *sharedSpace ) ); try { - if ( !downloadGenesisForSyncNode ) - downloadAndProccessSnapshot( - snapshotManager, chainParams, urlToDownloadSnapshotFrom, true ); - else { - try { - downloadAndProccessSnapshot( - snapshotManager, chainParams, urlToDownloadSnapshotFrom, false ); - snapshotManager->restoreSnapshot( 0 ); - } catch ( SnapshotManager::SnapshotAbsent& ) { - clog( VerbosityWarning, "main" ) << "Snapshot for 0 block is not found"; - } - } + downloadAndProccessSnapshot( + snapshotManager, chainParams, urlToDownloadSnapshotFrom, true ); // if we dont have 0 snapshot yet try { @@ -1672,6 +1645,26 @@ int main( int argc, char** argv ) try { } // if --download-snapshot + // download 0 snapshot if needed + if ( chainParams.nodeInfo.syncNode ) { + auto bc = BlockChain( chainParams, getDataDir() ); + if ( bc.number() == 0 ) { + if ( chainParams.nodeInfo.syncFromCatchup && !downloadSnapshotFlag ) { + statusAndControl->setExitState( StatusAndControl::StartAgain, true ); + statusAndControl->setExitState( StatusAndControl::StartFromSnapshot, true ); + statusAndControl->setSubsystemRunning( StatusAndControl::SnapshotDownloader, true ); + + try { + downloadAndProccessSnapshot( + snapshotManager, chainParams, urlToDownloadSnapshotFrom, false ); + snapshotManager->restoreSnapshot( 0 ); + } catch ( SnapshotManager::SnapshotAbsent& ) { + clog( VerbosityWarning, "main" ) << "Snapshot for 0 block is not found"; + } + } + } + } + statusAndControl->setSubsystemRunning( StatusAndControl::SnapshotDownloader, false ); statusAndControl->setExitState( StatusAndControl::StartAgain, true ); From cefd6af628c53477be57cdab5dda3ad442886387 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 23 Oct 2024 15:07:37 +0100 Subject: [PATCH 5/7] IS 968 fix snapshot dowloading for small snapshots --- libskale/SnapshotManager.cpp | 8 -------- skaled/main.cpp | 4 ++-- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/libskale/SnapshotManager.cpp b/libskale/SnapshotManager.cpp index c0c5f0c02..1101b52e7 100644 --- a/libskale/SnapshotManager.cpp +++ b/libskale/SnapshotManager.cpp @@ -165,10 +165,6 @@ void SnapshotManager::restoreSnapshot( unsigned _blockNumber ) { if ( _blockNumber > 0 ) volumes = allVolumes; #endif - // if ( chainParams.nodeInfo.archiveMode && _blockNumber == 0 ) - // volumes = coreVolumes; - // else - // volumes = allVolumes; int dummy_counter = 0; for ( const string& vol : volumes ) { @@ -769,10 +765,6 @@ void SnapshotManager::computeSnapshotHash( unsigned _blockNumber, bool is_checki if ( _blockNumber > 0 ) volumes = allVolumes; #endif - // if ( chainParams.nodeInfo.archiveMode && _blockNumber == 0 ) - // volumes = coreVolumes; - // else - // volumes = allVolumes; for ( const auto& volume : volumes ) { int res = btrfs.subvolume.property_set( diff --git a/skaled/main.cpp b/skaled/main.cpp index f9827c4c4..0fbe7a6af 100644 --- a/skaled/main.cpp +++ b/skaled/main.cpp @@ -1652,9 +1652,9 @@ int main( int argc, char** argv ) try { // sleep before send skale_getSnapshot again - will receive error clog( VerbosityInfo, "main" ) << std::string( "Will sleep for " ) - << chainParams.sChain.snapshotDownloadInactiveTimeout + << chainParams.sChain.snapshotDownloadInactiveTimeout + 10 << std::string( " seconds before downloading 0 snapshot" ); - sleep( chainParams.sChain.snapshotDownloadInactiveTimeout ); + sleep( chainParams.sChain.snapshotDownloadInactiveTimeout + 10 ); downloadAndProccessSnapshot( snapshotManager, chainParams, urlToDownloadSnapshotFrom, false ); From ab180dc8861739d0202bbc6a933e7cc711283789 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 23 Oct 2024 17:31:18 +0100 Subject: [PATCH 6/7] IS 968 rename timeout --- libweb3jsonrpc/Skale.cpp | 3 ++- libweb3jsonrpc/Skale.h | 6 ++++++ skaled/main.cpp | 6 ++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libweb3jsonrpc/Skale.cpp b/libweb3jsonrpc/Skale.cpp index c2c1d6f42..e8930dc1d 100644 --- a/libweb3jsonrpc/Skale.cpp +++ b/libweb3jsonrpc/Skale.cpp @@ -66,6 +66,7 @@ std::string exceptionToErrorMessage(); volatile bool Skale::g_bShutdownViaWeb3Enabled = false; volatile bool Skale::g_bNodeInstanceShouldShutdown = false; Skale::list_fn_on_shutdown_t Skale::g_list_fn_on_shutdown; +const uint64_t Skale::SNAPSHOT_DOWNLOAD_MONITOR_THREAD_SLEEP_MS = 10; Skale::Skale( Client& _client, std::shared_ptr< SharedSpace > _sharedSpace ) : m_client( _client ), m_shared_space( _sharedSpace ) {} @@ -211,7 +212,7 @@ nlohmann::json Skale::impl_skale_getSnapshot( const nlohmann::json& joRequest, C m_client.chainParams().nodeInfo.archiveMode ) ) { if ( threadExitRequested ) break; - sleep( 10 ); + sleep( SNAPSHOT_DOWNLOAD_MONITOR_THREAD_SLEEP_MS ); } clog( VerbosityInfo, "skale_downloadSnapshotFragmentMonitorThread" ) diff --git a/libweb3jsonrpc/Skale.h b/libweb3jsonrpc/Skale.h index 3a39db3e7..e76ee732a 100644 --- a/libweb3jsonrpc/Skale.h +++ b/libweb3jsonrpc/Skale.h @@ -84,6 +84,10 @@ class Skale : public dev::rpc::SkaleFace { typedef std::function< void() > fn_on_shutdown_t; static void onShutdownInvoke( fn_on_shutdown_t fn ); + static uint64_t snapshotDownloadFragmentMonitorThreadTimeout() { + return SNAPSHOT_DOWNLOAD_MONITOR_THREAD_SLEEP_MS; + } + public: nlohmann::json impl_skale_getSnapshot( const nlohmann::json& joRequest, dev::eth::Client& client ); @@ -99,6 +103,8 @@ class Skale : public dev::rpc::SkaleFace { typedef std::list< fn_on_shutdown_t > list_fn_on_shutdown_t; static list_fn_on_shutdown_t g_list_fn_on_shutdown; + static const uint64_t SNAPSHOT_DOWNLOAD_MONITOR_THREAD_SLEEP_MS; + dev::eth::Client& m_client; std::shared_ptr< SharedSpace > m_shared_space; int currentSnapshotBlockNumber = -1; diff --git a/skaled/main.cpp b/skaled/main.cpp index 0fbe7a6af..0be868555 100644 --- a/skaled/main.cpp +++ b/skaled/main.cpp @@ -1652,9 +1652,11 @@ int main( int argc, char** argv ) try { // sleep before send skale_getSnapshot again - will receive error clog( VerbosityInfo, "main" ) << std::string( "Will sleep for " ) - << chainParams.sChain.snapshotDownloadInactiveTimeout + 10 + << chainParams.sChain.snapshotDownloadInactiveTimeout + + dev::rpc::Skale::snapshotDownloadFragmentMonitorThreadTimeout() << std::string( " seconds before downloading 0 snapshot" ); - sleep( chainParams.sChain.snapshotDownloadInactiveTimeout + 10 ); + sleep( chainParams.sChain.snapshotDownloadInactiveTimeout + + dev::rpc::Skale::snapshotDownloadFragmentMonitorThreadTimeout() ); downloadAndProccessSnapshot( snapshotManager, chainParams, urlToDownloadSnapshotFrom, false ); From c358786d2855f2e24b764518cc746c0fd683fb66 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 24 Oct 2024 15:16:48 +0100 Subject: [PATCH 7/7] IS 1083 snapshotdownload flag --- skaled/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/skaled/main.cpp b/skaled/main.cpp index 86969b032..c7eb2389d 100644 --- a/skaled/main.cpp +++ b/skaled/main.cpp @@ -1592,6 +1592,7 @@ int main( int argc, char** argv ) try { std::string urlToDownloadSnapshotFrom = ""; if ( vm.count( "no-snapshot-majority" ) ) { + downloadSnapshotFlag = true; urlToDownloadSnapshotFrom = vm["no-snapshot-majority"].as< string >(); clog( VerbosityInfo, "main" ) << "Manually set url to download snapshot from: " << urlToDownloadSnapshotFrom;