From f876db751c916a0e98f56d928a1c60040041d49f Mon Sep 17 00:00:00 2001 From: canepat <16927169+canepat@users.noreply.github.com> Date: Thu, 30 Jan 2025 22:14:52 +0100 Subject: [PATCH] execution: use domain GetAsOf queries in LocalState --- .../datastore/snapshots/history_get_query.hpp | 2 +- silkworm/execution/local_state.cpp | 68 ++++++++----------- silkworm/execution/local_state.hpp | 5 ++ 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/silkworm/db/datastore/snapshots/history_get_query.hpp b/silkworm/db/datastore/snapshots/history_get_query.hpp index 19adeece59..517b7f5f1a 100644 --- a/silkworm/db/datastore/snapshots/history_get_query.hpp +++ b/silkworm/db/datastore/snapshots/history_get_query.hpp @@ -48,7 +48,7 @@ struct HistoryGetQuery { private: InvertedIndexSeekQuery timestamp_query_; - FindByTimestampMapQuery, segment::SegmentReader, segment_names>> value_query_; + FindByTimestampMapQuery, TValueDecoder, segment_names>> value_query_; }; } // namespace silkworm::snapshots diff --git a/silkworm/execution/local_state.cpp b/silkworm/execution/local_state.cpp index 327b35840b..5fbdf43ab0 100644 --- a/silkworm/execution/local_state.cpp +++ b/silkworm/execution/local_state.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include namespace silkworm::execution { @@ -28,18 +27,14 @@ using namespace db::state; using namespace datastore; std::optional LocalState::read_account(const evmc::address& address) const noexcept { - if (!txn_id_) { - AccountsDomainGetLatestQuery query{ - data_store_.chaindata, - tx_, - data_store_.state_repository, - }; - auto result = query.exec(address); - if (result) { - return std::move(result->value); - } - } else { - // TODO(canepat) historical AccountsDomainGetAsOfQuery on *txn_id timestamp + if (txn_id_) { + // Query historical state at required timestamp + return make_query().exec(address, *txn_id_); + } + // Query latest i.e. current state + auto result = make_query().exec(address); + if (result) { + return std::move(result->value); } return std::nullopt; } @@ -49,39 +44,34 @@ ByteView LocalState::read_code(const evmc::address& address, const evmc::bytes32 return code_[address]; // NOLINT(runtime/arrays) } - if (!txn_id_) { - CodeDomainGetLatestQuery query{ - data_store_.chaindata, - tx_, - data_store_.state_repository, - }; - auto result = query.exec(address); + if (txn_id_) { + // Query historical state at required timestamp + auto result = make_query().exec(address, *txn_id_); if (result) { - auto [it, _] = code_.emplace(address, std::move(result->value)); + auto [it, _] = code_.emplace(address, std::move(*result)); return it->second; } - } else { - // TODO(canepat) historical CodeDomainGetAsOfQuery on *txn_id timestamp + return ByteView{}; + } + // Query latest i.e. current state + auto result = make_query().exec(address); + if (result) { + auto [it, _] = code_.emplace(address, std::move(result->value)); + return it->second; } return ByteView{}; } -evmc::bytes32 LocalState::read_storage( - const evmc::address& address, - uint64_t /*incarnation*/, - const evmc::bytes32& location) const noexcept { - if (!txn_id_) { - StorageDomainGetLatestQuery query{ - data_store_.chaindata, - tx_, - data_store_.state_repository, - }; - auto result = query.exec({address, location}); - if (result) { - return result->value; - } - } else { - // TODO(canepat) historical StorageDomainGetAsOfQuery on *txn_id timestamp +evmc::bytes32 LocalState::read_storage(const evmc::address& address, uint64_t /*incarnation*/, const evmc::bytes32& location) const noexcept { + if (txn_id_) { + // Query historical state at required timestamp + auto result = make_query().exec({address, location}, *txn_id_); + return result.value_or(evmc::bytes32{}); + } + // Query latest i.e. current state + auto result = make_query().exec({address, location}); + if (result) { + return result->value; } return {}; } diff --git a/silkworm/execution/local_state.hpp b/silkworm/execution/local_state.hpp index 7e52102d92..ad3fdca318 100644 --- a/silkworm/execution/local_state.hpp +++ b/silkworm/execution/local_state.hpp @@ -97,6 +97,11 @@ class LocalState : public State { return db::DataModelFactory{data_store_}(tx_); } + template + auto make_query() const { + return DomainQuery{data_store_.chaindata, tx_, data_store_.state_repository}; + } + std::optional txn_id_; db::DataStoreRef data_store_; mutable datastore::kvdb::ROTxnManaged tx_;