diff --git a/silkworm/capi/silkworm.cpp b/silkworm/capi/silkworm.cpp index e041bb4a0d..633d4f97e2 100644 --- a/silkworm/capi/silkworm.cpp +++ b/silkworm/capi/silkworm.cpp @@ -368,12 +368,10 @@ SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle handle, SilkwormChainSn transactions_snapshot->reopen_index(); snapshots::SnapshotBundle bundle{ - .headers_snapshot_path = *headers_segment_path, .headers_snapshot = std::move(headers_snapshot), - .bodies_snapshot_path = *bodies_segment_path, .bodies_snapshot = std::move(bodies_snapshot), - .tx_snapshot_path = *transactions_segment_path, - .tx_snapshot = std::move(transactions_snapshot)}; + .tx_snapshot = std::move(transactions_snapshot), + }; handle->snapshot_repository->add_snapshot_bundle(std::move(bundle)); return SILKWORM_OK; } diff --git a/silkworm/db/snapshots/repository.cpp b/silkworm/db/snapshots/repository.cpp index 57950477c7..80cba6a0e5 100644 --- a/silkworm/db/snapshots/repository.cpp +++ b/silkworm/db/snapshots/repository.cpp @@ -78,13 +78,23 @@ SnapshotRepository::~SnapshotRepository() { close(); } -void SnapshotRepository::add_snapshot_bundle(SnapshotBundle&& bundle) { - header_segments_[bundle.headers_snapshot_path.path()] = std::move(bundle.headers_snapshot); - body_segments_[bundle.bodies_snapshot_path.path()] = std::move(bundle.bodies_snapshot); - tx_segments_[bundle.tx_snapshot_path.path()] = std::move(bundle.tx_snapshot); - if (bundle.tx_snapshot_path.block_to() > segment_max_block_) { - segment_max_block_ = bundle.tx_snapshot_path.block_to() - 1; +void SnapshotRepository::add_snapshot_bundle(SnapshotBundle bundle) { + if (bundle.headers_snapshot && bundle.bodies_snapshot && bundle.tx_snapshot) { + // assume that all snapshot types end with the same block, and use one of them + BlockNum last_block = bundle.tx_snapshot->block_to() - 1; + segment_max_block_ = std::max(segment_max_block_, last_block); } + + if (bundle.headers_snapshot) { + header_segments_[bundle.headers_snapshot->fs_path()] = std::move(bundle.headers_snapshot); + } + if (bundle.bodies_snapshot) { + body_segments_[bundle.bodies_snapshot->fs_path()] = std::move(bundle.bodies_snapshot); + } + if (bundle.tx_snapshot) { + tx_segments_[bundle.tx_snapshot->fs_path()] = std::move(bundle.tx_snapshot); + } + idx_max_block_ = max_idx_available(); } @@ -297,9 +307,8 @@ void SnapshotRepository::reopen_list(const SnapshotPathList& segment_files, bool } ensure(snapshot_valid, [&]() { return "invalid empty snapshot " + seg_file.filename(); }); - if (seg_file.block_to() > segment_max_block) { - segment_max_block = seg_file.block_to() - 1; - } + BlockNum last_block = seg_file.block_to() - 1; + segment_max_block = std::max(segment_max_block, last_block); } catch (const std::exception& exc) { SILK_WARN << "Reopen failed for: " << seg_file.path() << " [" << exc.what() << "]"; if (!optimistic) throw; diff --git a/silkworm/db/snapshots/repository.hpp b/silkworm/db/snapshots/repository.hpp index 063a96b560..041af33c74 100644 --- a/silkworm/db/snapshots/repository.hpp +++ b/silkworm/db/snapshots/repository.hpp @@ -46,11 +46,8 @@ using BodySnapshotWalker = SnapshotWalker; using TransactionSnapshotWalker = SnapshotWalker; struct SnapshotBundle { - SnapshotPath headers_snapshot_path; std::unique_ptr headers_snapshot; - SnapshotPath bodies_snapshot_path; std::unique_ptr bodies_snapshot; - SnapshotPath tx_snapshot_path; std::unique_ptr tx_snapshot; }; @@ -74,7 +71,7 @@ class SnapshotRepository { return get_files(kSegmentExtension); } - void add_snapshot_bundle(SnapshotBundle&& bundle); + void add_snapshot_bundle(SnapshotBundle bundle); void reopen_list(const SnapshotPathList& segment_files, bool optimistic = false); void reopen_file(const SnapshotPath& segment_path, bool optimistic = false);