Skip to content

Commit

Permalink
execution: use read-only tx in LocalState (#2681)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat authored Jan 28, 2025
1 parent 8859967 commit d24f0e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 61 deletions.
50 changes: 3 additions & 47 deletions silkworm/execution/local_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ std::optional<Account> LocalState::read_account(const evmc::address& address) co
return std::move(result->value);
}
} else {
// historical request on *txn_id timestamp
// TODO(canepat) historical AccountsDomainGetAsOfQuery on *txn_id timestamp
}
return std::nullopt;
}
Expand All @@ -57,7 +57,7 @@ ByteView LocalState::read_code(const evmc::address& address, const evmc::bytes32
return result->value;
}
} else {
// historical request on *txn_id timestamp
// TODO(canepat) historical CodeDomainGetAsOfQuery on *txn_id timestamp
}
return ByteView{};
}
Expand All @@ -77,7 +77,7 @@ evmc::bytes32 LocalState::read_storage(
return result->value;
}
} else {
// historical request on *txn_id timestamp
// TODO(canepat) historical StorageDomainGetAsOfQuery on *txn_id timestamp
}
return {};
}
Expand Down Expand Up @@ -112,48 +112,4 @@ std::optional<evmc::bytes32> LocalState::canonical_hash(BlockNum block_num) cons
return data_model().read_canonical_header_hash(block_num);
}

void LocalState::update_account(
const evmc::address& address,
std::optional<Account> initial,
std::optional<Account> current) {
/* should be managed request on Latest(txn_id == nullopt) and historical (txn_id != nullopt) */

Step current_step = Step::from_txn_id(*txn_id_);
if (current) {
AccountsDomainPutQuery query{tx_, data_store_.state_db().accounts_domain()};
query.exec(address, *current, *txn_id_, initial, current_step);
} else {
AccountsDomainDeleteQuery query{tx_, data_store_.state_db().accounts_domain()};
query.exec(address, *txn_id_, initial, current_step);
}
}

void LocalState::update_account_code(
const evmc::address& address,
uint64_t /*incarnation*/,
const evmc::bytes32& /*code_hash*/,
ByteView code) {
/* should be managed request on Latest(txn_id == nullopt) and historical (txn_id != nullopt) */

Step current_step = Step::from_txn_id(*txn_id_);
CodeDomainPutQuery query{tx_, data_store_.state_db().code_domain()};
std::optional<ByteView> initial_code = read_code(address, evmc::bytes32{});
if (initial_code && initial_code->empty())
initial_code = std::nullopt;
query.exec(address, code, *txn_id_, initial_code, current_step);
}

void LocalState::update_storage(
const evmc::address& address,
uint64_t /*incarnation*/,
const evmc::bytes32& location,
const evmc::bytes32& initial,
const evmc::bytes32& current) {
/* should be managed request on Latest(txn_id == nullopt) and historical (txn_id != nullopt) */

Step current_step = Step::from_txn_id(*txn_id_);
StorageDomainPutQuery query{tx_, data_store_.state_db().storage_domain()};
query.exec({address, location}, current, *txn_id_, initial, current_step);
}

} // namespace silkworm::execution
28 changes: 14 additions & 14 deletions silkworm/execution/local_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LocalState : public State {
db::DataStoreRef data_store)
: txn_id_{txn_id},
data_store_{std::move(data_store)},
tx_{data_store_.chaindata.access_rw().start_rw_tx()} {}
tx_{data_store_.chaindata.access_ro().start_ro_tx()} {}

std::optional<Account> read_account(const evmc::address& address) const noexcept override;

Expand Down Expand Up @@ -73,22 +73,22 @@ class LocalState : public State {
void begin_block(BlockNum /*block_num*/, size_t /*updated_accounts_count*/) override {}

void update_account(
const evmc::address& address,
std::optional<Account> initial,
std::optional<Account> current) override;
const evmc::address& /*address*/,
std::optional<Account> /*initial*/,
std::optional<Account> /*current*/) override {}

void update_account_code(
const evmc::address& address,
uint64_t incarnation,
const evmc::bytes32& code_hash,
ByteView code) override;
const evmc::address& /*address*/,
uint64_t /*incarnation*/,
const evmc::bytes32& /*code_hash*/,
ByteView /*code*/) override {}

void update_storage(
const evmc::address& address,
uint64_t incarnation,
const evmc::bytes32& location,
const evmc::bytes32& initial,
const evmc::bytes32& current) override;
const evmc::address& /*address*/,
uint64_t /*incarnation*/,
const evmc::bytes32& /*location*/,
const evmc::bytes32& /*initial*/,
const evmc::bytes32& /*current*/) override {}

void unwind_state_changes(BlockNum /*block_num*/) override {}

Expand All @@ -99,7 +99,7 @@ class LocalState : public State {

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

} // namespace silkworm::execution

0 comments on commit d24f0e4

Please sign in to comment.