Skip to content

Commit

Permalink
execution: use domain GetAsOf queries in LocalState
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat committed Jan 30, 2025
1 parent 43d4b52 commit f876db7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
2 changes: 1 addition & 1 deletion silkworm/db/datastore/snapshots/history_get_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct HistoryGetQuery {

private:
InvertedIndexSeekQuery<TKeyEncoder> timestamp_query_;
FindByTimestampMapQuery<FindByKeySegmentQuery<HistoryAccessorIndexKeyEncoder<TKeyEncoder>, segment::SegmentReader<TValueDecoder>, segment_names>> value_query_;
FindByTimestampMapQuery<FindByKeySegmentQuery<HistoryAccessorIndexKeyEncoder<TKeyEncoder>, TValueDecoder, segment_names>> value_query_;
};

} // namespace silkworm::snapshots
68 changes: 29 additions & 39 deletions silkworm/execution/local_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <silkworm/core/common/util.hpp>
#include <silkworm/db/state/accounts_domain.hpp>
#include <silkworm/db/state/code_domain.hpp>
#include <silkworm/db/state/schema_config.hpp>
#include <silkworm/db/state/storage_domain.hpp>

namespace silkworm::execution {
Expand All @@ -28,18 +27,14 @@ using namespace db::state;
using namespace datastore;

std::optional<Account> 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<AccountsDomainGetAsOfQuery>().exec(address, *txn_id_);
}
// Query latest i.e. current state
auto result = make_query<AccountsDomainGetLatestQuery>().exec(address);
if (result) {
return std::move(result->value);
}
return std::nullopt;
}
Expand All @@ -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<CodeDomainGetAsOfQuery>().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<CodeDomainGetLatestQuery>().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<StorageDomainGetAsOfQuery>().exec({address, location}, *txn_id_);
return result.value_or(evmc::bytes32{});
}
// Query latest i.e. current state
auto result = make_query<StorageDomainGetLatestQuery>().exec({address, location});
if (result) {
return result->value;
}
return {};
}
Expand Down
5 changes: 5 additions & 0 deletions silkworm/execution/local_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class LocalState : public State {
return db::DataModelFactory{data_store_}(tx_);
}

template <typename DomainQuery>
auto make_query() const {
return DomainQuery{data_store_.chaindata, tx_, data_store_.state_repository};
}

std::optional<TxnId> txn_id_;
db::DataStoreRef data_store_;
mutable datastore::kvdb::ROTxnManaged tx_;
Expand Down

0 comments on commit f876db7

Please sign in to comment.