Skip to content

Commit

Permalink
refactor query call sites to use query objects
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Apr 19, 2024
1 parent d700b91 commit c129c26
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 135 deletions.
28 changes: 20 additions & 8 deletions cmd/dev/snapshots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
#include <silkworm/db/snapshot_sync.hpp>
#include <silkworm/db/snapshots/bittorrent/client.hpp>
#include <silkworm/db/snapshots/body_index.hpp>
#include <silkworm/db/snapshots/body_queries.hpp>
#include <silkworm/db/snapshots/header_index.hpp>
#include <silkworm/db/snapshots/header_queries.hpp>
#include <silkworm/db/snapshots/index_builder.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/seg/seg_zip.hpp>
#include <silkworm/db/snapshots/snapshot.hpp>
#include <silkworm/db/snapshots/txn_index.hpp>
#include <silkworm/db/snapshots/txn_queries.hpp>
#include <silkworm/db/snapshots/txn_to_block_index.hpp>
#include <silkworm/infra/common/ensure.hpp>
#include <silkworm/infra/common/log.hpp>
Expand Down Expand Up @@ -416,7 +419,8 @@ void lookup_header_by_hash(const SnapSettings& settings) {
SnapshotRepository snapshot_repository{settings};
snapshot_repository.reopen_folder();
snapshot_repository.view_header_segments([&](const HeaderSnapshot& snapshot) -> bool {
const auto header{snapshot.header_by_hash(*hash)};
Index idx_header_hash{*snapshot.idx_header_hash()};
const auto header = HeaderFindByHashQuery{snapshot, idx_header_hash}.exec(*hash);
if (header) {
matching_header = header;
matching_snapshot = &snapshot;
Expand Down Expand Up @@ -445,7 +449,8 @@ void lookup_header_by_number(const SnapSettings& settings) {
snapshot_repository.reopen_folder();
const auto header_snapshot{snapshot_repository.find_header_segment(block_number)};
if (header_snapshot) {
const auto header{header_snapshot->header_by_number(block_number)};
Index idx_header_hash{*header_snapshot->idx_header_hash()};
const auto header = HeaderFindByBlockNumQuery{*header_snapshot, idx_header_hash}.exec(block_number);
ensure(header.has_value(),
[&]() { return "lookup_header_by_number: " + std::to_string(block_number) + " NOT found in " + header_snapshot->path().filename(); });
SILK_INFO << "Lookup header number: " << block_number << " found in: " << header_snapshot->path().filename();
Expand Down Expand Up @@ -485,7 +490,9 @@ void lookup_body_in_one(const SnapSettings& settings, BlockNum block_number, con
std::chrono::time_point start{std::chrono::steady_clock::now()};
const auto body_snapshot{snapshot_repository.get_body_segment(*snapshot_path)};
ensure(body_snapshot, [&]() { return "lookup_body: body segment not found for snapshot file: " + snapshot_path->path().string(); });
const auto body{body_snapshot->body_by_number(block_number)};

Index idx_body_number{*body_snapshot->idx_body_number()};
const auto body = BodyFindByBlockNumQuery{*body_snapshot, idx_body_number}.exec(block_number);
if (body) {
SILK_INFO << "Lookup body number: " << block_number << " found in: " << body_snapshot->path().filename();
if (settings.print) {
Expand All @@ -505,7 +512,8 @@ void lookup_body_in_all(const SnapSettings& settings, BlockNum block_number) {
std::chrono::time_point start{std::chrono::steady_clock::now()};
const auto body_snapshot{snapshot_repository.find_body_segment(block_number)};
if (body_snapshot) {
const auto body{body_snapshot->body_by_number(block_number)};
Index idx_body_number{*body_snapshot->idx_body_number()};
const auto body = BodyFindByBlockNumQuery{*body_snapshot, idx_body_number}.exec(block_number);
ensure(body.has_value(),
[&]() { return "lookup_body: " + std::to_string(block_number) + " NOT found in " + body_snapshot->path().filename(); });
SILK_INFO << "Lookup body number: " << block_number << " found in: " << body_snapshot->path().filename();
Expand Down Expand Up @@ -588,7 +596,8 @@ void lookup_txn_by_hash_in_one(const SnapSettings& settings, const Hash& hash, c
std::chrono::time_point start{std::chrono::steady_clock::now()};
const auto tx_snapshot{snapshot_repository.get_tx_segment(*snapshot_path)};
if (tx_snapshot) {
const auto transaction{tx_snapshot->txn_by_hash(hash)};
Index idx_txn_hash{*tx_snapshot->idx_txn_hash()};
const auto transaction = TransactionFindByHashQuery{*tx_snapshot, idx_txn_hash}.exec(hash);
if (transaction) {
SILK_INFO << "Lookup txn hash: " << hash.to_hex() << " found in: " << tx_snapshot->path().filename();
if (settings.print) {
Expand All @@ -609,7 +618,8 @@ void lookup_txn_by_hash_in_all(const SnapSettings& settings, const Hash& hash) {
const TransactionSnapshot* matching_snapshot{nullptr};
std::chrono::time_point start{std::chrono::steady_clock::now()};
snapshot_repository.view_tx_segments([&](const TransactionSnapshot& snapshot) -> bool {
const auto transaction{snapshot.txn_by_hash(hash)};
Index idx_txn_hash{*snapshot.idx_txn_hash()};
const auto transaction = TransactionFindByHashQuery{snapshot, idx_txn_hash}.exec(hash);
if (transaction) {
matching_snapshot = &snapshot;
if (settings.print) {
Expand Down Expand Up @@ -648,7 +658,8 @@ void lookup_txn_by_id_in_one(const SnapSettings& settings, uint64_t txn_id, cons
std::chrono::time_point start{std::chrono::steady_clock::now()};
const auto tx_snapshot{snapshot_repository.get_tx_segment(*snapshot_path)};
if (tx_snapshot) {
const auto transaction{tx_snapshot->txn_by_id(txn_id)};
Index idx_txn_hash{*tx_snapshot->idx_txn_hash()};
const auto transaction = TransactionFindByIdQuery{*tx_snapshot, idx_txn_hash}.exec(txn_id);
if (transaction) {
SILK_INFO << "Lookup txn ID: " << txn_id << " found in: " << tx_snapshot->path().filename();
if (settings.print) {
Expand All @@ -669,7 +680,8 @@ void lookup_txn_by_id_in_all(const SnapSettings& settings, uint64_t txn_id) {
const TransactionSnapshot* matching_snapshot{nullptr};
std::chrono::time_point start{std::chrono::steady_clock::now()};
snapshot_repository.view_tx_segments([&](const TransactionSnapshot& snapshot) -> bool {
const auto transaction{snapshot.txn_by_id(txn_id)};
Index idx_txn_hash{*snapshot.idx_txn_hash()};
const auto transaction = TransactionFindByIdQuery{snapshot, idx_txn_hash}.exec(txn_id);
if (transaction) {
matching_snapshot = &snapshot;
if (settings.print) {
Expand Down
26 changes: 19 additions & 7 deletions silkworm/db/access_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@
#include <silkworm/core/types/evmc_bytes32.hpp>
#include <silkworm/db/mdbx/bitmap.hpp>
#include <silkworm/db/receipt_cbor.hpp>
#include <silkworm/db/snapshots/body_queries.hpp>
#include <silkworm/db/snapshots/header_queries.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/txn_queries.hpp>
#include <silkworm/db/tables.hpp>
#include <silkworm/infra/common/decoding_exception.hpp>
#include <silkworm/infra/common/ensure.hpp>

namespace silkworm::db {

using namespace snapshots;

std::optional<VersionBase> read_schema_version(ROTxn& txn) {
auto cursor = txn.ro_cursor(db::table::kDatabaseInfo);
if (!cursor->seek(mdbx::slice{kDbSchemaVersionKey})) {
Expand Down Expand Up @@ -1220,7 +1225,8 @@ std::optional<BlockHeader> DataModel::read_header_from_snapshot(BlockNum height)
// We know the header snapshot in advance: find it based on target block number
const auto header_snapshot = repository_->find_header_segment(height);
if (header_snapshot) {
block_header = header_snapshot->header_by_number(height);
Index index{*header_snapshot->idx_header_hash()};
block_header = HeaderFindByBlockNumQuery{*header_snapshot, index}.exec(height);
}
return block_header;
}
Expand All @@ -1233,7 +1239,8 @@ std::optional<BlockHeader> DataModel::read_header_from_snapshot(const Hash& hash
std::optional<BlockHeader> block_header;
// We don't know the header snapshot in advance: search for block hash in each header snapshot in reverse order
repository_->view_header_segments([&](const snapshots::HeaderSnapshot& snapshot) -> bool {
block_header = snapshot.header_by_hash(hash);
Index index{*snapshot.idx_header_hash()};
block_header = HeaderFindByHashQuery{snapshot, index}.exec(hash);
return block_header.has_value();
});
return block_header;
Expand All @@ -1248,7 +1255,8 @@ bool DataModel::read_body_from_snapshot(BlockNum height, bool read_senders, Bloc
const auto body_snapshot = repository_->find_body_segment(height);
if (!body_snapshot) return false;

auto stored_body = body_snapshot->body_by_number(height);
Index idx_body_number{*body_snapshot->idx_body_number()};
auto stored_body = BodyFindByBlockNumQuery{*body_snapshot, idx_body_number}.exec(height);
if (!stored_body) return false;

// Skip first and last *system transactions* in block body
Expand All @@ -1273,7 +1281,8 @@ bool DataModel::is_body_in_snapshot(BlockNum height) {
// We know the body snapshot in advance: find it based on target block number
const auto body_snapshot = repository_->find_body_segment(height);
if (body_snapshot) {
const auto stored_body = body_snapshot->body_by_number(height);
Index idx_body_number{*body_snapshot->idx_body_number()};
const auto stored_body = BodyFindByBlockNumQuery{*body_snapshot, idx_body_number}.exec(height);
return stored_body.has_value();
}

Expand All @@ -1290,15 +1299,17 @@ bool DataModel::read_transactions_from_snapshot(BlockNum height, uint64_t base_t
const auto tx_snapshot = repository_->find_tx_segment(height);
if (!tx_snapshot) return false;

txs = tx_snapshot->txn_range(base_txn_id, txn_count);
Index idx_txn_hash{*tx_snapshot->idx_txn_hash()};
txs = TransactionRangeFromIdQuery{*tx_snapshot, idx_txn_hash}.exec_into_vector(base_txn_id, txn_count);

return true;
}

bool DataModel::read_rlp_transactions_from_snapshot(BlockNum height, std::vector<Bytes>& rlp_txs) {
const auto body_snapshot = repository_->find_body_segment(height);
if (body_snapshot) {
auto stored_body = body_snapshot->body_by_number(height);
Index idx_body_number{*body_snapshot->idx_body_number()};
auto stored_body = BodyFindByBlockNumQuery{*body_snapshot, idx_body_number}.exec(height);
if (!stored_body) return false;

// Skip first and last *system transactions* in block body
Expand All @@ -1310,7 +1321,8 @@ bool DataModel::read_rlp_transactions_from_snapshot(BlockNum height, std::vector
const auto tx_snapshot = repository_->find_tx_segment(height);
if (!tx_snapshot) return false;

rlp_txs = tx_snapshot->txn_rlp_range(base_txn_id, txn_count);
Index idx_txn_hash{*tx_snapshot->idx_txn_hash()};
rlp_txs = TransactionPayloadRlpRangeFromIdQuery{*tx_snapshot, idx_txn_hash}.exec_into_vector(base_txn_id, txn_count);

return true;
}
Expand Down
6 changes: 6 additions & 0 deletions silkworm/db/snapshots/body_queries.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ namespace silkworm::snapshots {

struct BodyFindByBlockNumQuery : public FindByIdQuery<BodySnapshotReader> {
using FindByIdQuery<BodySnapshotReader>::FindByIdQuery;

std::optional<BlockBodyForStorage> exec(BlockNum id) {
// TODO: move this check inside ordinal_lookup_by_data_id if possible and remove this method
if (id < reader_.block_from()) return std::nullopt;
return FindByIdQuery<BodySnapshotReader>::exec(id);
}
};

} // namespace silkworm::snapshots
6 changes: 6 additions & 0 deletions silkworm/db/snapshots/header_queries.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ namespace silkworm::snapshots {

struct HeaderFindByBlockNumQuery : public FindByIdQuery<HeaderSnapshotReader> {
using FindByIdQuery<HeaderSnapshotReader>::FindByIdQuery;

std::optional<BlockHeader> exec(BlockNum id) {
// TODO: move this check inside ordinal_lookup_by_data_id if possible and remove this method
if ((id < reader_.block_from()) || (id >= reader_.block_to())) return std::nullopt;
return FindByIdQuery<HeaderSnapshotReader>::exec(id);
}
};

struct HeaderFindByHashQuery : public FindByHashQuery<HeaderSnapshotReader> {
Expand Down
6 changes: 5 additions & 1 deletion silkworm/db/snapshots/repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <silkworm/db/snapshots/header_snapshot.hpp>
#include <silkworm/db/snapshots/index_builder.hpp>
#include <silkworm/db/snapshots/txn_index.hpp>
#include <silkworm/db/snapshots/txn_queries.hpp>
#include <silkworm/db/snapshots/txn_to_block_index.hpp>
#include <silkworm/infra/common/ensure.hpp>
#include <silkworm/infra/common/log.hpp>
Expand Down Expand Up @@ -201,7 +202,10 @@ const TransactionSnapshot* SnapshotRepository::find_tx_segment(BlockNum number)
std::optional<BlockNum> SnapshotRepository::find_block_number(Hash txn_hash) const {
for (const auto& it : std::ranges::reverse_view(tx_segments_)) {
const auto& snapshot = it.second;
auto block = snapshot->block_num_by_txn_hash(txn_hash);

Index idx_txn_hash{*snapshot->idx_txn_hash()};
Index idx_txn_hash_2_block{*snapshot->idx_txn_hash_2_block()};
auto block = TransactionBlockNumByTxnHashQuery{idx_txn_hash_2_block, TransactionFindByHashQuery{*snapshot, idx_txn_hash}}.exec(txn_hash);
if (block) {
return block;
}
Expand Down
74 changes: 0 additions & 74 deletions silkworm/db/snapshots/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@
#include <silkworm/infra/common/ensure.hpp>
#include <silkworm/infra/common/log.hpp>

#include "body_queries.hpp"
#include "body_snapshot.hpp"
#include "header_queries.hpp"
#include "header_snapshot.hpp"
#include "txn_queries.hpp"
#include "txn_snapshot.hpp"
#include "txn_snapshot_word_serializer.hpp"

namespace silkworm::snapshots {

HeaderSnapshot::HeaderSnapshot(SnapshotPath path) : Snapshot(std::move(path)) {}
Expand All @@ -43,23 +35,6 @@ HeaderSnapshot::~HeaderSnapshot() {
close();
}

std::optional<BlockHeader> HeaderSnapshot::header_by_hash(const Hash& block_hash) const {
if (!idx_header_hash_) {
return {};
}

return HeaderFindByHashQuery{*this, Index{*idx_header_hash_}}.exec(block_hash);
}

std::optional<BlockHeader> HeaderSnapshot::header_by_number(BlockNum block_height) const {
// TODO: move block_height checks inside ordinal_lookup_by_data_id or FindByIdQuery
if (!idx_header_hash_ || block_height < path_.block_from() || block_height >= path_.block_to()) {
return {};
}

return HeaderFindByBlockNumQuery{*this, Index{*idx_header_hash_}}.exec(block_height);
}

void HeaderSnapshot::reopen_index() {
ensure(decoder_.is_open(), "HeaderSnapshot: segment not open, call reopen_segment");

Expand Down Expand Up @@ -91,15 +66,6 @@ BodySnapshot::~BodySnapshot() {
close();
}

std::optional<StoredBlockBody> BodySnapshot::body_by_number(BlockNum block_height) const {
// TODO: move block_height check inside ordinal_lookup_by_data_id
if (!idx_body_number_ || block_height < idx_body_number_->base_data_id()) {
return {};
}

return BodyFindByBlockNumQuery{*this, Index{*idx_body_number_}}.exec(block_height);
}

void BodySnapshot::reopen_index() {
ensure(decoder_.is_open(), "BodySnapshot: segment not open, call reopen_segment");

Expand Down Expand Up @@ -132,46 +98,6 @@ TransactionSnapshot::~TransactionSnapshot() {
close();
}

std::optional<Transaction> TransactionSnapshot::txn_by_hash(const Hash& txn_hash) const {
if (!idx_txn_hash_) {
return {};
}

return TransactionFindByHashQuery{*this, Index{*idx_txn_hash_}}.exec(txn_hash);
}

std::optional<Transaction> TransactionSnapshot::txn_by_id(uint64_t txn_id) const {
if (!idx_txn_hash_) {
return {};
}

return TransactionFindByIdQuery{*this, Index{*idx_txn_hash_}}.exec(txn_id);
}

std::optional<BlockNum> TransactionSnapshot::block_num_by_txn_hash(const Hash& txn_hash) const {
if (!idx_txn_hash_2_block_) {
return {};
}

Index idx_txn_hash{*idx_txn_hash_};
TransactionFindByHashQuery txn_by_hash_query{*this, idx_txn_hash};
return TransactionBlockNumByTxnHashQuery{Index{*idx_txn_hash_2_block_}, txn_by_hash_query}.exec(txn_hash);
}

std::vector<Transaction> TransactionSnapshot::txn_range(uint64_t first_txn_id, uint64_t count) const {
if (!idx_txn_hash_) {
return {};
}
return TransactionRangeFromIdQuery{*this, Index{*idx_txn_hash_}}.exec_into_vector(first_txn_id, count);
}

std::vector<Bytes> TransactionSnapshot::txn_rlp_range(uint64_t first_txn_id, uint64_t count) const {
if (!idx_txn_hash_) {
return {};
}
return TransactionPayloadRlpRangeFromIdQuery{*this, Index{*idx_txn_hash_}}.exec_into_vector(first_txn_id, count);
}

void TransactionSnapshot::reopen_index() {
ensure(decoder_.is_open(), "TransactionSnapshot: segment not open, call reopen_segment");

Expand Down
12 changes: 0 additions & 12 deletions silkworm/db/snapshots/snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class HeaderSnapshot : public Snapshot {

[[nodiscard]] const rec_split::RecSplitIndex* idx_header_hash() const { return idx_header_hash_.get(); }

[[nodiscard]] std::optional<BlockHeader> header_by_hash(const Hash& block_hash) const;
[[nodiscard]] std::optional<BlockHeader> header_by_number(BlockNum block_height) const;

void reopen_index() override;

protected:
Expand All @@ -83,8 +80,6 @@ class BodySnapshot : public Snapshot {

[[nodiscard]] const rec_split::RecSplitIndex* idx_body_number() const { return idx_body_number_.get(); }

[[nodiscard]] std::optional<StoredBlockBody> body_by_number(BlockNum block_height) const;

void reopen_index() override;

protected:
Expand All @@ -107,13 +102,6 @@ class TransactionSnapshot : public Snapshot {
[[nodiscard]] const rec_split::RecSplitIndex* idx_txn_hash() const { return idx_txn_hash_.get(); }
[[nodiscard]] const rec_split::RecSplitIndex* idx_txn_hash_2_block() const { return idx_txn_hash_2_block_.get(); }

[[nodiscard]] std::optional<Transaction> txn_by_hash(const Hash& txn_hash) const;
[[nodiscard]] std::optional<Transaction> txn_by_id(uint64_t txn_id) const;
[[nodiscard]] std::vector<Transaction> txn_range(uint64_t first_txn_id, uint64_t count) const;
[[nodiscard]] std::vector<Bytes> txn_rlp_range(uint64_t first_txn_id, uint64_t count) const;

[[nodiscard]] std::optional<BlockNum> block_num_by_txn_hash(const Hash& txn_hash) const;

void reopen_index() override;

protected:
Expand Down
3 changes: 3 additions & 0 deletions silkworm/db/snapshots/snapshot_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class SnapshotReader {
return iterator_read_into_vector(std::move(it), count);
}

[[nodiscard]] BlockNum block_from() const { return snapshot_.block_from(); }
[[nodiscard]] BlockNum block_to() const { return snapshot_.block_to(); }

private:
const Snapshot& snapshot_;
};
Expand Down
Loading

0 comments on commit c129c26

Please sign in to comment.