From c129c26bd91517eb8c9ee19e6f2bfa35ee5aa61d Mon Sep 17 00:00:00 2001 From: battlmonstr Date: Thu, 18 Apr 2024 16:55:51 +0200 Subject: [PATCH] refactor query call sites to use query objects --- cmd/dev/snapshots.cpp | 28 +++++-- silkworm/db/access_layer.cpp | 26 +++++-- silkworm/db/snapshots/body_queries.hpp | 6 ++ silkworm/db/snapshots/header_queries.hpp | 6 ++ silkworm/db/snapshots/repository.cpp | 6 +- silkworm/db/snapshots/snapshot.cpp | 74 ------------------ silkworm/db/snapshots/snapshot.hpp | 12 --- silkworm/db/snapshots/snapshot_reader.hpp | 3 + silkworm/db/snapshots/snapshot_test.cpp | 91 +++++++++++++++-------- 9 files changed, 117 insertions(+), 135 deletions(-) diff --git a/cmd/dev/snapshots.cpp b/cmd/dev/snapshots.cpp index c65f4807bb..9b05d1b80e 100644 --- a/cmd/dev/snapshots.cpp +++ b/cmd/dev/snapshots.cpp @@ -34,12 +34,15 @@ #include #include #include +#include #include +#include #include #include #include #include #include +#include #include #include #include @@ -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; @@ -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(); @@ -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) { @@ -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(); @@ -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) { @@ -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) { @@ -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) { @@ -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) { diff --git a/silkworm/db/access_layer.cpp b/silkworm/db/access_layer.cpp index a215678532..37c51b003b 100644 --- a/silkworm/db/access_layer.cpp +++ b/silkworm/db/access_layer.cpp @@ -26,13 +26,18 @@ #include #include #include +#include +#include #include +#include #include #include #include namespace silkworm::db { +using namespace snapshots; + std::optional read_schema_version(ROTxn& txn) { auto cursor = txn.ro_cursor(db::table::kDatabaseInfo); if (!cursor->seek(mdbx::slice{kDbSchemaVersionKey})) { @@ -1220,7 +1225,8 @@ std::optional 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; } @@ -1233,7 +1239,8 @@ std::optional DataModel::read_header_from_snapshot(const Hash& hash std::optional 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; @@ -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 @@ -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(); } @@ -1290,7 +1299,8 @@ 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; } @@ -1298,7 +1308,8 @@ bool DataModel::read_transactions_from_snapshot(BlockNum height, uint64_t base_t bool DataModel::read_rlp_transactions_from_snapshot(BlockNum height, std::vector& 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 @@ -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; } diff --git a/silkworm/db/snapshots/body_queries.hpp b/silkworm/db/snapshots/body_queries.hpp index 95cc41cacc..23af935a8e 100644 --- a/silkworm/db/snapshots/body_queries.hpp +++ b/silkworm/db/snapshots/body_queries.hpp @@ -23,6 +23,12 @@ namespace silkworm::snapshots { struct BodyFindByBlockNumQuery : public FindByIdQuery { using FindByIdQuery::FindByIdQuery; + + std::optional 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::exec(id); + } }; } // namespace silkworm::snapshots diff --git a/silkworm/db/snapshots/header_queries.hpp b/silkworm/db/snapshots/header_queries.hpp index 6bf311139a..b4a38b35fb 100644 --- a/silkworm/db/snapshots/header_queries.hpp +++ b/silkworm/db/snapshots/header_queries.hpp @@ -23,6 +23,12 @@ namespace silkworm::snapshots { struct HeaderFindByBlockNumQuery : public FindByIdQuery { using FindByIdQuery::FindByIdQuery; + + std::optional 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::exec(id); + } }; struct HeaderFindByHashQuery : public FindByHashQuery { diff --git a/silkworm/db/snapshots/repository.cpp b/silkworm/db/snapshots/repository.cpp index 247b581665..57950477c7 100644 --- a/silkworm/db/snapshots/repository.cpp +++ b/silkworm/db/snapshots/repository.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -201,7 +202,10 @@ const TransactionSnapshot* SnapshotRepository::find_tx_segment(BlockNum number) std::optional 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; } diff --git a/silkworm/db/snapshots/snapshot.cpp b/silkworm/db/snapshots/snapshot.cpp index 57d38ff5c5..6c45ce5cc7 100644 --- a/silkworm/db/snapshots/snapshot.cpp +++ b/silkworm/db/snapshots/snapshot.cpp @@ -24,14 +24,6 @@ #include #include -#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)) {} @@ -43,23 +35,6 @@ HeaderSnapshot::~HeaderSnapshot() { close(); } -std::optional 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 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"); @@ -91,15 +66,6 @@ BodySnapshot::~BodySnapshot() { close(); } -std::optional 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"); @@ -132,46 +98,6 @@ TransactionSnapshot::~TransactionSnapshot() { close(); } -std::optional 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 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 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 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 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"); diff --git a/silkworm/db/snapshots/snapshot.hpp b/silkworm/db/snapshots/snapshot.hpp index 18f1411dfd..59c2717f99 100644 --- a/silkworm/db/snapshots/snapshot.hpp +++ b/silkworm/db/snapshots/snapshot.hpp @@ -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 header_by_hash(const Hash& block_hash) const; - [[nodiscard]] std::optional header_by_number(BlockNum block_height) const; - void reopen_index() override; protected: @@ -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 body_by_number(BlockNum block_height) const; - void reopen_index() override; protected: @@ -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 txn_by_hash(const Hash& txn_hash) const; - [[nodiscard]] std::optional txn_by_id(uint64_t txn_id) const; - [[nodiscard]] std::vector txn_range(uint64_t first_txn_id, uint64_t count) const; - [[nodiscard]] std::vector txn_rlp_range(uint64_t first_txn_id, uint64_t count) const; - - [[nodiscard]] std::optional block_num_by_txn_hash(const Hash& txn_hash) const; - void reopen_index() override; protected: diff --git a/silkworm/db/snapshots/snapshot_reader.hpp b/silkworm/db/snapshots/snapshot_reader.hpp index b97b1ea219..60354aff26 100644 --- a/silkworm/db/snapshots/snapshot_reader.hpp +++ b/silkworm/db/snapshots/snapshot_reader.hpp @@ -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_; }; diff --git a/silkworm/db/snapshots/snapshot_test.cpp b/silkworm/db/snapshots/snapshot_test.cpp index 7d202ecd39..79bfcc79db 100644 --- a/silkworm/db/snapshots/snapshot_test.cpp +++ b/silkworm/db/snapshots/snapshot_test.cpp @@ -22,10 +22,13 @@ #include #include +#include #include +#include #include #include #include +#include #include #include #include @@ -144,9 +147,12 @@ TEST_CASE("HeaderSnapshot::header_by_number OK", "[silkworm][node][snapshot][ind header_snapshot.reopen_segment(); header_snapshot.reopen_index(); - CHECK(!header_snapshot.header_by_number(1'500'011)); - CHECK(header_snapshot.header_by_number(1'500'012)); - const auto header = header_snapshot.header_by_number(1'500'013); + Index idx_header_hash{*header_snapshot.idx_header_hash()}; + HeaderFindByBlockNumQuery header_by_number{header_snapshot, idx_header_hash}; + + CHECK(!header_by_number.exec(1'500'011)); + CHECK(header_by_number.exec(1'500'012)); + const auto header = header_by_number.exec(1'500'013); CHECK(header.has_value()); if (header) { CHECK(header->hash() == 0xbef48d7de01f2d7ea1a7e4d1ed401f73d6d0257a364f6770b25ba51a123ac35f_bytes32); @@ -167,7 +173,7 @@ TEST_CASE("HeaderSnapshot::header_by_number OK", "[silkworm][node][snapshot][ind CHECK(header->prev_randao == 0x799895e28a837bbdf28b8ecf5fc0e6251398ecb0ffc7ff5bbb457c21b14ce982_bytes32); CHECK(header->nonce == std::array{0x86, 0x98, 0x76, 0x20, 0x12, 0xb4, 0x6f, 0xef}); } - CHECK(!header_snapshot.header_by_number(1'500'014)); + CHECK(!header_by_number.exec(1'500'014)); } // https://etherscan.io/block/1500013 @@ -183,9 +189,12 @@ TEST_CASE("BodySnapshot::body_by_number OK", "[silkworm][node][snapshot][index]" body_snapshot.reopen_segment(); body_snapshot.reopen_index(); - CHECK(!body_snapshot.body_by_number(1'500'011)); - CHECK(body_snapshot.body_by_number(1'500'012)); - const auto body_for_storage = body_snapshot.body_by_number(1'500'013); + Index idx_body_number{*body_snapshot.idx_body_number()}; + BodyFindByBlockNumQuery body_by_number{body_snapshot, idx_body_number}; + + CHECK(!body_by_number.exec(1'500'011)); + CHECK(body_by_number.exec(1'500'012)); + const auto body_for_storage = body_by_number.exec(1'500'013); CHECK(body_for_storage.has_value()); if (body_for_storage) { CHECK(body_for_storage->base_txn_id == 7'341'271); @@ -208,7 +217,11 @@ TEST_CASE("TransactionSnapshot::txn_by_id OK", "[silkworm][node][snapshot][index TransactionSnapshot tx_snapshot{tx_snapshot_path}; tx_snapshot.reopen_segment(); tx_snapshot.reopen_index(); - const auto transaction = tx_snapshot.txn_by_id(7'341'272); + + Index idx_txn_hash{*tx_snapshot.idx_txn_hash()}; + TransactionFindByIdQuery txn_by_id{tx_snapshot, idx_txn_hash}; + + const auto transaction = txn_by_id.exec(7'341'272); CHECK(transaction.has_value()); if (transaction) { CHECK(transaction->type == TransactionType::kLegacy); @@ -234,23 +247,29 @@ TEST_CASE("TransactionSnapshot::block_num_by_txn_hash OK", "[silkworm][node][sna tx_snapshot.reopen_segment(); tx_snapshot.reopen_index(); + Index idx_txn_hash{*tx_snapshot.idx_txn_hash()}; + TransactionFindByIdQuery txn_by_id{tx_snapshot, idx_txn_hash}; + + Index idx_txn_hash_2_block{*tx_snapshot.idx_txn_hash_2_block()}; + TransactionBlockNumByTxnHashQuery block_num_by_txn_hash{idx_txn_hash_2_block, TransactionFindByHashQuery{tx_snapshot, idx_txn_hash}}; + // block 1'500'012: base_txn_id is 7'341'263, txn_count is 7 - auto transaction = tx_snapshot.txn_by_id(7'341'269); // known txn id in block 1'500'012 + auto transaction = txn_by_id.exec(7'341'269); // known txn id in block 1'500'012 CHECK(transaction.has_value()); - auto block_number = tx_snapshot.block_num_by_txn_hash(transaction->hash()); + auto block_number = block_num_by_txn_hash.exec(transaction->hash()); CHECK(block_number.has_value()); CHECK(block_number.value() == 1'500'012); // block 1'500'013: base_txn_id is 7'341'272, txn_count is 1 - transaction = tx_snapshot.txn_by_id(7'341'272); // known txn id in block 1'500'013 + transaction = txn_by_id.exec(7'341'272); // known txn id in block 1'500'013 CHECK(transaction.has_value()); - block_number = tx_snapshot.block_num_by_txn_hash(transaction->hash()); + block_number = block_num_by_txn_hash.exec(transaction->hash()); CHECK(block_number.has_value()); CHECK(block_number.value() == 1'500'013); // transaction hash not present in snapshot (first txn hash in block 1'500'014) - block_number = tx_snapshot.block_num_by_txn_hash(0xfa496b4cd9748754a28c66690c283ec9429440eb8609998901216908ad1b48eb_bytes32); + block_number = block_num_by_txn_hash.exec(0xfa496b4cd9748754a28c66690c283ec9429440eb8609998901216908ad1b48eb_bytes32); CHECK_FALSE(block_number.has_value()); } @@ -269,26 +288,29 @@ TEST_CASE("TransactionSnapshot::txn_range OK", "[silkworm][node][snapshot][index tx_snapshot.reopen_segment(); tx_snapshot.reopen_index(); + Index idx_txn_hash{*tx_snapshot.idx_txn_hash()}; + TransactionRangeFromIdQuery txn_range{tx_snapshot, idx_txn_hash}; + // block 1'500'012: base_txn_id is 7'341'263, txn_count is 7 SECTION("1'500'012 OK") { - CHECK(tx_snapshot.txn_range(7'341'263, 0).empty()); - CHECK(tx_snapshot.txn_range(7'341'263, 7).size() == 7); + CHECK(txn_range.exec_into_vector(7'341'263, 0).empty()); + CHECK(txn_range.exec_into_vector(7'341'263, 7).size() == 7); } SECTION("1'500'012 KO") { - CHECK_THROWS(tx_snapshot.txn_range(7'341'262, 7)); // invalid base_txn_id - CHECK_THROWS(tx_snapshot.txn_range(7'341'264, 7)); // invalid base_txn_id - CHECK_THROWS(tx_snapshot.txn_range(7'341'263, 8)); // invalid txn_count + CHECK_THROWS(txn_range.exec_into_vector(7'341'262, 7)); // invalid base_txn_id + CHECK_THROWS(txn_range.exec_into_vector(7'341'264, 7)); // invalid base_txn_id + CHECK_THROWS(txn_range.exec_into_vector(7'341'263, 8)); // invalid txn_count } // block 1'500'013: base_txn_id is 7'341'272, txn_count is 1 SECTION("1'500'013 OK") { - CHECK(tx_snapshot.txn_range(7'341'272, 0).empty()); - CHECK(tx_snapshot.txn_range(7'341'272, 1).size() == 1); + CHECK(txn_range.exec_into_vector(7'341'272, 0).empty()); + CHECK(txn_range.exec_into_vector(7'341'272, 1).size() == 1); } SECTION("1'500'013 KO") { - CHECK_THROWS(tx_snapshot.txn_range(7'341'271, 1)); // invalid base_txn_id - CHECK_THROWS(tx_snapshot.txn_range(7'341'273, 1)); // invalid base_txn_id - CHECK_THROWS(tx_snapshot.txn_range(7'341'272, 2)); // invalid txn_count + CHECK_THROWS(txn_range.exec_into_vector(7'341'271, 1)); // invalid base_txn_id + CHECK_THROWS(txn_range.exec_into_vector(7'341'273, 1)); // invalid base_txn_id + CHECK_THROWS(txn_range.exec_into_vector(7'341'272, 2)); // invalid txn_count } } @@ -306,26 +328,29 @@ TEST_CASE("TransactionSnapshot::txn_rlp_range OK", "[silkworm][node][snapshot][i tx_snapshot.reopen_segment(); tx_snapshot.reopen_index(); + Index idx_txn_hash{*tx_snapshot.idx_txn_hash()}; + TransactionPayloadRlpRangeFromIdQuery txn_rlp_range{tx_snapshot, idx_txn_hash}; + // block 1'500'012: base_txn_id is 7'341'263, txn_count is 7 SECTION("1'500'012 OK") { - CHECK(tx_snapshot.txn_rlp_range(7'341'263, 0).empty()); - CHECK(tx_snapshot.txn_rlp_range(7'341'263, 7).size() == 7); + CHECK(txn_rlp_range.exec_into_vector(7'341'263, 0).empty()); + CHECK(txn_rlp_range.exec_into_vector(7'341'263, 7).size() == 7); } SECTION("1'500'012 KO") { - CHECK_THROWS(tx_snapshot.txn_rlp_range(7'341'262, 7)); // invalid base_txn_id - CHECK_THROWS(tx_snapshot.txn_rlp_range(7'341'264, 7)); // invalid base_txn_id - CHECK_THROWS(tx_snapshot.txn_rlp_range(7'341'263, 8)); // invalid txn_count + CHECK_THROWS(txn_rlp_range.exec_into_vector(7'341'262, 7)); // invalid base_txn_id + CHECK_THROWS(txn_rlp_range.exec_into_vector(7'341'264, 7)); // invalid base_txn_id + CHECK_THROWS(txn_rlp_range.exec_into_vector(7'341'263, 8)); // invalid txn_count } // block 1'500'013: base_txn_id is 7'341'272, txn_count is 1 SECTION("1'500'013 OK") { - CHECK(tx_snapshot.txn_rlp_range(7'341'272, 0).empty()); - CHECK(tx_snapshot.txn_rlp_range(7'341'272, 1).size() == 1); + CHECK(txn_rlp_range.exec_into_vector(7'341'272, 0).empty()); + CHECK(txn_rlp_range.exec_into_vector(7'341'272, 1).size() == 1); } SECTION("1'500'013 KO") { - CHECK_THROWS(tx_snapshot.txn_rlp_range(7'341'271, 1)); // invalid base_txn_id - CHECK_THROWS(tx_snapshot.txn_rlp_range(7'341'273, 1)); // invalid base_txn_id - CHECK_THROWS(tx_snapshot.txn_rlp_range(7'341'272, 2)); // invalid txn_count + CHECK_THROWS(txn_rlp_range.exec_into_vector(7'341'271, 1)); // invalid base_txn_id + CHECK_THROWS(txn_rlp_range.exec_into_vector(7'341'273, 1)); // invalid base_txn_id + CHECK_THROWS(txn_rlp_range.exec_into_vector(7'341'272, 2)); // invalid txn_count } }