Skip to content

Commit 4bbd106

Browse files
committed
db: Data API history_seek in LocalTransaction
1 parent 17bd7ae commit 4bbd106

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

silkworm/db/kv/api/local_transaction.cpp

+41-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <silkworm/db/chain/local_chain_storage.hpp>
2525
#include <silkworm/db/datastore/domain_get_as_of_query.hpp>
2626
#include <silkworm/db/datastore/domain_get_latest_query.hpp>
27+
#include <silkworm/db/datastore/history_get_query.hpp>
2728
#include <silkworm/db/datastore/history_range_query.hpp>
2829
#include <silkworm/db/datastore/inverted_index_range_by_key_query.hpp>
2930
#include <silkworm/db/datastore/kvdb/raw_codec.hpp>
@@ -68,6 +69,17 @@ using CodeDomainGetAsOfQuery = RawDomainGetAsOfQuery<state::kHistorySegmentAndId
6869
using CommitmentDomainGetAsOfQuery = RawDomainGetAsOfQuery<state::kHistorySegmentAndIdxNamesCommitment>;
6970
using ReceiptsDomainGetAsOfQuery = RawDomainGetAsOfQuery<state::kHistorySegmentAndIdxNamesReceipts>;
7071

72+
template <const snapshots::SegmentAndAccessorIndexNames& history_segment_names>
73+
using RawHistoryGetQuery = HistoryGetQuery<
74+
kvdb::RawEncoder<ByteView>, snapshots::RawEncoder<ByteView>,
75+
kvdb::RawDecoder<Bytes>, snapshots::RawDecoder<Bytes>,
76+
history_segment_names>;
77+
using AccountsHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesAccounts>;
78+
using StorageHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesStorage>;
79+
using CodeHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesCode>;
80+
using CommitmentHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesCommitment>;
81+
using ReceiptsHistoryGetQuery = RawHistoryGetQuery<state::kHistorySegmentAndIdxNamesReceipts>;
82+
7183
using RawInvertedIndexRangeByKeyQuery = InvertedIndexRangeByKeyQuery<
7284
kvdb::RawEncoder<Bytes>, snapshots::RawEncoder<Bytes>>; // TODO(canepat) try ByteView
7385

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

183-
Task<HistoryPointResult> LocalTransaction::history_seek(HistoryPointQuery /*query*/) {
184-
// TODO(canepat) implement using E3-like aggregator abstraction [tx_id_ must be changed]
185-
co_return HistoryPointResult{};
195+
Task<HistoryPointResult> LocalTransaction::history_seek(HistoryPointQuery query) {
196+
if (!kTable2EntityNames.contains(query.table)) {
197+
co_return HistoryPointResult{};
198+
}
199+
200+
const auto domain_name = kTable2EntityNames.at(query.table);
201+
const auto domain = data_store_.chaindata.domain(domain_name);
202+
if (!domain.history) {
203+
co_return HistoryPointResult{};
204+
}
205+
206+
const auto timestamp = static_cast<datastore::Timestamp>(query.timestamp);
207+
208+
std::optional<Bytes> value;
209+
if (domain_name == state::kDomainNameAccounts) {
210+
value = query_history_get<AccountsHistoryGetQuery>(*domain.history, query.key, timestamp);
211+
} else if (domain_name == state::kDomainNameStorage) {
212+
value = query_history_get<StorageHistoryGetQuery>(*domain.history, query.key, timestamp);
213+
} else if (domain_name == state::kDomainNameCode) {
214+
value = query_history_get<CodeHistoryGetQuery>(*domain.history, query.key, timestamp);
215+
} else if (domain_name == state::kDomainNameCommitment) {
216+
value = query_history_get<CommitmentHistoryGetQuery>(*domain.history, query.key, timestamp);
217+
} else if (domain_name == state::kDomainNameReceipts) {
218+
value = query_history_get<ReceiptsHistoryGetQuery>(*domain.history, query.key, timestamp);
219+
}
220+
if (!value) {
221+
co_return HistoryPointResult{};
222+
}
223+
co_return HistoryPointResult{.success = true, .value = std::move(*value)};
186224
}
187225

188226
Task<PaginatedTimestamps> LocalTransaction::index_range(IndexRangeQuery query) {

silkworm/db/kv/api/local_transaction.hpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,21 @@ class LocalTransaction : public BaseTransaction {
8181
Task<PaginatedKeysValues> range_as_of(DomainRangeQuery query) override;
8282

8383
private:
84-
template <typename DomainGetLatest>
85-
auto query_domain_latest(const datastore::EntityName domain_name, ByteView key) {
86-
DomainGetLatest query(
84+
template <typename DomainGetAsOfQuery>
85+
auto query_domain_as_of(const datastore::EntityName domain_name, ByteView key, Timestamp ts) {
86+
DomainGetAsOfQuery query(
8787
data_store_.chaindata.domain(domain_name),
8888
tx_,
8989
data_store_.state_repository_latest,
9090
data_store_.state_repository_historical);
91-
return query.exec(key);
91+
return query.exec(key, ts);
9292
}
9393

94-
template <typename DomainGetAsOfQuery>
95-
auto query_domain_as_of(const datastore::EntityName domain_name, ByteView key, Timestamp ts) {
96-
DomainGetAsOfQuery query(
97-
data_store_.chaindata.domain(domain_name),
94+
template <typename HistoryGetQuery>
95+
auto query_history_get(datastore::kvdb::History kvdb_entity, ByteView key, datastore::Timestamp ts) {
96+
HistoryGetQuery query(
97+
kvdb_entity,
9898
tx_,
99-
data_store_.state_repository_latest,
10099
data_store_.state_repository_historical);
101100
return query.exec(key, ts);
102101
}

0 commit comments

Comments
 (0)