diff --git a/cmd/capi/execute.cpp b/cmd/capi/execute.cpp index 1728408a1f..db6ee2ce69 100644 --- a/cmd/capi/execute.cpp +++ b/cmd/capi/execute.cpp @@ -205,9 +205,9 @@ std::vector collect_all_snapshots(SnapshotRepository& sna return true; }); - ensure(headers_snapshot_sequence.size() == snapshot_repository.header_snapshots_count(), "invalid header snapshot count"); - ensure(bodies_snapshot_sequence.size() == snapshot_repository.body_snapshots_count(), "invalid body snapshot count"); - ensure(transactions_snapshot_sequence.size() == snapshot_repository.tx_snapshots_count(), "invalid tx snapshot count"); + ensure(headers_snapshot_sequence.size() == snapshot_repository.bundles_count(), "invalid header snapshot count"); + ensure(bodies_snapshot_sequence.size() == snapshot_repository.bundles_count(), "invalid body snapshot count"); + ensure(transactions_snapshot_sequence.size() == snapshot_repository.bundles_count(), "invalid tx snapshot count"); std::vector snapshot_sequence; snapshot_sequence.reserve(headers_snapshot_sequence.size()); diff --git a/silkworm/db/snapshot_sync.cpp b/silkworm/db/snapshot_sync.cpp index 5f82fa2cd4..69502426c2 100644 --- a/silkworm/db/snapshot_sync.cpp +++ b/silkworm/db/snapshot_sync.cpp @@ -73,9 +73,7 @@ bool SnapshotSync::download_and_index_snapshots(db::RWTxn& txn) { repository_->reopen_folder(); const auto max_block_available = repository_->max_block_available(); - SILK_INFO << "SnapshotSync: max block available: " << max_block_available - << " (segment max block: " << repository_->segment_max_block() - << ", idx max block: " << repository_->idx_max_block() << ")"; + SILK_INFO << "SnapshotSync: max block available: " << max_block_available; const auto snapshot_config = Config::lookup_known_config(config_.chain_id, snapshot_file_names); const auto configured_max_block_number = snapshot_config.max_block_number(); diff --git a/silkworm/db/snapshots/repository.cpp b/silkworm/db/snapshots/repository.cpp index 52c63786bc..f174c063d5 100644 --- a/silkworm/db/snapshots/repository.cpp +++ b/silkworm/db/snapshots/repository.cpp @@ -58,15 +58,8 @@ SnapshotRepository::~SnapshotRepository() { } void SnapshotRepository::add_snapshot_bundle(SnapshotBundle bundle) { - BlockNum block_from = bundle.block_from(); - BlockNum block_to = bundle.block_to(); - bundle.reopen(); - - bundles_.emplace(block_from, std::move(bundle)); - - segment_max_block_ = std::max(segment_max_block_, block_to - 1); - idx_max_block_ = max_idx_available(); + bundles_.emplace(bundle.block_from(), std::move(bundle)); } void SnapshotBundle::reopen() { @@ -98,6 +91,15 @@ void SnapshotRepository::close() { } } +BlockNum SnapshotRepository::max_block_available() const { + if (bundles_.empty()) + return 0; + + // a bundle with the max block range is last in the sorted bundles map + auto& bundle = bundles_.rbegin()->second; + return (bundle.block_from() < bundle.block_to()) ? bundle.block_to() - 1 : bundle.block_from(); +} + std::vector SnapshotRepository::missing_block_ranges() const { const auto ordered_segments = get_segment_files(); @@ -292,7 +294,6 @@ void SnapshotRepository::reopen_folder() { } auto& bundle = bundles_.at(num); - segment_max_block_ = std::max(segment_max_block_, bundle.block_to() - 1); if (num < bundle.block_to()) { num = bundle.block_to(); @@ -301,8 +302,9 @@ void SnapshotRepository::reopen_folder() { } } - idx_max_block_ = max_idx_available(); - SILK_INFO << "Total reopened snapshots: " << total_snapshots_count(); + SILK_INFO << "Total reopened bundles: " << bundles_count() + << " snapshots: " << total_snapshots_count() + << " indexes: " << total_indexes_count(); } const SnapshotBundle* SnapshotRepository::find_bundle(BlockNum number) const { @@ -345,20 +347,6 @@ SnapshotPathList SnapshotRepository::get_files(const std::string& ext) const { return snapshot_files; } -BlockNum SnapshotRepository::max_idx_available() { - BlockNum result = 0; - for (auto& entry : bundles_) { - auto& bundle = entry.second; - for (auto& index_ref : bundle.indexes()) { - if (!index_ref.get().is_open()) { - return result; - } - } - result = bundle.block_to() - 1; - } - return result; -} - bool is_stale_index_path(const SnapshotPath& index_path) { SnapshotType snapshot_type = (index_path.type() == SnapshotType::transactions_to_block) ? SnapshotType::transactions diff --git a/silkworm/db/snapshots/repository.hpp b/silkworm/db/snapshots/repository.hpp index 2161dd1459..6a025cd93e 100644 --- a/silkworm/db/snapshots/repository.hpp +++ b/silkworm/db/snapshots/repository.hpp @@ -127,18 +127,15 @@ class SnapshotRepository { void add_snapshot_bundle(SnapshotBundle bundle); - [[nodiscard]] std::size_t header_snapshots_count() const { return bundles_.size(); } - [[nodiscard]] std::size_t body_snapshots_count() const { return bundles_.size(); } - [[nodiscard]] std::size_t tx_snapshots_count() const { return bundles_.size(); } - [[nodiscard]] std::size_t total_snapshots_count() const { - return header_snapshots_count() + body_snapshots_count() + tx_snapshots_count(); - } + [[nodiscard]] std::size_t bundles_count() const { return bundles_.size(); } + [[nodiscard]] std::size_t total_snapshots_count() const { return bundles_count() * SnapshotBundle::kSnapshotsCount; } + [[nodiscard]] std::size_t total_indexes_count() const { return bundles_count() * SnapshotBundle::kIndexesCount; } - [[nodiscard]] BlockNum segment_max_block() const { return segment_max_block_; } - [[nodiscard]] BlockNum idx_max_block() const { return idx_max_block_; } - [[nodiscard]] BlockNum max_block_available() const { return std::min(segment_max_block_, idx_max_block_); } + //! All types of .seg and .idx files are available up to this block number + [[nodiscard]] BlockNum max_block_available() const; [[nodiscard]] std::vector missing_block_ranges() const; + [[nodiscard]] std::vector> missing_indexes() const; void remove_stale_indexes() const; @@ -183,18 +180,11 @@ class SnapshotRepository { [[nodiscard]] SnapshotPathList get_files(const std::string& ext) const; - [[nodiscard]] BlockNum max_idx_available(); SnapshotPathList stale_index_paths() const; //! The configuration settings for snapshots SnapshotSettings settings_; - //! All types of .seg files are available - up to this block number - BlockNum segment_max_block_{0}; - - //! All types of .idx files are available - up to this block number - BlockNum idx_max_block_{0}; - //! Full snapshot bundles ordered by block_from std::map bundles_; }; diff --git a/silkworm/db/snapshots/repository_test.cpp b/silkworm/db/snapshots/repository_test.cpp index 03c281e704..32a3f7db26 100644 --- a/silkworm/db/snapshots/repository_test.cpp +++ b/silkworm/db/snapshots/repository_test.cpp @@ -51,9 +51,7 @@ TEST_CASE("SnapshotRepository::reopen_folder.partial_bundle", "[silkworm][node][ SnapshotSettings settings{tmp_dir.path()}; SnapshotRepository repository{settings}; repository.reopen_folder(); - CHECK(repository.header_snapshots_count() == 0); - CHECK(repository.body_snapshots_count() == 0); - CHECK(repository.tx_snapshots_count() == 0); + CHECK(repository.bundles_count() == 0); CHECK(repository.max_block_available() == 0); } @@ -203,7 +201,7 @@ TEST_CASE("SnapshotRepository::find_segment", "[silkworm][node][snapshot]") { CHECK_FALSE(repository.find_tx_segment(1'500'014)); } SECTION("greater than max_block_available") { - CHECK_FALSE(repository.find_body_segment(repository.max_block_available() + 10)); + CHECK_FALSE(repository.find_body_segment(repository.max_block_available() + 1)); } }