Skip to content

Commit

Permalink
simplify count/max computations
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Apr 24, 2024
1 parent 1f01c89 commit cf43085
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 51 deletions.
6 changes: 3 additions & 3 deletions cmd/capi/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ std::vector<SilkwormChainSnapshot> 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<SilkwormChainSnapshot> snapshot_sequence;
snapshot_sequence.reserve(headers_snapshot_sequence.size());
Expand Down
4 changes: 1 addition & 3 deletions silkworm/db/snapshot_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
37 changes: 12 additions & 25 deletions silkworm/db/snapshots/repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -98,6 +91,14 @@ void SnapshotRepository::close() {
}
}

BlockNum SnapshotRepository::max_block_available() const {
if (bundles_.empty())
return 0;

auto& first = bundles_.begin()->second;
return (first.block_from() < first.block_to()) ? first.block_to() - 1 : first.block_from();
}

std::vector<BlockNumRange> SnapshotRepository::missing_block_ranges() const {
const auto ordered_segments = get_segment_files();

Expand Down Expand Up @@ -292,7 +293,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();
Expand All @@ -301,8 +301,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 {
Expand Down Expand Up @@ -345,20 +346,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
Expand Down
22 changes: 6 additions & 16 deletions silkworm/db/snapshots/repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockNumRange> missing_block_ranges() const;

[[nodiscard]] std::vector<std::shared_ptr<IndexBuilder>> missing_indexes() const;
void remove_stale_indexes() const;

Expand Down Expand Up @@ -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<BlockNum, SnapshotBundle> bundles_;
};
Expand Down
6 changes: 2 additions & 4 deletions silkworm/db/snapshots/repository_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit cf43085

Please sign in to comment.