Skip to content

Commit

Permalink
db: Data API history_seek in LocalTransaction (#2746)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat authored Feb 23, 2025
1 parent 17bd7ae commit 0adf8ca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
44 changes: 41 additions & 3 deletions silkworm/db/kv/api/local_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <silkworm/db/chain/local_chain_storage.hpp>
#include <silkworm/db/datastore/domain_get_as_of_query.hpp>
#include <silkworm/db/datastore/domain_get_latest_query.hpp>
#include <silkworm/db/datastore/history_get_query.hpp>
#include <silkworm/db/datastore/history_range_query.hpp>
#include <silkworm/db/datastore/inverted_index_range_by_key_query.hpp>
#include <silkworm/db/datastore/kvdb/raw_codec.hpp>
Expand Down Expand Up @@ -68,6 +69,17 @@ using CodeDomainGetAsOfQuery = RawDomainGetAsOfQuery<state::kHistorySegmentAndId
using CommitmentDomainGetAsOfQuery = RawDomainGetAsOfQuery<state::kHistorySegmentAndIdxNamesCommitment>;
using ReceiptsDomainGetAsOfQuery = RawDomainGetAsOfQuery<state::kHistorySegmentAndIdxNamesReceipts>;

template <const snapshots::SegmentAndAccessorIndexNames& history_segment_names>
using RawHistoryGetQuery = HistoryGetQuery<
kvdb::RawEncoder<ByteView>, snapshots::RawEncoder<ByteView>,
kvdb::RawDecoder<Bytes>, snapshots::RawDecoder<Bytes>,
history_segment_names>;
using AccountsHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesAccounts>;
using StorageHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesStorage>;
using CodeHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesCode>;
using CommitmentHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesCommitment>;
using ReceiptsHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesReceipts>;

using RawInvertedIndexRangeByKeyQuery = InvertedIndexRangeByKeyQuery<
kvdb::RawEncoder<Bytes>, snapshots::RawEncoder<Bytes>>; // TODO(canepat) try ByteView

Expand Down Expand Up @@ -180,9 +192,35 @@ Task<GetAsOfResult> LocalTransaction::get_as_of(GetAsOfQuery query) {
co_return GetAsOfResult{.success = true, .value = std::move(*value)};
}

Task<HistoryPointResult> LocalTransaction::history_seek(HistoryPointQuery /*query*/) {
// TODO(canepat) implement using E3-like aggregator abstraction [tx_id_ must be changed]
co_return HistoryPointResult{};
Task<HistoryPointResult> LocalTransaction::history_seek(HistoryPointQuery query) {
if (!kTable2EntityNames.contains(query.table)) {
co_return HistoryPointResult{};
}

const auto domain_name = kTable2EntityNames.at(query.table);
const auto domain = data_store_.chaindata.domain(domain_name);
if (!domain.history) {
co_return HistoryPointResult{};
}

const auto timestamp = static_cast<datastore::Timestamp>(query.timestamp);

std::optional<Bytes> value;
if (domain_name == state::kDomainNameAccounts) {
value = query_history_get<AccountsHistoryGetQuery>(*domain.history, query.key, timestamp);
} else if (domain_name == state::kDomainNameStorage) {
value = query_history_get<StorageHistoryGetQuery>(*domain.history, query.key, timestamp);
} else if (domain_name == state::kDomainNameCode) {
value = query_history_get<CodeHistoryGetQuery>(*domain.history, query.key, timestamp);
} else if (domain_name == state::kDomainNameCommitment) {
value = query_history_get<CommitmentHistoryGetQuery>(*domain.history, query.key, timestamp);
} else if (domain_name == state::kDomainNameReceipts) {
value = query_history_get<ReceiptsHistoryGetQuery>(*domain.history, query.key, timestamp);
}
if (!value) {
co_return HistoryPointResult{};
}
co_return HistoryPointResult{.success = true, .value = std::move(*value)};
}

Task<PaginatedTimestamps> LocalTransaction::index_range(IndexRangeQuery query) {
Expand Down
17 changes: 8 additions & 9 deletions silkworm/db/kv/api/local_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,21 @@ class LocalTransaction : public BaseTransaction {
Task<PaginatedKeysValues> range_as_of(DomainRangeQuery query) override;

private:
template <typename DomainGetLatest>
auto query_domain_latest(const datastore::EntityName domain_name, ByteView key) {
DomainGetLatest query(
template <typename DomainGetAsOfQuery>
auto query_domain_as_of(const datastore::EntityName domain_name, ByteView key, Timestamp ts) {
DomainGetAsOfQuery query(
data_store_.chaindata.domain(domain_name),
tx_,
data_store_.state_repository_latest,
data_store_.state_repository_historical);
return query.exec(key);
return query.exec(key, ts);
}

template <typename DomainGetAsOfQuery>
auto query_domain_as_of(const datastore::EntityName domain_name, ByteView key, Timestamp ts) {
DomainGetAsOfQuery query(
data_store_.chaindata.domain(domain_name),
template <typename HistoryGetQuery>
auto query_history_get(datastore::kvdb::History kvdb_entity, ByteView key, datastore::Timestamp ts) {
HistoryGetQuery query(
kvdb_entity,
tx_,
data_store_.state_repository_latest,
data_store_.state_repository_historical);
return query.exec(key, ts);
}
Expand Down

0 comments on commit 0adf8ca

Please sign in to comment.